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