Imported Upstream version 2.53.6
[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   /* Check month limits */
750   dt = g_date_time_new_utc (2016, 1, 31, 22, 10, 42);
751   ASSERT_DATE (dt, 2016, 1, 31);
752   g_date_time_unref (dt);
753   dt = g_date_time_new_utc (2016, 1, 32, 22, 10, 42);
754   g_assert_null (dt);
755   dt = g_date_time_new_utc (2016, 2, 29, 22, 10, 42);
756   ASSERT_DATE (dt, 2016, 2, 29);
757   g_date_time_unref (dt);
758   dt = g_date_time_new_utc (2016, 2, 30, 22, 10, 42);
759   g_assert_null (dt);
760   dt = g_date_time_new_utc (2017, 2, 28, 22, 10, 42);
761   ASSERT_DATE (dt, 2017, 2, 28);
762   g_date_time_unref (dt);
763   dt = g_date_time_new_utc (2017, 2, 29, 22, 10, 42);
764   g_assert_null (dt);
765   dt = g_date_time_new_utc (2016, 3, 31, 22, 10, 42);
766   ASSERT_DATE (dt, 2016, 3, 31);
767   g_date_time_unref (dt);
768   dt = g_date_time_new_utc (2016, 3, 32, 22, 10, 42);
769   g_assert_null (dt);
770   dt = g_date_time_new_utc (2016, 4, 30, 22, 10, 42);
771   ASSERT_DATE (dt, 2016, 4, 30);
772   g_date_time_unref (dt);
773   dt = g_date_time_new_utc (2016, 4, 31, 22, 10, 42);
774   g_assert_null (dt);
775   dt = g_date_time_new_utc (2016, 5, 31, 22, 10, 42);
776   ASSERT_DATE (dt, 2016, 5, 31);
777   g_date_time_unref (dt);
778   dt = g_date_time_new_utc (2016, 5, 32, 22, 10, 42);
779   g_assert_null (dt);
780   dt = g_date_time_new_utc (2016, 6, 30, 22, 10, 42);
781   ASSERT_DATE (dt, 2016, 6, 30);
782   g_date_time_unref (dt);
783   dt = g_date_time_new_utc (2016, 6, 31, 22, 10, 42);
784   g_assert_null (dt);
785   dt = g_date_time_new_utc (2016, 7, 31, 22, 10, 42);
786   ASSERT_DATE (dt, 2016, 7, 31);
787   g_date_time_unref (dt);
788   dt = g_date_time_new_utc (2016, 7, 32, 22, 10, 42);
789   g_assert_null (dt);
790   dt = g_date_time_new_utc (2016, 8, 31, 22, 10, 42);
791   ASSERT_DATE (dt, 2016, 8, 31);
792   g_date_time_unref (dt);
793   dt = g_date_time_new_utc (2016, 8, 32, 22, 10, 42);
794   g_assert_null (dt);
795   dt = g_date_time_new_utc (2016, 9, 30, 22, 10, 42);
796   ASSERT_DATE (dt, 2016, 9, 30);
797   g_date_time_unref (dt);
798   dt = g_date_time_new_utc (2016, 9, 31, 22, 10, 42);
799   g_assert_null (dt);
800   dt = g_date_time_new_utc (2016, 10, 31, 22, 10, 42);
801   ASSERT_DATE (dt, 2016, 10, 31);
802   g_date_time_unref (dt);
803   dt = g_date_time_new_utc (2016, 10, 32, 22, 10, 42);
804   g_assert_null (dt);
805   dt = g_date_time_new_utc (2016, 11, 30, 22, 10, 42);
806   ASSERT_DATE (dt, 2016, 11, 30);
807   g_date_time_unref (dt);
808   dt = g_date_time_new_utc (2016, 11, 31, 22, 10, 42);
809   g_assert_null (dt);
810   dt = g_date_time_new_utc (2016, 12, 31, 22, 10, 42);
811   ASSERT_DATE (dt, 2016, 12, 31);
812   g_date_time_unref (dt);
813   dt = g_date_time_new_utc (2016, 12, 32, 22, 10, 42);
814   g_assert_null (dt);
815 }
816
817 static void
818 test_GDateTime_now_utc (void)
819 {
820   GDateTime *dt;
821   struct tm  tm;
822   time_t     t;
823   time_t     after;
824
825   /* t <= dt.to_unix() <= after, but the inequalities might not be
826    * equality if we're close to the boundary between seconds.
827    * We loop until t == after (and hence dt.to_unix() should equal both)
828    * to guard against that. */
829   do
830     {
831       t = g_get_real_time () / G_TIME_SPAN_SECOND;
832 #ifdef HAVE_GMTIME_R
833       gmtime_r (&t, &tm);
834 #else
835       {
836         struct tm *tmp = gmtime (&t);
837         /* Assume gmtime() can't fail as we got t from time(NULL). (Note
838          * that on Windows, gmtime() *is* MT-safe, it uses a thread-local
839          * buffer.)
840          */
841         memcpy (&tm, tmp, sizeof (struct tm));
842       }
843 #endif
844       dt = g_date_time_new_now_utc ();
845
846       after = g_get_real_time () / G_TIME_SPAN_SECOND;
847     }
848   while (t != after);
849
850   g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
851   g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
852   g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
853   g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
854   g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
855   g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
856   g_date_time_unref (dt);
857 }
858
859 static void
860 test_GDateTime_new_from_unix_utc (void)
861 {
862   GDateTime *dt;
863   gint64 t;
864
865   t = g_get_real_time ();
866
867 #if 0
868   dt = g_date_time_new_from_unix_utc (t);
869   g_assert (dt == NULL);
870 #endif
871
872   t = t / 1e6;  /* oops, this was microseconds */
873
874   dt = g_date_time_new_from_unix_utc (t);
875   g_assert (dt != NULL);
876
877   g_assert (dt == g_date_time_ref (dt));
878   g_date_time_unref (dt);
879   g_assert_cmpint (g_date_time_to_unix (dt), ==, t);
880   g_date_time_unref (dt);
881 }
882
883 static void
884 test_GDateTime_get_utc_offset (void)
885 {
886 #if defined (HAVE_STRUCT_TM_TM_GMTOFF) || defined (HAVE_STRUCT_TM___TM_GMTOFF)
887   GDateTime *dt;
888   GTimeSpan ts;
889   struct tm tm;
890
891   memset (&tm, 0, sizeof (tm));
892   get_localtime_tm (g_get_real_time () / G_TIME_SPAN_SECOND, &tm);
893
894   dt = g_date_time_new_now_local ();
895   ts = g_date_time_get_utc_offset (dt);
896 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
897   g_assert_cmpint (ts, ==, (tm.tm_gmtoff * G_TIME_SPAN_SECOND));
898 #endif
899 #ifdef HAVE_STRUCT_TM___TM_GMTOFF
900   g_assert_cmpint (ts, ==, (tm.__tm_gmtoff * G_TIME_SPAN_SECOND));
901 #endif
902   g_date_time_unref (dt);
903 #endif
904 }
905
906 static void
907 test_GDateTime_to_timeval (void)
908 {
909   GTimeVal tv1, tv2;
910   GDateTime *dt;
911
912   memset (&tv1, 0, sizeof (tv1));
913   memset (&tv2, 0, sizeof (tv2));
914
915   g_get_current_time (&tv1);
916   dt = g_date_time_new_from_timeval_local (&tv1);
917   g_date_time_to_timeval (dt, &tv2);
918   g_assert_cmpint (tv1.tv_sec, ==, tv2.tv_sec);
919   g_assert_cmpint (tv1.tv_usec, ==, tv2.tv_usec);
920   g_date_time_unref (dt);
921 }
922
923 static void
924 test_GDateTime_to_local (void)
925 {
926   GDateTime *utc = NULL, *now = NULL, *dt;
927   time_t before, after;
928
929   /* before <= utc.to_unix() <= now.to_unix() <= after, but the inequalities
930    * might not be equality if we're close to the boundary between seconds.
931    * We loop until before == after (and hence the GDateTimes should match)
932    * to guard against that. */
933   do
934     {
935       before = g_get_real_time () / G_TIME_SPAN_SECOND;
936       g_clear_pointer (&utc, g_date_time_unref);
937       g_clear_pointer (&now, g_date_time_unref);
938       utc = g_date_time_new_now_utc ();
939       now = g_date_time_new_now_local ();
940       after = g_get_real_time () / G_TIME_SPAN_SECOND;
941     }
942   while (before != after);
943
944   dt = g_date_time_to_local (utc);
945
946   g_assert_cmpint (g_date_time_get_year (now), ==, g_date_time_get_year (dt));
947   g_assert_cmpint (g_date_time_get_month (now), ==, g_date_time_get_month (dt));
948   g_assert_cmpint (g_date_time_get_day_of_month (now), ==, g_date_time_get_day_of_month (dt));
949   g_assert_cmpint (g_date_time_get_hour (now), ==, g_date_time_get_hour (dt));
950   g_assert_cmpint (g_date_time_get_minute (now), ==, g_date_time_get_minute (dt));
951   g_assert_cmpint (g_date_time_get_second (now), ==, g_date_time_get_second (dt));
952
953   g_date_time_unref (now);
954   g_date_time_unref (utc);
955   g_date_time_unref (dt);
956 }
957
958 static void
959 test_GDateTime_to_utc (void)
960 {
961   GDateTime *dt, *dt2;
962   time_t     t;
963   struct tm  tm;
964
965   t = time (NULL);
966 #ifdef HAVE_GMTIME_R
967   gmtime_r (&t, &tm);
968 #else
969   {
970     struct tm *tmp = gmtime (&t);
971     memcpy (&tm, tmp, sizeof (struct tm));
972   }
973 #endif
974   dt2 = g_date_time_new_from_unix_local (t);
975   dt = g_date_time_to_utc (dt2);
976   g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
977   g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
978   g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
979   g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
980   g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
981   g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
982   g_date_time_unref (dt);
983   g_date_time_unref (dt2);
984 }
985
986 static void
987 test_GDateTime_get_day_of_year (void)
988 {
989 #define TEST_DAY_OF_YEAR(y,m,d,o)                       G_STMT_START {  \
990   GDateTime *__dt = g_date_time_new_local ((y), (m), (d), 0, 0, 0);     \
991   g_assert_cmpint ((o), ==, g_date_time_get_day_of_year (__dt));        \
992   g_date_time_unref (__dt);                             } G_STMT_END
993
994   TEST_DAY_OF_YEAR (2009, 1, 1, 1);
995   TEST_DAY_OF_YEAR (2009, 2, 1, 32);
996   TEST_DAY_OF_YEAR (2009, 8, 16, 228);
997   TEST_DAY_OF_YEAR (2008, 8, 16, 229);
998 }
999
1000 static void
1001 test_GDateTime_printf (void)
1002 {
1003 /* 64 seems big, but one zoneinfo file, Factory, has an abbreviation
1004  * that long, and it will cause the test to fail if dst isn't big
1005  * enough.
1006  */
1007   gchar dst[64];
1008   struct tm tt;
1009   time_t t;
1010
1011 #define TEST_PRINTF(f,o)                        G_STMT_START {  \
1012 GDateTime *__dt = g_date_time_new_local (2009, 10, 24, 0, 0, 0);\
1013   gchar *__p = g_date_time_format (__dt, (f));                  \
1014   g_assert_cmpstr (__p, ==, (o));                               \
1015   g_date_time_unref (__dt);                                     \
1016   g_free (__p);                                 } G_STMT_END
1017
1018 #define TEST_PRINTF_DATE(y,m,d,f,o)             G_STMT_START {  \
1019   GDateTime *dt = g_date_time_new_local (y, m, d, 0, 0, 0);     \
1020   gchar *p = g_date_time_format (dt, (f));                      \
1021   g_assert_cmpstr (p, ==, (o));                                 \
1022   g_date_time_unref (dt);                                       \
1023   g_free (p);                                   } G_STMT_END
1024
1025 #define TEST_PRINTF_TIME(h,m,s,f,o)             G_STMT_START { \
1026   GDateTime *dt = g_date_time_new_local (2009, 10, 24, (h), (m), (s)); \
1027   gchar *p = g_date_time_format (dt, (f));                      \
1028   g_assert_cmpstr (p, ==, (o));                                 \
1029   g_date_time_unref (dt);                                       \
1030   g_free (p);                                   } G_STMT_END
1031
1032   /*
1033    * This is a little helper to make sure we can compare timezones to
1034    * that of the generated timezone.
1035    */
1036   t = time (NULL);
1037   memset (&tt, 0, sizeof(tt));
1038   get_localtime_tm (t, &tt);
1039   tt.tm_year = 2009 - 1900;
1040   tt.tm_mon = 9; /* 0 indexed */
1041   tt.tm_mday = 24;
1042   t = mktime (&tt);
1043   memset (&tt, 0, sizeof(tt));
1044   get_localtime_tm (t, &tt);
1045   strftime (dst, sizeof(dst), "%Z", &tt);
1046
1047   /* get current time_t for 20090924 in the local timezone */
1048   tt.tm_sec = 0;
1049   tt.tm_min = 0;
1050   tt.tm_hour = 0;
1051   t = mktime (&tt);
1052
1053   TEST_PRINTF ("%a", "Sat");
1054   TEST_PRINTF ("%A", "Saturday");
1055   TEST_PRINTF ("%b", "Oct");
1056   TEST_PRINTF ("%B", "October");
1057   TEST_PRINTF ("%d", "24");
1058   TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
1059   TEST_PRINTF ("%e", "24"); // fixme
1060   TEST_PRINTF ("%h", "Oct");
1061   TEST_PRINTF ("%H", "00");
1062   TEST_PRINTF_TIME (15, 0, 0, "%H", "15");
1063   TEST_PRINTF ("%I", "12");
1064   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
1065   TEST_PRINTF_TIME (15, 0, 0, "%I", "03");
1066   TEST_PRINTF ("%j", "297");
1067   TEST_PRINTF ("%k", " 0");
1068   TEST_PRINTF_TIME (13, 13, 13, "%k", "13");
1069   TEST_PRINTF ("%l", "12");
1070   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
1071   TEST_PRINTF_TIME (13, 13, 13, "%l", " 1");
1072   TEST_PRINTF_TIME (10, 13, 13, "%l", "10");
1073   TEST_PRINTF ("%m", "10");
1074   TEST_PRINTF ("%M", "00");
1075   TEST_PRINTF ("%p", "AM");
1076   TEST_PRINTF_TIME (13, 13, 13, "%p", "PM");
1077   TEST_PRINTF ("%P", "am");
1078   TEST_PRINTF_TIME (13, 13, 13, "%P", "pm");
1079   TEST_PRINTF ("%r", "12:00:00 AM");
1080   TEST_PRINTF_TIME (13, 13, 13, "%r", "01:13:13 PM");
1081   TEST_PRINTF ("%R", "00:00");
1082   TEST_PRINTF_TIME (13, 13, 31, "%R", "13:13");
1083   TEST_PRINTF ("%S", "00");
1084   TEST_PRINTF ("%t", "  ");
1085   TEST_PRINTF ("%u", "6");
1086   TEST_PRINTF ("%x", "10/24/09");
1087   TEST_PRINTF ("%X", "00:00:00");
1088   TEST_PRINTF_TIME (13, 14, 15, "%X", "13:14:15");
1089   TEST_PRINTF ("%y", "09");
1090   TEST_PRINTF ("%Y", "2009");
1091   TEST_PRINTF ("%%", "%");
1092   TEST_PRINTF ("%", "");
1093   TEST_PRINTF ("%9", NULL);
1094 #ifdef G_OS_UNIX
1095   TEST_PRINTF ("%Z", dst);
1096 #elif defined G_OS_WIN32
1097   TEST_PRINTF ("%Z", "Pacific Standard Time");
1098 #endif
1099 }
1100
1101 static void
1102 test_non_utf8_printf (void)
1103 {
1104   gchar *oldlocale;
1105
1106   oldlocale = g_strdup (setlocale (LC_ALL, NULL));
1107   setlocale (LC_ALL, "ja_JP.eucjp");
1108   if (strstr (setlocale (LC_ALL, NULL), "ja_JP") == NULL)
1109     {
1110       g_test_message ("locale ja_JP.eucjp not available, skipping non-UTF8 tests");
1111       g_free (oldlocale);
1112       return;
1113     }
1114   if (g_get_charset (NULL))
1115     {
1116       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.");
1117       g_test_message ("This is a known issue on Darwin");
1118       setlocale (LC_ALL, oldlocale);
1119       g_free (oldlocale);
1120       return;
1121     }
1122
1123   /* These are the outputs that ja_JP.UTF-8 generates; if everything
1124    * is working then ja_JP.eucjp should generate the same.
1125    */
1126   TEST_PRINTF ("%a", "\345\234\237");
1127   TEST_PRINTF ("%A", "\345\234\237\346\233\234\346\227\245");
1128 #ifndef HAVE_CARBON /* OSX just returns the number */
1129   TEST_PRINTF ("%b", "10\346\234\210");
1130 #endif
1131   TEST_PRINTF ("%B", "10\346\234\210");
1132   TEST_PRINTF ("%d", "24");
1133   TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
1134   TEST_PRINTF ("%e", "24"); // fixme
1135 #ifndef HAVE_CARBON /* OSX just returns the number */
1136   TEST_PRINTF ("%h", "10\346\234\210");
1137 #endif
1138   TEST_PRINTF ("%H", "00");
1139   TEST_PRINTF_TIME (15, 0, 0, "%H", "15");
1140   TEST_PRINTF ("%I", "12");
1141   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
1142   TEST_PRINTF_TIME (15, 0, 0, "%I", "03");
1143   TEST_PRINTF ("%j", "297");
1144   TEST_PRINTF ("%k", " 0");
1145   TEST_PRINTF_TIME (13, 13, 13, "%k", "13");
1146   TEST_PRINTF ("%l", "12");
1147   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
1148   TEST_PRINTF_TIME (13, 13, 13, "%l", " 1");
1149   TEST_PRINTF_TIME (10, 13, 13, "%l", "10");
1150   TEST_PRINTF ("%m", "10");
1151   TEST_PRINTF ("%M", "00");
1152 #ifndef HAVE_CARBON /* OSX returns latin "AM", not japanese */
1153   TEST_PRINTF ("%p", "\345\215\210\345\211\215");
1154   TEST_PRINTF_TIME (13, 13, 13, "%p", "\345\215\210\345\276\214");
1155   TEST_PRINTF ("%P", "\345\215\210\345\211\215");
1156   TEST_PRINTF_TIME (13, 13, 13, "%P", "\345\215\210\345\276\214");
1157   TEST_PRINTF ("%r", "\345\215\210\345\211\21512\346\231\20200\345\210\20600\347\247\222");
1158   TEST_PRINTF_TIME (13, 13, 13, "%r", "\345\215\210\345\276\21401\346\231\20213\345\210\20613\347\247\222");
1159 #endif
1160   TEST_PRINTF ("%R", "00:00");
1161   TEST_PRINTF_TIME (13, 13, 31, "%R", "13:13");
1162   TEST_PRINTF ("%S", "00");
1163   TEST_PRINTF ("%t", "  ");
1164   TEST_PRINTF ("%u", "6");
1165 #ifndef HAVE_CARBON /* OSX returns YYYY/MM/DD in ASCII */
1166   TEST_PRINTF ("%x", "2009\345\271\26410\346\234\21024\346\227\245");
1167 #endif
1168   TEST_PRINTF ("%X", "00\346\231\20200\345\210\20600\347\247\222");
1169   TEST_PRINTF_TIME (13, 14, 15, "%X", "13\346\231\20214\345\210\20615\347\247\222");
1170   TEST_PRINTF ("%y", "09");
1171   TEST_PRINTF ("%Y", "2009");
1172   TEST_PRINTF ("%%", "%");
1173   TEST_PRINTF ("%", "");
1174   TEST_PRINTF ("%9", NULL);
1175
1176   setlocale (LC_ALL, oldlocale);
1177   g_free (oldlocale);
1178 }
1179
1180 static void
1181 test_modifiers (void)
1182 {
1183   gchar *oldlocale;
1184
1185   TEST_PRINTF_DATE (2009, 1,  1,  "%d", "01");
1186   TEST_PRINTF_DATE (2009, 1,  1, "%_d", " 1");
1187   TEST_PRINTF_DATE (2009, 1,  1, "%-d", "1");
1188   TEST_PRINTF_DATE (2009, 1,  1, "%0d", "01");
1189   TEST_PRINTF_DATE (2009, 1, 21,  "%d", "21");
1190   TEST_PRINTF_DATE (2009, 1, 21, "%_d", "21");
1191   TEST_PRINTF_DATE (2009, 1, 21, "%-d", "21");
1192   TEST_PRINTF_DATE (2009, 1, 21, "%0d", "21");
1193
1194   TEST_PRINTF_DATE (2009, 1,  1,  "%e", " 1");
1195   TEST_PRINTF_DATE (2009, 1,  1, "%_e", " 1");
1196   TEST_PRINTF_DATE (2009, 1,  1, "%-e", "1");
1197   TEST_PRINTF_DATE (2009, 1,  1, "%0e", "01");
1198   TEST_PRINTF_DATE (2009, 1, 21,  "%e", "21");
1199   TEST_PRINTF_DATE (2009, 1, 21, "%_e", "21");
1200   TEST_PRINTF_DATE (2009, 1, 21, "%-e", "21");
1201   TEST_PRINTF_DATE (2009, 1, 21, "%0e", "21");
1202
1203   TEST_PRINTF_TIME ( 1, 0, 0,  "%H", "01");
1204   TEST_PRINTF_TIME ( 1, 0, 0, "%_H", " 1");
1205   TEST_PRINTF_TIME ( 1, 0, 0, "%-H", "1");
1206   TEST_PRINTF_TIME ( 1, 0, 0, "%0H", "01");
1207   TEST_PRINTF_TIME (21, 0, 0,  "%H", "21");
1208   TEST_PRINTF_TIME (21, 0, 0, "%_H", "21");
1209   TEST_PRINTF_TIME (21, 0, 0, "%-H", "21");
1210   TEST_PRINTF_TIME (21, 0, 0, "%0H", "21");
1211
1212   TEST_PRINTF_TIME ( 1, 0, 0,  "%I", "01");
1213   TEST_PRINTF_TIME ( 1, 0, 0, "%_I", " 1");
1214   TEST_PRINTF_TIME ( 1, 0, 0, "%-I", "1");
1215   TEST_PRINTF_TIME ( 1, 0, 0, "%0I", "01");
1216   TEST_PRINTF_TIME (23, 0, 0,  "%I", "11");
1217   TEST_PRINTF_TIME (23, 0, 0, "%_I", "11");
1218   TEST_PRINTF_TIME (23, 0, 0, "%-I", "11");
1219   TEST_PRINTF_TIME (23, 0, 0, "%0I", "11");
1220
1221   TEST_PRINTF_TIME ( 1, 0, 0,  "%k", " 1");
1222   TEST_PRINTF_TIME ( 1, 0, 0, "%_k", " 1");
1223   TEST_PRINTF_TIME ( 1, 0, 0, "%-k", "1");
1224   TEST_PRINTF_TIME ( 1, 0, 0, "%0k", "01");
1225
1226   oldlocale = g_strdup (setlocale (LC_ALL, NULL));
1227   setlocale (LC_ALL, "fa_IR.utf-8");
1228   if (strstr (setlocale (LC_ALL, NULL), "fa_IR") != NULL)
1229     {
1230       TEST_PRINTF_TIME (23, 0, 0, "%OH", "\333\262\333\263");    /* '23' */
1231       TEST_PRINTF_TIME (23, 0, 0, "%OI", "\333\261\333\261");    /* '11' */
1232       TEST_PRINTF_TIME (23, 0, 0, "%OM", "\333\260\333\260");    /* '00' */
1233
1234       TEST_PRINTF_DATE (2011, 7, 1, "%Om", "\333\260\333\267");  /* '07' */
1235       TEST_PRINTF_DATE (2011, 7, 1, "%0Om", "\333\260\333\267"); /* '07' */
1236       TEST_PRINTF_DATE (2011, 7, 1, "%-Om", "\333\267");         /* '7' */
1237       TEST_PRINTF_DATE (2011, 7, 1, "%_Om", " \333\267");        /* ' 7' */
1238     }
1239   else
1240     g_test_message ("locale fa_IR not available, skipping O modifier tests");
1241   setlocale (LC_ALL, oldlocale);
1242   g_free (oldlocale);
1243 }
1244
1245 static void
1246 test_GDateTime_dst (void)
1247 {
1248   GDateTime *dt1, *dt2;
1249   GTimeZone *tz;
1250
1251   /* this date has the DST state set for Europe/London */
1252 #ifdef G_OS_UNIX
1253   tz = g_time_zone_new ("Europe/London");
1254 #elif defined G_OS_WIN32
1255   tz = g_time_zone_new ("GMT Standard Time");
1256 #endif
1257   dt1 = g_date_time_new (tz, 2009, 8, 15, 3, 0, 1);
1258   g_assert (g_date_time_is_daylight_savings (dt1));
1259   g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, 3600);
1260   g_assert_cmpint (g_date_time_get_hour (dt1), ==, 3);
1261
1262   /* add 6 months to clear the DST flag but keep the same time */
1263   dt2 = g_date_time_add_months (dt1, 6);
1264   g_assert (!g_date_time_is_daylight_savings (dt2));
1265   g_assert_cmpint (g_date_time_get_utc_offset (dt2) / G_USEC_PER_SEC, ==, 0);
1266   g_assert_cmpint (g_date_time_get_hour (dt2), ==, 3);
1267
1268   g_date_time_unref (dt2);
1269   g_date_time_unref (dt1);
1270
1271   /* now do the reverse: start with a non-DST state and move to DST */
1272   dt1 = g_date_time_new (tz, 2009, 2, 15, 2, 0, 1);
1273   g_assert (!g_date_time_is_daylight_savings (dt1));
1274   g_assert_cmpint (g_date_time_get_hour (dt1), ==, 2);
1275
1276   dt2 = g_date_time_add_months (dt1, 6);
1277   g_assert (g_date_time_is_daylight_savings (dt2));
1278   g_assert_cmpint (g_date_time_get_hour (dt2), ==, 2);
1279
1280   g_date_time_unref (dt2);
1281   g_date_time_unref (dt1);
1282   g_time_zone_unref (tz);
1283 }
1284
1285 static inline gboolean
1286 is_leap_year (gint year)
1287 {
1288   g_assert (1 <= year && year <= 9999);
1289
1290   return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
1291 }
1292
1293 static inline gint
1294 days_in_month (gint year, gint month)
1295 {
1296   const gint table[2][13] = {
1297     {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
1298     {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
1299   };
1300
1301   g_assert (1 <= month && month <= 12);
1302
1303   return table[is_leap_year (year)][month];
1304 }
1305
1306 static void
1307 test_all_dates (void)
1308 {
1309   gint year, month, day;
1310   GTimeZone *timezone;
1311   gint64 unix_time;
1312   gint day_of_year;
1313   gint week_year;
1314   gint week_num;
1315   gint weekday;
1316
1317   /* save some time by hanging on to this. */
1318   timezone = g_time_zone_new_utc ();
1319
1320   unix_time = G_GINT64_CONSTANT(-62135596800);
1321
1322   /* 0001-01-01 is 0001-W01-1 */
1323   week_year = 1;
1324   week_num = 1;
1325   weekday = 1;
1326
1327
1328   /* The calendar makes a full cycle every 400 years, so we could
1329    * theoretically just test years 1 through 400.  That assumes that our
1330    * software has no bugs, so probably we should just test them all. :)
1331    */
1332   for (year = 1; year <= 9999; year++)
1333     {
1334       day_of_year = 1;
1335
1336       for (month = 1; month <= 12; month++)
1337         for (day = 1; day <= days_in_month (year, month); day++)
1338           {
1339             GDateTime *dt;
1340
1341             dt = g_date_time_new (timezone, year, month, day, 0, 0, 0);
1342
1343 #if 0
1344             g_printerr ("%04d-%02d-%02d = %04d-W%02d-%d = %04d-%03d\n",
1345                      year, month, day,
1346                      week_year, week_num, weekday,
1347                      year, day_of_year);
1348 #endif
1349
1350             /* sanity check */
1351             if G_UNLIKELY (g_date_time_get_year (dt) != year ||
1352                            g_date_time_get_month (dt) != month ||
1353                            g_date_time_get_day_of_month (dt) != day)
1354               g_error ("%04d-%02d-%02d comes out as %04d-%02d-%02d",
1355                        year, month, day,
1356                        g_date_time_get_year (dt),
1357                        g_date_time_get_month (dt),
1358                        g_date_time_get_day_of_month (dt));
1359
1360             if G_UNLIKELY (g_date_time_get_week_numbering_year (dt) != week_year ||
1361                            g_date_time_get_week_of_year (dt) != week_num ||
1362                            g_date_time_get_day_of_week (dt) != weekday)
1363               g_error ("%04d-%02d-%02d should be %04d-W%02d-%d but "
1364                        "comes out as %04d-W%02d-%d", year, month, day,
1365                        week_year, week_num, weekday,
1366                        g_date_time_get_week_numbering_year (dt),
1367                        g_date_time_get_week_of_year (dt),
1368                        g_date_time_get_day_of_week (dt));
1369
1370             if G_UNLIKELY (g_date_time_to_unix (dt) != unix_time)
1371               g_error ("%04d-%02d-%02d 00:00:00 UTC should have unix time %"
1372                        G_GINT64_FORMAT " but comes out as %"G_GINT64_FORMAT,
1373                        year, month, day, unix_time, g_date_time_to_unix (dt));
1374
1375             if G_UNLIKELY (g_date_time_get_day_of_year (dt) != day_of_year)
1376               g_error ("%04d-%02d-%02d should be day of year %d"
1377                        " but comes out as %d", year, month, day,
1378                        day_of_year, g_date_time_get_day_of_year (dt));
1379
1380             if G_UNLIKELY (g_date_time_get_hour (dt) != 0 ||
1381                            g_date_time_get_minute (dt) != 0 ||
1382                            g_date_time_get_seconds (dt) != 0)
1383               g_error ("%04d-%02d-%02d 00:00:00 UTC comes out "
1384                        "as %02d:%02d:%02.6f", year, month, day,
1385                        g_date_time_get_hour (dt),
1386                        g_date_time_get_minute (dt),
1387                        g_date_time_get_seconds (dt));
1388             /* done */
1389
1390             /* add 24 hours to unix time */
1391             unix_time += 24 * 60 * 60;
1392
1393             /* move day of year forward */
1394             day_of_year++;
1395
1396             /* move the week date forward */
1397             if (++weekday == 8)
1398               {
1399                 weekday = 1; /* Sunday -> Monday */
1400
1401                 /* NOTE: year/month/day is the final day of the week we
1402                  * just finished.
1403                  *
1404                  * If we just finished the last week of last year then
1405                  * we are definitely starting the first week of this
1406                  * year.
1407                  *
1408                  * Otherwise, if we're still in this year, but Sunday
1409                  * fell on or after December 28 then December 29, 30, 31
1410                  * could be days within the next year's first year.
1411                  */
1412                 if (year != week_year || (month == 12 && day >= 28))
1413                   {
1414                     /* first week of the new year */
1415                     week_num = 1;
1416                     week_year++;
1417                   }
1418                 else
1419                   week_num++;
1420               }
1421
1422             g_date_time_unref (dt);
1423           }
1424     }
1425
1426   g_time_zone_unref (timezone);
1427 }
1428
1429 static void
1430 test_z (void)
1431 {
1432   GTimeZone *tz;
1433   GDateTime *dt;
1434   gchar *p;
1435
1436   g_test_bug ("642935");
1437
1438   tz = g_time_zone_new ("-08:00");
1439   dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1440
1441   p = g_date_time_format (dt, "%z");
1442   g_assert_cmpstr (p, ==, "-0800");
1443   g_free (p);
1444
1445   p = g_date_time_format (dt, "%:z");
1446   g_assert_cmpstr (p, ==, "-08:00");
1447   g_free (p);
1448
1449   p = g_date_time_format (dt, "%::z");
1450   g_assert_cmpstr (p, ==, "-08:00:00");
1451   g_free (p);
1452
1453   p = g_date_time_format (dt, "%:::z");
1454   g_assert_cmpstr (p, ==, "-08");
1455   g_free (p);
1456
1457   g_date_time_unref (dt);
1458   g_time_zone_unref (tz);
1459
1460   tz = g_time_zone_new ("+00:00");
1461   dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1462   p = g_date_time_format (dt, "%:::z");
1463   g_assert_cmpstr (p, ==, "+00");
1464   g_free (p);
1465   g_date_time_unref (dt);
1466   g_time_zone_unref (tz);
1467
1468   tz = g_time_zone_new ("+08:23");
1469   dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1470   p = g_date_time_format (dt, "%:::z");
1471   g_assert_cmpstr (p, ==, "+08:23");
1472   g_free (p);
1473   g_date_time_unref (dt);
1474   g_time_zone_unref (tz);
1475
1476   tz = g_time_zone_new ("+08:23:45");
1477   dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1478   p = g_date_time_format (dt, "%:::z");
1479   g_assert_cmpstr (p, ==, "+08:23:45");
1480   g_free (p);
1481   g_date_time_unref (dt);
1482   g_time_zone_unref (tz);
1483 }
1484
1485 #pragma GCC diagnostic push
1486 #pragma GCC diagnostic ignored "-Wformat-y2k"
1487 static void
1488 test_strftime (void)
1489 {
1490 #ifdef __linux__
1491 #define TEST_FORMAT \
1492   "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 " \
1493   "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 %%"
1494   time_t t;
1495
1496   /* 127997 is prime, 1315005118 is now */
1497   for (t = 0; t < 1315005118; t += 127997)
1498     {
1499       GDateTime *date_time;
1500       gchar c_str[1000];
1501       gchar *dt_str;
1502
1503       date_time = g_date_time_new_from_unix_local (t);
1504       dt_str = g_date_time_format (date_time, TEST_FORMAT);
1505       strftime (c_str, sizeof c_str, TEST_FORMAT, localtime (&t));
1506       g_assert_cmpstr (c_str, ==, dt_str);
1507       g_date_time_unref (date_time);
1508       g_free (dt_str);
1509     }
1510 #endif
1511 }
1512 #pragma GCC diagnostic pop
1513
1514 static void
1515 test_find_interval (void)
1516 {
1517   GTimeZone *tz;
1518   GDateTime *dt;
1519   gint64 u;
1520   gint i1, i2;
1521
1522 #ifdef G_OS_UNIX
1523   tz = g_time_zone_new ("America/Toronto");
1524 #elif defined G_OS_WIN32
1525   tz = g_time_zone_new ("Eastern Standard Time");
1526 #endif
1527   dt = g_date_time_new_utc (2010, 11, 7, 1, 30, 0);
1528   u = g_date_time_to_unix (dt);
1529
1530   i1 = g_time_zone_find_interval (tz, G_TIME_TYPE_STANDARD, u);
1531   i2 = g_time_zone_find_interval (tz, G_TIME_TYPE_DAYLIGHT, u);
1532
1533   g_assert_cmpint (i1, !=, i2);
1534
1535   g_date_time_unref (dt);
1536
1537   dt = g_date_time_new_utc (2010, 3, 14, 2, 0, 0);
1538   u = g_date_time_to_unix (dt);
1539
1540   i1 = g_time_zone_find_interval (tz, G_TIME_TYPE_STANDARD, u);
1541   g_assert_cmpint (i1, ==, -1);
1542
1543   g_date_time_unref (dt);
1544   g_time_zone_unref (tz);
1545 }
1546
1547 static void
1548 test_adjust_time (void)
1549 {
1550   GTimeZone *tz;
1551   GDateTime *dt;
1552   gint64 u, u2;
1553   gint i1, i2;
1554
1555 #ifdef G_OS_UNIX
1556   tz = g_time_zone_new ("America/Toronto");
1557 #elif defined G_OS_WIN32
1558   tz = g_time_zone_new ("Eastern Standard Time");
1559 #endif
1560   dt = g_date_time_new_utc (2010, 11, 7, 1, 30, 0);
1561   u = g_date_time_to_unix (dt);
1562   u2 = u;
1563
1564   i1 = g_time_zone_find_interval (tz, G_TIME_TYPE_DAYLIGHT, u);
1565   i2 = g_time_zone_adjust_time (tz, G_TIME_TYPE_DAYLIGHT, &u2);
1566
1567   g_assert_cmpint (i1, ==, i2);
1568   g_assert (u == u2);
1569
1570   g_date_time_unref (dt);
1571
1572   dt = g_date_time_new_utc (2010, 3, 14, 2, 30, 0);
1573   u2 = g_date_time_to_unix (dt);
1574   g_date_time_unref (dt);
1575
1576   dt = g_date_time_new_utc (2010, 3, 14, 3, 0, 0);
1577   u = g_date_time_to_unix (dt);
1578   g_date_time_unref (dt);
1579
1580   i1 = g_time_zone_adjust_time (tz, G_TIME_TYPE_DAYLIGHT, &u2);
1581   g_assert (u == u2);
1582
1583   g_time_zone_unref (tz);
1584 }
1585
1586 static void
1587 test_no_header (void)
1588 {
1589   GTimeZone *tz;
1590
1591   tz = g_time_zone_new ("blabla");
1592
1593   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "UTC");
1594   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 0);
1595   g_assert (!g_time_zone_is_dst (tz, 0));
1596
1597   g_time_zone_unref (tz);
1598 }
1599
1600 static void
1601 test_posix_parse (void)
1602 {
1603   GTimeZone *tz;
1604   GDateTime *gdt1, *gdt2;
1605
1606   tz = g_time_zone_new ("PST");
1607   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "UTC");
1608   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 0);
1609   g_assert (!g_time_zone_is_dst (tz, 0));
1610   g_time_zone_unref (tz);
1611
1612   tz = g_time_zone_new ("PST8");
1613   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "PST");
1614   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, - 8 * 3600);
1615   g_assert (!g_time_zone_is_dst (tz, 0));
1616   g_time_zone_unref (tz);
1617
1618 /* This fails rules_from_identifier on Unix (though not on Windows)
1619  * but passes anyway because PST8PDT is a zone name.
1620  */
1621   tz = g_time_zone_new ("PST8PDT");
1622   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "PST");
1623   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, - 8 * 3600);
1624   g_assert (!g_time_zone_is_dst (tz, 0));
1625   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "PDT");
1626   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==,- 7 * 3600);
1627   g_assert (g_time_zone_is_dst (tz, 1));
1628   g_time_zone_unref (tz);
1629
1630   tz = g_time_zone_new ("PST8PDT6:32:15");
1631 #ifdef G_OS_WIN32
1632   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "PST");
1633   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, - 8 * 3600);
1634   g_assert (!g_time_zone_is_dst (tz, 0));
1635   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "PDT");
1636   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, - 6 * 3600 - 32 *60 - 15);
1637   g_assert (g_time_zone_is_dst (tz, 1));
1638   gdt1 = g_date_time_new (tz, 2012, 12, 6, 11, 15, 23.0);
1639   gdt2 = g_date_time_new (tz, 2012, 6, 6, 11, 15, 23.0);
1640   g_assert (!g_date_time_is_daylight_savings (gdt1));
1641   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) /  1000000, ==, -28800);
1642   g_assert (g_date_time_is_daylight_savings (gdt2));
1643   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, -23535);
1644   g_date_time_unref (gdt1);
1645   g_date_time_unref (gdt2);
1646 #else
1647   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "UTC");
1648   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 0);
1649   g_assert (!g_time_zone_is_dst (tz, 0));
1650 #endif
1651   g_time_zone_unref (tz);
1652
1653   tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,M10.1.0,M3.3.0");
1654   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
1655   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
1656   g_assert (!g_time_zone_is_dst (tz, 0));
1657   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
1658   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
1659   g_assert (g_time_zone_is_dst (tz, 1));
1660   gdt1 = g_date_time_new (tz, 2012, 3, 18, 0, 15, 23.0);
1661   gdt2 = g_date_time_new (tz, 2012, 3, 18, 3, 15, 23.0);
1662   g_assert (g_date_time_is_daylight_savings (gdt1));
1663   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1664   g_assert (!g_date_time_is_daylight_savings (gdt2));
1665   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1666   g_date_time_unref (gdt1);
1667   g_date_time_unref (gdt2);
1668   gdt1 = g_date_time_new (tz, 2012, 10, 7, 3, 15, 23.0);
1669   gdt2 = g_date_time_new (tz, 2012, 10, 7, 1, 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   g_time_zone_unref (tz);
1677
1678   tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,280,77");
1679   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
1680   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
1681   g_assert (!g_time_zone_is_dst (tz, 0));
1682   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
1683   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
1684   g_assert (g_time_zone_is_dst (tz, 1));
1685   gdt1 = g_date_time_new (tz, 2012, 3, 18, 0, 15, 23.0);
1686   gdt2 = g_date_time_new (tz, 2012, 3, 18, 3, 15, 23.0);
1687   g_assert (g_date_time_is_daylight_savings (gdt1));
1688   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1689   g_assert (!g_date_time_is_daylight_savings (gdt2));
1690   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1691   g_date_time_unref (gdt1);
1692   g_date_time_unref (gdt2);
1693   gdt1 = g_date_time_new (tz, 2012, 10, 7, 3, 15, 23.0);
1694   gdt2 = g_date_time_new (tz, 2012, 10, 7, 1, 15, 23.0);
1695   g_assert (g_date_time_is_daylight_savings (gdt1));
1696   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1697   g_assert (!g_date_time_is_daylight_savings (gdt2));
1698   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1699   g_date_time_unref (gdt1);
1700   g_date_time_unref (gdt2);
1701   g_time_zone_unref (tz);
1702
1703   tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,J279,J76");
1704   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
1705   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
1706   g_assert (!g_time_zone_is_dst (tz, 0));
1707   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
1708   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
1709   g_assert (g_time_zone_is_dst (tz, 1));
1710   gdt1 = g_date_time_new (tz, 2012, 3, 18, 0, 15, 23.0);
1711   gdt2 = g_date_time_new (tz, 2012, 3, 18, 3, 15, 23.0);
1712   g_assert (g_date_time_is_daylight_savings (gdt1));
1713   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1714   g_assert (!g_date_time_is_daylight_savings (gdt2));
1715   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1716   g_date_time_unref (gdt1);
1717   g_date_time_unref (gdt2);
1718   gdt1 = g_date_time_new (tz, 2012, 10, 7, 3, 15, 23.0);
1719   gdt2 = g_date_time_new (tz, 2012, 10, 7, 1, 15, 23.0);
1720   g_assert (g_date_time_is_daylight_savings (gdt1));
1721   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1722   g_assert (!g_date_time_is_daylight_savings (gdt2));
1723   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1724   g_date_time_unref (gdt1);
1725   g_date_time_unref (gdt2);
1726   g_time_zone_unref (tz);
1727
1728   tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,M10.1.0/07:00,M3.3.0/07:00");
1729   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
1730   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
1731   g_assert (!g_time_zone_is_dst (tz, 0));
1732   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
1733   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
1734   g_assert (g_time_zone_is_dst (tz, 1));
1735   gdt1 = g_date_time_new (tz, 2012, 3, 18, 5, 15, 23.0);
1736   gdt2 = g_date_time_new (tz, 2012, 3, 18, 8, 15, 23.0);
1737   g_assert (g_date_time_is_daylight_savings (gdt1));
1738   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1739   g_assert (!g_date_time_is_daylight_savings (gdt2));
1740   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1741   g_date_time_unref (gdt1);
1742   g_date_time_unref (gdt2);
1743   gdt1 = g_date_time_new (tz, 2012, 10, 7, 8, 15, 23.0);
1744   gdt2 = g_date_time_new (tz, 2012, 10, 7, 6, 15, 23.0);
1745   g_assert (g_date_time_is_daylight_savings (gdt1));
1746   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1747   g_assert (!g_date_time_is_daylight_savings (gdt2));
1748   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1749   g_date_time_unref (gdt1);
1750   g_date_time_unref (gdt2);
1751   gdt1 = g_date_time_new (tz, 1902, 10, 7, 8, 15, 23.0);
1752   gdt2 = g_date_time_new (tz, 1902, 10, 7, 6, 15, 23.0);
1753   g_assert (!g_date_time_is_daylight_savings (gdt1));
1754   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 43200);
1755   g_assert (!g_date_time_is_daylight_savings (gdt2));
1756   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1757   g_date_time_unref (gdt1);
1758   g_date_time_unref (gdt2);
1759   gdt1 = g_date_time_new (tz, 2142, 10, 7, 8, 15, 23.0);
1760   gdt2 = g_date_time_new (tz, 2142, 10, 7, 6, 15, 23.0);
1761   g_assert (g_date_time_is_daylight_savings (gdt1));
1762   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1763   g_assert (!g_date_time_is_daylight_savings (gdt2));
1764   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1765   g_date_time_unref (gdt1);
1766   g_date_time_unref (gdt2);
1767   gdt1 = g_date_time_new (tz, 3212, 10, 7, 8, 15, 23.0);
1768   gdt2 = g_date_time_new (tz, 3212, 10, 7, 6, 15, 23.0);
1769   g_assert (!g_date_time_is_daylight_savings (gdt1));
1770   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 43200);
1771   g_assert (!g_date_time_is_daylight_savings (gdt2));
1772   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1773   g_date_time_unref (gdt1);
1774   g_date_time_unref (gdt2);
1775   g_time_zone_unref (tz);
1776 }
1777
1778 gint
1779 main (gint   argc,
1780       gchar *argv[])
1781 {
1782   g_test_init (&argc, &argv, NULL);
1783   g_test_bug_base ("http://bugzilla.gnome.org/");
1784
1785   /* GDateTime Tests */
1786
1787   g_test_add_func ("/GDateTime/invalid", test_GDateTime_invalid);
1788   g_test_add_func ("/GDateTime/add_days", test_GDateTime_add_days);
1789   g_test_add_func ("/GDateTime/add_full", test_GDateTime_add_full);
1790   g_test_add_func ("/GDateTime/add_hours", test_GDateTime_add_hours);
1791   g_test_add_func ("/GDateTime/add_minutes", test_GDateTime_add_minutes);
1792   g_test_add_func ("/GDateTime/add_months", test_GDateTime_add_months);
1793   g_test_add_func ("/GDateTime/add_seconds", test_GDateTime_add_seconds);
1794   g_test_add_func ("/GDateTime/add_weeks", test_GDateTime_add_weeks);
1795   g_test_add_func ("/GDateTime/add_years", test_GDateTime_add_years);
1796   g_test_add_func ("/GDateTime/compare", test_GDateTime_compare);
1797   g_test_add_func ("/GDateTime/diff", test_GDateTime_diff);
1798   g_test_add_func ("/GDateTime/equal", test_GDateTime_equal);
1799   g_test_add_func ("/GDateTime/get_day_of_week", test_GDateTime_get_day_of_week);
1800   g_test_add_func ("/GDateTime/get_day_of_month", test_GDateTime_get_day_of_month);
1801   g_test_add_func ("/GDateTime/get_day_of_year", test_GDateTime_get_day_of_year);
1802   g_test_add_func ("/GDateTime/get_hour", test_GDateTime_get_hour);
1803   g_test_add_func ("/GDateTime/get_microsecond", test_GDateTime_get_microsecond);
1804   g_test_add_func ("/GDateTime/get_minute", test_GDateTime_get_minute);
1805   g_test_add_func ("/GDateTime/get_month", test_GDateTime_get_month);
1806   g_test_add_func ("/GDateTime/get_second", test_GDateTime_get_second);
1807   g_test_add_func ("/GDateTime/get_utc_offset", test_GDateTime_get_utc_offset);
1808   g_test_add_func ("/GDateTime/get_year", test_GDateTime_get_year);
1809   g_test_add_func ("/GDateTime/hash", test_GDateTime_hash);
1810   g_test_add_func ("/GDateTime/new_from_unix", test_GDateTime_new_from_unix);
1811   g_test_add_func ("/GDateTime/new_from_unix_utc", test_GDateTime_new_from_unix_utc);
1812   g_test_add_func ("/GDateTime/new_from_unix/overflow", test_GDateTime_new_from_unix_overflow);
1813   g_test_add_func ("/GDateTime/new_from_timeval", test_GDateTime_new_from_timeval);
1814   g_test_add_func ("/GDateTime/new_from_timeval_utc", test_GDateTime_new_from_timeval_utc);
1815   g_test_add_func ("/GDateTime/new_from_timeval/overflow", test_GDateTime_new_from_timeval_overflow);
1816   g_test_add_func ("/GDateTime/new_full", test_GDateTime_new_full);
1817   g_test_add_func ("/GDateTime/now", test_GDateTime_now);
1818   g_test_add_func ("/GDateTime/printf", test_GDateTime_printf);
1819   g_test_add_func ("/GDateTime/non_utf8_printf", test_non_utf8_printf);
1820   g_test_add_func ("/GDateTime/strftime", test_strftime);
1821   g_test_add_func ("/GDateTime/modifiers", test_modifiers);
1822   g_test_add_func ("/GDateTime/to_local", test_GDateTime_to_local);
1823   g_test_add_func ("/GDateTime/to_unix", test_GDateTime_to_unix);
1824   g_test_add_func ("/GDateTime/to_timeval", test_GDateTime_to_timeval);
1825   g_test_add_func ("/GDateTime/to_utc", test_GDateTime_to_utc);
1826   g_test_add_func ("/GDateTime/now_utc", test_GDateTime_now_utc);
1827   g_test_add_func ("/GDateTime/dst", test_GDateTime_dst);
1828   g_test_add_func ("/GDateTime/test_z", test_z);
1829   g_test_add_func ("/GDateTime/test-all-dates", test_all_dates);
1830   g_test_add_func ("/GTimeZone/find-interval", test_find_interval);
1831   g_test_add_func ("/GTimeZone/adjust-time", test_adjust_time);
1832   g_test_add_func ("/GTimeZone/no-header", test_no_header);
1833   g_test_add_func ("/GTimeZone/posix-parse", test_posix_parse);
1834
1835   return g_test_run ();
1836 }