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