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