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