1 /*
2  * Distributed under the Boost Software License, Version 1.0.
3  *    (See accompanying file LICENSE_1_0.txt or copy at
4  *          http://www.boost.org/LICENSE_1_0.txt)
5  */
6 module glib.gdate;
7 
8 import glib.gtypes;
9 import glib.gquark;
10 
11 import core.stdc.time;
12 import std.bitmanip;
13 
14 
15 alias GTime = gint32;
16 alias GDateYear = guint16;
17 alias GDateDay = guint8;
18 
19 enum GDateDMY
20 {
21   G_DATE_DAY   = 0,
22   G_DATE_MONTH = 1,
23   G_DATE_YEAR  = 2
24 }
25 
26 enum GDateWeekday
27 {
28   G_DATE_BAD_WEEKDAY  = 0,
29   G_DATE_MONDAY       = 1,
30   G_DATE_TUESDAY      = 2,
31   G_DATE_WEDNESDAY    = 3,
32   G_DATE_THURSDAY     = 4,
33   G_DATE_FRIDAY       = 5,
34   G_DATE_SATURDAY     = 6,
35   G_DATE_SUNDAY       = 7
36 }
37 
38 enum GDateMonth
39 {
40   G_DATE_BAD_MONTH = 0,
41   G_DATE_JANUARY   = 1,
42   G_DATE_FEBRUARY  = 2,
43   G_DATE_MARCH     = 3,
44   G_DATE_APRIL     = 4,
45   G_DATE_MAY       = 5,
46   G_DATE_JUNE      = 6,
47   G_DATE_JULY      = 7,
48   G_DATE_AUGUST    = 8,
49   G_DATE_SEPTEMBER = 9,
50   G_DATE_OCTOBER   = 10,
51   G_DATE_NOVEMBER  = 11,
52   G_DATE_DECEMBER  = 12
53 }
54 
55 enum uint G_DATE_BAD_JULIAN = 0;
56 enum uint G_DATE_BAD_DAY = 0;
57 enum uint G_DATE_BAD_YEAR = 0;
58 
59 
60 struct GDate
61 {
62     mixin(bitfields!(
63                 guint, "julian_days", 32,
64                 guint, "julian", 1,
65                 guint, "dmy", 1,
66                 guint, "day", 6,
67                 guint, "month", 4,
68                 guint, "year", 16,
69                 guint, "", 4));
70 
71 };
72 
73 extern (C) {
74 
75     GDate*       g_date_new                   ();
76 
77     GDate*       g_date_new_dmy               (GDateDay     day,
78                                                GDateMonth   month,
79                                                GDateYear    year);
80 
81     GDate*       g_date_new_julian            (guint32      julian_day);
82 
83     void         g_date_free                  (GDate       *date);
84 
85     /* check g_date_valid() after doing an operation that might fail, like
86      * _parse.  Almost all g_date operations are undefined on invalid
87      * dates (the exceptions are the mutators, since you need those to
88      * return to validity).
89      */
90 
91     gboolean     g_date_valid                 (const(GDate)* date);
92 
93     gboolean     g_date_valid_day             (GDateDay     day);
94 
95     gboolean     g_date_valid_month           (GDateMonth month);
96 
97     gboolean     g_date_valid_year            (GDateYear  year);
98 
99     gboolean     g_date_valid_weekday         (GDateWeekday weekday);
100 
101     gboolean     g_date_valid_julian          (guint32 julian_date);
102 
103     gboolean     g_date_valid_dmy             (GDateDay     day,
104                                                GDateMonth   month,
105                                                GDateYear    year);
106 
107 
108     GDateWeekday g_date_get_weekday           (const(GDate) *date);
109 
110     GDateMonth   g_date_get_month             (const(GDate) *date);
111 
112     GDateYear    g_date_get_year              (const(GDate) *date);
113 
114     GDateDay     g_date_get_day               (const(GDate) *date);
115 
116     guint32      g_date_get_julian            (const(GDate) *date);
117 
118     guint        g_date_get_day_of_year       (const(GDate) *date);
119     /* First monday/sunday is the start of week 1; if we haven't reached
120      * that day, return 0. These are not ISO weeks of the year; that
121      * routine needs to be added.
122      * these functions return the number of weeks, starting on the
123      * corrsponding day
124      */
125 
126     guint        g_date_get_monday_week_of_year (const(GDate) *date);
127 
128     guint        g_date_get_sunday_week_of_year (const(GDate) *date);
129 
130     guint        g_date_get_iso8601_week_of_year (const(GDate) *date);
131 
132     /* If you create a static date struct you need to clear it to get it
133      * in a sane state before use. You can clear a whole array at
134      * once with the ndates argument.
135      */
136 
137     void         g_date_clear                 (GDate       *date,
138                                                guint        n_dates);
139 
140     /* The parse routine is meant for dates typed in by a user, so it
141      * permits many formats but tries to catch common typos. If your data
142      * needs to be strictly validated, it is not an appropriate function.
143      */
144 
145     void         g_date_set_parse             (GDate       *date,
146                                                const(gchar) *str);
147 
148     void         g_date_set_time_t            (GDate       *date,
149                            time_t       timet);
150 
151     void         g_date_set_time_val          (GDate       *date,
152                            GTimeVal    *timeval);
153 
154     deprecated("use g_date_set_time_t")
155     void         g_date_set_time              (GDate       *date,
156                                                GTime        time_);
157 
158     void         g_date_set_month             (GDate       *date,
159                                                GDateMonth   month);
160 
161     void         g_date_set_day               (GDate       *date,
162                                                GDateDay     day);
163 
164     void         g_date_set_year              (GDate       *date,
165                                                GDateYear    year);
166 
167     void         g_date_set_dmy               (GDate       *date,
168                                                GDateDay     day,
169                                                GDateMonth   month,
170                                                GDateYear    y);
171 
172     void         g_date_set_julian            (GDate       *date,
173                                                guint32      julian_date);
174 
175     gboolean     g_date_is_first_of_month     (const(GDate) *date);
176 
177     gboolean     g_date_is_last_of_month      (const(GDate) *date);
178 
179     /* To go forward by some number of weeks just go forward weeks*7 days */
180 
181     void         g_date_add_days              (GDate       *date,
182                                                guint        n_days);
183 
184     void         g_date_subtract_days         (GDate       *date,
185                                                guint        n_days);
186 
187     /* If you add/sub months while day > 28, the day might change */
188 
189     void         g_date_add_months            (GDate       *date,
190                                                guint        n_months);
191 
192     void         g_date_subtract_months       (GDate       *date,
193                                                guint        n_months);
194 
195     /* If it's feb 29, changing years can move you to the 28th */
196 
197     void         g_date_add_years             (GDate       *date,
198                                                guint        n_years);
199 
200     void         g_date_subtract_years        (GDate       *date,
201                                                guint        n_years);
202 
203     gboolean     g_date_is_leap_year          (GDateYear    year);
204 
205     guint8       g_date_get_days_in_month     (GDateMonth   month,
206                                                GDateYear    year);
207 
208     guint8       g_date_get_monday_weeks_in_year  (GDateYear    year);
209 
210     guint8       g_date_get_sunday_weeks_in_year  (GDateYear    year);
211 
212     /* Returns the number of days between the two dates.  If date2 comes
213        before date1, a negative value is return. */
214 
215     gint         g_date_days_between          (const(GDate) *date1,
216                            const(GDate) *date2);
217 
218     /* qsort-friendly (with a cast...) */
219 
220     gint         g_date_compare               (const(GDate) *lhs,
221                                                const(GDate) *rhs);
222 
223     void         g_date_to_struct_tm          (const(GDate) *date,
224                                                tm   *tm_);
225 
226 
227     void         g_date_clamp                 (GDate *date,
228                            const(GDate) *min_date,
229                            const(GDate) *max_date);
230 
231     /* Swap date1 and date2's values if date1 > date2. */
232 
233     void         g_date_order                 (GDate *date1, GDate *date2);
234 
235     /* Just like strftime() except you can only use date-related formats.
236      *   Using a time format is undefined.
237      */
238 
239     gsize        g_date_strftime              (gchar       *s,
240                                                gsize        slen,
241                                                const(gchar) *format,
242                                                const(GDate) *date);
243 }
244