Change LGPL-2.1+ to LGPL-2.1-or-later
[platform/upstream/glib.git] / glib / gdate.h
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/.
25  */
26
27 #ifndef __G_DATE_H__
28 #define __G_DATE_H__
29
30 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
31 #error "Only <glib.h> can be included directly."
32 #endif
33
34 #include <time.h>
35
36 #include <glib/gtypes.h>
37 #include <glib/gquark.h>
38
39 G_BEGIN_DECLS
40
41 /* GDate
42  *
43  * Date calculations (not time for now, to be resolved). These are a
44  * mutant combination of Steffen Beyer's DateCalc routines
45  * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
46  * date routines (written for in-house software).  Written by Havoc
47  * Pennington <hp@pobox.com>
48  */
49
50 typedef gint32  GTime GLIB_DEPRECATED_TYPE_IN_2_62_FOR(GDateTime);
51 typedef guint16 GDateYear;
52 typedef guint8  GDateDay;   /* day of the month */
53 typedef struct _GDate GDate;
54
55 /* enum used to specify order of appearance in parsed date strings */
56 typedef enum
57 {
58   G_DATE_DAY   = 0,
59   G_DATE_MONTH = 1,
60   G_DATE_YEAR  = 2
61 } GDateDMY;
62
63 /* actual week and month values */
64 typedef enum
65 {
66   G_DATE_BAD_WEEKDAY  = 0,
67   G_DATE_MONDAY       = 1,
68   G_DATE_TUESDAY      = 2,
69   G_DATE_WEDNESDAY    = 3,
70   G_DATE_THURSDAY     = 4,
71   G_DATE_FRIDAY       = 5,
72   G_DATE_SATURDAY     = 6,
73   G_DATE_SUNDAY       = 7
74 } GDateWeekday;
75 typedef enum
76 {
77   G_DATE_BAD_MONTH = 0,
78   G_DATE_JANUARY   = 1,
79   G_DATE_FEBRUARY  = 2,
80   G_DATE_MARCH     = 3,
81   G_DATE_APRIL     = 4,
82   G_DATE_MAY       = 5,
83   G_DATE_JUNE      = 6,
84   G_DATE_JULY      = 7,
85   G_DATE_AUGUST    = 8,
86   G_DATE_SEPTEMBER = 9,
87   G_DATE_OCTOBER   = 10,
88   G_DATE_NOVEMBER  = 11,
89   G_DATE_DECEMBER  = 12
90 } GDateMonth;
91
92 #define G_DATE_BAD_JULIAN 0U
93 #define G_DATE_BAD_DAY    0U
94 #define G_DATE_BAD_YEAR   0U
95
96 /* Note: directly manipulating structs is generally a bad idea, but
97  * in this case it's an *incredibly* bad idea, because all or part
98  * of this struct can be invalid at any given time. Use the functions,
99  * or you will get hosed, I promise.
100  */
101 struct _GDate
102 {
103   guint julian_days : 32; /* julian days representation - we use a
104                            *  bitfield hoping that 64 bit platforms
105                            *  will pack this whole struct in one big
106                            *  int
107                            */
108
109   guint julian : 1;    /* julian is valid */
110   guint dmy    : 1;    /* dmy is valid */
111
112   /* DMY representation */
113   guint day    : 6;
114   guint month  : 4;
115   guint year   : 16;
116 };
117
118 /* g_date_new() returns an invalid date, you then have to _set() stuff
119  * to get a usable object. You can also allocate a GDate statically,
120  * then call g_date_clear() to initialize.
121  */
122 GLIB_AVAILABLE_IN_ALL
123 GDate*       g_date_new                   (void);
124 GLIB_AVAILABLE_IN_ALL
125 GDate*       g_date_new_dmy               (GDateDay     day,
126                                            GDateMonth   month,
127                                            GDateYear    year);
128 GLIB_AVAILABLE_IN_ALL
129 GDate*       g_date_new_julian            (guint32      julian_day);
130 GLIB_AVAILABLE_IN_ALL
131 void         g_date_free                  (GDate       *date);
132 GLIB_AVAILABLE_IN_2_56
133 GDate*       g_date_copy                  (const GDate *date);
134
135 /* check g_date_valid() after doing an operation that might fail, like
136  * _parse.  Almost all g_date operations are undefined on invalid
137  * dates (the exceptions are the mutators, since you need those to
138  * return to validity).
139  */
140 GLIB_AVAILABLE_IN_ALL
141 gboolean     g_date_valid                 (const GDate *date);
142 GLIB_AVAILABLE_IN_ALL
143 gboolean     g_date_valid_day             (GDateDay     day) G_GNUC_CONST;
144 GLIB_AVAILABLE_IN_ALL
145 gboolean     g_date_valid_month           (GDateMonth month) G_GNUC_CONST;
146 GLIB_AVAILABLE_IN_ALL
147 gboolean     g_date_valid_year            (GDateYear  year) G_GNUC_CONST;
148 GLIB_AVAILABLE_IN_ALL
149 gboolean     g_date_valid_weekday         (GDateWeekday weekday) G_GNUC_CONST;
150 GLIB_AVAILABLE_IN_ALL
151 gboolean     g_date_valid_julian          (guint32 julian_date) G_GNUC_CONST;
152 GLIB_AVAILABLE_IN_ALL
153 gboolean     g_date_valid_dmy             (GDateDay     day,
154                                            GDateMonth   month,
155                                            GDateYear    year) G_GNUC_CONST;
156
157 GLIB_AVAILABLE_IN_ALL
158 GDateWeekday g_date_get_weekday           (const GDate *date);
159 GLIB_AVAILABLE_IN_ALL
160 GDateMonth   g_date_get_month             (const GDate *date);
161 GLIB_AVAILABLE_IN_ALL
162 GDateYear    g_date_get_year              (const GDate *date);
163 GLIB_AVAILABLE_IN_ALL
164 GDateDay     g_date_get_day               (const GDate *date);
165 GLIB_AVAILABLE_IN_ALL
166 guint32      g_date_get_julian            (const GDate *date);
167 GLIB_AVAILABLE_IN_ALL
168 guint        g_date_get_day_of_year       (const GDate *date);
169 /* First monday/sunday is the start of week 1; if we haven't reached
170  * that day, return 0. These are not ISO weeks of the year; that
171  * routine needs to be added.
172  * these functions return the number of weeks, starting on the
173  * corresponding day
174  */
175 GLIB_AVAILABLE_IN_ALL
176 guint        g_date_get_monday_week_of_year (const GDate *date);
177 GLIB_AVAILABLE_IN_ALL
178 guint        g_date_get_sunday_week_of_year (const GDate *date);
179 GLIB_AVAILABLE_IN_ALL
180 guint        g_date_get_iso8601_week_of_year (const GDate *date);
181
182 /* If you create a static date struct you need to clear it to get it
183  * in a safe state before use. You can clear a whole array at
184  * once with the ndates argument.
185  */
186 GLIB_AVAILABLE_IN_ALL
187 void         g_date_clear                 (GDate       *date,
188                                            guint        n_dates);
189
190 /* The parse routine is meant for dates typed in by a user, so it
191  * permits many formats but tries to catch common typos. If your data
192  * needs to be strictly validated, it is not an appropriate function.
193  */
194 GLIB_AVAILABLE_IN_ALL
195 void         g_date_set_parse             (GDate       *date,
196                                            const gchar *str);
197 GLIB_AVAILABLE_IN_ALL
198 void         g_date_set_time_t            (GDate       *date,
199                                            time_t       timet);
200 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
201 GLIB_DEPRECATED_IN_2_62_FOR(g_date_set_time_t)
202 void         g_date_set_time_val          (GDate       *date,
203                                            GTimeVal    *timeval);
204 GLIB_DEPRECATED_FOR(g_date_set_time_t)
205 void         g_date_set_time              (GDate       *date,
206                                            GTime        time_);
207 G_GNUC_END_IGNORE_DEPRECATIONS
208 GLIB_AVAILABLE_IN_ALL
209 void         g_date_set_month             (GDate       *date,
210                                            GDateMonth   month);
211 GLIB_AVAILABLE_IN_ALL
212 void         g_date_set_day               (GDate       *date,
213                                            GDateDay     day);
214 GLIB_AVAILABLE_IN_ALL
215 void         g_date_set_year              (GDate       *date,
216                                            GDateYear    year);
217 GLIB_AVAILABLE_IN_ALL
218 void         g_date_set_dmy               (GDate       *date,
219                                            GDateDay     day,
220                                            GDateMonth   month,
221                                            GDateYear    y);
222 GLIB_AVAILABLE_IN_ALL
223 void         g_date_set_julian            (GDate       *date,
224                                            guint32      julian_date);
225 GLIB_AVAILABLE_IN_ALL
226 gboolean     g_date_is_first_of_month     (const GDate *date);
227 GLIB_AVAILABLE_IN_ALL
228 gboolean     g_date_is_last_of_month      (const GDate *date);
229
230 /* To go forward by some number of weeks just go forward weeks*7 days */
231 GLIB_AVAILABLE_IN_ALL
232 void         g_date_add_days              (GDate       *date,
233                                            guint        n_days);
234 GLIB_AVAILABLE_IN_ALL
235 void         g_date_subtract_days         (GDate       *date,
236                                            guint        n_days);
237
238 /* If you add/sub months while day > 28, the day might change */
239 GLIB_AVAILABLE_IN_ALL
240 void         g_date_add_months            (GDate       *date,
241                                            guint        n_months);
242 GLIB_AVAILABLE_IN_ALL
243 void         g_date_subtract_months       (GDate       *date,
244                                            guint        n_months);
245
246 /* If it's feb 29, changing years can move you to the 28th */
247 GLIB_AVAILABLE_IN_ALL
248 void         g_date_add_years             (GDate       *date,
249                                            guint        n_years);
250 GLIB_AVAILABLE_IN_ALL
251 void         g_date_subtract_years        (GDate       *date,
252                                            guint        n_years);
253 GLIB_AVAILABLE_IN_ALL
254 gboolean     g_date_is_leap_year          (GDateYear    year) G_GNUC_CONST;
255 GLIB_AVAILABLE_IN_ALL
256 guint8       g_date_get_days_in_month     (GDateMonth   month,
257                                            GDateYear    year) G_GNUC_CONST;
258 GLIB_AVAILABLE_IN_ALL
259 guint8       g_date_get_monday_weeks_in_year  (GDateYear    year) G_GNUC_CONST;
260 GLIB_AVAILABLE_IN_ALL
261 guint8       g_date_get_sunday_weeks_in_year  (GDateYear    year) G_GNUC_CONST;
262
263 /* Returns the number of days between the two dates.  If date2 comes
264    before date1, a negative value is return. */
265 GLIB_AVAILABLE_IN_ALL
266 gint         g_date_days_between          (const GDate *date1,
267                                            const GDate *date2);
268
269 /* qsort-friendly (with a cast...) */
270 GLIB_AVAILABLE_IN_ALL
271 gint         g_date_compare               (const GDate *lhs,
272                                            const GDate *rhs);
273 GLIB_AVAILABLE_IN_ALL
274 void         g_date_to_struct_tm          (const GDate *date,
275                                            struct tm   *tm);
276
277 GLIB_AVAILABLE_IN_ALL
278 void         g_date_clamp                 (GDate *date,
279                                            const GDate *min_date,
280                                            const GDate *max_date);
281
282 /* Swap date1 and date2's values if date1 > date2. */
283 GLIB_AVAILABLE_IN_ALL
284 void         g_date_order                 (GDate *date1, GDate *date2);
285
286 /* Just like strftime() except you can only use date-related formats.
287  *   Using a time format is undefined.
288  */
289 GLIB_AVAILABLE_IN_ALL
290 gsize        g_date_strftime              (gchar       *s,
291                                            gsize        slen,
292                                            const gchar *format,
293                                            const GDate *date);
294
295 #define g_date_weekday                  g_date_get_weekday GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_date_get_weekday)
296 #define g_date_month                    g_date_get_month GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_date_get_month)
297 #define g_date_year                     g_date_get_year GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_date_get_year)
298 #define g_date_day                      g_date_get_day GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_date_get_day)
299 #define g_date_julian                   g_date_get_julian GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_date_get_julian)
300 #define g_date_day_of_year              g_date_get_day_of_year GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_date_get_day_of_year)
301 #define g_date_monday_week_of_year      g_date_get_monday_week_of_year GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_date_get_monday_week_of_year)
302 #define g_date_sunday_week_of_year      g_date_get_sunday_week_of_year GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_date_get_sunday_week_of_year)
303 #define g_date_days_in_month            g_date_get_days_in_month GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_date_get_days_in_month)
304 #define g_date_monday_weeks_in_year     g_date_get_monday_weeks_in_year GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_date_get_monday_weeks_in_year)
305 #define g_date_sunday_weeks_in_year     g_date_get_sunday_weeks_in_year GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_date_get_sunday_weeks_in_year)
306
307 G_END_DECLS
308
309 #endif /* __G_DATE_H__ */