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