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