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