Imported Upstream version 2.55.1
[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_skip ("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_skip ("locale ja_JP.eucjp may be available, but glib seems to think that it's equivalent to UTF-8, skipping non-UTF-8 tests. This is a known issue on Darwin");
1425       setlocale (LC_ALL, oldlocale);
1426       g_free (oldlocale);
1427       return;
1428     }
1429
1430   /* These are the outputs that ja_JP.UTF-8 generates; if everything
1431    * is working then ja_JP.eucjp should generate the same.
1432    */
1433   TEST_PRINTF ("%a", "\345\234\237");
1434   TEST_PRINTF ("%A", "\345\234\237\346\233\234\346\227\245");
1435 #ifndef __APPLE__ /* OSX just returns the number */
1436   TEST_PRINTF ("%b", "10\346\234\210");
1437 #endif
1438   TEST_PRINTF ("%B", "10\346\234\210");
1439   TEST_PRINTF ("%d", "24");
1440   TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
1441   TEST_PRINTF ("%e", "24"); // fixme
1442 #ifndef __APPLE__ /* OSX just returns the number */
1443   TEST_PRINTF ("%h", "10\346\234\210");
1444 #endif
1445   TEST_PRINTF ("%H", "00");
1446   TEST_PRINTF_TIME (15, 0, 0, "%H", "15");
1447   TEST_PRINTF ("%I", "12");
1448   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
1449   TEST_PRINTF_TIME (15, 0, 0, "%I", "03");
1450   TEST_PRINTF ("%j", "297");
1451   TEST_PRINTF ("%k", " 0");
1452   TEST_PRINTF_TIME (13, 13, 13, "%k", "13");
1453   TEST_PRINTF ("%l", "12");
1454   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
1455   TEST_PRINTF_TIME (13, 13, 13, "%l", " 1");
1456   TEST_PRINTF_TIME (10, 13, 13, "%l", "10");
1457   TEST_PRINTF ("%m", "10");
1458   TEST_PRINTF ("%M", "00");
1459 #ifndef __APPLE__ /* OSX returns latin "AM", not japanese */
1460   TEST_PRINTF ("%p", "\345\215\210\345\211\215");
1461   TEST_PRINTF_TIME (13, 13, 13, "%p", "\345\215\210\345\276\214");
1462   TEST_PRINTF ("%P", "\345\215\210\345\211\215");
1463   TEST_PRINTF_TIME (13, 13, 13, "%P", "\345\215\210\345\276\214");
1464   TEST_PRINTF ("%r", "\345\215\210\345\211\21512\346\231\20200\345\210\20600\347\247\222");
1465   TEST_PRINTF_TIME (13, 13, 13, "%r", "\345\215\210\345\276\21401\346\231\20213\345\210\20613\347\247\222");
1466 #endif
1467   TEST_PRINTF ("%R", "00:00");
1468   TEST_PRINTF_TIME (13, 13, 31, "%R", "13:13");
1469   TEST_PRINTF ("%S", "00");
1470   TEST_PRINTF ("%t", "  ");
1471   TEST_PRINTF ("%u", "6");
1472 #ifndef __APPLE__ /* OSX returns YYYY/MM/DD in ASCII */
1473   TEST_PRINTF ("%x", "2009\345\271\26410\346\234\21024\346\227\245");
1474 #endif
1475   TEST_PRINTF ("%X", "00\346\231\20200\345\210\20600\347\247\222");
1476   TEST_PRINTF_TIME (13, 14, 15, "%X", "13\346\231\20214\345\210\20615\347\247\222");
1477   TEST_PRINTF ("%y", "09");
1478   TEST_PRINTF ("%Y", "2009");
1479   TEST_PRINTF ("%%", "%");
1480   TEST_PRINTF ("%", "");
1481   TEST_PRINTF ("%9", NULL);
1482
1483   setlocale (LC_ALL, oldlocale);
1484   g_free (oldlocale);
1485 }
1486
1487 static void
1488 test_modifiers (void)
1489 {
1490   gchar *oldlocale;
1491
1492   TEST_PRINTF_DATE (2009, 1,  1,  "%d", "01");
1493   TEST_PRINTF_DATE (2009, 1,  1, "%_d", " 1");
1494   TEST_PRINTF_DATE (2009, 1,  1, "%-d", "1");
1495   TEST_PRINTF_DATE (2009, 1,  1, "%0d", "01");
1496   TEST_PRINTF_DATE (2009, 1, 21,  "%d", "21");
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, "%0d", "21");
1500
1501   TEST_PRINTF_DATE (2009, 1,  1,  "%e", " 1");
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, "%0e", "01");
1505   TEST_PRINTF_DATE (2009, 1, 21,  "%e", "21");
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, "%0e", "21");
1509
1510   TEST_PRINTF_TIME ( 1, 0, 0,  "%H", "01");
1511   TEST_PRINTF_TIME ( 1, 0, 0, "%_H", " 1");
1512   TEST_PRINTF_TIME ( 1, 0, 0, "%-H", "1");
1513   TEST_PRINTF_TIME ( 1, 0, 0, "%0H", "01");
1514   TEST_PRINTF_TIME (21, 0, 0,  "%H", "21");
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, "%0H", "21");
1518
1519   TEST_PRINTF_TIME ( 1, 0, 0,  "%I", "01");
1520   TEST_PRINTF_TIME ( 1, 0, 0, "%_I", " 1");
1521   TEST_PRINTF_TIME ( 1, 0, 0, "%-I", "1");
1522   TEST_PRINTF_TIME ( 1, 0, 0, "%0I", "01");
1523   TEST_PRINTF_TIME (23, 0, 0,  "%I", "11");
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, "%0I", "11");
1527
1528   TEST_PRINTF_TIME ( 1, 0, 0,  "%k", " 1");
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, "%0k", "01");
1532
1533   oldlocale = g_strdup (setlocale (LC_ALL, NULL));
1534   setlocale (LC_ALL, "fa_IR.utf-8");
1535   if (strstr (setlocale (LC_ALL, NULL), "fa_IR") != NULL)
1536     {
1537       TEST_PRINTF_TIME (23, 0, 0, "%OH", "\333\262\333\263");    /* '23' */
1538       TEST_PRINTF_TIME (23, 0, 0, "%OI", "\333\261\333\261");    /* '11' */
1539       TEST_PRINTF_TIME (23, 0, 0, "%OM", "\333\260\333\260");    /* '00' */
1540
1541       TEST_PRINTF_DATE (2011, 7, 1, "%Om", "\333\260\333\267");  /* '07' */
1542       TEST_PRINTF_DATE (2011, 7, 1, "%0Om", "\333\260\333\267"); /* '07' */
1543       TEST_PRINTF_DATE (2011, 7, 1, "%-Om", "\333\267");         /* '7' */
1544       TEST_PRINTF_DATE (2011, 7, 1, "%_Om", " \333\267");        /* ' 7' */
1545     }
1546   else
1547     g_test_skip ("locale fa_IR not available, skipping O modifier tests");
1548   setlocale (LC_ALL, oldlocale);
1549   g_free (oldlocale);
1550 }
1551
1552 static void
1553 test_GDateTime_dst (void)
1554 {
1555   GDateTime *dt1, *dt2;
1556   GTimeZone *tz;
1557
1558   /* this date has the DST state set for Europe/London */
1559 #ifdef G_OS_UNIX
1560   tz = g_time_zone_new ("Europe/London");
1561 #elif defined G_OS_WIN32
1562   tz = g_time_zone_new ("GMT Standard Time");
1563 #endif
1564   dt1 = g_date_time_new (tz, 2009, 8, 15, 3, 0, 1);
1565   g_assert (g_date_time_is_daylight_savings (dt1));
1566   g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, 3600);
1567   g_assert_cmpint (g_date_time_get_hour (dt1), ==, 3);
1568
1569   /* add 6 months to clear the DST flag but keep the same time */
1570   dt2 = g_date_time_add_months (dt1, 6);
1571   g_assert (!g_date_time_is_daylight_savings (dt2));
1572   g_assert_cmpint (g_date_time_get_utc_offset (dt2) / G_USEC_PER_SEC, ==, 0);
1573   g_assert_cmpint (g_date_time_get_hour (dt2), ==, 3);
1574
1575   g_date_time_unref (dt2);
1576   g_date_time_unref (dt1);
1577
1578   /* now do the reverse: start with a non-DST state and move to DST */
1579   dt1 = g_date_time_new (tz, 2009, 2, 15, 2, 0, 1);
1580   g_assert (!g_date_time_is_daylight_savings (dt1));
1581   g_assert_cmpint (g_date_time_get_hour (dt1), ==, 2);
1582
1583   dt2 = g_date_time_add_months (dt1, 6);
1584   g_assert (g_date_time_is_daylight_savings (dt2));
1585   g_assert_cmpint (g_date_time_get_hour (dt2), ==, 2);
1586
1587   g_date_time_unref (dt2);
1588   g_date_time_unref (dt1);
1589   g_time_zone_unref (tz);
1590 }
1591
1592 static inline gboolean
1593 is_leap_year (gint year)
1594 {
1595   g_assert (1 <= year && year <= 9999);
1596
1597   return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
1598 }
1599
1600 static inline gint
1601 days_in_month (gint year, gint month)
1602 {
1603   const gint table[2][13] = {
1604     {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
1605     {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
1606   };
1607
1608   g_assert (1 <= month && month <= 12);
1609
1610   return table[is_leap_year (year)][month];
1611 }
1612
1613 static void
1614 test_all_dates (void)
1615 {
1616   gint year, month, day;
1617   GTimeZone *timezone;
1618   gint64 unix_time;
1619   gint day_of_year;
1620   gint week_year;
1621   gint week_num;
1622   gint weekday;
1623
1624   /* save some time by hanging on to this. */
1625   timezone = g_time_zone_new_utc ();
1626
1627   unix_time = G_GINT64_CONSTANT(-62135596800);
1628
1629   /* 0001-01-01 is 0001-W01-1 */
1630   week_year = 1;
1631   week_num = 1;
1632   weekday = 1;
1633
1634
1635   /* The calendar makes a full cycle every 400 years, so we could
1636    * theoretically just test years 1 through 400.  That assumes that our
1637    * software has no bugs, so probably we should just test them all. :)
1638    */
1639   for (year = 1; year <= 9999; year++)
1640     {
1641       day_of_year = 1;
1642
1643       for (month = 1; month <= 12; month++)
1644         for (day = 1; day <= days_in_month (year, month); day++)
1645           {
1646             GDateTime *dt;
1647
1648             dt = g_date_time_new (timezone, year, month, day, 0, 0, 0);
1649
1650 #if 0
1651             g_printerr ("%04d-%02d-%02d = %04d-W%02d-%d = %04d-%03d\n",
1652                      year, month, day,
1653                      week_year, week_num, weekday,
1654                      year, day_of_year);
1655 #endif
1656
1657             /* sanity check */
1658             if G_UNLIKELY (g_date_time_get_year (dt) != year ||
1659                            g_date_time_get_month (dt) != month ||
1660                            g_date_time_get_day_of_month (dt) != day)
1661               g_error ("%04d-%02d-%02d comes out as %04d-%02d-%02d",
1662                        year, month, day,
1663                        g_date_time_get_year (dt),
1664                        g_date_time_get_month (dt),
1665                        g_date_time_get_day_of_month (dt));
1666
1667             if G_UNLIKELY (g_date_time_get_week_numbering_year (dt) != week_year ||
1668                            g_date_time_get_week_of_year (dt) != week_num ||
1669                            g_date_time_get_day_of_week (dt) != weekday)
1670               g_error ("%04d-%02d-%02d should be %04d-W%02d-%d but "
1671                        "comes out as %04d-W%02d-%d", year, month, day,
1672                        week_year, week_num, weekday,
1673                        g_date_time_get_week_numbering_year (dt),
1674                        g_date_time_get_week_of_year (dt),
1675                        g_date_time_get_day_of_week (dt));
1676
1677             if G_UNLIKELY (g_date_time_to_unix (dt) != unix_time)
1678               g_error ("%04d-%02d-%02d 00:00:00 UTC should have unix time %"
1679                        G_GINT64_FORMAT " but comes out as %"G_GINT64_FORMAT,
1680                        year, month, day, unix_time, g_date_time_to_unix (dt));
1681
1682             if G_UNLIKELY (g_date_time_get_day_of_year (dt) != day_of_year)
1683               g_error ("%04d-%02d-%02d should be day of year %d"
1684                        " but comes out as %d", year, month, day,
1685                        day_of_year, g_date_time_get_day_of_year (dt));
1686
1687             if G_UNLIKELY (g_date_time_get_hour (dt) != 0 ||
1688                            g_date_time_get_minute (dt) != 0 ||
1689                            g_date_time_get_seconds (dt) != 0)
1690               g_error ("%04d-%02d-%02d 00:00:00 UTC comes out "
1691                        "as %02d:%02d:%02.6f", year, month, day,
1692                        g_date_time_get_hour (dt),
1693                        g_date_time_get_minute (dt),
1694                        g_date_time_get_seconds (dt));
1695             /* done */
1696
1697             /* add 24 hours to unix time */
1698             unix_time += 24 * 60 * 60;
1699
1700             /* move day of year forward */
1701             day_of_year++;
1702
1703             /* move the week date forward */
1704             if (++weekday == 8)
1705               {
1706                 weekday = 1; /* Sunday -> Monday */
1707
1708                 /* NOTE: year/month/day is the final day of the week we
1709                  * just finished.
1710                  *
1711                  * If we just finished the last week of last year then
1712                  * we are definitely starting the first week of this
1713                  * year.
1714                  *
1715                  * Otherwise, if we're still in this year, but Sunday
1716                  * fell on or after December 28 then December 29, 30, 31
1717                  * could be days within the next year's first year.
1718                  */
1719                 if (year != week_year || (month == 12 && day >= 28))
1720                   {
1721                     /* first week of the new year */
1722                     week_num = 1;
1723                     week_year++;
1724                   }
1725                 else
1726                   week_num++;
1727               }
1728
1729             g_date_time_unref (dt);
1730           }
1731     }
1732
1733   g_time_zone_unref (timezone);
1734 }
1735
1736 static void
1737 test_z (void)
1738 {
1739   GTimeZone *tz;
1740   GDateTime *dt;
1741   gchar *p;
1742
1743   g_test_bug ("642935");
1744
1745   tz = g_time_zone_new ("-08:00");
1746   dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1747
1748   p = g_date_time_format (dt, "%z");
1749   g_assert_cmpstr (p, ==, "-0800");
1750   g_free (p);
1751
1752   p = g_date_time_format (dt, "%:z");
1753   g_assert_cmpstr (p, ==, "-08:00");
1754   g_free (p);
1755
1756   p = g_date_time_format (dt, "%::z");
1757   g_assert_cmpstr (p, ==, "-08:00:00");
1758   g_free (p);
1759
1760   p = g_date_time_format (dt, "%:::z");
1761   g_assert_cmpstr (p, ==, "-08");
1762   g_free (p);
1763
1764   g_date_time_unref (dt);
1765   g_time_zone_unref (tz);
1766
1767   tz = g_time_zone_new ("+00:00");
1768   dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1769   p = g_date_time_format (dt, "%:::z");
1770   g_assert_cmpstr (p, ==, "+00");
1771   g_free (p);
1772   g_date_time_unref (dt);
1773   g_time_zone_unref (tz);
1774
1775   tz = g_time_zone_new ("+08:23");
1776   dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1777   p = g_date_time_format (dt, "%:::z");
1778   g_assert_cmpstr (p, ==, "+08:23");
1779   g_free (p);
1780   g_date_time_unref (dt);
1781   g_time_zone_unref (tz);
1782
1783   tz = g_time_zone_new ("+08:23:45");
1784   dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1785   p = g_date_time_format (dt, "%:::z");
1786   g_assert_cmpstr (p, ==, "+08:23:45");
1787   g_free (p);
1788   g_date_time_unref (dt);
1789   g_time_zone_unref (tz);
1790 }
1791
1792 #pragma GCC diagnostic push
1793 #pragma GCC diagnostic ignored "-Wformat-y2k"
1794 static void
1795 test_strftime (void)
1796 {
1797 #ifdef __linux__
1798 #define TEST_FORMAT \
1799   "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 " \
1800   "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 %%"
1801   time_t t;
1802
1803   /* 127997 is prime, 1315005118 is now */
1804   for (t = 0; t < 1315005118; t += 127997)
1805     {
1806       GDateTime *date_time;
1807       gchar c_str[1000];
1808       gchar *dt_str;
1809
1810       date_time = g_date_time_new_from_unix_local (t);
1811       dt_str = g_date_time_format (date_time, TEST_FORMAT);
1812       strftime (c_str, sizeof c_str, TEST_FORMAT, localtime (&t));
1813       g_assert_cmpstr (c_str, ==, dt_str);
1814       g_date_time_unref (date_time);
1815       g_free (dt_str);
1816     }
1817 #endif
1818 }
1819 #pragma GCC diagnostic pop
1820
1821 /* Check that g_date_time_format() correctly returns %NULL for format
1822  * placeholders which are not supported in the current locale. */
1823 static void
1824 test_GDateTime_strftime_error_handling (void)
1825 {
1826   gchar *oldlocale;
1827
1828   oldlocale = g_strdup (setlocale (LC_ALL, NULL));
1829   setlocale (LC_ALL, "de_DE.utf-8");
1830   if (strstr (setlocale (LC_ALL, NULL), "de_DE") != NULL)
1831     {
1832       /* de_DE doesn’t ever write time in 12-hour notation, so %r is
1833        * unsupported for it. */
1834       TEST_PRINTF_TIME (23, 0, 0, "%r", NULL);
1835     }
1836   else
1837     g_test_skip ("locale de_DE not available, skipping error handling tests");
1838   setlocale (LC_ALL, oldlocale);
1839   g_free (oldlocale);
1840 }
1841
1842 static void
1843 test_find_interval (void)
1844 {
1845   GTimeZone *tz;
1846   GDateTime *dt;
1847   gint64 u;
1848   gint i1, i2;
1849
1850 #ifdef G_OS_UNIX
1851   tz = g_time_zone_new ("America/Toronto");
1852 #elif defined G_OS_WIN32
1853   tz = g_time_zone_new ("Eastern Standard Time");
1854 #endif
1855   dt = g_date_time_new_utc (2010, 11, 7, 1, 30, 0);
1856   u = g_date_time_to_unix (dt);
1857
1858   i1 = g_time_zone_find_interval (tz, G_TIME_TYPE_STANDARD, u);
1859   i2 = g_time_zone_find_interval (tz, G_TIME_TYPE_DAYLIGHT, u);
1860
1861   g_assert_cmpint (i1, !=, i2);
1862
1863   g_date_time_unref (dt);
1864
1865   dt = g_date_time_new_utc (2010, 3, 14, 2, 0, 0);
1866   u = g_date_time_to_unix (dt);
1867
1868   i1 = g_time_zone_find_interval (tz, G_TIME_TYPE_STANDARD, u);
1869   g_assert_cmpint (i1, ==, -1);
1870
1871   g_date_time_unref (dt);
1872   g_time_zone_unref (tz);
1873 }
1874
1875 static void
1876 test_adjust_time (void)
1877 {
1878   GTimeZone *tz;
1879   GDateTime *dt;
1880   gint64 u, u2;
1881   gint i1, i2;
1882
1883 #ifdef G_OS_UNIX
1884   tz = g_time_zone_new ("America/Toronto");
1885 #elif defined G_OS_WIN32
1886   tz = g_time_zone_new ("Eastern Standard Time");
1887 #endif
1888   dt = g_date_time_new_utc (2010, 11, 7, 1, 30, 0);
1889   u = g_date_time_to_unix (dt);
1890   u2 = u;
1891
1892   i1 = g_time_zone_find_interval (tz, G_TIME_TYPE_DAYLIGHT, u);
1893   i2 = g_time_zone_adjust_time (tz, G_TIME_TYPE_DAYLIGHT, &u2);
1894
1895   g_assert_cmpint (i1, ==, i2);
1896   g_assert (u == u2);
1897
1898   g_date_time_unref (dt);
1899
1900   dt = g_date_time_new_utc (2010, 3, 14, 2, 30, 0);
1901   u2 = g_date_time_to_unix (dt);
1902   g_date_time_unref (dt);
1903
1904   dt = g_date_time_new_utc (2010, 3, 14, 3, 0, 0);
1905   u = g_date_time_to_unix (dt);
1906   g_date_time_unref (dt);
1907
1908   i1 = g_time_zone_adjust_time (tz, G_TIME_TYPE_DAYLIGHT, &u2);
1909   g_assert (u == u2);
1910
1911   g_time_zone_unref (tz);
1912 }
1913
1914 static void
1915 test_no_header (void)
1916 {
1917   GTimeZone *tz;
1918
1919   tz = g_time_zone_new ("blabla");
1920
1921   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "UTC");
1922   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 0);
1923   g_assert (!g_time_zone_is_dst (tz, 0));
1924
1925   g_time_zone_unref (tz);
1926 }
1927
1928 static void
1929 test_posix_parse (void)
1930 {
1931   GTimeZone *tz;
1932   GDateTime *gdt1, *gdt2;
1933
1934   tz = g_time_zone_new ("PST");
1935   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "UTC");
1936   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 0);
1937   g_assert (!g_time_zone_is_dst (tz, 0));
1938   g_time_zone_unref (tz);
1939
1940   tz = g_time_zone_new ("PST8");
1941   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "PST");
1942   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, - 8 * 3600);
1943   g_assert (!g_time_zone_is_dst (tz, 0));
1944   g_time_zone_unref (tz);
1945
1946 /* This fails rules_from_identifier on Unix (though not on Windows)
1947  * but passes anyway because PST8PDT is a zone name.
1948  */
1949   tz = g_time_zone_new ("PST8PDT");
1950   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "PST");
1951   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, - 8 * 3600);
1952   g_assert (!g_time_zone_is_dst (tz, 0));
1953   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "PDT");
1954   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==,- 7 * 3600);
1955   g_assert (g_time_zone_is_dst (tz, 1));
1956   g_time_zone_unref (tz);
1957
1958   tz = g_time_zone_new ("PST8PDT6:32:15");
1959 #ifdef G_OS_WIN32
1960   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "PST");
1961   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, - 8 * 3600);
1962   g_assert (!g_time_zone_is_dst (tz, 0));
1963   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "PDT");
1964   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, - 6 * 3600 - 32 *60 - 15);
1965   g_assert (g_time_zone_is_dst (tz, 1));
1966   gdt1 = g_date_time_new (tz, 2012, 12, 6, 11, 15, 23.0);
1967   gdt2 = g_date_time_new (tz, 2012, 6, 6, 11, 15, 23.0);
1968   g_assert (!g_date_time_is_daylight_savings (gdt1));
1969   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) /  1000000, ==, -28800);
1970   g_assert (g_date_time_is_daylight_savings (gdt2));
1971   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, -23535);
1972   g_date_time_unref (gdt1);
1973   g_date_time_unref (gdt2);
1974 #else
1975   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "UTC");
1976   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 0);
1977   g_assert (!g_time_zone_is_dst (tz, 0));
1978 #endif
1979   g_time_zone_unref (tz);
1980
1981   tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,M10.1.0,M3.3.0");
1982   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
1983   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
1984   g_assert (!g_time_zone_is_dst (tz, 0));
1985   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
1986   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
1987   g_assert (g_time_zone_is_dst (tz, 1));
1988   gdt1 = g_date_time_new (tz, 2012, 3, 18, 0, 15, 23.0);
1989   gdt2 = g_date_time_new (tz, 2012, 3, 18, 3, 15, 23.0);
1990   g_assert (g_date_time_is_daylight_savings (gdt1));
1991   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1992   g_assert (!g_date_time_is_daylight_savings (gdt2));
1993   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1994   g_date_time_unref (gdt1);
1995   g_date_time_unref (gdt2);
1996   gdt1 = g_date_time_new (tz, 2012, 10, 7, 3, 15, 23.0);
1997   gdt2 = g_date_time_new (tz, 2012, 10, 7, 1, 15, 23.0);
1998   g_assert (g_date_time_is_daylight_savings (gdt1));
1999   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
2000   g_assert (!g_date_time_is_daylight_savings (gdt2));
2001   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2002   g_date_time_unref (gdt1);
2003   g_date_time_unref (gdt2);
2004   g_time_zone_unref (tz);
2005
2006   tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,280,77");
2007   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
2008   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
2009   g_assert (!g_time_zone_is_dst (tz, 0));
2010   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
2011   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
2012   g_assert (g_time_zone_is_dst (tz, 1));
2013   gdt1 = g_date_time_new (tz, 2012, 3, 18, 0, 15, 23.0);
2014   gdt2 = g_date_time_new (tz, 2012, 3, 18, 3, 15, 23.0);
2015   g_assert (g_date_time_is_daylight_savings (gdt1));
2016   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
2017   g_assert (!g_date_time_is_daylight_savings (gdt2));
2018   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2019   g_date_time_unref (gdt1);
2020   g_date_time_unref (gdt2);
2021   gdt1 = g_date_time_new (tz, 2012, 10, 7, 3, 15, 23.0);
2022   gdt2 = g_date_time_new (tz, 2012, 10, 7, 1, 15, 23.0);
2023   g_assert (g_date_time_is_daylight_savings (gdt1));
2024   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
2025   g_assert (!g_date_time_is_daylight_savings (gdt2));
2026   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2027   g_date_time_unref (gdt1);
2028   g_date_time_unref (gdt2);
2029   g_time_zone_unref (tz);
2030
2031   tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,J279,J76");
2032   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
2033   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
2034   g_assert (!g_time_zone_is_dst (tz, 0));
2035   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
2036   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
2037   g_assert (g_time_zone_is_dst (tz, 1));
2038   gdt1 = g_date_time_new (tz, 2012, 3, 18, 0, 15, 23.0);
2039   gdt2 = g_date_time_new (tz, 2012, 3, 18, 3, 15, 23.0);
2040   g_assert (g_date_time_is_daylight_savings (gdt1));
2041   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
2042   g_assert (!g_date_time_is_daylight_savings (gdt2));
2043   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2044   g_date_time_unref (gdt1);
2045   g_date_time_unref (gdt2);
2046   gdt1 = g_date_time_new (tz, 2012, 10, 7, 3, 15, 23.0);
2047   gdt2 = g_date_time_new (tz, 2012, 10, 7, 1, 15, 23.0);
2048   g_assert (g_date_time_is_daylight_savings (gdt1));
2049   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
2050   g_assert (!g_date_time_is_daylight_savings (gdt2));
2051   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2052   g_date_time_unref (gdt1);
2053   g_date_time_unref (gdt2);
2054   g_time_zone_unref (tz);
2055
2056   tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,M10.1.0/07:00,M3.3.0/07:00");
2057   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
2058   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
2059   g_assert (!g_time_zone_is_dst (tz, 0));
2060   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
2061   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
2062   g_assert (g_time_zone_is_dst (tz, 1));
2063   gdt1 = g_date_time_new (tz, 2012, 3, 18, 5, 15, 23.0);
2064   gdt2 = g_date_time_new (tz, 2012, 3, 18, 8, 15, 23.0);
2065   g_assert (g_date_time_is_daylight_savings (gdt1));
2066   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
2067   g_assert (!g_date_time_is_daylight_savings (gdt2));
2068   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2069   g_date_time_unref (gdt1);
2070   g_date_time_unref (gdt2);
2071   gdt1 = g_date_time_new (tz, 2012, 10, 7, 8, 15, 23.0);
2072   gdt2 = g_date_time_new (tz, 2012, 10, 7, 6, 15, 23.0);
2073   g_assert (g_date_time_is_daylight_savings (gdt1));
2074   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
2075   g_assert (!g_date_time_is_daylight_savings (gdt2));
2076   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2077   g_date_time_unref (gdt1);
2078   g_date_time_unref (gdt2);
2079   gdt1 = g_date_time_new (tz, 1902, 10, 7, 8, 15, 23.0);
2080   gdt2 = g_date_time_new (tz, 1902, 10, 7, 6, 15, 23.0);
2081   g_assert (!g_date_time_is_daylight_savings (gdt1));
2082   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 43200);
2083   g_assert (!g_date_time_is_daylight_savings (gdt2));
2084   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2085   g_date_time_unref (gdt1);
2086   g_date_time_unref (gdt2);
2087   gdt1 = g_date_time_new (tz, 2142, 10, 7, 8, 15, 23.0);
2088   gdt2 = g_date_time_new (tz, 2142, 10, 7, 6, 15, 23.0);
2089   g_assert (g_date_time_is_daylight_savings (gdt1));
2090   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
2091   g_assert (!g_date_time_is_daylight_savings (gdt2));
2092   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2093   g_date_time_unref (gdt1);
2094   g_date_time_unref (gdt2);
2095   gdt1 = g_date_time_new (tz, 3212, 10, 7, 8, 15, 23.0);
2096   gdt2 = g_date_time_new (tz, 3212, 10, 7, 6, 15, 23.0);
2097   g_assert (!g_date_time_is_daylight_savings (gdt1));
2098   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 43200);
2099   g_assert (!g_date_time_is_daylight_savings (gdt2));
2100   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2101   g_date_time_unref (gdt1);
2102   g_date_time_unref (gdt2);
2103   g_time_zone_unref (tz);
2104 }
2105
2106 static void
2107 test_GDateTime_floating_point (void)
2108 {
2109   GDateTime *dt;
2110   GTimeZone *tz;
2111
2112   g_test_bug ("697715");
2113
2114   tz = g_time_zone_new ("-03:00");
2115   dt = g_date_time_new (tz, 2010, 5, 24,  8, 0, 1.000001);
2116   g_time_zone_unref (tz);
2117   g_assert_cmpint (g_date_time_get_microsecond (dt), ==, 1);
2118   g_date_time_unref (dt);
2119 }
2120
2121 gint
2122 main (gint   argc,
2123       gchar *argv[])
2124 {
2125   g_test_init (&argc, &argv, NULL);
2126   g_test_bug_base ("http://bugzilla.gnome.org/");
2127
2128   /* GDateTime Tests */
2129
2130   g_test_add_func ("/GDateTime/invalid", test_GDateTime_invalid);
2131   g_test_add_func ("/GDateTime/add_days", test_GDateTime_add_days);
2132   g_test_add_func ("/GDateTime/add_full", test_GDateTime_add_full);
2133   g_test_add_func ("/GDateTime/add_hours", test_GDateTime_add_hours);
2134   g_test_add_func ("/GDateTime/add_minutes", test_GDateTime_add_minutes);
2135   g_test_add_func ("/GDateTime/add_months", test_GDateTime_add_months);
2136   g_test_add_func ("/GDateTime/add_seconds", test_GDateTime_add_seconds);
2137   g_test_add_func ("/GDateTime/add_weeks", test_GDateTime_add_weeks);
2138   g_test_add_func ("/GDateTime/add_years", test_GDateTime_add_years);
2139   g_test_add_func ("/GDateTime/compare", test_GDateTime_compare);
2140   g_test_add_func ("/GDateTime/diff", test_GDateTime_diff);
2141   g_test_add_func ("/GDateTime/equal", test_GDateTime_equal);
2142   g_test_add_func ("/GDateTime/get_day_of_week", test_GDateTime_get_day_of_week);
2143   g_test_add_func ("/GDateTime/get_day_of_month", test_GDateTime_get_day_of_month);
2144   g_test_add_func ("/GDateTime/get_day_of_year", test_GDateTime_get_day_of_year);
2145   g_test_add_func ("/GDateTime/get_hour", test_GDateTime_get_hour);
2146   g_test_add_func ("/GDateTime/get_microsecond", test_GDateTime_get_microsecond);
2147   g_test_add_func ("/GDateTime/get_minute", test_GDateTime_get_minute);
2148   g_test_add_func ("/GDateTime/get_month", test_GDateTime_get_month);
2149   g_test_add_func ("/GDateTime/get_second", test_GDateTime_get_second);
2150   g_test_add_func ("/GDateTime/get_utc_offset", test_GDateTime_get_utc_offset);
2151   g_test_add_func ("/GDateTime/get_year", test_GDateTime_get_year);
2152   g_test_add_func ("/GDateTime/hash", test_GDateTime_hash);
2153   g_test_add_func ("/GDateTime/new_from_unix", test_GDateTime_new_from_unix);
2154   g_test_add_func ("/GDateTime/new_from_unix_utc", test_GDateTime_new_from_unix_utc);
2155   g_test_add_func ("/GDateTime/new_from_unix/overflow", test_GDateTime_new_from_unix_overflow);
2156   g_test_add_func ("/GDateTime/new_from_timeval", test_GDateTime_new_from_timeval);
2157   g_test_add_func ("/GDateTime/new_from_timeval_utc", test_GDateTime_new_from_timeval_utc);
2158   g_test_add_func ("/GDateTime/new_from_timeval/overflow", test_GDateTime_new_from_timeval_overflow);
2159   g_test_add_func ("/GDateTime/new_from_iso8601", test_GDateTime_new_from_iso8601);
2160   g_test_add_func ("/GDateTime/new_full", test_GDateTime_new_full);
2161   g_test_add_func ("/GDateTime/now", test_GDateTime_now);
2162   g_test_add_func ("/GDateTime/printf", test_GDateTime_printf);
2163   g_test_add_func ("/GDateTime/non_utf8_printf", test_non_utf8_printf);
2164   g_test_add_func ("/GDateTime/strftime", test_strftime);
2165   g_test_add_func ("/GDateTime/strftime/error_handling", test_GDateTime_strftime_error_handling);
2166   g_test_add_func ("/GDateTime/modifiers", test_modifiers);
2167   g_test_add_func ("/GDateTime/to_local", test_GDateTime_to_local);
2168   g_test_add_func ("/GDateTime/to_unix", test_GDateTime_to_unix);
2169   g_test_add_func ("/GDateTime/to_timeval", test_GDateTime_to_timeval);
2170   g_test_add_func ("/GDateTime/to_utc", test_GDateTime_to_utc);
2171   g_test_add_func ("/GDateTime/now_utc", test_GDateTime_now_utc);
2172   g_test_add_func ("/GDateTime/dst", test_GDateTime_dst);
2173   g_test_add_func ("/GDateTime/test_z", test_z);
2174   g_test_add_func ("/GDateTime/test-all-dates", test_all_dates);
2175   g_test_add_func ("/GTimeZone/find-interval", test_find_interval);
2176   g_test_add_func ("/GTimeZone/adjust-time", test_adjust_time);
2177   g_test_add_func ("/GTimeZone/no-header", test_no_header);
2178   g_test_add_func ("/GTimeZone/posix-parse", test_posix_parse);
2179   g_test_add_func ("/GTimeZone/floating-point", test_GDateTime_floating_point);
2180
2181   return g_test_run ();
2182 }