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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
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/.
36 * Date calculations (not time for now, to be resolved). These are a
37 * mutant combination of Steffen Beyer's DateCalc routines
38 * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
39 * date routines (written for in-house software). Written by Havoc
40 * Pennington <hp@pobox.com>
44 typedef guint16 GDateYear;
45 typedef guint8 GDateDay; /* day of the month */
46 typedef struct _GDate GDate;
47 /* make struct tm known without having to include time.h */
50 /* enum used to specify order of appearance in parsed date strings */
58 /* actual week and month values */
61 G_DATE_BAD_WEEKDAY = 0,
87 #define G_DATE_BAD_JULIAN 0U
88 #define G_DATE_BAD_DAY 0U
89 #define G_DATE_BAD_YEAR 0U
91 /* Note: directly manipulating structs is generally a bad idea, but
92 * in this case it's an *incredibly* bad idea, because all or part
93 * of this struct can be invalid at any given time. Use the functions,
94 * or you will get hosed, I promise.
98 guint julian_days : 32; /* julian days representation - we use a
99 * bitfield hoping that 64 bit platforms
100 * will pack this whole struct in one big
104 guint julian : 1; /* julian is valid */
105 guint dmy : 1; /* dmy is valid */
107 /* DMY representation */
113 /* g_date_new() returns an invalid date, you then have to _set() stuff
114 * to get a usable object. You can also allocate a GDate statically,
115 * then call g_date_clear() to initialize.
117 GDate* g_date_new (void);
118 GDate* g_date_new_dmy (GDateDay day,
121 GDate* g_date_new_julian (guint32 julian_day);
122 void g_date_free (GDate *date);
124 /* check g_date_valid() after doing an operation that might fail, like
125 * _parse. Almost all g_date operations are undefined on invalid
126 * dates (the exceptions are the mutators, since you need those to
127 * return to validity).
129 gboolean g_date_valid (GDate *date);
130 gboolean g_date_valid_day (GDateDay day) G_GNUC_CONST;
131 gboolean g_date_valid_month (GDateMonth month) G_GNUC_CONST;
132 gboolean g_date_valid_year (GDateYear year) G_GNUC_CONST;
133 gboolean g_date_valid_weekday (GDateWeekday weekday) G_GNUC_CONST;
134 gboolean g_date_valid_julian (guint32 julian_date) G_GNUC_CONST;
135 gboolean g_date_valid_dmy (GDateDay day,
137 GDateYear year) G_GNUC_CONST;
139 GDateWeekday g_date_get_weekday (GDate *date);
140 GDateMonth g_date_get_month (GDate *date);
141 GDateYear g_date_get_year (GDate *date);
142 GDateDay g_date_get_day (GDate *date);
143 guint32 g_date_get_julian (GDate *date);
144 guint g_date_get_day_of_year (GDate *date);
145 /* First monday/sunday is the start of week 1; if we haven't reached
146 * that day, return 0. These are not ISO weeks of the year; that
147 * routine needs to be added.
148 * these functions return the number of weeks, starting on the
151 guint g_date_get_monday_week_of_year (GDate *date);
152 guint g_date_get_sunday_week_of_year (GDate *date);
154 /* If you create a static date struct you need to clear it to get it
155 * in a sane state before use. You can clear a whole array at
156 * once with the ndates argument.
158 void g_date_clear (GDate *date,
161 /* The parse routine is meant for dates typed in by a user, so it
162 * permits many formats but tries to catch common typos. If your data
163 * needs to be strictly validated, it is not an appropriate function.
165 void g_date_set_parse (GDate *date,
167 void g_date_set_time (GDate *date,
169 void g_date_set_month (GDate *date,
171 void g_date_set_day (GDate *date,
173 void g_date_set_year (GDate *date,
175 void g_date_set_dmy (GDate *date,
179 void g_date_set_julian (GDate *date,
180 guint32 julian_date);
181 gboolean g_date_is_first_of_month (GDate *date);
182 gboolean g_date_is_last_of_month (GDate *date);
184 /* To go forward by some number of weeks just go forward weeks*7 days */
185 void g_date_add_days (GDate *date,
187 void g_date_subtract_days (GDate *date,
190 /* If you add/sub months while day > 28, the day might change */
191 void g_date_add_months (GDate *date,
193 void g_date_subtract_months (GDate *date,
196 /* If it's feb 29, changing years can move you to the 28th */
197 void g_date_add_years (GDate *date,
199 void g_date_subtract_years (GDate *date,
201 gboolean g_date_is_leap_year (GDateYear year) G_GNUC_CONST;
202 guint8 g_date_get_days_in_month (GDateMonth month,
203 GDateYear year) G_GNUC_CONST;
204 guint8 g_date_get_monday_weeks_in_year (GDateYear year) G_GNUC_CONST;
205 guint8 g_date_get_sunday_weeks_in_year (GDateYear year) G_GNUC_CONST;
207 /* qsort-friendly (with a cast...) */
208 gint g_date_compare (GDate *lhs,
210 void g_date_to_struct_tm (GDate *date,
213 /* Just like strftime() except you can only use date-related formats.
214 * Using a time format is undefined.
216 gsize g_date_strftime (gchar *s,
223 #endif /* __G_DATE_H__ */