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