3 * Copyright (C) 2009-2010 Christian Hergert <chris@dronelabs.com>
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.
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.
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/>.
25 #include <glib/gstdio.h>
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))); \
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))); \
43 get_localtime_tm (time_t time_,
46 #ifdef HAVE_LOCALTIME_R
47 localtime_r (&time_, retval);
50 struct tm *ptm = localtime (&time_);
54 /* Happens at least in Microsoft's C library if you pass a
55 * negative time_t. Use 2000-01-01 as default date.
57 #ifndef G_DISABLE_CHECKS
58 g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, "ptm != NULL");
63 retval->tm_year = 100;
66 memcpy ((void *) retval, (void *) ptm, sizeof (struct tm));
68 #endif /* HAVE_LOCALTIME_R */
72 test_GDateTime_now (void)
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. */
85 before = g_get_real_time () / G_TIME_SPAN_SECOND;
87 memset (&tm, 0, sizeof (tm));
88 get_localtime_tm (before, &tm);
90 dt = g_date_time_new_now_local ();
92 after = g_get_real_time () / G_TIME_SPAN_SECOND;
94 while (before != after);
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);
103 g_date_time_unref (dt);
107 test_GDateTime_new_from_unix (void)
113 memset (&tm, 0, sizeof (tm));
115 g_assert_cmpint (t, !=, (time_t) -1);
116 get_localtime_tm (t, &tm);
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);
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));
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);
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. */
154 test_GDateTime_new_from_unix_overflow (void)
158 g_test_bug ("782089");
160 dt = g_date_time_new_from_unix_utc (G_MAXINT64);
163 dt = g_date_time_new_from_unix_local (G_MAXINT64);
168 test_GDateTime_invalid (void)
172 g_test_bug ("702674");
174 dt = g_date_time_new_utc (2013, -2147483647, 31, 17, 15, 48);
175 g_assert (dt == NULL);
179 test_GDateTime_compare (void)
181 GDateTime *dt1, *dt2;
184 dt1 = g_date_time_new_utc (2000, 1, 1, 0, 0, 0);
186 for (i = 1; i < 2000; i++)
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);
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);
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);
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);
208 test_GDateTime_equal (void)
210 GDateTime *dt1, *dt2;
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);
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);
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));
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);
234 g_assert (g_date_time_equal (dt1, dt2));
235 g_date_time_unref (dt1);
237 /* America/Recife is in UTC-0300 */
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");
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);
252 test_GDateTime_get_day_of_week (void)
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);
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);
266 test_GDateTime_get_day_of_month (void)
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);
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);
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);
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);
288 test_GDateTime_get_hour (void)
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);
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);
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);
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);
310 test_GDateTime_get_microsecond (void)
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);
322 test_GDateTime_get_year (void)
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);
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);
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);
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);
344 test_GDateTime_hash (void)
348 h = g_hash_table_new_full (g_date_time_hash, g_date_time_equal,
349 (GDestroyNotify)g_date_time_unref,
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);
357 test_GDateTime_new_from_timeval (void)
362 g_get_current_time (&tv);
363 dt = g_date_time_new_from_timeval_local (&tv);
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));
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);
382 find_maximum_supported_tv_sec (void)
384 glong highest_success = 0, lowest_failure = G_MAXLONG;
386 GDateTime *dt = NULL;
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);
395 highest_success = tv.tv_sec;
396 g_date_time_unref (dt);
399 while (highest_success < lowest_failure - 1)
401 tv.tv_sec = highest_success + (lowest_failure - highest_success) / 2;
402 dt = g_date_time_new_from_timeval_utc (&tv);
406 highest_success = tv.tv_sec;
407 g_date_time_unref (dt);
411 lowest_failure = tv.tv_sec;
415 return highest_success;
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.
422 * This varies a bit between 32- and 64-bit architectures, due to the
423 * differences in the size of glong (tv.tv_sec). */
425 test_GDateTime_new_from_timeval_overflow (void)
430 g_test_bug ("782089");
432 tv.tv_sec = find_maximum_supported_tv_sec ();
433 tv.tv_usec = G_USEC_PER_SEC - 1;
435 g_test_message ("Maximum supported GTimeVal.tv_sec = %lu", tv.tv_sec);
437 /* Sanity check: do we support the year 2000? */
438 g_assert_cmpint (tv.tv_sec, >=, 946684800);
440 dt = g_date_time_new_from_timeval_utc (&tv);
441 g_assert_nonnull (dt);
442 g_date_time_unref (dt);
444 if (tv.tv_sec < G_MAXLONG)
449 dt = g_date_time_new_from_timeval_utc (&tv);
455 test_GDateTime_new_from_timeval_utc (void)
460 g_get_current_time (&tv);
461 dt = g_date_time_new_from_timeval_utc (&tv);
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));
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);
480 test_GDateTime_new_from_iso8601 (void)
485 /* Need non-empty string */
486 dt = g_date_time_new_from_iso8601 ("", NULL);
489 /* Needs to be correctly formatted */
490 dt = g_date_time_new_from_iso8601 ("not a date", NULL);
493 dt = g_date_time_new_from_iso8601 (" +55", NULL);
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);
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);
511 /* Can't have whitespace */
512 dt = g_date_time_new_from_iso8601 ("2016 08 24T22:10:42Z", NULL);
514 dt = g_date_time_new_from_iso8601 ("2016-08-24T22:10:42Z ", NULL);
516 dt = g_date_time_new_from_iso8601 (" 2016-08-24T22:10:42Z", NULL);
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);
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);
535 /* Months are two digits */
536 dt = g_date_time_new_from_iso8601 ("2016-1-01T22:10:42Z", NULL);
539 /* Days are two digits */
540 dt = g_date_time_new_from_iso8601 ("2016-01-1T22:10:42Z", NULL);
543 /* Need consistent usage of separators */
544 dt = g_date_time_new_from_iso8601 ("2016-0824T22:10:42Z", NULL);
546 dt = g_date_time_new_from_iso8601 ("201608-24T22:10:42Z", NULL);
549 /* Check month within valid range */
550 dt = g_date_time_new_from_iso8601 ("2016-00-13T22:10:42Z", NULL);
552 dt = g_date_time_new_from_iso8601 ("2016-13-13T22:10:42Z", NULL);
555 /* Check day within valid range */
556 dt = g_date_time_new_from_iso8601 ("2016-01-00T22:10:42Z", NULL);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
646 /* Days start at 1 */
647 dt = g_date_time_new_from_iso8601 ("2016-000T22:10:42Z", NULL);
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);
654 /* Days are two digits */
655 dt = g_date_time_new_from_iso8601 ("2016-1T22:10:42Z", NULL);
657 dt = g_date_time_new_from_iso8601 ("2016-12T22:10:42Z", NULL);
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);
670 /* We don't support weeks without weekdays (valid ISO 8601) */
671 dt = g_date_time_new_from_iso8601 ("2016-W34T22:10:42Z", NULL);
673 dt = g_date_time_new_from_iso8601 ("2016W34T22:10:42Z", NULL);
676 /* Weeks are two digits */
677 dt = g_date_time_new_from_iso8601 ("2016-W3-1T22:10:42Z", NULL);
680 /* Weeks start at 1 */
681 dt = g_date_time_new_from_iso8601 ("2016-W00-1T22:10:42Z", NULL);
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);
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);
694 /* Week 53 doesn't always exist */
695 dt = g_date_time_new_from_iso8601 ("2016-W53-1T22:10:42Z", NULL);
698 /* Limited to number of days in the week */
699 dt = g_date_time_new_from_iso8601 ("2016-W34-0T22:10:42Z", NULL);
701 dt = g_date_time_new_from_iso8601 ("2016-W34-8T22:10:42Z", NULL);
704 /* Days are one digit */
705 dt = g_date_time_new_from_iso8601 ("2016-W34-99T22:10:42Z", NULL);
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);
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);
718 /* YYYY-MM not allowed (NOT valid ISO 8601) */
719 dt = g_date_time_new_from_iso8601 ("2016-08T22:10:42Z", NULL);
722 /* We don't support omitted year (valid ISO 8601) */
723 dt = g_date_time_new_from_iso8601 ("--08-24T22:10:42Z", NULL);
725 dt = g_date_time_new_from_iso8601 ("--0824T22:10:42Z", NULL);
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);
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);
744 /* We don't support times without minutes / seconds (valid ISO 8601) */
745 dt = g_date_time_new_from_iso8601 ("2016-08-24T22Z", NULL);
747 dt = g_date_time_new_from_iso8601 ("2016-08-24T22:10Z", NULL);
749 dt = g_date_time_new_from_iso8601 ("2016-08-24T2210Z", NULL);
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);
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);
776 /* Timezone seconds not allowed */
777 dt = g_date_time_new_from_iso8601 ("2016-08-24T22-12:00:00", NULL);
779 dt = g_date_time_new_from_iso8601 ("2016-08-24T22-12:00:00.000", NULL);
782 /* Timezone hours two digits */
783 dt = g_date_time_new_from_iso8601 ("2016-08-24T22-2Z", NULL);
788 test_GDateTime_to_unix (void)
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);
801 test_GDateTime_add_years (void)
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);
813 test_GDateTime_add_months (void)
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); \
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);
838 test_GDateTime_add_days (void)
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); \
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);
861 test_GDateTime_add_weeks (void)
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); \
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);
881 test_GDateTime_add_hours (void)
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); \
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);
902 test_GDateTime_add_full (void)
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); \
918 TEST_ADD_FULL (2009, 10, 21, 0, 0, 0,
920 2010, 11, 22, 1, 1, 1);
921 TEST_ADD_FULL (2000, 1, 1, 1, 1, 1,
923 2000, 2, 1, 1, 1, 1);
924 TEST_ADD_FULL (2000, 1, 1, 0, 0, 0,
926 1999, 2, 1, 0, 0, 0);
927 TEST_ADD_FULL (2010, 10, 31, 0, 0, 0,
929 2011, 2, 28, 0, 0, 0);
930 TEST_ADD_FULL (2010, 8, 25, 22, 45, 0,
932 2010, 10, 2, 0, 10, 0);
936 test_GDateTime_add_minutes (void)
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); \
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);
955 test_GDateTime_add_seconds (void)
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); \
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);
978 test_GDateTime_diff (void)
980 #define TEST_DIFF(y,m,d,y2,m2,d2,u) G_STMT_START { \
981 GDateTime *dt1, *dt2; \
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); \
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);
996 /* TODO: Add usec tests */
1000 test_GDateTime_get_minute (void)
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);
1010 test_GDateTime_get_month (void)
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);
1020 test_GDateTime_get_second (void)
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);
1030 test_GDateTime_new_full (void)
1032 GTimeZone *tz, *dt_tz;
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);
1045 tz = g_time_zone_new ("America/Tijuana");
1046 #elif defined G_OS_WIN32
1047 tz = g_time_zone_new ("Pacific Standard Time");
1049 dt = g_date_time_new (tz, 2010, 11, 24, 8, 4, 0);
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));
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));
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));
1070 g_assert (!g_date_time_is_daylight_savings (dt));
1071 g_date_time_unref (dt);
1072 g_time_zone_unref (tz);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
1143 test_GDateTime_now_utc (void)
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. */
1156 t = g_get_real_time () / G_TIME_SPAN_SECOND;
1157 #ifdef HAVE_GMTIME_R
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
1166 memcpy (&tm, tmp, sizeof (struct tm));
1169 dt = g_date_time_new_now_utc ();
1171 after = g_get_real_time () / G_TIME_SPAN_SECOND;
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);
1185 test_GDateTime_new_from_unix_utc (void)
1190 t = g_get_real_time ();
1193 dt = g_date_time_new_from_unix_utc (t);
1194 g_assert (dt == NULL);
1197 t = t / 1e6; /* oops, this was microseconds */
1199 dt = g_date_time_new_from_unix_utc (t);
1200 g_assert (dt != NULL);
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);
1209 test_GDateTime_get_utc_offset (void)
1211 #if defined (HAVE_STRUCT_TM_TM_GMTOFF) || defined (HAVE_STRUCT_TM___TM_GMTOFF)
1216 memset (&tm, 0, sizeof (tm));
1217 get_localtime_tm (g_get_real_time () / G_TIME_SPAN_SECOND, &tm);
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));
1224 #ifdef HAVE_STRUCT_TM___TM_GMTOFF
1225 g_assert_cmpint (ts, ==, (tm.__tm_gmtoff * G_TIME_SPAN_SECOND));
1227 g_date_time_unref (dt);
1232 test_GDateTime_to_timeval (void)
1237 memset (&tv1, 0, sizeof (tv1));
1238 memset (&tv2, 0, sizeof (tv2));
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);
1249 test_GDateTime_to_local (void)
1251 GDateTime *utc = NULL, *now = NULL, *dt;
1252 time_t before, after;
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. */
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;
1267 while (before != after);
1269 dt = g_date_time_to_local (utc);
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));
1278 g_date_time_unref (now);
1279 g_date_time_unref (utc);
1280 g_date_time_unref (dt);
1284 test_GDateTime_to_utc (void)
1286 GDateTime *dt, *dt2;
1291 g_assert_cmpint (t, !=, (time_t) -1);
1292 #ifdef HAVE_GMTIME_R
1296 struct tm *tmp = gmtime (&t);
1297 memcpy (&tm, tmp, sizeof (struct tm));
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);
1313 test_GDateTime_get_day_of_year (void)
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
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);
1327 test_GDateTime_printf (void)
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
1333 gchar *old_lc_messages;
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
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
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
1363 old_lc_messages = g_strdup (g_getenv ("LC_MESSAGES"));
1364 g_setenv ("LC_MESSAGES", "C", TRUE);
1367 * This is a little helper to make sure we can compare timezones to
1368 * that of the generated timezone.
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 */
1378 memset (&tt, 0, sizeof(tt));
1379 get_localtime_tm (t, &tt);
1380 strftime (dst, sizeof(dst), "%Z", &tt);
1382 /* get current time_t for 20090924 in the local timezone */
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);
1430 TEST_PRINTF ("%Z", dst);
1431 #elif defined G_OS_WIN32
1432 TEST_PRINTF ("%Z", "Pacific Standard Time");
1435 if (old_lc_messages != NULL)
1436 g_setenv ("LC_MESSAGES", old_lc_messages, TRUE);
1438 g_unsetenv ("LC_MESSAGES");
1439 g_free (old_lc_messages);
1443 test_non_utf8_printf (void)
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)
1453 g_test_skip ("Skipping due to running uninstalled. "
1454 "This test can only be run when the translations are installed.");
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)
1462 g_test_skip ("locale ja_JP.eucjp not available, skipping non-UTF8 tests");
1466 if (g_get_charset (NULL))
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);
1474 /* These are the outputs that ja_JP.UTF-8 generates; if everything
1475 * is working then ja_JP.eucjp should generate the same.
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");
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");
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");
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");
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);
1527 setlocale (LC_ALL, oldlocale);
1531 /* Checks that it is possible to use format string that
1532 * is unrepresentable in current locale charset. */
1534 test_format_unrepresentable (void)
1536 gchar *oldlocale = g_strdup (setlocale (LC_ALL, NULL));
1537 setlocale (LC_ALL, "POSIX");
1539 TEST_PRINTF ("ąśćł", "ąśćł");
1541 /* We are using Unicode ratio symbol here, which is outside ASCII. */
1542 TEST_PRINTF_TIME (23, 15, 0, "%H∶%M", "23∶15");
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");
1548 g_test_skip ("locale pl_PL.ISO-8859-2 not available, skipping test");
1550 setlocale (LC_ALL, oldlocale);
1555 test_modifiers (void)
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");
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");
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");
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");
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");
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)
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' */
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' */
1614 g_test_skip ("locale fa_IR not available, skipping O modifier tests");
1615 setlocale (LC_ALL, oldlocale);
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. */
1622 test_month_names (void)
1626 g_test_bug ("749206");
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)
1634 g_test_skip ("Skipping due to running uninstalled. "
1635 "This test can only be run when the translations are installed.");
1639 oldlocale = g_strdup (setlocale (LC_ALL, NULL));
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)
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");
1653 g_test_skip ("locale en_GB not available, skipping English month names test");
1655 setlocale (LC_ALL, "de_DE.utf-8");
1656 if (strstr (setlocale (LC_ALL, NULL), "de_DE") != NULL)
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");
1666 g_test_skip ("locale de_DE not available, skipping German month names test");
1668 setlocale (LC_ALL, "es_ES.utf-8");
1669 if (strstr (setlocale (LC_ALL, NULL), "es_ES") != NULL)
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");
1679 g_test_skip ("locale es_ES not available, skipping Spanish month names test");
1681 setlocale (LC_ALL, "fr_FR.utf-8");
1682 if (strstr (setlocale (LC_ALL, NULL), "fr_FR") != NULL)
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.");
1692 g_test_skip ("locale fr_FR not available, skipping French month names test");
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)
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", "Αύγ");
1708 g_test_skip ("locale el_GR not available, skipping Greek month names test");
1710 setlocale (LC_ALL, "hr_HR.utf-8");
1711 if (strstr (setlocale (LC_ALL, NULL), "hr_HR") != NULL)
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");
1723 g_test_skip ("locale hr_HR not available, skipping Croatian month names test");
1725 setlocale (LC_ALL, "lt_LT.utf-8");
1726 if (strstr (setlocale (LC_ALL, NULL), "lt_LT") != NULL)
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.");
1738 g_test_skip ("locale lt_LT not available, skipping Lithuanian month names test");
1740 setlocale (LC_ALL, "pl_PL.utf-8");
1741 if (strstr (setlocale (LC_ALL, NULL), "pl_PL") != NULL)
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");
1753 g_test_skip ("locale pl_PL not available, skipping Polish month names test");
1755 setlocale (LC_ALL, "ru_RU.utf-8");
1756 if (strstr (setlocale (LC_ALL, NULL), "ru_RU") != NULL)
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", "май");
1771 g_test_skip ("locale ru_RU not available, skipping Russian month names test");
1773 setlocale (LC_ALL, oldlocale);
1778 test_GDateTime_dst (void)
1780 GDateTime *dt1, *dt2;
1783 /* this date has the DST state set for Europe/London */
1785 tz = g_time_zone_new ("Europe/London");
1786 #elif defined G_OS_WIN32
1787 tz = g_time_zone_new ("GMT Standard Time");
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);
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);
1800 g_date_time_unref (dt2);
1801 g_date_time_unref (dt1);
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);
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);
1812 g_date_time_unref (dt2);
1813 g_date_time_unref (dt1);
1814 g_time_zone_unref (tz);
1817 static inline gboolean
1818 is_leap_year (gint year)
1820 g_assert (1 <= year && year <= 9999);
1822 return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
1826 days_in_month (gint year, gint month)
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}
1833 g_assert (1 <= month && month <= 12);
1835 return table[is_leap_year (year)][month];
1839 test_all_dates (void)
1841 gint year, month, day;
1842 GTimeZone *timezone;
1849 /* save some time by hanging on to this. */
1850 timezone = g_time_zone_new_utc ();
1852 unix_time = G_GINT64_CONSTANT(-62135596800);
1854 /* 0001-01-01 is 0001-W01-1 */
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. :)
1864 for (year = 1; year <= 9999; year++)
1868 for (month = 1; month <= 12; month++)
1869 for (day = 1; day <= days_in_month (year, month); day++)
1873 dt = g_date_time_new (timezone, year, month, day, 0, 0, 0);
1876 g_printerr ("%04d-%02d-%02d = %04d-W%02d-%d = %04d-%03d\n",
1878 week_year, week_num, weekday,
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",
1888 g_date_time_get_year (dt),
1889 g_date_time_get_month (dt),
1890 g_date_time_get_day_of_month (dt));
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));
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));
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));
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));
1922 /* add 24 hours to unix time */
1923 unix_time += 24 * 60 * 60;
1925 /* move day of year forward */
1928 /* move the week date forward */
1931 weekday = 1; /* Sunday -> Monday */
1933 /* NOTE: year/month/day is the final day of the week we
1936 * If we just finished the last week of last year then
1937 * we are definitely starting the first week of this
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.
1944 if (year != week_year || (month == 12 && day >= 28))
1946 /* first week of the new year */
1954 g_date_time_unref (dt);
1958 g_time_zone_unref (timezone);
1968 g_test_bug ("642935");
1970 tz = g_time_zone_new ("-08:00");
1971 dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1973 p = g_date_time_format (dt, "%z");
1974 g_assert_cmpstr (p, ==, "-0800");
1977 p = g_date_time_format (dt, "%:z");
1978 g_assert_cmpstr (p, ==, "-08:00");
1981 p = g_date_time_format (dt, "%::z");
1982 g_assert_cmpstr (p, ==, "-08:00:00");
1985 p = g_date_time_format (dt, "%:::z");
1986 g_assert_cmpstr (p, ==, "-08");
1989 g_date_time_unref (dt);
1990 g_time_zone_unref (tz);
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");
1997 g_date_time_unref (dt);
1998 g_time_zone_unref (tz);
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");
2005 g_date_time_unref (dt);
2006 g_time_zone_unref (tz);
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");
2013 g_date_time_unref (dt);
2014 g_time_zone_unref (tz);
2016 tz = g_time_zone_new ("-00:15");
2017 dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
2019 p = g_date_time_format (dt, "%z");
2020 g_assert_cmpstr (p, ==, "-0015");
2023 p = g_date_time_format (dt, "%:z");
2024 g_assert_cmpstr (p, ==, "-00:15");
2027 p = g_date_time_format (dt, "%::z");
2028 g_assert_cmpstr (p, ==, "-00:15:00");
2031 p = g_date_time_format (dt, "%:::z");
2032 g_assert_cmpstr (p, ==, "-00:15");
2035 g_date_time_unref (dt);
2036 g_time_zone_unref (tz);
2039 #pragma GCC diagnostic push
2040 #pragma GCC diagnostic ignored "-Wformat-y2k"
2042 test_strftime (void)
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 %%"
2050 /* 127997 is prime, 1315005118 is now */
2051 for (t = 0; t < 1315005118; t += 127997)
2053 GDateTime *date_time;
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);
2066 #pragma GCC diagnostic pop
2068 /* Check that g_date_time_format() correctly returns %NULL for format
2069 * placeholders which are not supported in the current locale. */
2071 test_GDateTime_strftime_error_handling (void)
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)
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);
2084 g_test_skip ("locale de_DE not available, skipping error handling tests");
2085 setlocale (LC_ALL, oldlocale);
2090 test_find_interval (void)
2098 tz = g_time_zone_new ("America/Toronto");
2099 #elif defined G_OS_WIN32
2100 tz = g_time_zone_new ("Eastern Standard Time");
2102 dt = g_date_time_new_utc (2010, 11, 7, 1, 30, 0);
2103 u = g_date_time_to_unix (dt);
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);
2108 g_assert_cmpint (i1, !=, i2);
2110 g_date_time_unref (dt);
2112 dt = g_date_time_new_utc (2010, 3, 14, 2, 0, 0);
2113 u = g_date_time_to_unix (dt);
2115 i1 = g_time_zone_find_interval (tz, G_TIME_TYPE_STANDARD, u);
2116 g_assert_cmpint (i1, ==, -1);
2118 g_date_time_unref (dt);
2119 g_time_zone_unref (tz);
2123 test_adjust_time (void)
2131 tz = g_time_zone_new ("America/Toronto");
2132 #elif defined G_OS_WIN32
2133 tz = g_time_zone_new ("Eastern Standard Time");
2135 dt = g_date_time_new_utc (2010, 11, 7, 1, 30, 0);
2136 u = g_date_time_to_unix (dt);
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);
2142 g_assert_cmpint (i1, ==, i2);
2145 g_date_time_unref (dt);
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);
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);
2155 i1 = g_time_zone_adjust_time (tz, G_TIME_TYPE_DAYLIGHT, &u2);
2158 g_time_zone_unref (tz);
2162 test_no_header (void)
2166 tz = g_time_zone_new ("blabla");
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));
2173 g_time_zone_unref (tz);
2177 test_posix_parse (void)
2180 GDateTime *gdt1, *gdt2;
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);
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);
2198 /* This fails rules_from_identifier on Unix (though not on Windows)
2199 * but passes anyway because PST8PDT is a zone name.
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);
2211 tz = g_time_zone_new ("PST8PDT6:32:15");
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);
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));
2234 g_time_zone_unref (tz);
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);
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);
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);
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);
2366 test_GDateTime_floating_point (void)
2371 g_test_bug ("697715");
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);
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. */
2384 test_identifier (void)
2387 gchar *old_tz = g_strdup (g_getenv ("TZ"));
2389 tz = g_time_zone_new ("UTC");
2390 g_assert_cmpstr (g_time_zone_get_identifier (tz), ==, "UTC");
2391 g_time_zone_unref (tz);
2393 tz = g_time_zone_new_utc ();
2394 g_assert_cmpstr (g_time_zone_get_identifier (tz), ==, "UTC");
2395 g_time_zone_unref (tz);
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);
2401 tz = g_time_zone_new ("Z");
2402 g_assert_cmpstr (g_time_zone_get_identifier (tz), ==, "Z");
2403 g_time_zone_unref (tz);
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);
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);
2417 /* Local timezone tests. */
2418 if (g_setenv ("TZ", "America/Recife", TRUE))
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);
2425 if (g_setenv ("TZ", "some rubbish", TRUE))
2427 tz = g_time_zone_new_local ();
2428 g_assert_cmpstr (g_time_zone_get_identifier (tz), ==, "UTC");
2429 g_time_zone_unref (tz);
2433 g_assert_true (g_setenv ("TZ", old_tz, TRUE));
2440 /* Test various calls to g_time_zone_new_offset(). */
2442 test_new_offset (void)
2444 const gint32 vectors[] =
2460 for (i = 0; i < G_N_ELEMENTS (vectors); i++)
2462 GTimeZone *tz = NULL;
2464 g_test_message ("Vector %" G_GSIZE_FORMAT ": %d", i, vectors[i]);
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);
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");
2482 g_test_init (&argc, &argv, NULL);
2483 g_test_bug_base ("http://bugzilla.gnome.org/");
2485 /* GDateTime Tests */
2486 bind_textdomain_codeset ("glib20", "UTF-8");
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);
2543 return g_test_run ();