1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
28 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
29 #error "Only <glib.h> can be included directly."
34 #include <glib/gtypes.h>
35 #include <glib/gquark.h>
41 * Date calculations (not time for now, to be resolved). These are a
42 * mutant combination of Steffen Beyer's DateCalc routines
43 * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
44 * date routines (written for in-house software). Written by Havoc
45 * Pennington <hp@pobox.com>
49 typedef guint16 GDateYear;
50 typedef guint8 GDateDay; /* day of the month */
51 typedef struct _GDate GDate;
53 /* enum used to specify order of appearance in parsed date strings */
61 /* actual week and month values */
64 G_DATE_BAD_WEEKDAY = 0,
90 #define G_DATE_BAD_JULIAN 0U
91 #define G_DATE_BAD_DAY 0U
92 #define G_DATE_BAD_YEAR 0U
94 /* Note: directly manipulating structs is generally a bad idea, but
95 * in this case it's an *incredibly* bad idea, because all or part
96 * of this struct can be invalid at any given time. Use the functions,
97 * or you will get hosed, I promise.
101 guint julian_days : 32; /* julian days representation - we use a
102 * bitfield hoping that 64 bit platforms
103 * will pack this whole struct in one big
107 guint julian : 1; /* julian is valid */
108 guint dmy : 1; /* dmy is valid */
110 /* DMY representation */
116 /* g_date_new() returns an invalid date, you then have to _set() stuff
117 * to get a usable object. You can also allocate a GDate statically,
118 * then call g_date_clear() to initialize.
120 GLIB_AVAILABLE_IN_ALL
121 GDate* g_date_new (void);
122 GLIB_AVAILABLE_IN_ALL
123 GDate* g_date_new_dmy (GDateDay day,
126 GLIB_AVAILABLE_IN_ALL
127 GDate* g_date_new_julian (guint32 julian_day);
128 GLIB_AVAILABLE_IN_ALL
129 void g_date_free (GDate *date);
131 /* check g_date_valid() after doing an operation that might fail, like
132 * _parse. Almost all g_date operations are undefined on invalid
133 * dates (the exceptions are the mutators, since you need those to
134 * return to validity).
136 GLIB_AVAILABLE_IN_ALL
137 gboolean g_date_valid (const GDate *date);
138 GLIB_AVAILABLE_IN_ALL
139 gboolean g_date_valid_day (GDateDay day) G_GNUC_CONST;
140 GLIB_AVAILABLE_IN_ALL
141 gboolean g_date_valid_month (GDateMonth month) G_GNUC_CONST;
142 GLIB_AVAILABLE_IN_ALL
143 gboolean g_date_valid_year (GDateYear year) G_GNUC_CONST;
144 GLIB_AVAILABLE_IN_ALL
145 gboolean g_date_valid_weekday (GDateWeekday weekday) G_GNUC_CONST;
146 GLIB_AVAILABLE_IN_ALL
147 gboolean g_date_valid_julian (guint32 julian_date) G_GNUC_CONST;
148 GLIB_AVAILABLE_IN_ALL
149 gboolean g_date_valid_dmy (GDateDay day,
151 GDateYear year) G_GNUC_CONST;
153 GLIB_AVAILABLE_IN_ALL
154 GDateWeekday g_date_get_weekday (const GDate *date);
155 GLIB_AVAILABLE_IN_ALL
156 GDateMonth g_date_get_month (const GDate *date);
157 GLIB_AVAILABLE_IN_ALL
158 GDateYear g_date_get_year (const GDate *date);
159 GLIB_AVAILABLE_IN_ALL
160 GDateDay g_date_get_day (const GDate *date);
161 GLIB_AVAILABLE_IN_ALL
162 guint32 g_date_get_julian (const GDate *date);
163 GLIB_AVAILABLE_IN_ALL
164 guint g_date_get_day_of_year (const GDate *date);
165 /* First monday/sunday is the start of week 1; if we haven't reached
166 * that day, return 0. These are not ISO weeks of the year; that
167 * routine needs to be added.
168 * these functions return the number of weeks, starting on the
171 GLIB_AVAILABLE_IN_ALL
172 guint g_date_get_monday_week_of_year (const GDate *date);
173 GLIB_AVAILABLE_IN_ALL
174 guint g_date_get_sunday_week_of_year (const GDate *date);
175 GLIB_AVAILABLE_IN_ALL
176 guint g_date_get_iso8601_week_of_year (const GDate *date);
178 /* If you create a static date struct you need to clear it to get it
179 * in a sane state before use. You can clear a whole array at
180 * once with the ndates argument.
182 GLIB_AVAILABLE_IN_ALL
183 void g_date_clear (GDate *date,
186 /* The parse routine is meant for dates typed in by a user, so it
187 * permits many formats but tries to catch common typos. If your data
188 * needs to be strictly validated, it is not an appropriate function.
190 GLIB_AVAILABLE_IN_ALL
191 void g_date_set_parse (GDate *date,
193 GLIB_AVAILABLE_IN_ALL
194 void g_date_set_time_t (GDate *date,
196 GLIB_AVAILABLE_IN_ALL
197 void g_date_set_time_val (GDate *date,
199 #ifndef G_DISABLE_DEPRECATED
200 GLIB_DEPRECATED_FOR(g_date_set_time_t)
201 void g_date_set_time (GDate *date,
204 GLIB_AVAILABLE_IN_ALL
205 void g_date_set_month (GDate *date,
207 GLIB_AVAILABLE_IN_ALL
208 void g_date_set_day (GDate *date,
210 GLIB_AVAILABLE_IN_ALL
211 void g_date_set_year (GDate *date,
213 GLIB_AVAILABLE_IN_ALL
214 void g_date_set_dmy (GDate *date,
218 GLIB_AVAILABLE_IN_ALL
219 void g_date_set_julian (GDate *date,
220 guint32 julian_date);
221 GLIB_AVAILABLE_IN_ALL
222 gboolean g_date_is_first_of_month (const GDate *date);
223 GLIB_AVAILABLE_IN_ALL
224 gboolean g_date_is_last_of_month (const GDate *date);
226 /* To go forward by some number of weeks just go forward weeks*7 days */
227 GLIB_AVAILABLE_IN_ALL
228 void g_date_add_days (GDate *date,
230 GLIB_AVAILABLE_IN_ALL
231 void g_date_subtract_days (GDate *date,
234 /* If you add/sub months while day > 28, the day might change */
235 GLIB_AVAILABLE_IN_ALL
236 void g_date_add_months (GDate *date,
238 GLIB_AVAILABLE_IN_ALL
239 void g_date_subtract_months (GDate *date,
242 /* If it's feb 29, changing years can move you to the 28th */
243 GLIB_AVAILABLE_IN_ALL
244 void g_date_add_years (GDate *date,
246 GLIB_AVAILABLE_IN_ALL
247 void g_date_subtract_years (GDate *date,
249 GLIB_AVAILABLE_IN_ALL
250 gboolean g_date_is_leap_year (GDateYear year) G_GNUC_CONST;
251 GLIB_AVAILABLE_IN_ALL
252 guint8 g_date_get_days_in_month (GDateMonth month,
253 GDateYear year) G_GNUC_CONST;
254 GLIB_AVAILABLE_IN_ALL
255 guint8 g_date_get_monday_weeks_in_year (GDateYear year) G_GNUC_CONST;
256 GLIB_AVAILABLE_IN_ALL
257 guint8 g_date_get_sunday_weeks_in_year (GDateYear year) G_GNUC_CONST;
259 /* Returns the number of days between the two dates. If date2 comes
260 before date1, a negative value is return. */
261 GLIB_AVAILABLE_IN_ALL
262 gint g_date_days_between (const GDate *date1,
265 /* qsort-friendly (with a cast...) */
266 GLIB_AVAILABLE_IN_ALL
267 gint g_date_compare (const GDate *lhs,
269 GLIB_AVAILABLE_IN_ALL
270 void g_date_to_struct_tm (const GDate *date,
273 GLIB_AVAILABLE_IN_ALL
274 void g_date_clamp (GDate *date,
275 const GDate *min_date,
276 const GDate *max_date);
278 /* Swap date1 and date2's values if date1 > date2. */
279 GLIB_AVAILABLE_IN_ALL
280 void g_date_order (GDate *date1, GDate *date2);
282 /* Just like strftime() except you can only use date-related formats.
283 * Using a time format is undefined.
285 GLIB_AVAILABLE_IN_ALL
286 gsize g_date_strftime (gchar *s,
291 #ifndef G_DISABLE_DEPRECATED
293 #define g_date_weekday g_date_get_weekday
294 #define g_date_month g_date_get_month
295 #define g_date_year g_date_get_year
296 #define g_date_day g_date_get_day
297 #define g_date_julian g_date_get_julian
298 #define g_date_day_of_year g_date_get_day_of_year
299 #define g_date_monday_week_of_year g_date_get_monday_week_of_year
300 #define g_date_sunday_week_of_year g_date_get_sunday_week_of_year
301 #define g_date_days_in_month g_date_get_days_in_month
302 #define g_date_monday_weeks_in_year g_date_get_monday_weeks_in_year
303 #define g_date_sunday_weeks_in_year g_date_get_sunday_weeks_in_year
305 #endif /* G_DISABLE_DEPRECATED */
309 #endif /* __G_DATE_H__ */