Add tests for new '%z' g_date_time_format extensions
[platform/upstream/glib.git] / glib / tests / gdatetime.c
1 /* gdatetime-tests.c
2  *
3  * Copyright (C) 2009-2010 Christian Hergert <chris@dronelabs.com>
4  *
5  * This is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #include "config.h"
21
22 #include <string.h>
23 #include <time.h>
24 #include <glib.h>
25 #include <glib/gstdio.h>
26 #include <locale.h>
27
28 #define ASSERT_DATE(dt,y,m,d) G_STMT_START { \
29   g_assert_cmpint ((y), ==, g_date_time_get_year ((dt))); \
30   g_assert_cmpint ((m), ==, g_date_time_get_month ((dt))); \
31   g_assert_cmpint ((d), ==, g_date_time_get_day_of_month ((dt))); \
32 } G_STMT_END
33 #define ASSERT_TIME(dt,H,M,S) G_STMT_START { \
34   g_assert_cmpint ((H), ==, g_date_time_get_hour ((dt))); \
35   g_assert_cmpint ((M), ==, g_date_time_get_minute ((dt))); \
36   g_assert_cmpint ((S), ==, g_date_time_get_second ((dt))); \
37 } G_STMT_END
38
39 static void
40 get_localtime_tm (time_t     time_,
41                   struct tm *retval)
42 {
43 #ifdef HAVE_LOCALTIME_R
44   localtime_r (&time_, retval);
45 #else
46   {
47     struct tm *ptm = localtime (&time_);
48
49     if (ptm == NULL)
50       {
51         /* Happens at least in Microsoft's C library if you pass a
52          * negative time_t. Use 2000-01-01 as default date.
53          */
54 #ifndef G_DISABLE_CHECKS
55         g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, "ptm != NULL");
56 #endif
57
58         retval->tm_mon = 0;
59         retval->tm_mday = 1;
60         retval->tm_year = 100;
61       }
62     else
63       memcpy ((void *) retval, (void *) ptm, sizeof (struct tm));
64   }
65 #endif /* HAVE_LOCALTIME_R */
66 }
67
68 static void
69 test_GDateTime_now (void)
70 {
71   GDateTime *dt;
72   struct tm tm;
73
74   memset (&tm, 0, sizeof (tm));
75   get_localtime_tm (time (NULL), &tm);
76
77   dt = g_date_time_new_now_local ();
78
79   g_assert_cmpint (g_date_time_get_year (dt), ==, 1900 + tm.tm_year);
80   g_assert_cmpint (g_date_time_get_month (dt), ==, 1 + tm.tm_mon);
81   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, tm.tm_mday);
82   g_assert_cmpint (g_date_time_get_hour (dt), ==, tm.tm_hour);
83   g_assert_cmpint (g_date_time_get_minute (dt), ==, tm.tm_min);
84   /* XXX we need some fuzzyness here */
85   g_assert_cmpint (g_date_time_get_second (dt), >=, tm.tm_sec);
86
87   g_date_time_unref (dt);
88 }
89
90 static void
91 test_GDateTime_new_from_unix (void)
92 {
93   GDateTime *dt;
94   struct tm  tm;
95   time_t     t;
96
97   memset (&tm, 0, sizeof (tm));
98   t = time (NULL);
99   get_localtime_tm (t, &tm);
100
101   dt = g_date_time_new_from_unix_local (t);
102   g_assert_cmpint (g_date_time_get_year (dt), ==, 1900 + tm.tm_year);
103   g_assert_cmpint (g_date_time_get_month (dt), ==, 1 + tm.tm_mon);
104   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, tm.tm_mday);
105   g_assert_cmpint (g_date_time_get_hour (dt), ==, tm.tm_hour);
106   g_assert_cmpint (g_date_time_get_minute (dt), ==, tm.tm_min);
107   g_assert_cmpint (g_date_time_get_second (dt), ==, tm.tm_sec);
108   g_date_time_unref (dt);
109
110   memset (&tm, 0, sizeof (tm));
111   tm.tm_year = 90;
112   tm.tm_mday = 1;
113   tm.tm_mon = 0;
114   tm.tm_hour = 0;
115   tm.tm_min = 0;
116   tm.tm_sec = 0;
117   tm.tm_isdst = -1;
118   t = mktime (&tm);
119
120   dt = g_date_time_new_from_unix_local (t);
121   g_assert_cmpint (g_date_time_get_year (dt), ==, 1990);
122   g_assert_cmpint (g_date_time_get_month (dt), ==, 1);
123   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 1);
124   g_assert_cmpint (g_date_time_get_hour (dt), ==, 0);
125   g_assert_cmpint (g_date_time_get_minute (dt), ==, 0);
126   g_assert_cmpint (g_date_time_get_second (dt), ==, 0);
127   g_date_time_unref (dt);
128 }
129
130 static void
131 test_GDateTime_invalid (void)
132 {
133   GDateTime *dt;
134
135   g_test_bug ("702674");
136
137   dt = g_date_time_new_utc (2013, -2147483647, 31, 17, 15, 48);
138   g_assert (dt == NULL);
139 }
140
141 static void
142 test_GDateTime_compare (void)
143 {
144   GDateTime *dt1, *dt2;
145   gint       i;
146
147   dt1 = g_date_time_new_utc (2000, 1, 1, 0, 0, 0);
148
149   for (i = 1; i < 2000; i++)
150     {
151       dt2 = g_date_time_new_utc (i, 12, 31, 0, 0, 0);
152       g_assert_cmpint (1, ==, g_date_time_compare (dt1, dt2));
153       g_date_time_unref (dt2);
154     }
155
156   dt2 = g_date_time_new_utc (1999, 12, 31, 23, 59, 59);
157   g_assert_cmpint (1, ==, g_date_time_compare (dt1, dt2));
158   g_date_time_unref (dt2);
159
160   dt2 = g_date_time_new_utc (2000, 1, 1, 0, 0, 1);
161   g_assert_cmpint (-1, ==, g_date_time_compare (dt1, dt2));
162   g_date_time_unref (dt2);
163
164   dt2 = g_date_time_new_utc (2000, 1, 1, 0, 0, 0);
165   g_assert_cmpint (0, ==, g_date_time_compare (dt1, dt2));
166   g_date_time_unref (dt2);
167   g_date_time_unref (dt1);
168 }
169
170 static void
171 test_GDateTime_equal (void)
172 {
173   GDateTime *dt1, *dt2;
174   GTimeZone *tz;
175
176   dt1 = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
177   dt2 = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
178   g_assert (g_date_time_equal (dt1, dt2));
179   g_date_time_unref (dt1);
180   g_date_time_unref (dt2);
181
182   dt1 = g_date_time_new_local (2009, 10, 18, 0, 0, 0);
183   dt2 = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
184   g_assert (!g_date_time_equal (dt1, dt2));
185   g_date_time_unref (dt1);
186   g_date_time_unref (dt2);
187
188   /* UTC-0300 and not in DST */
189   tz = g_time_zone_new ("-03:00");
190   dt1 = g_date_time_new (tz, 2010, 5, 24,  8, 0, 0);
191   g_time_zone_unref (tz);
192   g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, (-3 * 3600));
193   /* UTC */
194   dt2 = g_date_time_new_utc (2010, 5, 24, 11, 0, 0);
195   g_assert_cmpint (g_date_time_get_utc_offset (dt2), ==, 0);
196
197   g_assert (g_date_time_equal (dt1, dt2));
198   g_date_time_unref (dt1);
199
200   /* America/Recife is in UTC-0300 */
201 #ifdef G_OS_UNIX
202   tz = g_time_zone_new ("America/Recife");
203 #elif defined G_OS_WIN32
204   tz = g_time_zone_new ("E. South America Standard Time");
205 #endif
206   dt1 = g_date_time_new (tz, 2010, 5, 24,  8, 0, 0);
207   g_time_zone_unref (tz);
208   g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, (-3 * 3600));
209   g_assert (g_date_time_equal (dt1, dt2));
210   g_date_time_unref (dt1);
211   g_date_time_unref (dt2);
212 }
213
214 static void
215 test_GDateTime_get_day_of_week (void)
216 {
217   GDateTime *dt;
218
219   dt = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
220   g_assert_cmpint (1, ==, g_date_time_get_day_of_week (dt));
221   g_date_time_unref (dt);
222
223   dt = g_date_time_new_local (2000, 10, 1, 0, 0, 0);
224   g_assert_cmpint (7, ==, g_date_time_get_day_of_week (dt));
225   g_date_time_unref (dt);
226 }
227
228 static void
229 test_GDateTime_get_day_of_month (void)
230 {
231   GDateTime *dt;
232
233   dt = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
234   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 19);
235   g_date_time_unref (dt);
236
237   dt = g_date_time_new_local (1400, 3, 12, 0, 0, 0);
238   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 12);
239   g_date_time_unref (dt);
240
241   dt = g_date_time_new_local (1800, 12, 31, 0, 0, 0);
242   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 31);
243   g_date_time_unref (dt);
244
245   dt = g_date_time_new_local (1000, 1, 1, 0, 0, 0);
246   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 1);
247   g_date_time_unref (dt);
248 }
249
250 static void
251 test_GDateTime_get_hour (void)
252 {
253   GDateTime *dt;
254
255   dt = g_date_time_new_utc (2009, 10, 19, 15, 13, 11);
256   g_assert_cmpint (15, ==, g_date_time_get_hour (dt));
257   g_date_time_unref (dt);
258
259   dt = g_date_time_new_utc (100, 10, 19, 1, 0, 0);
260   g_assert_cmpint (1, ==, g_date_time_get_hour (dt));
261   g_date_time_unref (dt);
262
263   dt = g_date_time_new_utc (100, 10, 19, 0, 0, 0);
264   g_assert_cmpint (0, ==, g_date_time_get_hour (dt));
265   g_date_time_unref (dt);
266
267   dt = g_date_time_new_utc (100, 10, 1, 23, 59, 59);
268   g_assert_cmpint (23, ==, g_date_time_get_hour (dt));
269   g_date_time_unref (dt);
270 }
271
272 static void
273 test_GDateTime_get_microsecond (void)
274 {
275   GTimeVal   tv;
276   GDateTime *dt;
277
278   g_get_current_time (&tv);
279   dt = g_date_time_new_from_timeval_local (&tv);
280   g_assert_cmpint (tv.tv_usec, ==, g_date_time_get_microsecond (dt));
281   g_date_time_unref (dt);
282 }
283
284 static void
285 test_GDateTime_get_year (void)
286 {
287   GDateTime *dt;
288
289   dt = g_date_time_new_local (2009, 1, 1, 0, 0, 0);
290   g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
291   g_date_time_unref (dt);
292
293   dt = g_date_time_new_local (1, 1, 1, 0, 0, 0);
294   g_assert_cmpint (1, ==, g_date_time_get_year (dt));
295   g_date_time_unref (dt);
296
297   dt = g_date_time_new_local (13, 1, 1, 0, 0, 0);
298   g_assert_cmpint (13, ==, g_date_time_get_year (dt));
299   g_date_time_unref (dt);
300
301   dt = g_date_time_new_local (3000, 1, 1, 0, 0, 0);
302   g_assert_cmpint (3000, ==, g_date_time_get_year (dt));
303   g_date_time_unref (dt);
304 }
305
306 static void
307 test_GDateTime_hash (void)
308 {
309   GHashTable *h;
310
311   h = g_hash_table_new_full (g_date_time_hash, g_date_time_equal,
312                              (GDestroyNotify)g_date_time_unref,
313                              NULL);
314   g_hash_table_insert (h, g_date_time_new_now_local (), NULL);
315   g_hash_table_remove_all (h);
316   g_hash_table_destroy (h);
317 }
318
319 static void
320 test_GDateTime_new_from_timeval (void)
321 {
322   GDateTime *dt;
323   GTimeVal   tv, tv2;
324
325   g_get_current_time (&tv);
326   dt = g_date_time_new_from_timeval_local (&tv);
327
328   if (g_test_verbose ())
329     g_print ("\nDT%04d-%02d-%02dT%02d:%02d:%02d%s\n",
330              g_date_time_get_year (dt),
331              g_date_time_get_month (dt),
332              g_date_time_get_day_of_month (dt),
333              g_date_time_get_hour (dt),
334              g_date_time_get_minute (dt),
335              g_date_time_get_second (dt),
336              g_date_time_get_timezone_abbreviation (dt));
337
338   g_date_time_to_timeval (dt, &tv2);
339   g_assert_cmpint (tv.tv_sec, ==, tv2.tv_sec);
340   g_assert_cmpint (tv.tv_usec, ==, tv2.tv_usec);
341   g_date_time_unref (dt);
342 }
343
344 static void
345 test_GDateTime_new_from_timeval_utc (void)
346 {
347   GDateTime *dt;
348   GTimeVal   tv, tv2;
349
350   g_get_current_time (&tv);
351   dt = g_date_time_new_from_timeval_utc (&tv);
352
353   if (g_test_verbose ())
354     g_print ("\nDT%04d-%02d-%02dT%02d:%02d:%02d%s\n",
355              g_date_time_get_year (dt),
356              g_date_time_get_month (dt),
357              g_date_time_get_day_of_month (dt),
358              g_date_time_get_hour (dt),
359              g_date_time_get_minute (dt),
360              g_date_time_get_second (dt),
361              g_date_time_get_timezone_abbreviation (dt));
362
363   g_date_time_to_timeval (dt, &tv2);
364   g_assert_cmpint (tv.tv_sec, ==, tv2.tv_sec);
365   g_assert_cmpint (tv.tv_usec, ==, tv2.tv_usec);
366   g_date_time_unref (dt);
367 }
368
369 static void
370 test_GDateTime_to_unix (void)
371 {
372   GDateTime *dt;
373   time_t     t;
374
375   t = time (NULL);
376   dt = g_date_time_new_from_unix_local (t);
377   g_assert_cmpint (g_date_time_to_unix (dt), ==, t);
378   g_date_time_unref (dt);
379 }
380
381 static void
382 test_GDateTime_add_years (void)
383 {
384   GDateTime *dt, *dt2;
385
386   dt = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
387   dt2 = g_date_time_add_years (dt, 1);
388   g_assert_cmpint (2010, ==, g_date_time_get_year (dt2));
389   g_date_time_unref (dt);
390   g_date_time_unref (dt2);
391 }
392
393 static void
394 test_GDateTime_add_months (void)
395 {
396 #define TEST_ADD_MONTHS(y,m,d,a,ny,nm,nd) G_STMT_START { \
397   GDateTime *dt, *dt2; \
398   dt = g_date_time_new_utc (y, m, d, 0, 0, 0); \
399   dt2 = g_date_time_add_months (dt, a); \
400   ASSERT_DATE (dt2, ny, nm, nd); \
401   g_date_time_unref (dt); \
402   g_date_time_unref (dt2); \
403 } G_STMT_END
404
405   TEST_ADD_MONTHS (2009, 12, 31,    1, 2010, 1, 31);
406   TEST_ADD_MONTHS (2009, 12, 31,    1, 2010, 1, 31);
407   TEST_ADD_MONTHS (2009,  6, 15,    1, 2009, 7, 15);
408   TEST_ADD_MONTHS (1400,  3,  1,    1, 1400, 4,  1);
409   TEST_ADD_MONTHS (1400,  1, 31,    1, 1400, 2, 28);
410   TEST_ADD_MONTHS (1400,  1, 31, 7200, 2000, 1, 31);
411   TEST_ADD_MONTHS (2008,  2, 29,   12, 2009, 2, 28);
412   TEST_ADD_MONTHS (2000,  8, 16,   -5, 2000, 3, 16);
413   TEST_ADD_MONTHS (2000,  8, 16,  -12, 1999, 8, 16);
414   TEST_ADD_MONTHS (2011,  2,  1,  -13, 2010, 1,  1);
415   TEST_ADD_MONTHS (1776,  7,  4, 1200, 1876, 7,  4);
416 }
417
418 static void
419 test_GDateTime_add_days (void)
420 {
421 #define TEST_ADD_DAYS(y,m,d,a,ny,nm,nd) G_STMT_START { \
422   GDateTime *dt, *dt2; \
423   dt = g_date_time_new_local (y, m, d, 0, 0, 0); \
424   dt2 = g_date_time_add_days (dt, a); \
425   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
426   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
427   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
428   g_date_time_unref (dt); \
429   g_date_time_unref (dt2); \
430 } G_STMT_END
431
432   TEST_ADD_DAYS (2009, 1, 31, 1, 2009, 2, 1);
433   TEST_ADD_DAYS (2009, 2, 1, -1, 2009, 1, 31);
434   TEST_ADD_DAYS (2008, 2, 28, 1, 2008, 2, 29);
435   TEST_ADD_DAYS (2008, 12, 31, 1, 2009, 1, 1);
436   TEST_ADD_DAYS (1, 1, 1, 1, 1, 1, 2);
437   TEST_ADD_DAYS (1955, 5, 24, 10, 1955, 6, 3);
438   TEST_ADD_DAYS (1955, 5, 24, -10, 1955, 5, 14);
439 }
440
441 static void
442 test_GDateTime_add_weeks (void)
443 {
444 #define TEST_ADD_WEEKS(y,m,d,a,ny,nm,nd) G_STMT_START { \
445   GDateTime *dt, *dt2; \
446   dt = g_date_time_new_local (y, m, d, 0, 0, 0); \
447   dt2 = g_date_time_add_weeks (dt, a); \
448   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
449   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
450   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
451   g_date_time_unref (dt); \
452   g_date_time_unref (dt2); \
453 } G_STMT_END
454
455   TEST_ADD_WEEKS (2009, 1, 1, 1, 2009, 1, 8);
456   TEST_ADD_WEEKS (2009, 8, 30, 1, 2009, 9, 6);
457   TEST_ADD_WEEKS (2009, 12, 31, 1, 2010, 1, 7);
458   TEST_ADD_WEEKS (2009, 1, 1, -1, 2008, 12, 25);
459 }
460
461 static void
462 test_GDateTime_add_hours (void)
463 {
464 #define TEST_ADD_HOURS(y,m,d,h,mi,s,a,ny,nm,nd,nh,nmi,ns) G_STMT_START { \
465   GDateTime *dt, *dt2; \
466   dt = g_date_time_new_utc (y, m, d, h, mi, s); \
467   dt2 = g_date_time_add_hours (dt, a); \
468   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
469   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
470   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
471   g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
472   g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
473   g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
474   g_date_time_unref (dt); \
475   g_date_time_unref (dt2); \
476 } G_STMT_END
477
478   TEST_ADD_HOURS (2009,  1,  1,  0, 0, 0, 1, 2009, 1, 1, 1, 0, 0);
479   TEST_ADD_HOURS (2008, 12, 31, 23, 0, 0, 1, 2009, 1, 1, 0, 0, 0);
480 }
481
482 static void
483 test_GDateTime_add_full (void)
484 {
485 #define TEST_ADD_FULL(y,m,d,h,mi,s,ay,am,ad,ah,ami,as,ny,nm,nd,nh,nmi,ns) G_STMT_START { \
486   GDateTime *dt, *dt2; \
487   dt = g_date_time_new_utc (y, m, d, h, mi, s); \
488   dt2 = g_date_time_add_full (dt, ay, am, ad, ah, ami, as); \
489   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
490   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
491   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
492   g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
493   g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
494   g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
495   g_date_time_unref (dt); \
496   g_date_time_unref (dt2); \
497 } G_STMT_END
498
499   TEST_ADD_FULL (2009, 10, 21,  0,  0, 0,
500                     1,  1,  1,  1,  1, 1,
501                  2010, 11, 22,  1,  1, 1);
502   TEST_ADD_FULL (2000,  1,  1,  1,  1, 1,
503                     0,  1,  0,  0,  0, 0,
504                  2000,  2,  1,  1,  1, 1);
505   TEST_ADD_FULL (2000,  1,  1,  0,  0, 0,
506                    -1,  1,  0,  0,  0, 0,
507                  1999,  2,  1,  0,  0, 0);
508   TEST_ADD_FULL (2010, 10, 31,  0,  0, 0,
509                     0,  4,  0,  0,  0, 0,
510                  2011,  2, 28,  0,  0, 0);
511   TEST_ADD_FULL (2010,  8, 25, 22, 45, 0,
512                     0,  1,  6,  1, 25, 0,
513                  2010, 10,  2,  0, 10, 0);
514 }
515
516 static void
517 test_GDateTime_add_minutes (void)
518 {
519 #define TEST_ADD_MINUTES(i,o) G_STMT_START { \
520   GDateTime *dt, *dt2; \
521   dt = g_date_time_new_local (2000, 1, 1, 0, 0, 0); \
522   dt2 = g_date_time_add_minutes (dt, i); \
523   g_assert_cmpint (o, ==, g_date_time_get_minute (dt2)); \
524   g_date_time_unref (dt); \
525   g_date_time_unref (dt2); \
526 } G_STMT_END
527
528   TEST_ADD_MINUTES (60, 0);
529   TEST_ADD_MINUTES (100, 40);
530   TEST_ADD_MINUTES (5, 5);
531   TEST_ADD_MINUTES (1441, 1);
532   TEST_ADD_MINUTES (-1441, 59);
533 }
534
535 static void
536 test_GDateTime_add_seconds (void)
537 {
538 #define TEST_ADD_SECONDS(i,o) G_STMT_START { \
539   GDateTime *dt, *dt2; \
540   dt = g_date_time_new_local (2000, 1, 1, 0, 0, 0); \
541   dt2 = g_date_time_add_seconds (dt, i); \
542   g_assert_cmpint (o, ==, g_date_time_get_second (dt2)); \
543   g_date_time_unref (dt); \
544   g_date_time_unref (dt2); \
545 } G_STMT_END
546
547   TEST_ADD_SECONDS (1, 1);
548   TEST_ADD_SECONDS (60, 0);
549   TEST_ADD_SECONDS (61, 1);
550   TEST_ADD_SECONDS (120, 0);
551   TEST_ADD_SECONDS (-61, 59);
552   TEST_ADD_SECONDS (86401, 1);
553   TEST_ADD_SECONDS (-86401, 59);
554   TEST_ADD_SECONDS (-31, 29);
555   TEST_ADD_SECONDS (13, 13);
556 }
557
558 static void
559 test_GDateTime_diff (void)
560 {
561 #define TEST_DIFF(y,m,d,y2,m2,d2,u) G_STMT_START { \
562   GDateTime *dt1, *dt2; \
563   GTimeSpan  ts = 0; \
564   dt1 = g_date_time_new_utc (y, m, d, 0, 0, 0); \
565   dt2 = g_date_time_new_utc (y2, m2, d2, 0, 0, 0); \
566   ts = g_date_time_difference (dt2, dt1); \
567   g_assert_cmpint (ts, ==, u); \
568   g_date_time_unref (dt1); \
569   g_date_time_unref (dt2); \
570 } G_STMT_END
571
572   TEST_DIFF (2009, 1, 1, 2009, 2, 1, G_TIME_SPAN_DAY * 31);
573   TEST_DIFF (2009, 1, 1, 2010, 1, 1, G_TIME_SPAN_DAY * 365);
574   TEST_DIFF (2008, 2, 28, 2008, 2, 29, G_TIME_SPAN_DAY);
575   TEST_DIFF (2008, 2, 29, 2008, 2, 28, -G_TIME_SPAN_DAY);
576
577   /* TODO: Add usec tests */
578 }
579
580 static void
581 test_GDateTime_get_minute (void)
582 {
583   GDateTime *dt;
584
585   dt = g_date_time_new_utc (2009, 12, 1, 1, 31, 0);
586   g_assert_cmpint (31, ==, g_date_time_get_minute (dt));
587   g_date_time_unref (dt);
588 }
589
590 static void
591 test_GDateTime_get_month (void)
592 {
593   GDateTime *dt;
594
595   dt = g_date_time_new_utc (2009, 12, 1, 1, 31, 0);
596   g_assert_cmpint (12, ==, g_date_time_get_month (dt));
597   g_date_time_unref (dt);
598 }
599
600 static void
601 test_GDateTime_get_second (void)
602 {
603   GDateTime *dt;
604
605   dt = g_date_time_new_utc (2009, 12, 1, 1, 31, 44);
606   g_assert_cmpint (44, ==, g_date_time_get_second (dt));
607   g_date_time_unref (dt);
608 }
609
610 static void
611 test_GDateTime_new_full (void)
612 {
613   GTimeZone *tz;
614   GDateTime *dt;
615
616   dt = g_date_time_new_utc (2009, 12, 11, 12, 11, 10);
617   g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
618   g_assert_cmpint (12, ==, g_date_time_get_month (dt));
619   g_assert_cmpint (11, ==, g_date_time_get_day_of_month (dt));
620   g_assert_cmpint (12, ==, g_date_time_get_hour (dt));
621   g_assert_cmpint (11, ==, g_date_time_get_minute (dt));
622   g_assert_cmpint (10, ==, g_date_time_get_second (dt));
623   g_date_time_unref (dt);
624
625 #ifdef G_OS_UNIX
626   tz = g_time_zone_new ("America/Recife");
627 #elif defined G_OS_WIN32
628   tz = g_time_zone_new ("E. South America Standard Time");
629 #endif
630   dt = g_date_time_new (tz, 2010, 5, 24, 8, 4, 0);
631   g_time_zone_unref (tz);
632   g_assert_cmpint (2010, ==, g_date_time_get_year (dt));
633   g_assert_cmpint (5, ==, g_date_time_get_month (dt));
634   g_assert_cmpint (24, ==, g_date_time_get_day_of_month (dt));
635   g_assert_cmpint (8, ==, g_date_time_get_hour (dt));
636   g_assert_cmpint (4, ==, g_date_time_get_minute (dt));
637   g_assert_cmpint (0, ==, g_date_time_get_second (dt));
638 #ifdef G_OS_UNIX
639   g_assert_cmpstr ("BRT", ==, g_date_time_get_timezone_abbreviation (dt));
640 #elif defined G_OS_WIN32
641   g_assert_cmpstr ("E. South America Standard Time", ==,
642                     g_date_time_get_timezone_abbreviation (dt));
643 #endif
644   g_assert (!g_date_time_is_daylight_savings (dt));
645   g_date_time_unref (dt);
646 }
647
648 static void
649 test_GDateTime_now_utc (void)
650 {
651   GDateTime *dt;
652   time_t     t;
653   struct tm  tm;
654
655   t = time (NULL);
656 #ifdef HAVE_GMTIME_R
657   gmtime_r (&t, &tm);
658 #else
659   {
660     struct tm *tmp = gmtime (&t);
661     /* Assume gmtime() can't fail as we got t from time(NULL). (Note
662      * that on Windows, gmtime() *is* MT-safe, it uses a thread-local
663      * buffer.)
664      */
665     memcpy (&tm, tmp, sizeof (struct tm));
666   }
667 #endif
668   dt = g_date_time_new_now_utc ();
669   g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
670   g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
671   g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
672   g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
673   g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
674   g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
675   g_date_time_unref (dt);
676 }
677
678 static void
679 test_GDateTime_new_from_unix_utc (void)
680 {
681   GDateTime *dt;
682   gint64 t;
683
684   t = g_get_real_time ();
685
686 #if 0
687   dt = g_date_time_new_from_unix_utc (t);
688   g_assert (dt == NULL);
689 #endif
690
691   t = t / 1e6;  /* oops, this was microseconds */
692
693   dt = g_date_time_new_from_unix_utc (t);
694   g_assert (dt != NULL);
695
696   g_assert (dt == g_date_time_ref (dt));
697   g_date_time_unref (dt);
698   g_assert_cmpint (g_date_time_to_unix (dt), ==, t);
699   g_date_time_unref (dt);
700 }
701
702 static void
703 test_GDateTime_get_utc_offset (void)
704 {
705 #if defined (HAVE_STRUCT_TM_TM_GMTOFF) || defined (HAVE_STRUCT_TM___TM_GMTOFF)
706   GDateTime *dt;
707   GTimeSpan ts;
708   struct tm tm;
709
710   memset (&tm, 0, sizeof (tm));
711   get_localtime_tm (time (NULL), &tm);
712
713   dt = g_date_time_new_now_local ();
714   ts = g_date_time_get_utc_offset (dt);
715 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
716   g_assert_cmpint (ts, ==, (tm.tm_gmtoff * G_TIME_SPAN_SECOND));
717 #endif
718 #ifdef HAVE_STRUCT_TM___TM_GMTOFF
719   g_assert_cmpint (ts, ==, (tm.__tm_gmtoff * G_TIME_SPAN_SECOND));
720 #endif
721   g_date_time_unref (dt);
722 #endif
723 }
724
725 static void
726 test_GDateTime_to_timeval (void)
727 {
728   GTimeVal tv1, tv2;
729   GDateTime *dt;
730
731   memset (&tv1, 0, sizeof (tv1));
732   memset (&tv2, 0, sizeof (tv2));
733
734   g_get_current_time (&tv1);
735   dt = g_date_time_new_from_timeval_local (&tv1);
736   g_date_time_to_timeval (dt, &tv2);
737   g_assert_cmpint (tv1.tv_sec, ==, tv2.tv_sec);
738   g_assert_cmpint (tv1.tv_usec, ==, tv2.tv_usec);
739   g_date_time_unref (dt);
740 }
741
742 static void
743 test_GDateTime_to_local (void)
744 {
745   GDateTime *utc, *now, *dt;
746
747   utc = g_date_time_new_now_utc ();
748   now = g_date_time_new_now_local ();
749   dt = g_date_time_to_local (utc);
750
751   g_assert_cmpint (g_date_time_get_year (now), ==, g_date_time_get_year (dt));
752   g_assert_cmpint (g_date_time_get_month (now), ==, g_date_time_get_month (dt));
753   g_assert_cmpint (g_date_time_get_day_of_month (now), ==, g_date_time_get_day_of_month (dt));
754   g_assert_cmpint (g_date_time_get_hour (now), ==, g_date_time_get_hour (dt));
755   g_assert_cmpint (g_date_time_get_minute (now), ==, g_date_time_get_minute (dt));
756   g_assert_cmpint (g_date_time_get_second (now), ==, g_date_time_get_second (dt));
757
758   g_date_time_unref (now);
759   g_date_time_unref (utc);
760   g_date_time_unref (dt);
761 }
762
763 static void
764 test_GDateTime_to_utc (void)
765 {
766   GDateTime *dt, *dt2;
767   time_t     t;
768   struct tm  tm;
769
770   t = time (NULL);
771 #ifdef HAVE_GMTIME_R
772   gmtime_r (&t, &tm);
773 #else
774   {
775     struct tm *tmp = gmtime (&t);
776     memcpy (&tm, tmp, sizeof (struct tm));
777   }
778 #endif
779   dt2 = g_date_time_new_from_unix_local (t);
780   dt = g_date_time_to_utc (dt2);
781   g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
782   g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
783   g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
784   g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
785   g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
786   g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
787   g_date_time_unref (dt);
788   g_date_time_unref (dt2);
789 }
790
791 static void
792 test_GDateTime_get_day_of_year (void)
793 {
794 #define TEST_DAY_OF_YEAR(y,m,d,o)                       G_STMT_START {  \
795   GDateTime *__dt = g_date_time_new_local ((y), (m), (d), 0, 0, 0);     \
796   g_assert_cmpint ((o), ==, g_date_time_get_day_of_year (__dt));        \
797   g_date_time_unref (__dt);                             } G_STMT_END
798
799   TEST_DAY_OF_YEAR (2009, 1, 1, 1);
800   TEST_DAY_OF_YEAR (2009, 2, 1, 32);
801   TEST_DAY_OF_YEAR (2009, 8, 16, 228);
802   TEST_DAY_OF_YEAR (2008, 8, 16, 229);
803 }
804
805 static void
806 test_GDateTime_printf (void)
807 {
808 /* 64 seems big, but one zoneinfo file, Factory, has an abbreviation
809  * that long, and it will cause the test to fail if dst isn't big
810  * enough.
811  */
812   gchar dst[64];
813   struct tm tt;
814   time_t t;
815
816 #define TEST_PRINTF(f,o)                        G_STMT_START {  \
817 GDateTime *__dt = g_date_time_new_local (2009, 10, 24, 0, 0, 0);\
818   gchar *__p = g_date_time_format (__dt, (f));                  \
819   g_assert_cmpstr (__p, ==, (o));                               \
820   g_date_time_unref (__dt);                                     \
821   g_free (__p);                                 } G_STMT_END
822
823 #define TEST_PRINTF_DATE(y,m,d,f,o)             G_STMT_START {  \
824   GDateTime *dt = g_date_time_new_local (y, m, d, 0, 0, 0);     \
825   gchar *p = g_date_time_format (dt, (f));                      \
826   g_assert_cmpstr (p, ==, (o));                                 \
827   g_date_time_unref (dt);                                       \
828   g_free (p);                                   } G_STMT_END
829
830 #define TEST_PRINTF_TIME(h,m,s,f,o)             G_STMT_START { \
831   GDateTime *dt = g_date_time_new_local (2009, 10, 24, (h), (m), (s)); \
832   gchar *p = g_date_time_format (dt, (f));                      \
833   g_assert_cmpstr (p, ==, (o));                                 \
834   g_date_time_unref (dt);                                       \
835   g_free (p);                                   } G_STMT_END
836
837   /*
838    * This is a little helper to make sure we can compare timezones to
839    * that of the generated timezone.
840    */
841   t = time (NULL);
842   memset (&tt, 0, sizeof(tt));
843   get_localtime_tm (t, &tt);
844   tt.tm_year = 2009 - 1900;
845   tt.tm_mon = 9; /* 0 indexed */
846   tt.tm_mday = 24;
847   t = mktime (&tt);
848   memset (&tt, 0, sizeof(tt));
849   get_localtime_tm (t, &tt);
850   strftime (dst, sizeof(dst), "%Z", &tt);
851
852   /* get current time_t for 20090924 in the local timezone */
853   tt.tm_sec = 0;
854   tt.tm_min = 0;
855   tt.tm_hour = 0;
856   t = mktime (&tt);
857
858   TEST_PRINTF ("%a", "Sat");
859   TEST_PRINTF ("%A", "Saturday");
860   TEST_PRINTF ("%b", "Oct");
861   TEST_PRINTF ("%B", "October");
862   TEST_PRINTF ("%d", "24");
863   TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
864   TEST_PRINTF ("%e", "24"); // fixme
865   TEST_PRINTF ("%h", "Oct");
866   TEST_PRINTF ("%H", "00");
867   TEST_PRINTF_TIME (15, 0, 0, "%H", "15");
868   TEST_PRINTF ("%I", "12");
869   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
870   TEST_PRINTF_TIME (15, 0, 0, "%I", "03");
871   TEST_PRINTF ("%j", "297");
872   TEST_PRINTF ("%k", " 0");
873   TEST_PRINTF_TIME (13, 13, 13, "%k", "13");
874   TEST_PRINTF ("%l", "12");
875   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
876   TEST_PRINTF_TIME (13, 13, 13, "%l", " 1");
877   TEST_PRINTF_TIME (10, 13, 13, "%l", "10");
878   TEST_PRINTF ("%m", "10");
879   TEST_PRINTF ("%M", "00");
880   TEST_PRINTF ("%p", "AM");
881   TEST_PRINTF_TIME (13, 13, 13, "%p", "PM");
882   TEST_PRINTF ("%P", "am");
883   TEST_PRINTF_TIME (13, 13, 13, "%P", "pm");
884   TEST_PRINTF ("%r", "12:00:00 AM");
885   TEST_PRINTF_TIME (13, 13, 13, "%r", "01:13:13 PM");
886   TEST_PRINTF ("%R", "00:00");
887   TEST_PRINTF_TIME (13, 13, 31, "%R", "13:13");
888   TEST_PRINTF ("%S", "00");
889   TEST_PRINTF ("%t", "  ");
890   TEST_PRINTF ("%u", "6");
891   TEST_PRINTF ("%x", "10/24/09");
892   TEST_PRINTF ("%X", "00:00:00");
893   TEST_PRINTF_TIME (13, 14, 15, "%X", "13:14:15");
894   TEST_PRINTF ("%y", "09");
895   TEST_PRINTF ("%Y", "2009");
896   TEST_PRINTF ("%%", "%");
897   TEST_PRINTF ("%", "");
898   TEST_PRINTF ("%9", NULL);
899 #ifdef G_OS_UNIX
900   TEST_PRINTF ("%Z", dst);
901 #elif defined G_OS_WIN32
902   TEST_PRINTF ("%Z", "Pacific Standard Time");
903 #endif
904 }
905
906 static void
907 test_non_utf8_printf (void)
908 {
909   gchar *oldlocale;
910
911   oldlocale = g_strdup (setlocale (LC_ALL, NULL));
912   setlocale (LC_ALL, "ja_JP.eucjp");
913   if (strstr (setlocale (LC_ALL, NULL), "ja_JP") == NULL)
914     {
915       g_test_message ("locale ja_JP.eucjp not available, skipping non-UTF8 tests");
916       g_free (oldlocale);
917       return;
918     }
919   if (g_get_charset (NULL))
920     {
921       g_test_message ("locale ja_JP.eucjp may be available, but glib seems to think that it's equivalent to UTF-8, skipping non-UTF-8 tests.");
922       g_test_message ("This is a known issue on Darwin");
923       setlocale (LC_ALL, oldlocale);
924       g_free (oldlocale);
925       return;
926     }
927
928   /* These are the outputs that ja_JP.UTF-8 generates; if everything
929    * is working then ja_JP.eucjp should generate the same.
930    */
931   TEST_PRINTF ("%a", "\345\234\237");
932   TEST_PRINTF ("%A", "\345\234\237\346\233\234\346\227\245");
933 #ifndef HAVE_CARBON /* OSX just returns the number */
934   TEST_PRINTF ("%b", "10\346\234\210");
935 #endif
936   TEST_PRINTF ("%B", "10\346\234\210");
937   TEST_PRINTF ("%d", "24");
938   TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
939   TEST_PRINTF ("%e", "24"); // fixme
940 #ifndef HAVE_CARBON /* OSX just returns the number */
941   TEST_PRINTF ("%h", "10\346\234\210");
942 #endif
943   TEST_PRINTF ("%H", "00");
944   TEST_PRINTF_TIME (15, 0, 0, "%H", "15");
945   TEST_PRINTF ("%I", "12");
946   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
947   TEST_PRINTF_TIME (15, 0, 0, "%I", "03");
948   TEST_PRINTF ("%j", "297");
949   TEST_PRINTF ("%k", " 0");
950   TEST_PRINTF_TIME (13, 13, 13, "%k", "13");
951   TEST_PRINTF ("%l", "12");
952   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
953   TEST_PRINTF_TIME (13, 13, 13, "%l", " 1");
954   TEST_PRINTF_TIME (10, 13, 13, "%l", "10");
955   TEST_PRINTF ("%m", "10");
956   TEST_PRINTF ("%M", "00");
957 #ifndef HAVE_CARBON /* OSX returns latin "AM", not japanese */
958   TEST_PRINTF ("%p", "\345\215\210\345\211\215");
959   TEST_PRINTF_TIME (13, 13, 13, "%p", "\345\215\210\345\276\214");
960   TEST_PRINTF ("%P", "\345\215\210\345\211\215");
961   TEST_PRINTF_TIME (13, 13, 13, "%P", "\345\215\210\345\276\214");
962   TEST_PRINTF ("%r", "\345\215\210\345\211\21512\346\231\20200\345\210\20600\347\247\222");
963   TEST_PRINTF_TIME (13, 13, 13, "%r", "\345\215\210\345\276\21401\346\231\20213\345\210\20613\347\247\222");
964 #endif
965   TEST_PRINTF ("%R", "00:00");
966   TEST_PRINTF_TIME (13, 13, 31, "%R", "13:13");
967   TEST_PRINTF ("%S", "00");
968   TEST_PRINTF ("%t", "  ");
969   TEST_PRINTF ("%u", "6");
970 #ifndef HAVE_CARBON /* OSX returns YYYY/MM/DD in ASCII */
971   TEST_PRINTF ("%x", "2009\345\271\26410\346\234\21024\346\227\245");
972 #endif
973   TEST_PRINTF ("%X", "00\346\231\20200\345\210\20600\347\247\222");
974   TEST_PRINTF_TIME (13, 14, 15, "%X", "13\346\231\20214\345\210\20615\347\247\222");
975   TEST_PRINTF ("%y", "09");
976   TEST_PRINTF ("%Y", "2009");
977   TEST_PRINTF ("%%", "%");
978   TEST_PRINTF ("%", "");
979   TEST_PRINTF ("%9", NULL);
980
981   setlocale (LC_ALL, oldlocale);
982   g_free (oldlocale);
983 }
984
985 static void
986 test_modifiers (void)
987 {
988   gchar *oldlocale;
989
990   TEST_PRINTF_DATE (2009, 1,  1,  "%d", "01");
991   TEST_PRINTF_DATE (2009, 1,  1, "%_d", " 1");
992   TEST_PRINTF_DATE (2009, 1,  1, "%-d", "1");
993   TEST_PRINTF_DATE (2009, 1,  1, "%0d", "01");
994   TEST_PRINTF_DATE (2009, 1, 21,  "%d", "21");
995   TEST_PRINTF_DATE (2009, 1, 21, "%_d", "21");
996   TEST_PRINTF_DATE (2009, 1, 21, "%-d", "21");
997   TEST_PRINTF_DATE (2009, 1, 21, "%0d", "21");
998
999   TEST_PRINTF_DATE (2009, 1,  1,  "%e", " 1");
1000   TEST_PRINTF_DATE (2009, 1,  1, "%_e", " 1");
1001   TEST_PRINTF_DATE (2009, 1,  1, "%-e", "1");
1002   TEST_PRINTF_DATE (2009, 1,  1, "%0e", "01");
1003   TEST_PRINTF_DATE (2009, 1, 21,  "%e", "21");
1004   TEST_PRINTF_DATE (2009, 1, 21, "%_e", "21");
1005   TEST_PRINTF_DATE (2009, 1, 21, "%-e", "21");
1006   TEST_PRINTF_DATE (2009, 1, 21, "%0e", "21");
1007
1008   TEST_PRINTF_TIME ( 1, 0, 0,  "%H", "01");
1009   TEST_PRINTF_TIME ( 1, 0, 0, "%_H", " 1");
1010   TEST_PRINTF_TIME ( 1, 0, 0, "%-H", "1");
1011   TEST_PRINTF_TIME ( 1, 0, 0, "%0H", "01");
1012   TEST_PRINTF_TIME (21, 0, 0,  "%H", "21");
1013   TEST_PRINTF_TIME (21, 0, 0, "%_H", "21");
1014   TEST_PRINTF_TIME (21, 0, 0, "%-H", "21");
1015   TEST_PRINTF_TIME (21, 0, 0, "%0H", "21");
1016
1017   TEST_PRINTF_TIME ( 1, 0, 0,  "%I", "01");
1018   TEST_PRINTF_TIME ( 1, 0, 0, "%_I", " 1");
1019   TEST_PRINTF_TIME ( 1, 0, 0, "%-I", "1");
1020   TEST_PRINTF_TIME ( 1, 0, 0, "%0I", "01");
1021   TEST_PRINTF_TIME (23, 0, 0,  "%I", "11");
1022   TEST_PRINTF_TIME (23, 0, 0, "%_I", "11");
1023   TEST_PRINTF_TIME (23, 0, 0, "%-I", "11");
1024   TEST_PRINTF_TIME (23, 0, 0, "%0I", "11");
1025
1026   TEST_PRINTF_TIME ( 1, 0, 0,  "%k", " 1");
1027   TEST_PRINTF_TIME ( 1, 0, 0, "%_k", " 1");
1028   TEST_PRINTF_TIME ( 1, 0, 0, "%-k", "1");
1029   TEST_PRINTF_TIME ( 1, 0, 0, "%0k", "01");
1030
1031   oldlocale = g_strdup (setlocale (LC_ALL, NULL));
1032   setlocale (LC_ALL, "fa_IR.utf-8");
1033   if (strstr (setlocale (LC_ALL, NULL), "fa_IR") != NULL)
1034     {
1035       TEST_PRINTF_TIME (23, 0, 0, "%OH", "\333\262\333\263");    /* '23' */
1036       TEST_PRINTF_TIME (23, 0, 0, "%OI", "\333\261\333\261");    /* '11' */
1037       TEST_PRINTF_TIME (23, 0, 0, "%OM", "\333\260\333\260");    /* '00' */
1038
1039       TEST_PRINTF_DATE (2011, 7, 1, "%Om", "\333\260\333\267");  /* '07' */
1040       TEST_PRINTF_DATE (2011, 7, 1, "%0Om", "\333\260\333\267"); /* '07' */
1041       TEST_PRINTF_DATE (2011, 7, 1, "%-Om", "\333\267");         /* '7' */
1042       TEST_PRINTF_DATE (2011, 7, 1, "%_Om", " \333\267");        /* ' 7' */
1043     }
1044   else
1045     g_test_message ("locale fa_IR not available, skipping O modifier tests");
1046   setlocale (LC_ALL, oldlocale);
1047   g_free (oldlocale);
1048 }
1049
1050 static void
1051 test_GDateTime_dst (void)
1052 {
1053   GDateTime *dt1, *dt2;
1054   GTimeZone *tz;
1055
1056   /* this date has the DST state set for Europe/London */
1057 #ifdef G_OS_UNIX
1058   tz = g_time_zone_new ("Europe/London");
1059 #elif defined G_OS_WIN32
1060   tz = g_time_zone_new ("GMT Standard Time");
1061 #endif
1062   dt1 = g_date_time_new (tz, 2009, 8, 15, 3, 0, 1);
1063   g_assert (g_date_time_is_daylight_savings (dt1));
1064   g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, 3600);
1065   g_assert_cmpint (g_date_time_get_hour (dt1), ==, 3);
1066
1067   /* add 6 months to clear the DST flag but keep the same time */
1068   dt2 = g_date_time_add_months (dt1, 6);
1069   g_assert (!g_date_time_is_daylight_savings (dt2));
1070   g_assert_cmpint (g_date_time_get_utc_offset (dt2) / G_USEC_PER_SEC, ==, 0);
1071   g_assert_cmpint (g_date_time_get_hour (dt2), ==, 3);
1072
1073   g_date_time_unref (dt2);
1074   g_date_time_unref (dt1);
1075
1076   /* now do the reverse: start with a non-DST state and move to DST */
1077   dt1 = g_date_time_new (tz, 2009, 2, 15, 2, 0, 1);
1078   g_assert (!g_date_time_is_daylight_savings (dt1));
1079   g_assert_cmpint (g_date_time_get_hour (dt1), ==, 2);
1080
1081   dt2 = g_date_time_add_months (dt1, 6);
1082   g_assert (g_date_time_is_daylight_savings (dt2));
1083   g_assert_cmpint (g_date_time_get_hour (dt2), ==, 2);
1084
1085   g_date_time_unref (dt2);
1086   g_date_time_unref (dt1);
1087   g_time_zone_unref (tz);
1088 }
1089
1090 static inline gboolean
1091 is_leap_year (gint year)
1092 {
1093   g_assert (1 <= year && year <= 9999);
1094
1095   return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
1096 }
1097
1098 static inline gint
1099 days_in_month (gint year, gint month)
1100 {
1101   const gint table[2][13] = {
1102     {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
1103     {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
1104   };
1105
1106   g_assert (1 <= month && month <= 12);
1107
1108   return table[is_leap_year (year)][month];
1109 }
1110
1111 static void
1112 test_all_dates (void)
1113 {
1114   gint year, month, day;
1115   GTimeZone *timezone;
1116   gint64 unix_time;
1117   gint day_of_year;
1118   gint week_year;
1119   gint week_num;
1120   gint weekday;
1121
1122   /* save some time by hanging on to this. */
1123   timezone = g_time_zone_new_utc ();
1124
1125   unix_time = G_GINT64_CONSTANT(-62135596800);
1126
1127   /* 0001-01-01 is 0001-W01-1 */
1128   week_year = 1;
1129   week_num = 1;
1130   weekday = 1;
1131
1132
1133   /* The calendar makes a full cycle every 400 years, so we could
1134    * theoretically just test years 1 through 400.  That assumes that our
1135    * software has no bugs, so probably we should just test them all. :)
1136    */
1137   for (year = 1; year <= 9999; year++)
1138     {
1139       day_of_year = 1;
1140
1141       for (month = 1; month <= 12; month++)
1142         for (day = 1; day <= days_in_month (year, month); day++)
1143           {
1144             GDateTime *dt;
1145
1146             dt = g_date_time_new (timezone, year, month, day, 0, 0, 0);
1147
1148 #if 0
1149             g_print ("%04d-%02d-%02d = %04d-W%02d-%d = %04d-%03d\n",
1150                      year, month, day,
1151                      week_year, week_num, weekday,
1152                      year, day_of_year);
1153 #endif
1154
1155             /* sanity check */
1156             if G_UNLIKELY (g_date_time_get_year (dt) != year ||
1157                            g_date_time_get_month (dt) != month ||
1158                            g_date_time_get_day_of_month (dt) != day)
1159               g_error ("%04d-%02d-%02d comes out as %04d-%02d-%02d",
1160                        year, month, day,
1161                        g_date_time_get_year (dt),
1162                        g_date_time_get_month (dt),
1163                        g_date_time_get_day_of_month (dt));
1164
1165             if G_UNLIKELY (g_date_time_get_week_numbering_year (dt) != week_year ||
1166                            g_date_time_get_week_of_year (dt) != week_num ||
1167                            g_date_time_get_day_of_week (dt) != weekday)
1168               g_error ("%04d-%02d-%02d should be %04d-W%02d-%d but "
1169                        "comes out as %04d-W%02d-%d", year, month, day,
1170                        week_year, week_num, weekday,
1171                        g_date_time_get_week_numbering_year (dt),
1172                        g_date_time_get_week_of_year (dt),
1173                        g_date_time_get_day_of_week (dt));
1174
1175             if G_UNLIKELY (g_date_time_to_unix (dt) != unix_time)
1176               g_error ("%04d-%02d-%02d 00:00:00 UTC should have unix time %"
1177                        G_GINT64_FORMAT " but comes out as %"G_GINT64_FORMAT,
1178                        year, month, day, unix_time, g_date_time_to_unix (dt));
1179
1180             if G_UNLIKELY (g_date_time_get_day_of_year (dt) != day_of_year)
1181               g_error ("%04d-%02d-%02d should be day of year %d"
1182                        " but comes out as %d", year, month, day,
1183                        day_of_year, g_date_time_get_day_of_year (dt));
1184
1185             if G_UNLIKELY (g_date_time_get_hour (dt) != 0 ||
1186                            g_date_time_get_minute (dt) != 0 ||
1187                            g_date_time_get_seconds (dt) != 0)
1188               g_error ("%04d-%02d-%02d 00:00:00 UTC comes out "
1189                        "as %02d:%02d:%02.6f", year, month, day,
1190                        g_date_time_get_hour (dt),
1191                        g_date_time_get_minute (dt),
1192                        g_date_time_get_seconds (dt));
1193             /* done */
1194
1195             /* add 24 hours to unix time */
1196             unix_time += 24 * 60 * 60;
1197
1198             /* move day of year forward */
1199             day_of_year++;
1200
1201             /* move the week date forward */
1202             if (++weekday == 8)
1203               {
1204                 weekday = 1; /* Sunday -> Monday */
1205
1206                 /* NOTE: year/month/day is the final day of the week we
1207                  * just finished.
1208                  *
1209                  * If we just finished the last week of last year then
1210                  * we are definitely starting the first week of this
1211                  * year.
1212                  *
1213                  * Otherwise, if we're still in this year, but Sunday
1214                  * fell on or after December 28 then December 29, 30, 31
1215                  * could be days within the next year's first year.
1216                  */
1217                 if (year != week_year || (month == 12 && day >= 28))
1218                   {
1219                     /* first week of the new year */
1220                     week_num = 1;
1221                     week_year++;
1222                   }
1223                 else
1224                   week_num++;
1225               }
1226
1227             g_date_time_unref (dt);
1228           }
1229     }
1230
1231   g_time_zone_unref (timezone);
1232 }
1233
1234 static void
1235 test_z (void)
1236 {
1237   GTimeZone *tz;
1238   GDateTime *dt;
1239   gchar *p;
1240
1241   g_test_bug ("642935");
1242
1243   tz = g_time_zone_new ("-08:00");
1244   dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1245
1246   p = g_date_time_format (dt, "%z");
1247   g_assert_cmpstr (p, ==, "-0800");
1248   g_free (p);
1249
1250   p = g_date_time_format (dt, "%:z");
1251   g_assert_cmpstr (p, ==, "-08:00");
1252   g_free (p);
1253
1254   p = g_date_time_format (dt, "%::z");
1255   g_assert_cmpstr (p, ==, "-08:00:00");
1256   g_free (p);
1257
1258   p = g_date_time_format (dt, "%:::z");
1259   g_assert_cmpstr (p, ==, "-08");
1260   g_free (p);
1261
1262   g_date_time_unref (dt);
1263   g_time_zone_unref (tz);
1264
1265   tz = g_time_zone_new ("+00:00");
1266   dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1267   p = g_date_time_format (dt, "%:::z");
1268   g_assert_cmpstr (p, ==, "+00");
1269   g_free (p);
1270   g_date_time_unref (dt);
1271   g_time_zone_unref (tz);
1272
1273   tz = g_time_zone_new ("+08:23");
1274   dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1275   p = g_date_time_format (dt, "%:::z");
1276   g_assert_cmpstr (p, ==, "+08:23");
1277   g_free (p);
1278   g_date_time_unref (dt);
1279   g_time_zone_unref (tz);
1280
1281   tz = g_time_zone_new ("+08:23:45");
1282   dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1283   p = g_date_time_format (dt, "%:::z");
1284   g_assert_cmpstr (p, ==, "+08:23:45");
1285   g_free (p);
1286   g_date_time_unref (dt);
1287   g_time_zone_unref (tz);
1288 }
1289
1290 static void
1291 test_strftime (void)
1292 {
1293 #ifdef __linux__
1294 #define TEST_FORMAT \
1295   "a%a A%A b%b B%B c%c C%C d%d e%e F%F g%g G%G h%h H%H I%I j%j m%m M%M " \
1296   "n%n p%p r%r R%R S%S t%t T%T u%u V%V w%w x%x X%X y%y Y%Y z%z Z%Z %%"
1297   time_t t;
1298
1299   /* 127997 is prime, 1315005118 is now */
1300   for (t = 0; t < 1315005118; t += 127997)
1301     {
1302       GDateTime *date_time;
1303       gchar c_str[1000];
1304       gchar *dt_str;
1305
1306       date_time = g_date_time_new_from_unix_local (t);
1307       dt_str = g_date_time_format (date_time, TEST_FORMAT);
1308       strftime (c_str, sizeof c_str, TEST_FORMAT, localtime (&t));
1309       g_assert_cmpstr (c_str, ==, dt_str);
1310       g_date_time_unref (date_time);
1311       g_free (dt_str);
1312     }
1313 #endif
1314 }
1315
1316 static void
1317 test_find_interval (void)
1318 {
1319   GTimeZone *tz;
1320   GDateTime *dt;
1321   gint64 u;
1322   gint i1, i2;
1323
1324 #ifdef G_OS_UNIX
1325   tz = g_time_zone_new ("Canada/Eastern");
1326 #elif defined G_OS_WIN32
1327   tz = g_time_zone_new ("Eastern Standard Time");
1328 #endif
1329   dt = g_date_time_new_utc (2010, 11, 7, 1, 30, 0);
1330   u = g_date_time_to_unix (dt);
1331
1332   i1 = g_time_zone_find_interval (tz, G_TIME_TYPE_STANDARD, u);
1333   i2 = g_time_zone_find_interval (tz, G_TIME_TYPE_DAYLIGHT, u);
1334
1335   g_assert_cmpint (i1, !=, i2);
1336
1337   g_date_time_unref (dt);
1338
1339   dt = g_date_time_new_utc (2010, 3, 14, 2, 0, 0);
1340   u = g_date_time_to_unix (dt);
1341
1342   i1 = g_time_zone_find_interval (tz, G_TIME_TYPE_STANDARD, u);
1343   g_assert_cmpint (i1, ==, -1);
1344
1345   g_date_time_unref (dt);
1346   g_time_zone_unref (tz);
1347 }
1348
1349 static void
1350 test_adjust_time (void)
1351 {
1352   GTimeZone *tz;
1353   GDateTime *dt;
1354   gint64 u, u2;
1355   gint i1, i2;
1356
1357 #ifdef G_OS_UNIX
1358   tz = g_time_zone_new ("Canada/Eastern");
1359 #elif defined G_OS_WIN32
1360   tz = g_time_zone_new ("Eastern Standard Time");
1361 #endif
1362   dt = g_date_time_new_utc (2010, 11, 7, 1, 30, 0);
1363   u = g_date_time_to_unix (dt);
1364   u2 = u;
1365
1366   i1 = g_time_zone_find_interval (tz, G_TIME_TYPE_DAYLIGHT, u);
1367   i2 = g_time_zone_adjust_time (tz, G_TIME_TYPE_DAYLIGHT, &u2);
1368
1369   g_assert_cmpint (i1, ==, i2);
1370   g_assert (u == u2);
1371
1372   g_date_time_unref (dt);
1373
1374   dt = g_date_time_new_utc (2010, 3, 14, 2, 30, 0);
1375   u2 = g_date_time_to_unix (dt);
1376   g_date_time_unref (dt);
1377
1378   dt = g_date_time_new_utc (2010, 3, 14, 3, 0, 0);
1379   u = g_date_time_to_unix (dt);
1380   g_date_time_unref (dt);
1381
1382   i1 = g_time_zone_adjust_time (tz, G_TIME_TYPE_DAYLIGHT, &u2);
1383   g_assert (u == u2);
1384
1385   g_time_zone_unref (tz);
1386 }
1387
1388 static void
1389 test_no_header (void)
1390 {
1391   GTimeZone *tz;
1392
1393   tz = g_time_zone_new ("blabla");
1394
1395   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "UTC");
1396   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 0);
1397   g_assert (!g_time_zone_is_dst (tz, 0));
1398
1399   g_time_zone_unref (tz);
1400 }
1401
1402 static void
1403 test_posix_parse (void)
1404 {
1405   GTimeZone *tz;
1406   GDateTime *gdt1, *gdt2;
1407
1408   tz = g_time_zone_new ("PST");
1409   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "UTC");
1410   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 0);
1411   g_assert (!g_time_zone_is_dst (tz, 0));
1412   g_time_zone_unref (tz);
1413
1414   tz = g_time_zone_new ("PST8");
1415   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "PST");
1416   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, - 8 * 3600);
1417   g_assert (!g_time_zone_is_dst (tz, 0));
1418   g_time_zone_unref (tz);
1419
1420 /* This fails rules_from_identifier on Unix (though not on Windows)
1421  * but passes anyway because PST8PDT is a zone name.
1422  */
1423   tz = g_time_zone_new ("PST8PDT");
1424   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "PST");
1425   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, - 8 * 3600);
1426   g_assert (!g_time_zone_is_dst (tz, 0));
1427   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "PDT");
1428   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==,- 7 * 3600);
1429   g_assert (g_time_zone_is_dst (tz, 1));
1430   g_time_zone_unref (tz);
1431
1432   tz = g_time_zone_new ("PST8PDT6:32:15");
1433 #ifdef G_OS_WIN32
1434   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "PST");
1435   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, - 8 * 3600);
1436   g_assert (!g_time_zone_is_dst (tz, 0));
1437   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "PDT");
1438   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, - 6 * 3600 - 32 *60 - 15);
1439   g_assert (g_time_zone_is_dst (tz, 1));
1440   gdt1 = g_date_time_new (tz, 2012, 12, 6, 11, 15, 23.0);
1441   gdt2 = g_date_time_new (tz, 2012, 6, 6, 11, 15, 23.0);
1442   g_assert (!g_date_time_is_daylight_savings (gdt1));
1443   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) /  1000000, ==, -28800);
1444   g_assert (g_date_time_is_daylight_savings (gdt2));
1445   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, -23535);
1446   g_date_time_unref (gdt1);
1447   g_date_time_unref (gdt2);
1448 #else
1449   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "UTC");
1450   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 0);
1451   g_assert (!g_time_zone_is_dst (tz, 0));
1452 #endif
1453   g_time_zone_unref (tz);
1454
1455   tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,M10.1.0,M3.3.0");
1456   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
1457   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
1458   g_assert (!g_time_zone_is_dst (tz, 0));
1459   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
1460   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
1461   g_assert (g_time_zone_is_dst (tz, 1));
1462   gdt1 = g_date_time_new (tz, 2012, 3, 18, 0, 15, 23.0);
1463   gdt2 = g_date_time_new (tz, 2012, 3, 18, 3, 15, 23.0);
1464   g_assert (g_date_time_is_daylight_savings (gdt1));
1465   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1466   g_assert (!g_date_time_is_daylight_savings (gdt2));
1467   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1468   g_date_time_unref (gdt1);
1469   g_date_time_unref (gdt2);
1470   gdt1 = g_date_time_new (tz, 2012, 10, 7, 3, 15, 23.0);
1471   gdt2 = g_date_time_new (tz, 2012, 10, 7, 1, 15, 23.0);
1472   g_assert (g_date_time_is_daylight_savings (gdt1));
1473   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1474   g_assert (!g_date_time_is_daylight_savings (gdt2));
1475   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1476   g_date_time_unref (gdt1);
1477   g_date_time_unref (gdt2);
1478   g_time_zone_unref (tz);
1479
1480   tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,280,77");
1481   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
1482   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
1483   g_assert (!g_time_zone_is_dst (tz, 0));
1484   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
1485   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
1486   g_assert (g_time_zone_is_dst (tz, 1));
1487   gdt1 = g_date_time_new (tz, 2012, 3, 18, 0, 15, 23.0);
1488   gdt2 = g_date_time_new (tz, 2012, 3, 18, 3, 15, 23.0);
1489   g_assert (g_date_time_is_daylight_savings (gdt1));
1490   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1491   g_assert (!g_date_time_is_daylight_savings (gdt2));
1492   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1493   g_date_time_unref (gdt1);
1494   g_date_time_unref (gdt2);
1495   gdt1 = g_date_time_new (tz, 2012, 10, 7, 3, 15, 23.0);
1496   gdt2 = g_date_time_new (tz, 2012, 10, 7, 1, 15, 23.0);
1497   g_assert (g_date_time_is_daylight_savings (gdt1));
1498   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1499   g_assert (!g_date_time_is_daylight_savings (gdt2));
1500   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1501   g_date_time_unref (gdt1);
1502   g_date_time_unref (gdt2);
1503   g_time_zone_unref (tz);
1504
1505   tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,J279,J76");
1506   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
1507   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
1508   g_assert (!g_time_zone_is_dst (tz, 0));
1509   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
1510   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
1511   g_assert (g_time_zone_is_dst (tz, 1));
1512   gdt1 = g_date_time_new (tz, 2012, 3, 18, 0, 15, 23.0);
1513   gdt2 = g_date_time_new (tz, 2012, 3, 18, 3, 15, 23.0);
1514   g_assert (g_date_time_is_daylight_savings (gdt1));
1515   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1516   g_assert (!g_date_time_is_daylight_savings (gdt2));
1517   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1518   g_date_time_unref (gdt1);
1519   g_date_time_unref (gdt2);
1520   gdt1 = g_date_time_new (tz, 2012, 10, 7, 3, 15, 23.0);
1521   gdt2 = g_date_time_new (tz, 2012, 10, 7, 1, 15, 23.0);
1522   g_assert (g_date_time_is_daylight_savings (gdt1));
1523   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1524   g_assert (!g_date_time_is_daylight_savings (gdt2));
1525   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1526   g_date_time_unref (gdt1);
1527   g_date_time_unref (gdt2);
1528   g_time_zone_unref (tz);
1529
1530   tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,M10.1.0/07:00,M3.3.0/07:00");
1531   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
1532   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
1533   g_assert (!g_time_zone_is_dst (tz, 0));
1534   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
1535   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
1536   g_assert (g_time_zone_is_dst (tz, 1));
1537   gdt1 = g_date_time_new (tz, 2012, 3, 18, 5, 15, 23.0);
1538   gdt2 = g_date_time_new (tz, 2012, 3, 18, 8, 15, 23.0);
1539   g_assert (g_date_time_is_daylight_savings (gdt1));
1540   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1541   g_assert (!g_date_time_is_daylight_savings (gdt2));
1542   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1543   g_date_time_unref (gdt1);
1544   g_date_time_unref (gdt2);
1545   gdt1 = g_date_time_new (tz, 2012, 10, 7, 8, 15, 23.0);
1546   gdt2 = g_date_time_new (tz, 2012, 10, 7, 6, 15, 23.0);
1547   g_assert (g_date_time_is_daylight_savings (gdt1));
1548   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1549   g_assert (!g_date_time_is_daylight_savings (gdt2));
1550   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1551   g_date_time_unref (gdt1);
1552   g_date_time_unref (gdt2);
1553   gdt1 = g_date_time_new (tz, 1902, 10, 7, 8, 15, 23.0);
1554   gdt2 = g_date_time_new (tz, 1902, 10, 7, 6, 15, 23.0);
1555   g_assert (!g_date_time_is_daylight_savings (gdt1));
1556   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 43200);
1557   g_assert (!g_date_time_is_daylight_savings (gdt2));
1558   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1559   g_date_time_unref (gdt1);
1560   g_date_time_unref (gdt2);
1561   gdt1 = g_date_time_new (tz, 2142, 10, 7, 8, 15, 23.0);
1562   gdt2 = g_date_time_new (tz, 2142, 10, 7, 6, 15, 23.0);
1563   g_assert (g_date_time_is_daylight_savings (gdt1));
1564   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1565   g_assert (!g_date_time_is_daylight_savings (gdt2));
1566   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1567   g_date_time_unref (gdt1);
1568   g_date_time_unref (gdt2);
1569   gdt1 = g_date_time_new (tz, 3212, 10, 7, 8, 15, 23.0);
1570   gdt2 = g_date_time_new (tz, 3212, 10, 7, 6, 15, 23.0);
1571   g_assert (!g_date_time_is_daylight_savings (gdt1));
1572   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 43200);
1573   g_assert (!g_date_time_is_daylight_savings (gdt2));
1574   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1575   g_date_time_unref (gdt1);
1576   g_date_time_unref (gdt2);
1577   g_time_zone_unref (tz);
1578 }
1579
1580 gint
1581 main (gint   argc,
1582       gchar *argv[])
1583 {
1584   g_test_init (&argc, &argv, NULL);
1585   g_test_bug_base ("http://bugzilla.gnome.org/");
1586
1587   /* GDateTime Tests */
1588
1589   g_test_add_func ("/GDateTime/invalid", test_GDateTime_invalid);
1590   g_test_add_func ("/GDateTime/add_days", test_GDateTime_add_days);
1591   g_test_add_func ("/GDateTime/add_full", test_GDateTime_add_full);
1592   g_test_add_func ("/GDateTime/add_hours", test_GDateTime_add_hours);
1593   g_test_add_func ("/GDateTime/add_minutes", test_GDateTime_add_minutes);
1594   g_test_add_func ("/GDateTime/add_months", test_GDateTime_add_months);
1595   g_test_add_func ("/GDateTime/add_seconds", test_GDateTime_add_seconds);
1596   g_test_add_func ("/GDateTime/add_weeks", test_GDateTime_add_weeks);
1597   g_test_add_func ("/GDateTime/add_years", test_GDateTime_add_years);
1598   g_test_add_func ("/GDateTime/compare", test_GDateTime_compare);
1599   g_test_add_func ("/GDateTime/diff", test_GDateTime_diff);
1600   g_test_add_func ("/GDateTime/equal", test_GDateTime_equal);
1601   g_test_add_func ("/GDateTime/get_day_of_week", test_GDateTime_get_day_of_week);
1602   g_test_add_func ("/GDateTime/get_day_of_month", test_GDateTime_get_day_of_month);
1603   g_test_add_func ("/GDateTime/get_day_of_year", test_GDateTime_get_day_of_year);
1604   g_test_add_func ("/GDateTime/get_hour", test_GDateTime_get_hour);
1605   g_test_add_func ("/GDateTime/get_microsecond", test_GDateTime_get_microsecond);
1606   g_test_add_func ("/GDateTime/get_minute", test_GDateTime_get_minute);
1607   g_test_add_func ("/GDateTime/get_month", test_GDateTime_get_month);
1608   g_test_add_func ("/GDateTime/get_second", test_GDateTime_get_second);
1609   g_test_add_func ("/GDateTime/get_utc_offset", test_GDateTime_get_utc_offset);
1610   g_test_add_func ("/GDateTime/get_year", test_GDateTime_get_year);
1611   g_test_add_func ("/GDateTime/hash", test_GDateTime_hash);
1612   g_test_add_func ("/GDateTime/new_from_unix", test_GDateTime_new_from_unix);
1613   g_test_add_func ("/GDateTime/new_from_unix_utc", test_GDateTime_new_from_unix_utc);
1614   g_test_add_func ("/GDateTime/new_from_timeval", test_GDateTime_new_from_timeval);
1615   g_test_add_func ("/GDateTime/new_from_timeval_utc", test_GDateTime_new_from_timeval_utc);
1616   g_test_add_func ("/GDateTime/new_full", test_GDateTime_new_full);
1617   g_test_add_func ("/GDateTime/now", test_GDateTime_now);
1618   g_test_add_func ("/GDateTime/printf", test_GDateTime_printf);
1619   g_test_add_func ("/GDateTime/non_utf8_printf", test_non_utf8_printf);
1620   g_test_add_func ("/GDateTime/strftime", test_strftime);
1621   g_test_add_func ("/GDateTime/modifiers", test_modifiers);
1622   g_test_add_func ("/GDateTime/to_local", test_GDateTime_to_local);
1623   g_test_add_func ("/GDateTime/to_unix", test_GDateTime_to_unix);
1624   g_test_add_func ("/GDateTime/to_timeval", test_GDateTime_to_timeval);
1625   g_test_add_func ("/GDateTime/to_utc", test_GDateTime_to_utc);
1626   g_test_add_func ("/GDateTime/now_utc", test_GDateTime_now_utc);
1627   g_test_add_func ("/GDateTime/dst", test_GDateTime_dst);
1628   g_test_add_func ("/GDateTime/test_z", test_z);
1629   g_test_add_func ("/GDateTime/test-all-dates", test_all_dates);
1630   g_test_add_func ("/GTimeZone/find-interval", test_find_interval);
1631   g_test_add_func ("/GTimeZone/adjust-time", test_adjust_time);
1632   g_test_add_func ("/GTimeZone/no-header", test_no_header);
1633   g_test_add_func ("/GTimeZone/posix-parse", test_posix_parse);
1634
1635   return g_test_run ();
1636 }