Increase the timeout for some GLib tests
[glib.git] / glib / tests / gdatetime.c
blob1d11541b747fe5c1b3325ac1daedf4f20bbc250c
1 /* gdatetime-tests.c
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/>.
19 #include "config.h"
21 #include <string.h>
22 #include <time.h>
23 #include <glib.h>
24 #include <glib/gstdio.h>
25 #include <locale.h>
27 #define ASSERT_DATE(dt,y,m,d) G_STMT_START { \
28 g_assert_nonnull ((dt)); \
29 g_assert_cmpint ((y), ==, g_date_time_get_year ((dt))); \
30 g_assert_cmpint ((m), ==, g_date_time_get_month ((dt))); \
31 g_assert_cmpint ((d), ==, g_date_time_get_day_of_month ((dt))); \
32 } G_STMT_END
33 #define ASSERT_TIME(dt,H,M,S,U) G_STMT_START { \
34 g_assert_nonnull ((dt)); \
35 g_assert_cmpint ((H), ==, g_date_time_get_hour ((dt))); \
36 g_assert_cmpint ((M), ==, g_date_time_get_minute ((dt))); \
37 g_assert_cmpint ((S), ==, g_date_time_get_second ((dt))); \
38 g_assert_cmpint ((U), ==, g_date_time_get_microsecond ((dt))); \
39 } G_STMT_END
41 static void
42 get_localtime_tm (time_t time_,
43 struct tm *retval)
45 #ifdef HAVE_LOCALTIME_R
46 localtime_r (&time_, retval);
47 #else
49 struct tm *ptm = localtime (&time_);
51 if (ptm == NULL)
53 /* Happens at least in Microsoft's C library if you pass a
54 * negative time_t. Use 2000-01-01 as default date.
56 #ifndef G_DISABLE_CHECKS
57 g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, "ptm != NULL");
58 #endif
60 retval->tm_mon = 0;
61 retval->tm_mday = 1;
62 retval->tm_year = 100;
64 else
65 memcpy ((void *) retval, (void *) ptm, sizeof (struct tm));
67 #endif /* HAVE_LOCALTIME_R */
70 static void
71 test_GDateTime_now (void)
73 GDateTime *dt;
74 struct tm tm;
75 time_t before;
76 time_t after;
78 /* before <= dt.to_unix() <= after, but the inequalities might not be
79 * equality if we're close to the boundary between seconds.
80 * We loop until before == after (and hence dt.to_unix() should equal both)
81 * to guard against that. */
84 before = g_get_real_time () / G_TIME_SPAN_SECOND;
86 memset (&tm, 0, sizeof (tm));
87 get_localtime_tm (before, &tm);
89 dt = g_date_time_new_now_local ();
91 after = g_get_real_time () / G_TIME_SPAN_SECOND;
93 while (before != after);
95 g_assert_cmpint (g_date_time_get_year (dt), ==, 1900 + tm.tm_year);
96 g_assert_cmpint (g_date_time_get_month (dt), ==, 1 + tm.tm_mon);
97 g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, tm.tm_mday);
98 g_assert_cmpint (g_date_time_get_hour (dt), ==, tm.tm_hour);
99 g_assert_cmpint (g_date_time_get_minute (dt), ==, tm.tm_min);
100 g_assert_cmpint (g_date_time_get_second (dt), ==, tm.tm_sec);
102 g_date_time_unref (dt);
105 static void
106 test_GDateTime_new_from_unix (void)
108 GDateTime *dt;
109 struct tm tm;
110 time_t t;
112 memset (&tm, 0, sizeof (tm));
113 t = time (NULL);
114 get_localtime_tm (t, &tm);
116 dt = g_date_time_new_from_unix_local (t);
117 g_assert_cmpint (g_date_time_get_year (dt), ==, 1900 + tm.tm_year);
118 g_assert_cmpint (g_date_time_get_month (dt), ==, 1 + tm.tm_mon);
119 g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, tm.tm_mday);
120 g_assert_cmpint (g_date_time_get_hour (dt), ==, tm.tm_hour);
121 g_assert_cmpint (g_date_time_get_minute (dt), ==, tm.tm_min);
122 g_assert_cmpint (g_date_time_get_second (dt), ==, tm.tm_sec);
123 g_date_time_unref (dt);
125 /* Choose 1990-01-01 04:00:00 because no DST leaps happened then. The more
126 * obvious 1990-01-01 00:00:00 was a DST leap in America/Lima (which has,
127 * confusingly, since stopped using DST). */
128 memset (&tm, 0, sizeof (tm));
129 tm.tm_year = 90;
130 tm.tm_mday = 1;
131 tm.tm_mon = 0;
132 tm.tm_hour = 4;
133 tm.tm_min = 0;
134 tm.tm_sec = 0;
135 tm.tm_isdst = -1;
136 t = mktime (&tm);
138 dt = g_date_time_new_from_unix_local (t);
139 g_assert_cmpint (g_date_time_get_year (dt), ==, 1990);
140 g_assert_cmpint (g_date_time_get_month (dt), ==, 1);
141 g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 1);
142 g_assert_cmpint (g_date_time_get_hour (dt), ==, 4);
143 g_assert_cmpint (g_date_time_get_minute (dt), ==, 0);
144 g_assert_cmpint (g_date_time_get_second (dt), ==, 0);
145 g_date_time_unref (dt);
148 /* Check that trying to create a #GDateTime too far in the future reliably
149 * fails. Previously, the checks for this overflowed and it silently returned
150 * an incorrect #GDateTime. */
151 static void
152 test_GDateTime_new_from_unix_overflow (void)
154 GDateTime *dt;
156 g_test_bug ("782089");
158 dt = g_date_time_new_from_unix_utc (G_MAXINT64);
159 g_assert_null (dt);
161 dt = g_date_time_new_from_unix_local (G_MAXINT64);
162 g_assert_null (dt);
165 static void
166 test_GDateTime_invalid (void)
168 GDateTime *dt;
170 g_test_bug ("702674");
172 dt = g_date_time_new_utc (2013, -2147483647, 31, 17, 15, 48);
173 g_assert (dt == NULL);
176 static void
177 test_GDateTime_compare (void)
179 GDateTime *dt1, *dt2;
180 gint i;
182 dt1 = g_date_time_new_utc (2000, 1, 1, 0, 0, 0);
184 for (i = 1; i < 2000; i++)
186 dt2 = g_date_time_new_utc (i, 12, 31, 0, 0, 0);
187 g_assert_cmpint (1, ==, g_date_time_compare (dt1, dt2));
188 g_date_time_unref (dt2);
191 dt2 = g_date_time_new_utc (1999, 12, 31, 23, 59, 59);
192 g_assert_cmpint (1, ==, g_date_time_compare (dt1, dt2));
193 g_date_time_unref (dt2);
195 dt2 = g_date_time_new_utc (2000, 1, 1, 0, 0, 1);
196 g_assert_cmpint (-1, ==, g_date_time_compare (dt1, dt2));
197 g_date_time_unref (dt2);
199 dt2 = g_date_time_new_utc (2000, 1, 1, 0, 0, 0);
200 g_assert_cmpint (0, ==, g_date_time_compare (dt1, dt2));
201 g_date_time_unref (dt2);
202 g_date_time_unref (dt1);
205 static void
206 test_GDateTime_equal (void)
208 GDateTime *dt1, *dt2;
209 GTimeZone *tz;
211 dt1 = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
212 dt2 = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
213 g_assert (g_date_time_equal (dt1, dt2));
214 g_date_time_unref (dt1);
215 g_date_time_unref (dt2);
217 dt1 = g_date_time_new_local (2009, 10, 18, 0, 0, 0);
218 dt2 = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
219 g_assert (!g_date_time_equal (dt1, dt2));
220 g_date_time_unref (dt1);
221 g_date_time_unref (dt2);
223 /* UTC-0300 and not in DST */
224 tz = g_time_zone_new ("-03:00");
225 dt1 = g_date_time_new (tz, 2010, 5, 24, 8, 0, 0);
226 g_time_zone_unref (tz);
227 g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, (-3 * 3600));
228 /* UTC */
229 dt2 = g_date_time_new_utc (2010, 5, 24, 11, 0, 0);
230 g_assert_cmpint (g_date_time_get_utc_offset (dt2), ==, 0);
232 g_assert (g_date_time_equal (dt1, dt2));
233 g_date_time_unref (dt1);
235 /* America/Recife is in UTC-0300 */
236 #ifdef G_OS_UNIX
237 tz = g_time_zone_new ("America/Recife");
238 #elif defined G_OS_WIN32
239 tz = g_time_zone_new ("E. South America Standard Time");
240 #endif
241 dt1 = g_date_time_new (tz, 2010, 5, 24, 8, 0, 0);
242 g_time_zone_unref (tz);
243 g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, (-3 * 3600));
244 g_assert (g_date_time_equal (dt1, dt2));
245 g_date_time_unref (dt1);
246 g_date_time_unref (dt2);
249 static void
250 test_GDateTime_get_day_of_week (void)
252 GDateTime *dt;
254 dt = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
255 g_assert_cmpint (1, ==, g_date_time_get_day_of_week (dt));
256 g_date_time_unref (dt);
258 dt = g_date_time_new_local (2000, 10, 1, 0, 0, 0);
259 g_assert_cmpint (7, ==, g_date_time_get_day_of_week (dt));
260 g_date_time_unref (dt);
263 static void
264 test_GDateTime_get_day_of_month (void)
266 GDateTime *dt;
268 dt = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
269 g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 19);
270 g_date_time_unref (dt);
272 dt = g_date_time_new_local (1400, 3, 12, 0, 0, 0);
273 g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 12);
274 g_date_time_unref (dt);
276 dt = g_date_time_new_local (1800, 12, 31, 0, 0, 0);
277 g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 31);
278 g_date_time_unref (dt);
280 dt = g_date_time_new_local (1000, 1, 1, 0, 0, 0);
281 g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 1);
282 g_date_time_unref (dt);
285 static void
286 test_GDateTime_get_hour (void)
288 GDateTime *dt;
290 dt = g_date_time_new_utc (2009, 10, 19, 15, 13, 11);
291 g_assert_cmpint (15, ==, g_date_time_get_hour (dt));
292 g_date_time_unref (dt);
294 dt = g_date_time_new_utc (100, 10, 19, 1, 0, 0);
295 g_assert_cmpint (1, ==, g_date_time_get_hour (dt));
296 g_date_time_unref (dt);
298 dt = g_date_time_new_utc (100, 10, 19, 0, 0, 0);
299 g_assert_cmpint (0, ==, g_date_time_get_hour (dt));
300 g_date_time_unref (dt);
302 dt = g_date_time_new_utc (100, 10, 1, 23, 59, 59);
303 g_assert_cmpint (23, ==, g_date_time_get_hour (dt));
304 g_date_time_unref (dt);
307 static void
308 test_GDateTime_get_microsecond (void)
310 GTimeVal tv;
311 GDateTime *dt;
313 g_get_current_time (&tv);
314 dt = g_date_time_new_from_timeval_local (&tv);
315 g_assert_cmpint (tv.tv_usec, ==, g_date_time_get_microsecond (dt));
316 g_date_time_unref (dt);
319 static void
320 test_GDateTime_get_year (void)
322 GDateTime *dt;
324 dt = g_date_time_new_local (2009, 1, 1, 0, 0, 0);
325 g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
326 g_date_time_unref (dt);
328 dt = g_date_time_new_local (1, 1, 1, 0, 0, 0);
329 g_assert_cmpint (1, ==, g_date_time_get_year (dt));
330 g_date_time_unref (dt);
332 dt = g_date_time_new_local (13, 1, 1, 0, 0, 0);
333 g_assert_cmpint (13, ==, g_date_time_get_year (dt));
334 g_date_time_unref (dt);
336 dt = g_date_time_new_local (3000, 1, 1, 0, 0, 0);
337 g_assert_cmpint (3000, ==, g_date_time_get_year (dt));
338 g_date_time_unref (dt);
341 static void
342 test_GDateTime_hash (void)
344 GHashTable *h;
346 h = g_hash_table_new_full (g_date_time_hash, g_date_time_equal,
347 (GDestroyNotify)g_date_time_unref,
348 NULL);
349 g_hash_table_add (h, g_date_time_new_now_local ());
350 g_hash_table_remove_all (h);
351 g_hash_table_destroy (h);
354 static void
355 test_GDateTime_new_from_timeval (void)
357 GDateTime *dt;
358 GTimeVal tv, tv2;
360 g_get_current_time (&tv);
361 dt = g_date_time_new_from_timeval_local (&tv);
363 if (g_test_verbose ())
364 g_printerr ("\nDT%04d-%02d-%02dT%02d:%02d:%02d%s\n",
365 g_date_time_get_year (dt),
366 g_date_time_get_month (dt),
367 g_date_time_get_day_of_month (dt),
368 g_date_time_get_hour (dt),
369 g_date_time_get_minute (dt),
370 g_date_time_get_second (dt),
371 g_date_time_get_timezone_abbreviation (dt));
373 g_date_time_to_timeval (dt, &tv2);
374 g_assert_cmpint (tv.tv_sec, ==, tv2.tv_sec);
375 g_assert_cmpint (tv.tv_usec, ==, tv2.tv_usec);
376 g_date_time_unref (dt);
379 static glong
380 find_maximum_supported_tv_sec (void)
382 glong highest_success = 0, lowest_failure = G_MAXLONG;
383 GTimeVal tv;
384 GDateTime *dt = NULL;
386 tv.tv_usec = 0;
388 /* Corner case of all glong values being valid. */
389 tv.tv_sec = G_MAXLONG;
390 dt = g_date_time_new_from_timeval_utc (&tv);
391 if (dt != NULL)
393 highest_success = tv.tv_sec;
394 g_date_time_unref (dt);
397 while (highest_success < lowest_failure - 1)
399 tv.tv_sec = highest_success + (lowest_failure - highest_success) / 2;
400 dt = g_date_time_new_from_timeval_utc (&tv);
402 if (dt != NULL)
404 highest_success = tv.tv_sec;
405 g_date_time_unref (dt);
407 else
409 lowest_failure = tv.tv_sec;
413 return highest_success;
416 /* Check that trying to create a #GDateTime too far in the future reliably
417 * fails. With a #GTimeVal, this is subtle, as the tv_usec are added into the
418 * calculation part-way through.
420 * This varies a bit between 32- and 64-bit architectures, due to the
421 * differences in the size of glong (tv.tv_sec). */
422 static void
423 test_GDateTime_new_from_timeval_overflow (void)
425 GDateTime *dt;
426 GTimeVal tv;
428 g_test_bug ("782089");
430 tv.tv_sec = find_maximum_supported_tv_sec ();
431 tv.tv_usec = G_USEC_PER_SEC - 1;
433 g_test_message ("Maximum supported GTimeVal.tv_sec = %lu", tv.tv_sec);
435 /* Sanity check: do we support the year 2000? */
436 g_assert_cmpint (tv.tv_sec, >=, 946684800);
438 dt = g_date_time_new_from_timeval_utc (&tv);
439 g_assert_nonnull (dt);
440 g_date_time_unref (dt);
442 if (tv.tv_sec < G_MAXLONG)
444 tv.tv_sec++;
445 tv.tv_usec = 0;
447 dt = g_date_time_new_from_timeval_utc (&tv);
448 g_assert_null (dt);
452 static void
453 test_GDateTime_new_from_timeval_utc (void)
455 GDateTime *dt;
456 GTimeVal tv, tv2;
458 g_get_current_time (&tv);
459 dt = g_date_time_new_from_timeval_utc (&tv);
461 if (g_test_verbose ())
462 g_printerr ("\nDT%04d-%02d-%02dT%02d:%02d:%02d%s\n",
463 g_date_time_get_year (dt),
464 g_date_time_get_month (dt),
465 g_date_time_get_day_of_month (dt),
466 g_date_time_get_hour (dt),
467 g_date_time_get_minute (dt),
468 g_date_time_get_second (dt),
469 g_date_time_get_timezone_abbreviation (dt));
471 g_date_time_to_timeval (dt, &tv2);
472 g_assert_cmpint (tv.tv_sec, ==, tv2.tv_sec);
473 g_assert_cmpint (tv.tv_usec, ==, tv2.tv_usec);
474 g_date_time_unref (dt);
477 static void
478 test_GDateTime_new_from_iso8601 (void)
480 GDateTime *dt;
481 GTimeZone *tz;
483 /* Need non-empty string */
484 dt = g_date_time_new_from_iso8601 ("", NULL);
485 g_assert_null (dt);
487 /* Needs to be correctly formatted */
488 dt = g_date_time_new_from_iso8601 ("not a date", NULL);
489 g_assert_null (dt);
491 /* Check common case */
492 dt = g_date_time_new_from_iso8601 ("2016-08-24T22:10:42Z", NULL);
493 ASSERT_DATE (dt, 2016, 8, 24);
494 ASSERT_TIME (dt, 22, 10, 42, 0);
495 g_date_time_unref (dt);
497 /* Timezone is required in text or passed as arg */
498 tz = g_time_zone_new_utc ();
499 dt = g_date_time_new_from_iso8601 ("2016-08-24T22:10:42", tz);
500 ASSERT_DATE (dt, 2016, 8, 24);
501 g_date_time_unref (dt);
502 g_time_zone_unref (tz);
503 dt = g_date_time_new_from_iso8601 ("2016-08-24T22:10:42", NULL);
504 g_assert_null (dt);
506 /* Can't have whitespace */
507 dt = g_date_time_new_from_iso8601 ("2016 08 24T22:10:42Z", NULL);
508 g_assert_null (dt);
509 dt = g_date_time_new_from_iso8601 ("2016-08-24T22:10:42Z ", NULL);
510 g_assert_null (dt);
511 dt = g_date_time_new_from_iso8601 (" 2016-08-24T22:10:42Z", NULL);
512 g_assert_null (dt);
514 /* Check lowercase time separator or space allowed */
515 dt = g_date_time_new_from_iso8601 ("2016-08-24t22:10:42Z", NULL);
516 ASSERT_DATE (dt, 2016, 8, 24);
517 ASSERT_TIME (dt, 22, 10, 42, 0);
518 g_date_time_unref (dt);
519 dt = g_date_time_new_from_iso8601 ("2016-08-24 22:10:42Z", NULL);
520 ASSERT_DATE (dt, 2016, 8, 24);
521 ASSERT_TIME (dt, 22, 10, 42, 0);
522 g_date_time_unref (dt);
524 /* Check dates without separators allowed */
525 dt = g_date_time_new_from_iso8601 ("20160824T22:10:42Z", NULL);
526 ASSERT_DATE (dt, 2016, 8, 24);
527 ASSERT_TIME (dt, 22, 10, 42, 0);
528 g_date_time_unref (dt);
530 /* Months are two digits */
531 dt = g_date_time_new_from_iso8601 ("2016-1-01T22:10:42Z", NULL);
532 g_assert_null (dt);
534 /* Days are two digits */
535 dt = g_date_time_new_from_iso8601 ("2016-01-1T22:10:42Z", NULL);
536 g_assert_null (dt);
538 /* Need consistent usage of separators */
539 dt = g_date_time_new_from_iso8601 ("2016-0824T22:10:42Z", NULL);
540 g_assert_null (dt);
541 dt = g_date_time_new_from_iso8601 ("201608-24T22:10:42Z", NULL);
542 g_assert_null (dt);
544 /* Check month within valid range */
545 dt = g_date_time_new_from_iso8601 ("2016-00-13T22:10:42Z", NULL);
546 g_assert_null (dt);
547 dt = g_date_time_new_from_iso8601 ("2016-13-13T22:10:42Z", NULL);
548 g_assert_null (dt);
550 /* Check day within valid range */
551 dt = g_date_time_new_from_iso8601 ("2016-01-00T22:10:42Z", NULL);
552 g_assert_null (dt);
553 dt = g_date_time_new_from_iso8601 ("2016-01-31T22:10:42Z", NULL);
554 ASSERT_DATE (dt, 2016, 1, 31);
555 g_date_time_unref (dt);
556 dt = g_date_time_new_from_iso8601 ("2016-01-32T22:10:42Z", NULL);
557 g_assert_null (dt);
558 dt = g_date_time_new_from_iso8601 ("2016-02-29T22:10:42Z", NULL);
559 ASSERT_DATE (dt, 2016, 2, 29);
560 g_date_time_unref (dt);
561 dt = g_date_time_new_from_iso8601 ("2016-02-30T22:10:42Z", NULL);
562 g_assert_null (dt);
563 dt = g_date_time_new_from_iso8601 ("2017-02-28T22:10:42Z", NULL);
564 ASSERT_DATE (dt, 2017, 2, 28);
565 g_date_time_unref (dt);
566 dt = g_date_time_new_from_iso8601 ("2017-02-29T22:10:42Z", NULL);
567 g_assert_null (dt);
568 dt = g_date_time_new_from_iso8601 ("2016-03-31T22:10:42Z", NULL);
569 ASSERT_DATE (dt, 2016, 3, 31);
570 g_date_time_unref (dt);
571 dt = g_date_time_new_from_iso8601 ("2016-03-32T22:10:42Z", NULL);
572 g_assert_null (dt);
573 dt = g_date_time_new_from_iso8601 ("2016-04-30T22:10:42Z", NULL);
574 ASSERT_DATE (dt, 2016, 4, 30);
575 g_date_time_unref (dt);
576 dt = g_date_time_new_from_iso8601 ("2016-04-31T22:10:42Z", NULL);
577 g_assert_null (dt);
578 dt = g_date_time_new_from_iso8601 ("2016-05-31T22:10:42Z", NULL);
579 ASSERT_DATE (dt, 2016, 5, 31);
580 g_date_time_unref (dt);
581 dt = g_date_time_new_from_iso8601 ("2016-05-32T22:10:42Z", NULL);
582 g_assert_null (dt);
583 dt = g_date_time_new_from_iso8601 ("2016-06-30T22:10:42Z", NULL);
584 ASSERT_DATE (dt, 2016, 6, 30);
585 g_date_time_unref (dt);
586 dt = g_date_time_new_from_iso8601 ("2016-06-31T22:10:42Z", NULL);
587 g_assert_null (dt);
588 dt = g_date_time_new_from_iso8601 ("2016-07-31T22:10:42Z", NULL);
589 ASSERT_DATE (dt, 2016, 7, 31);
590 g_date_time_unref (dt);
591 dt = g_date_time_new_from_iso8601 ("2016-07-32T22:10:42Z", NULL);
592 g_assert_null (dt);
593 dt = g_date_time_new_from_iso8601 ("2016-08-31T22:10:42Z", NULL);
594 ASSERT_DATE (dt, 2016, 8, 31);
595 g_date_time_unref (dt);
596 dt = g_date_time_new_from_iso8601 ("2016-08-32T22:10:42Z", NULL);
597 g_assert_null (dt);
598 dt = g_date_time_new_from_iso8601 ("2016-09-30T22:10:42Z", NULL);
599 ASSERT_DATE (dt, 2016, 9, 30);
600 g_date_time_unref (dt);
601 dt = g_date_time_new_from_iso8601 ("2016-09-31T22:10:42Z", NULL);
602 g_assert_null (dt);
603 dt = g_date_time_new_from_iso8601 ("2016-10-31T22:10:42Z", NULL);
604 ASSERT_DATE (dt, 2016, 10, 31);
605 g_date_time_unref (dt);
606 dt = g_date_time_new_from_iso8601 ("2016-10-32T22:10:42Z", NULL);
607 g_assert_null (dt);
608 dt = g_date_time_new_from_iso8601 ("2016-11-30T22:10:42Z", NULL);
609 ASSERT_DATE (dt, 2016, 11, 30);
610 g_date_time_unref (dt);
611 dt = g_date_time_new_from_iso8601 ("2016-11-31T22:10:42Z", NULL);
612 g_assert_null (dt);
613 dt = g_date_time_new_from_iso8601 ("2016-12-31T22:10:42Z", NULL);
614 ASSERT_DATE (dt, 2016, 12, 31);
615 g_date_time_unref (dt);
616 dt = g_date_time_new_from_iso8601 ("2016-12-32T22:10:42Z", NULL);
617 g_assert_null (dt);
619 /* Check ordinal days work */
620 dt = g_date_time_new_from_iso8601 ("2016-237T22:10:42Z", NULL);
621 ASSERT_DATE (dt, 2016, 8, 24);
622 ASSERT_TIME (dt, 22, 10, 42, 0);
623 g_date_time_unref (dt);
624 dt = g_date_time_new_from_iso8601 ("2016237T22:10:42Z", NULL);
625 ASSERT_DATE (dt, 2016, 8, 24);
626 ASSERT_TIME (dt, 22, 10, 42, 0);
627 g_date_time_unref (dt);
629 /* Check ordinal leap days */
630 dt = g_date_time_new_from_iso8601 ("2016-366T22:10:42Z", NULL);
631 ASSERT_DATE (dt, 2016, 12, 31);
632 ASSERT_TIME (dt, 22, 10, 42, 0);
633 g_date_time_unref (dt);
634 dt = g_date_time_new_from_iso8601 ("2017-365T22:10:42Z", NULL);
635 ASSERT_DATE (dt, 2017, 12, 31);
636 ASSERT_TIME (dt, 22, 10, 42, 0);
637 g_date_time_unref (dt);
638 dt = g_date_time_new_from_iso8601 ("2017-366T22:10:42Z", NULL);
639 g_assert_null (dt);
641 /* Days start at 1 */
642 dt = g_date_time_new_from_iso8601 ("2016-000T22:10:42Z", NULL);
643 g_assert_null (dt);
645 /* Limited to number of days in the year (2016 is a leap year) */
646 dt = g_date_time_new_from_iso8601 ("2016-367T22:10:42Z", NULL);
647 g_assert_null (dt);
649 /* Days are two digits */
650 dt = g_date_time_new_from_iso8601 ("2016-1T22:10:42Z", NULL);
651 g_assert_null (dt);
652 dt = g_date_time_new_from_iso8601 ("2016-12T22:10:42Z", NULL);
653 g_assert_null (dt);
655 /* Check week days work */
656 dt = g_date_time_new_from_iso8601 ("2016-W34-3T22:10:42Z", NULL);
657 ASSERT_DATE (dt, 2016, 8, 24);
658 ASSERT_TIME (dt, 22, 10, 42, 0);
659 g_date_time_unref (dt);
660 dt = g_date_time_new_from_iso8601 ("2016W343T22:10:42Z", NULL);
661 ASSERT_DATE (dt, 2016, 8, 24);
662 ASSERT_TIME (dt, 22, 10, 42, 0);
663 g_date_time_unref (dt);
665 /* We don't support weeks without weekdays (valid ISO 8601) */
666 dt = g_date_time_new_from_iso8601 ("2016-W34T22:10:42Z", NULL);
667 g_assert_null (dt);
668 dt = g_date_time_new_from_iso8601 ("2016W34T22:10:42Z", NULL);
669 g_assert_null (dt);
671 /* Weeks are two digits */
672 dt = g_date_time_new_from_iso8601 ("2016-W3-1T22:10:42Z", NULL);
673 g_assert_null (dt);
675 /* Weeks start at 1 */
676 dt = g_date_time_new_from_iso8601 ("2016-W00-1T22:10:42Z", NULL);
677 g_assert_null (dt);
679 /* Week one might be in the previous year */
680 dt = g_date_time_new_from_iso8601 ("2015-W01-1T22:10:42Z", NULL);
681 ASSERT_DATE (dt, 2014, 12, 29);
682 g_date_time_unref (dt);
684 /* Last week might be in next year */
685 dt = g_date_time_new_from_iso8601 ("2015-W53-7T22:10:42Z", NULL);
686 ASSERT_DATE (dt, 2016, 1, 3);
687 g_date_time_unref (dt);
689 /* Week 53 doesn't always exist */
690 dt = g_date_time_new_from_iso8601 ("2016-W53-1T22:10:42Z", NULL);
691 g_assert_null (dt);
693 /* Limited to number of days in the week */
694 dt = g_date_time_new_from_iso8601 ("2016-W34-0T22:10:42Z", NULL);
695 g_assert_null (dt);
696 dt = g_date_time_new_from_iso8601 ("2016-W34-8T22:10:42Z", NULL);
697 g_assert_null (dt);
699 /* Days are one digit */
700 dt = g_date_time_new_from_iso8601 ("2016-W34-99T22:10:42Z", NULL);
701 g_assert_null (dt);
703 /* Check week day changes depending on year */
704 dt = g_date_time_new_from_iso8601 ("2017-W34-1T22:10:42Z", NULL);
705 ASSERT_DATE (dt, 2017, 8, 21);
706 g_date_time_unref (dt);
708 /* Check week day changes depending on leap years */
709 dt = g_date_time_new_from_iso8601 ("1900-W01-1T22:10:42Z", NULL);
710 ASSERT_DATE (dt, 1900, 1, 1);
711 g_date_time_unref (dt);
713 /* YYYY-MM not allowed (NOT valid ISO 8601) */
714 dt = g_date_time_new_from_iso8601 ("2016-08T22:10:42Z", NULL);
715 g_assert_null (dt);
717 /* We don't support omitted year (valid ISO 8601) */
718 dt = g_date_time_new_from_iso8601 ("--08-24T22:10:42Z", NULL);
719 g_assert_null (dt);
720 dt = g_date_time_new_from_iso8601 ("--0824T22:10:42Z", NULL);
721 g_assert_null (dt);
723 /* Check subseconds work */
724 dt = g_date_time_new_from_iso8601 ("2016-08-24T22:10:42.123456Z", NULL);
725 ASSERT_DATE (dt, 2016, 8, 24);
726 ASSERT_TIME (dt, 22, 10, 42, 123456);
727 g_date_time_unref (dt);
728 dt = g_date_time_new_from_iso8601 ("2016-08-24T22:10:42,123456Z", NULL);
729 ASSERT_DATE (dt, 2016, 8, 24);
730 ASSERT_TIME (dt, 22, 10, 42, 123456);
731 g_date_time_unref (dt);
732 dt = g_date_time_new_from_iso8601 ("2016-08-24T22:10:42.Z", NULL);
733 g_assert_null (dt);
734 dt = g_date_time_new_from_iso8601 ("2016-08-24T221042.123456Z", NULL);
735 ASSERT_DATE (dt, 2016, 8, 24);
736 ASSERT_TIME (dt, 22, 10, 42, 123456);
737 g_date_time_unref (dt);
739 /* We don't support times without minutes / seconds (valid ISO 8601) */
740 dt = g_date_time_new_from_iso8601 ("2016-08-24T22Z", NULL);
741 g_assert_null (dt);
742 dt = g_date_time_new_from_iso8601 ("2016-08-24T22:10Z", NULL);
743 g_assert_null (dt);
744 dt = g_date_time_new_from_iso8601 ("2016-08-24T2210Z", NULL);
745 g_assert_null (dt);
747 /* UTC time uses 'Z' */
748 dt = g_date_time_new_from_iso8601 ("2016-08-24T22:10:42Z", NULL);
749 ASSERT_DATE (dt, 2016, 8, 24);
750 ASSERT_TIME (dt, 22, 10, 42, 0);
751 g_assert_cmpint (g_date_time_get_utc_offset (dt), ==, 0);
752 g_date_time_unref (dt);
754 /* Check timezone works */
755 dt = g_date_time_new_from_iso8601 ("2016-08-24T22:10:42+12:00", NULL);
756 ASSERT_DATE (dt, 2016, 8, 24);
757 ASSERT_TIME (dt, 22, 10, 42, 0);
758 g_assert_cmpint (g_date_time_get_utc_offset (dt), ==, 12 * G_TIME_SPAN_HOUR);
759 g_date_time_unref (dt);
760 dt = g_date_time_new_from_iso8601 ("2016-08-24T22:10:42+12", 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-02", 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), ==, -2 * G_TIME_SPAN_HOUR);
769 g_date_time_unref (dt);
771 /* Timezone seconds not allowed */
772 dt = g_date_time_new_from_iso8601 ("2016-08-24T22-12:00:00", NULL);
773 g_assert_null (dt);
774 dt = g_date_time_new_from_iso8601 ("2016-08-24T22-12:00:00.000", NULL);
775 g_assert_null (dt);
777 /* Timezone hours two digits */
778 dt = g_date_time_new_from_iso8601 ("2016-08-24T22-2Z", NULL);
779 g_assert_null (dt);
782 static void
783 test_GDateTime_to_unix (void)
785 GDateTime *dt;
786 time_t t;
788 t = time (NULL);
789 dt = g_date_time_new_from_unix_local (t);
790 g_assert_cmpint (g_date_time_to_unix (dt), ==, t);
791 g_date_time_unref (dt);
794 static void
795 test_GDateTime_add_years (void)
797 GDateTime *dt, *dt2;
799 dt = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
800 dt2 = g_date_time_add_years (dt, 1);
801 g_assert_cmpint (2010, ==, g_date_time_get_year (dt2));
802 g_date_time_unref (dt);
803 g_date_time_unref (dt2);
806 static void
807 test_GDateTime_add_months (void)
809 #define TEST_ADD_MONTHS(y,m,d,a,ny,nm,nd) G_STMT_START { \
810 GDateTime *dt, *dt2; \
811 dt = g_date_time_new_utc (y, m, d, 0, 0, 0); \
812 dt2 = g_date_time_add_months (dt, a); \
813 ASSERT_DATE (dt2, ny, nm, nd); \
814 g_date_time_unref (dt); \
815 g_date_time_unref (dt2); \
816 } G_STMT_END
818 TEST_ADD_MONTHS (2009, 12, 31, 1, 2010, 1, 31);
819 TEST_ADD_MONTHS (2009, 12, 31, 1, 2010, 1, 31);
820 TEST_ADD_MONTHS (2009, 6, 15, 1, 2009, 7, 15);
821 TEST_ADD_MONTHS (1400, 3, 1, 1, 1400, 4, 1);
822 TEST_ADD_MONTHS (1400, 1, 31, 1, 1400, 2, 28);
823 TEST_ADD_MONTHS (1400, 1, 31, 7200, 2000, 1, 31);
824 TEST_ADD_MONTHS (2008, 2, 29, 12, 2009, 2, 28);
825 TEST_ADD_MONTHS (2000, 8, 16, -5, 2000, 3, 16);
826 TEST_ADD_MONTHS (2000, 8, 16, -12, 1999, 8, 16);
827 TEST_ADD_MONTHS (2011, 2, 1, -13, 2010, 1, 1);
828 TEST_ADD_MONTHS (1776, 7, 4, 1200, 1876, 7, 4);
831 static void
832 test_GDateTime_add_days (void)
834 #define TEST_ADD_DAYS(y,m,d,a,ny,nm,nd) G_STMT_START { \
835 GDateTime *dt, *dt2; \
836 dt = g_date_time_new_local (y, m, d, 0, 0, 0); \
837 dt2 = g_date_time_add_days (dt, a); \
838 g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
839 g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
840 g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
841 g_date_time_unref (dt); \
842 g_date_time_unref (dt2); \
843 } G_STMT_END
845 TEST_ADD_DAYS (2009, 1, 31, 1, 2009, 2, 1);
846 TEST_ADD_DAYS (2009, 2, 1, -1, 2009, 1, 31);
847 TEST_ADD_DAYS (2008, 2, 28, 1, 2008, 2, 29);
848 TEST_ADD_DAYS (2008, 12, 31, 1, 2009, 1, 1);
849 TEST_ADD_DAYS (1, 1, 1, 1, 1, 1, 2);
850 TEST_ADD_DAYS (1955, 5, 24, 10, 1955, 6, 3);
851 TEST_ADD_DAYS (1955, 5, 24, -10, 1955, 5, 14);
854 static void
855 test_GDateTime_add_weeks (void)
857 #define TEST_ADD_WEEKS(y,m,d,a,ny,nm,nd) G_STMT_START { \
858 GDateTime *dt, *dt2; \
859 dt = g_date_time_new_local (y, m, d, 0, 0, 0); \
860 dt2 = g_date_time_add_weeks (dt, a); \
861 g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
862 g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
863 g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
864 g_date_time_unref (dt); \
865 g_date_time_unref (dt2); \
866 } G_STMT_END
868 TEST_ADD_WEEKS (2009, 1, 1, 1, 2009, 1, 8);
869 TEST_ADD_WEEKS (2009, 8, 30, 1, 2009, 9, 6);
870 TEST_ADD_WEEKS (2009, 12, 31, 1, 2010, 1, 7);
871 TEST_ADD_WEEKS (2009, 1, 1, -1, 2008, 12, 25);
874 static void
875 test_GDateTime_add_hours (void)
877 #define TEST_ADD_HOURS(y,m,d,h,mi,s,a,ny,nm,nd,nh,nmi,ns) G_STMT_START { \
878 GDateTime *dt, *dt2; \
879 dt = g_date_time_new_utc (y, m, d, h, mi, s); \
880 dt2 = g_date_time_add_hours (dt, a); \
881 g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
882 g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
883 g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
884 g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
885 g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
886 g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
887 g_date_time_unref (dt); \
888 g_date_time_unref (dt2); \
889 } G_STMT_END
891 TEST_ADD_HOURS (2009, 1, 1, 0, 0, 0, 1, 2009, 1, 1, 1, 0, 0);
892 TEST_ADD_HOURS (2008, 12, 31, 23, 0, 0, 1, 2009, 1, 1, 0, 0, 0);
895 static void
896 test_GDateTime_add_full (void)
898 #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 { \
899 GDateTime *dt, *dt2; \
900 dt = g_date_time_new_utc (y, m, d, h, mi, s); \
901 dt2 = g_date_time_add_full (dt, ay, am, ad, ah, ami, as); \
902 g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
903 g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
904 g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
905 g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
906 g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
907 g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
908 g_date_time_unref (dt); \
909 g_date_time_unref (dt2); \
910 } G_STMT_END
912 TEST_ADD_FULL (2009, 10, 21, 0, 0, 0,
913 1, 1, 1, 1, 1, 1,
914 2010, 11, 22, 1, 1, 1);
915 TEST_ADD_FULL (2000, 1, 1, 1, 1, 1,
916 0, 1, 0, 0, 0, 0,
917 2000, 2, 1, 1, 1, 1);
918 TEST_ADD_FULL (2000, 1, 1, 0, 0, 0,
919 -1, 1, 0, 0, 0, 0,
920 1999, 2, 1, 0, 0, 0);
921 TEST_ADD_FULL (2010, 10, 31, 0, 0, 0,
922 0, 4, 0, 0, 0, 0,
923 2011, 2, 28, 0, 0, 0);
924 TEST_ADD_FULL (2010, 8, 25, 22, 45, 0,
925 0, 1, 6, 1, 25, 0,
926 2010, 10, 2, 0, 10, 0);
929 static void
930 test_GDateTime_add_minutes (void)
932 #define TEST_ADD_MINUTES(i,o) G_STMT_START { \
933 GDateTime *dt, *dt2; \
934 dt = g_date_time_new_local (2000, 1, 1, 0, 0, 0); \
935 dt2 = g_date_time_add_minutes (dt, i); \
936 g_assert_cmpint (o, ==, g_date_time_get_minute (dt2)); \
937 g_date_time_unref (dt); \
938 g_date_time_unref (dt2); \
939 } G_STMT_END
941 TEST_ADD_MINUTES (60, 0);
942 TEST_ADD_MINUTES (100, 40);
943 TEST_ADD_MINUTES (5, 5);
944 TEST_ADD_MINUTES (1441, 1);
945 TEST_ADD_MINUTES (-1441, 59);
948 static void
949 test_GDateTime_add_seconds (void)
951 #define TEST_ADD_SECONDS(i,o) G_STMT_START { \
952 GDateTime *dt, *dt2; \
953 dt = g_date_time_new_local (2000, 1, 1, 0, 0, 0); \
954 dt2 = g_date_time_add_seconds (dt, i); \
955 g_assert_cmpint (o, ==, g_date_time_get_second (dt2)); \
956 g_date_time_unref (dt); \
957 g_date_time_unref (dt2); \
958 } G_STMT_END
960 TEST_ADD_SECONDS (1, 1);
961 TEST_ADD_SECONDS (60, 0);
962 TEST_ADD_SECONDS (61, 1);
963 TEST_ADD_SECONDS (120, 0);
964 TEST_ADD_SECONDS (-61, 59);
965 TEST_ADD_SECONDS (86401, 1);
966 TEST_ADD_SECONDS (-86401, 59);
967 TEST_ADD_SECONDS (-31, 29);
968 TEST_ADD_SECONDS (13, 13);
971 static void
972 test_GDateTime_diff (void)
974 #define TEST_DIFF(y,m,d,y2,m2,d2,u) G_STMT_START { \
975 GDateTime *dt1, *dt2; \
976 GTimeSpan ts = 0; \
977 dt1 = g_date_time_new_utc (y, m, d, 0, 0, 0); \
978 dt2 = g_date_time_new_utc (y2, m2, d2, 0, 0, 0); \
979 ts = g_date_time_difference (dt2, dt1); \
980 g_assert_cmpint (ts, ==, u); \
981 g_date_time_unref (dt1); \
982 g_date_time_unref (dt2); \
983 } G_STMT_END
985 TEST_DIFF (2009, 1, 1, 2009, 2, 1, G_TIME_SPAN_DAY * 31);
986 TEST_DIFF (2009, 1, 1, 2010, 1, 1, G_TIME_SPAN_DAY * 365);
987 TEST_DIFF (2008, 2, 28, 2008, 2, 29, G_TIME_SPAN_DAY);
988 TEST_DIFF (2008, 2, 29, 2008, 2, 28, -G_TIME_SPAN_DAY);
990 /* TODO: Add usec tests */
993 static void
994 test_GDateTime_get_minute (void)
996 GDateTime *dt;
998 dt = g_date_time_new_utc (2009, 12, 1, 1, 31, 0);
999 g_assert_cmpint (31, ==, g_date_time_get_minute (dt));
1000 g_date_time_unref (dt);
1003 static void
1004 test_GDateTime_get_month (void)
1006 GDateTime *dt;
1008 dt = g_date_time_new_utc (2009, 12, 1, 1, 31, 0);
1009 g_assert_cmpint (12, ==, g_date_time_get_month (dt));
1010 g_date_time_unref (dt);
1013 static void
1014 test_GDateTime_get_second (void)
1016 GDateTime *dt;
1018 dt = g_date_time_new_utc (2009, 12, 1, 1, 31, 44);
1019 g_assert_cmpint (44, ==, g_date_time_get_second (dt));
1020 g_date_time_unref (dt);
1023 static void
1024 test_GDateTime_new_full (void)
1026 GTimeZone *tz;
1027 GDateTime *dt;
1029 dt = g_date_time_new_utc (2009, 12, 11, 12, 11, 10);
1030 g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
1031 g_assert_cmpint (12, ==, g_date_time_get_month (dt));
1032 g_assert_cmpint (11, ==, g_date_time_get_day_of_month (dt));
1033 g_assert_cmpint (12, ==, g_date_time_get_hour (dt));
1034 g_assert_cmpint (11, ==, g_date_time_get_minute (dt));
1035 g_assert_cmpint (10, ==, g_date_time_get_second (dt));
1036 g_date_time_unref (dt);
1038 #ifdef G_OS_UNIX
1039 tz = g_time_zone_new ("America/Tijuana");
1040 #elif defined G_OS_WIN32
1041 tz = g_time_zone_new ("Pacific Standard Time");
1042 #endif
1043 dt = g_date_time_new (tz, 2010, 11, 24, 8, 4, 0);
1044 g_time_zone_unref (tz);
1045 g_assert_cmpint (2010, ==, g_date_time_get_year (dt));
1046 g_assert_cmpint (11, ==, g_date_time_get_month (dt));
1047 g_assert_cmpint (24, ==, g_date_time_get_day_of_month (dt));
1048 g_assert_cmpint (8, ==, g_date_time_get_hour (dt));
1049 g_assert_cmpint (4, ==, g_date_time_get_minute (dt));
1050 g_assert_cmpint (0, ==, g_date_time_get_second (dt));
1051 #ifdef G_OS_UNIX
1052 g_assert_cmpstr ("PST", ==, g_date_time_get_timezone_abbreviation (dt));
1053 #elif defined G_OS_WIN32
1054 g_assert_cmpstr ("Pacific Standard Time", ==,
1055 g_date_time_get_timezone_abbreviation (dt));
1056 #endif
1057 g_assert (!g_date_time_is_daylight_savings (dt));
1058 g_date_time_unref (dt);
1060 /* Check month limits */
1061 dt = g_date_time_new_utc (2016, 1, 31, 22, 10, 42);
1062 ASSERT_DATE (dt, 2016, 1, 31);
1063 g_date_time_unref (dt);
1064 dt = g_date_time_new_utc (2016, 1, 32, 22, 10, 42);
1065 g_assert_null (dt);
1066 dt = g_date_time_new_utc (2016, 2, 29, 22, 10, 42);
1067 ASSERT_DATE (dt, 2016, 2, 29);
1068 g_date_time_unref (dt);
1069 dt = g_date_time_new_utc (2016, 2, 30, 22, 10, 42);
1070 g_assert_null (dt);
1071 dt = g_date_time_new_utc (2017, 2, 28, 22, 10, 42);
1072 ASSERT_DATE (dt, 2017, 2, 28);
1073 g_date_time_unref (dt);
1074 dt = g_date_time_new_utc (2017, 2, 29, 22, 10, 42);
1075 g_assert_null (dt);
1076 dt = g_date_time_new_utc (2016, 3, 31, 22, 10, 42);
1077 ASSERT_DATE (dt, 2016, 3, 31);
1078 g_date_time_unref (dt);
1079 dt = g_date_time_new_utc (2016, 3, 32, 22, 10, 42);
1080 g_assert_null (dt);
1081 dt = g_date_time_new_utc (2016, 4, 30, 22, 10, 42);
1082 ASSERT_DATE (dt, 2016, 4, 30);
1083 g_date_time_unref (dt);
1084 dt = g_date_time_new_utc (2016, 4, 31, 22, 10, 42);
1085 g_assert_null (dt);
1086 dt = g_date_time_new_utc (2016, 5, 31, 22, 10, 42);
1087 ASSERT_DATE (dt, 2016, 5, 31);
1088 g_date_time_unref (dt);
1089 dt = g_date_time_new_utc (2016, 5, 32, 22, 10, 42);
1090 g_assert_null (dt);
1091 dt = g_date_time_new_utc (2016, 6, 30, 22, 10, 42);
1092 ASSERT_DATE (dt, 2016, 6, 30);
1093 g_date_time_unref (dt);
1094 dt = g_date_time_new_utc (2016, 6, 31, 22, 10, 42);
1095 g_assert_null (dt);
1096 dt = g_date_time_new_utc (2016, 7, 31, 22, 10, 42);
1097 ASSERT_DATE (dt, 2016, 7, 31);
1098 g_date_time_unref (dt);
1099 dt = g_date_time_new_utc (2016, 7, 32, 22, 10, 42);
1100 g_assert_null (dt);
1101 dt = g_date_time_new_utc (2016, 8, 31, 22, 10, 42);
1102 ASSERT_DATE (dt, 2016, 8, 31);
1103 g_date_time_unref (dt);
1104 dt = g_date_time_new_utc (2016, 8, 32, 22, 10, 42);
1105 g_assert_null (dt);
1106 dt = g_date_time_new_utc (2016, 9, 30, 22, 10, 42);
1107 ASSERT_DATE (dt, 2016, 9, 30);
1108 g_date_time_unref (dt);
1109 dt = g_date_time_new_utc (2016, 9, 31, 22, 10, 42);
1110 g_assert_null (dt);
1111 dt = g_date_time_new_utc (2016, 10, 31, 22, 10, 42);
1112 ASSERT_DATE (dt, 2016, 10, 31);
1113 g_date_time_unref (dt);
1114 dt = g_date_time_new_utc (2016, 10, 32, 22, 10, 42);
1115 g_assert_null (dt);
1116 dt = g_date_time_new_utc (2016, 11, 30, 22, 10, 42);
1117 ASSERT_DATE (dt, 2016, 11, 30);
1118 g_date_time_unref (dt);
1119 dt = g_date_time_new_utc (2016, 11, 31, 22, 10, 42);
1120 g_assert_null (dt);
1121 dt = g_date_time_new_utc (2016, 12, 31, 22, 10, 42);
1122 ASSERT_DATE (dt, 2016, 12, 31);
1123 g_date_time_unref (dt);
1124 dt = g_date_time_new_utc (2016, 12, 32, 22, 10, 42);
1125 g_assert_null (dt);
1128 static void
1129 test_GDateTime_now_utc (void)
1131 GDateTime *dt;
1132 struct tm tm;
1133 time_t t;
1134 time_t after;
1136 /* t <= dt.to_unix() <= after, but the inequalities might not be
1137 * equality if we're close to the boundary between seconds.
1138 * We loop until t == after (and hence dt.to_unix() should equal both)
1139 * to guard against that. */
1142 t = g_get_real_time () / G_TIME_SPAN_SECOND;
1143 #ifdef HAVE_GMTIME_R
1144 gmtime_r (&t, &tm);
1145 #else
1147 struct tm *tmp = gmtime (&t);
1148 /* Assume gmtime() can't fail as we got t from time(NULL). (Note
1149 * that on Windows, gmtime() *is* MT-safe, it uses a thread-local
1150 * buffer.)
1152 memcpy (&tm, tmp, sizeof (struct tm));
1154 #endif
1155 dt = g_date_time_new_now_utc ();
1157 after = g_get_real_time () / G_TIME_SPAN_SECOND;
1159 while (t != after);
1161 g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
1162 g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
1163 g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
1164 g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
1165 g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
1166 g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
1167 g_date_time_unref (dt);
1170 static void
1171 test_GDateTime_new_from_unix_utc (void)
1173 GDateTime *dt;
1174 gint64 t;
1176 t = g_get_real_time ();
1178 #if 0
1179 dt = g_date_time_new_from_unix_utc (t);
1180 g_assert (dt == NULL);
1181 #endif
1183 t = t / 1e6; /* oops, this was microseconds */
1185 dt = g_date_time_new_from_unix_utc (t);
1186 g_assert (dt != NULL);
1188 g_assert (dt == g_date_time_ref (dt));
1189 g_date_time_unref (dt);
1190 g_assert_cmpint (g_date_time_to_unix (dt), ==, t);
1191 g_date_time_unref (dt);
1194 static void
1195 test_GDateTime_get_utc_offset (void)
1197 #if defined (HAVE_STRUCT_TM_TM_GMTOFF) || defined (HAVE_STRUCT_TM___TM_GMTOFF)
1198 GDateTime *dt;
1199 GTimeSpan ts;
1200 struct tm tm;
1202 memset (&tm, 0, sizeof (tm));
1203 get_localtime_tm (g_get_real_time () / G_TIME_SPAN_SECOND, &tm);
1205 dt = g_date_time_new_now_local ();
1206 ts = g_date_time_get_utc_offset (dt);
1207 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
1208 g_assert_cmpint (ts, ==, (tm.tm_gmtoff * G_TIME_SPAN_SECOND));
1209 #endif
1210 #ifdef HAVE_STRUCT_TM___TM_GMTOFF
1211 g_assert_cmpint (ts, ==, (tm.__tm_gmtoff * G_TIME_SPAN_SECOND));
1212 #endif
1213 g_date_time_unref (dt);
1214 #endif
1217 static void
1218 test_GDateTime_to_timeval (void)
1220 GTimeVal tv1, tv2;
1221 GDateTime *dt;
1223 memset (&tv1, 0, sizeof (tv1));
1224 memset (&tv2, 0, sizeof (tv2));
1226 g_get_current_time (&tv1);
1227 dt = g_date_time_new_from_timeval_local (&tv1);
1228 g_date_time_to_timeval (dt, &tv2);
1229 g_assert_cmpint (tv1.tv_sec, ==, tv2.tv_sec);
1230 g_assert_cmpint (tv1.tv_usec, ==, tv2.tv_usec);
1231 g_date_time_unref (dt);
1234 static void
1235 test_GDateTime_to_local (void)
1237 GDateTime *utc = NULL, *now = NULL, *dt;
1238 time_t before, after;
1240 /* before <= utc.to_unix() <= now.to_unix() <= after, but the inequalities
1241 * might not be equality if we're close to the boundary between seconds.
1242 * We loop until before == after (and hence the GDateTimes should match)
1243 * to guard against that. */
1246 before = g_get_real_time () / G_TIME_SPAN_SECOND;
1247 g_clear_pointer (&utc, g_date_time_unref);
1248 g_clear_pointer (&now, g_date_time_unref);
1249 utc = g_date_time_new_now_utc ();
1250 now = g_date_time_new_now_local ();
1251 after = g_get_real_time () / G_TIME_SPAN_SECOND;
1253 while (before != after);
1255 dt = g_date_time_to_local (utc);
1257 g_assert_cmpint (g_date_time_get_year (now), ==, g_date_time_get_year (dt));
1258 g_assert_cmpint (g_date_time_get_month (now), ==, g_date_time_get_month (dt));
1259 g_assert_cmpint (g_date_time_get_day_of_month (now), ==, g_date_time_get_day_of_month (dt));
1260 g_assert_cmpint (g_date_time_get_hour (now), ==, g_date_time_get_hour (dt));
1261 g_assert_cmpint (g_date_time_get_minute (now), ==, g_date_time_get_minute (dt));
1262 g_assert_cmpint (g_date_time_get_second (now), ==, g_date_time_get_second (dt));
1264 g_date_time_unref (now);
1265 g_date_time_unref (utc);
1266 g_date_time_unref (dt);
1269 static void
1270 test_GDateTime_to_utc (void)
1272 GDateTime *dt, *dt2;
1273 time_t t;
1274 struct tm tm;
1276 t = time (NULL);
1277 #ifdef HAVE_GMTIME_R
1278 gmtime_r (&t, &tm);
1279 #else
1281 struct tm *tmp = gmtime (&t);
1282 memcpy (&tm, tmp, sizeof (struct tm));
1284 #endif
1285 dt2 = g_date_time_new_from_unix_local (t);
1286 dt = g_date_time_to_utc (dt2);
1287 g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
1288 g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
1289 g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
1290 g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
1291 g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
1292 g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
1293 g_date_time_unref (dt);
1294 g_date_time_unref (dt2);
1297 static void
1298 test_GDateTime_get_day_of_year (void)
1300 #define TEST_DAY_OF_YEAR(y,m,d,o) G_STMT_START { \
1301 GDateTime *__dt = g_date_time_new_local ((y), (m), (d), 0, 0, 0); \
1302 g_assert_cmpint ((o), ==, g_date_time_get_day_of_year (__dt)); \
1303 g_date_time_unref (__dt); } G_STMT_END
1305 TEST_DAY_OF_YEAR (2009, 1, 1, 1);
1306 TEST_DAY_OF_YEAR (2009, 2, 1, 32);
1307 TEST_DAY_OF_YEAR (2009, 8, 16, 228);
1308 TEST_DAY_OF_YEAR (2008, 8, 16, 229);
1311 static void
1312 test_GDateTime_printf (void)
1314 /* 64 seems big, but one zoneinfo file, Factory, has an abbreviation
1315 * that long, and it will cause the test to fail if dst isn't big
1316 * enough.
1318 gchar dst[64];
1319 struct tm tt;
1320 time_t t;
1322 #define TEST_PRINTF(f,o) G_STMT_START { \
1323 GDateTime *__dt = g_date_time_new_local (2009, 10, 24, 0, 0, 0);\
1324 gchar *__p = g_date_time_format (__dt, (f)); \
1325 g_assert_cmpstr (__p, ==, (o)); \
1326 g_date_time_unref (__dt); \
1327 g_free (__p); } G_STMT_END
1329 #define TEST_PRINTF_DATE(y,m,d,f,o) G_STMT_START { \
1330 GDateTime *dt = g_date_time_new_local (y, m, d, 0, 0, 0); \
1331 gchar *p = g_date_time_format (dt, (f)); \
1332 g_assert_cmpstr (p, ==, (o)); \
1333 g_date_time_unref (dt); \
1334 g_free (p); } G_STMT_END
1336 #define TEST_PRINTF_TIME(h,m,s,f,o) G_STMT_START { \
1337 GDateTime *dt = g_date_time_new_local (2009, 10, 24, (h), (m), (s)); \
1338 gchar *p = g_date_time_format (dt, (f)); \
1339 g_assert_cmpstr (p, ==, (o)); \
1340 g_date_time_unref (dt); \
1341 g_free (p); } G_STMT_END
1344 * This is a little helper to make sure we can compare timezones to
1345 * that of the generated timezone.
1347 t = time (NULL);
1348 memset (&tt, 0, sizeof(tt));
1349 get_localtime_tm (t, &tt);
1350 tt.tm_year = 2009 - 1900;
1351 tt.tm_mon = 9; /* 0 indexed */
1352 tt.tm_mday = 24;
1353 t = mktime (&tt);
1354 memset (&tt, 0, sizeof(tt));
1355 get_localtime_tm (t, &tt);
1356 strftime (dst, sizeof(dst), "%Z", &tt);
1358 /* get current time_t for 20090924 in the local timezone */
1359 tt.tm_sec = 0;
1360 tt.tm_min = 0;
1361 tt.tm_hour = 0;
1362 t = mktime (&tt);
1364 TEST_PRINTF ("%a", "Sat");
1365 TEST_PRINTF ("%A", "Saturday");
1366 TEST_PRINTF ("%b", "Oct");
1367 TEST_PRINTF ("%B", "October");
1368 TEST_PRINTF ("%d", "24");
1369 TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
1370 TEST_PRINTF ("%e", "24"); // fixme
1371 TEST_PRINTF ("%h", "Oct");
1372 TEST_PRINTF ("%H", "00");
1373 TEST_PRINTF_TIME (15, 0, 0, "%H", "15");
1374 TEST_PRINTF ("%I", "12");
1375 TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
1376 TEST_PRINTF_TIME (15, 0, 0, "%I", "03");
1377 TEST_PRINTF ("%j", "297");
1378 TEST_PRINTF ("%k", " 0");
1379 TEST_PRINTF_TIME (13, 13, 13, "%k", "13");
1380 TEST_PRINTF ("%l", "12");
1381 TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
1382 TEST_PRINTF_TIME (13, 13, 13, "%l", " 1");
1383 TEST_PRINTF_TIME (10, 13, 13, "%l", "10");
1384 TEST_PRINTF ("%m", "10");
1385 TEST_PRINTF ("%M", "00");
1386 TEST_PRINTF ("%p", "AM");
1387 TEST_PRINTF_TIME (13, 13, 13, "%p", "PM");
1388 TEST_PRINTF ("%P", "am");
1389 TEST_PRINTF_TIME (13, 13, 13, "%P", "pm");
1390 TEST_PRINTF ("%r", "12:00:00 AM");
1391 TEST_PRINTF_TIME (13, 13, 13, "%r", "01:13:13 PM");
1392 TEST_PRINTF ("%R", "00:00");
1393 TEST_PRINTF_TIME (13, 13, 31, "%R", "13:13");
1394 TEST_PRINTF ("%S", "00");
1395 TEST_PRINTF ("%t", " ");
1396 TEST_PRINTF ("%u", "6");
1397 TEST_PRINTF ("%x", "10/24/09");
1398 TEST_PRINTF ("%X", "00:00:00");
1399 TEST_PRINTF_TIME (13, 14, 15, "%X", "13:14:15");
1400 TEST_PRINTF ("%y", "09");
1401 TEST_PRINTF ("%Y", "2009");
1402 TEST_PRINTF ("%%", "%");
1403 TEST_PRINTF ("%", "");
1404 TEST_PRINTF ("%9", NULL);
1405 #ifdef G_OS_UNIX
1406 TEST_PRINTF ("%Z", dst);
1407 #elif defined G_OS_WIN32
1408 TEST_PRINTF ("%Z", "Pacific Standard Time");
1409 #endif
1412 static void
1413 test_non_utf8_printf (void)
1415 gchar *oldlocale;
1417 oldlocale = g_strdup (setlocale (LC_ALL, NULL));
1418 setlocale (LC_ALL, "ja_JP.eucjp");
1419 if (strstr (setlocale (LC_ALL, NULL), "ja_JP") == NULL)
1421 g_test_skip ("locale ja_JP.eucjp not available, skipping non-UTF8 tests");
1422 g_free (oldlocale);
1423 return;
1425 if (g_get_charset (NULL))
1427 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");
1428 setlocale (LC_ALL, oldlocale);
1429 g_free (oldlocale);
1430 return;
1433 /* These are the outputs that ja_JP.UTF-8 generates; if everything
1434 * is working then ja_JP.eucjp should generate the same.
1436 TEST_PRINTF ("%a", "\345\234\237");
1437 TEST_PRINTF ("%A", "\345\234\237\346\233\234\346\227\245");
1438 #ifndef __APPLE__ /* OSX just returns the number */
1439 TEST_PRINTF ("%b", "10\346\234\210");
1440 #endif
1441 TEST_PRINTF ("%B", "10\346\234\210");
1442 TEST_PRINTF ("%d", "24");
1443 TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
1444 TEST_PRINTF ("%e", "24"); // fixme
1445 #ifndef __APPLE__ /* OSX just returns the number */
1446 TEST_PRINTF ("%h", "10\346\234\210");
1447 #endif
1448 TEST_PRINTF ("%H", "00");
1449 TEST_PRINTF_TIME (15, 0, 0, "%H", "15");
1450 TEST_PRINTF ("%I", "12");
1451 TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
1452 TEST_PRINTF_TIME (15, 0, 0, "%I", "03");
1453 TEST_PRINTF ("%j", "297");
1454 TEST_PRINTF ("%k", " 0");
1455 TEST_PRINTF_TIME (13, 13, 13, "%k", "13");
1456 TEST_PRINTF ("%l", "12");
1457 TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
1458 TEST_PRINTF_TIME (13, 13, 13, "%l", " 1");
1459 TEST_PRINTF_TIME (10, 13, 13, "%l", "10");
1460 TEST_PRINTF ("%m", "10");
1461 TEST_PRINTF ("%M", "00");
1462 #ifndef __APPLE__ /* OSX returns latin "AM", not japanese */
1463 TEST_PRINTF ("%p", "\345\215\210\345\211\215");
1464 TEST_PRINTF_TIME (13, 13, 13, "%p", "\345\215\210\345\276\214");
1465 TEST_PRINTF ("%P", "\345\215\210\345\211\215");
1466 TEST_PRINTF_TIME (13, 13, 13, "%P", "\345\215\210\345\276\214");
1467 TEST_PRINTF ("%r", "\345\215\210\345\211\21512\346\231\20200\345\210\20600\347\247\222");
1468 TEST_PRINTF_TIME (13, 13, 13, "%r", "\345\215\210\345\276\21401\346\231\20213\345\210\20613\347\247\222");
1469 #endif
1470 TEST_PRINTF ("%R", "00:00");
1471 TEST_PRINTF_TIME (13, 13, 31, "%R", "13:13");
1472 TEST_PRINTF ("%S", "00");
1473 TEST_PRINTF ("%t", " ");
1474 TEST_PRINTF ("%u", "6");
1475 #ifndef __APPLE__ /* OSX returns YYYY/MM/DD in ASCII */
1476 TEST_PRINTF ("%x", "2009\345\271\26410\346\234\21024\346\227\245");
1477 #endif
1478 TEST_PRINTF ("%X", "00\346\231\20200\345\210\20600\347\247\222");
1479 TEST_PRINTF_TIME (13, 14, 15, "%X", "13\346\231\20214\345\210\20615\347\247\222");
1480 TEST_PRINTF ("%y", "09");
1481 TEST_PRINTF ("%Y", "2009");
1482 TEST_PRINTF ("%%", "%");
1483 TEST_PRINTF ("%", "");
1484 TEST_PRINTF ("%9", NULL);
1486 setlocale (LC_ALL, oldlocale);
1487 g_free (oldlocale);
1490 static void
1491 test_modifiers (void)
1493 gchar *oldlocale;
1495 TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
1496 TEST_PRINTF_DATE (2009, 1, 1, "%_d", " 1");
1497 TEST_PRINTF_DATE (2009, 1, 1, "%-d", "1");
1498 TEST_PRINTF_DATE (2009, 1, 1, "%0d", "01");
1499 TEST_PRINTF_DATE (2009, 1, 21, "%d", "21");
1500 TEST_PRINTF_DATE (2009, 1, 21, "%_d", "21");
1501 TEST_PRINTF_DATE (2009, 1, 21, "%-d", "21");
1502 TEST_PRINTF_DATE (2009, 1, 21, "%0d", "21");
1504 TEST_PRINTF_DATE (2009, 1, 1, "%e", " 1");
1505 TEST_PRINTF_DATE (2009, 1, 1, "%_e", " 1");
1506 TEST_PRINTF_DATE (2009, 1, 1, "%-e", "1");
1507 TEST_PRINTF_DATE (2009, 1, 1, "%0e", "01");
1508 TEST_PRINTF_DATE (2009, 1, 21, "%e", "21");
1509 TEST_PRINTF_DATE (2009, 1, 21, "%_e", "21");
1510 TEST_PRINTF_DATE (2009, 1, 21, "%-e", "21");
1511 TEST_PRINTF_DATE (2009, 1, 21, "%0e", "21");
1513 TEST_PRINTF_TIME ( 1, 0, 0, "%H", "01");
1514 TEST_PRINTF_TIME ( 1, 0, 0, "%_H", " 1");
1515 TEST_PRINTF_TIME ( 1, 0, 0, "%-H", "1");
1516 TEST_PRINTF_TIME ( 1, 0, 0, "%0H", "01");
1517 TEST_PRINTF_TIME (21, 0, 0, "%H", "21");
1518 TEST_PRINTF_TIME (21, 0, 0, "%_H", "21");
1519 TEST_PRINTF_TIME (21, 0, 0, "%-H", "21");
1520 TEST_PRINTF_TIME (21, 0, 0, "%0H", "21");
1522 TEST_PRINTF_TIME ( 1, 0, 0, "%I", "01");
1523 TEST_PRINTF_TIME ( 1, 0, 0, "%_I", " 1");
1524 TEST_PRINTF_TIME ( 1, 0, 0, "%-I", "1");
1525 TEST_PRINTF_TIME ( 1, 0, 0, "%0I", "01");
1526 TEST_PRINTF_TIME (23, 0, 0, "%I", "11");
1527 TEST_PRINTF_TIME (23, 0, 0, "%_I", "11");
1528 TEST_PRINTF_TIME (23, 0, 0, "%-I", "11");
1529 TEST_PRINTF_TIME (23, 0, 0, "%0I", "11");
1531 TEST_PRINTF_TIME ( 1, 0, 0, "%k", " 1");
1532 TEST_PRINTF_TIME ( 1, 0, 0, "%_k", " 1");
1533 TEST_PRINTF_TIME ( 1, 0, 0, "%-k", "1");
1534 TEST_PRINTF_TIME ( 1, 0, 0, "%0k", "01");
1536 oldlocale = g_strdup (setlocale (LC_ALL, NULL));
1537 setlocale (LC_ALL, "fa_IR.utf-8");
1538 if (strstr (setlocale (LC_ALL, NULL), "fa_IR") != NULL)
1540 TEST_PRINTF_TIME (23, 0, 0, "%OH", "\333\262\333\263"); /* '23' */
1541 TEST_PRINTF_TIME (23, 0, 0, "%OI", "\333\261\333\261"); /* '11' */
1542 TEST_PRINTF_TIME (23, 0, 0, "%OM", "\333\260\333\260"); /* '00' */
1544 TEST_PRINTF_DATE (2011, 7, 1, "%Om", "\333\260\333\267"); /* '07' */
1545 TEST_PRINTF_DATE (2011, 7, 1, "%0Om", "\333\260\333\267"); /* '07' */
1546 TEST_PRINTF_DATE (2011, 7, 1, "%-Om", "\333\267"); /* '7' */
1547 TEST_PRINTF_DATE (2011, 7, 1, "%_Om", " \333\267"); /* ' 7' */
1549 else
1550 g_test_skip ("locale fa_IR not available, skipping O modifier tests");
1551 setlocale (LC_ALL, oldlocale);
1552 g_free (oldlocale);
1555 /* Test that the `O` modifier for g_date_time_format() works with %B, %b and %h;
1556 * i.e. whether genitive month names are supported. */
1557 static void
1558 test_month_names (void)
1560 gchar *oldlocale;
1562 g_test_bug ("749206");
1564 oldlocale = g_strdup (setlocale (LC_ALL, NULL));
1566 /* Make sure that nothing has been changed in western European languages. */
1567 setlocale (LC_ALL, "en_GB.utf-8");
1568 if (strstr (setlocale (LC_ALL, NULL), "en_GB") != NULL)
1570 TEST_PRINTF_DATE (2018, 1, 1, "%B", "January");
1571 TEST_PRINTF_DATE (2018, 2, 1, "%OB", "February");
1572 TEST_PRINTF_DATE (2018, 3, 1, "%b", "Mar");
1573 TEST_PRINTF_DATE (2018, 4, 1, "%Ob", "Apr");
1574 TEST_PRINTF_DATE (2018, 5, 1, "%h", "May");
1575 TEST_PRINTF_DATE (2018, 6, 1, "%Oh", "Jun");
1577 else
1578 g_test_skip ("locale en_GB not available, skipping English month names test");
1580 setlocale (LC_ALL, "de_DE.utf-8");
1581 if (strstr (setlocale (LC_ALL, NULL), "de_DE") != NULL)
1583 TEST_PRINTF_DATE (2018, 7, 1, "%B", "Juli");
1584 TEST_PRINTF_DATE (2018, 8, 1, "%OB", "August");
1585 TEST_PRINTF_DATE (2018, 9, 1, "%b", "Sep");
1586 TEST_PRINTF_DATE (2018, 10, 1, "%Ob", "Okt");
1587 TEST_PRINTF_DATE (2018, 11, 1, "%h", "Nov");
1588 TEST_PRINTF_DATE (2018, 12, 1, "%Oh", "Dez");
1590 else
1591 g_test_skip ("locale de_DE not available, skipping German month names test");
1593 setlocale (LC_ALL, "es_ES.utf-8");
1594 if (strstr (setlocale (LC_ALL, NULL), "es_ES") != NULL)
1596 TEST_PRINTF_DATE (2018, 1, 1, "%B", "enero");
1597 TEST_PRINTF_DATE (2018, 2, 1, "%OB", "febrero");
1598 TEST_PRINTF_DATE (2018, 3, 1, "%b", "mar");
1599 TEST_PRINTF_DATE (2018, 4, 1, "%Ob", "abr");
1600 TEST_PRINTF_DATE (2018, 5, 1, "%h", "may");
1601 TEST_PRINTF_DATE (2018, 6, 1, "%Oh", "jun");
1603 else
1604 g_test_skip ("locale es_ES not available, skipping Spanish month names test");
1606 setlocale (LC_ALL, "fr_FR.utf-8");
1607 if (strstr (setlocale (LC_ALL, NULL), "fr_FR") != NULL)
1609 TEST_PRINTF_DATE (2018, 7, 1, "%B", "juillet");
1610 TEST_PRINTF_DATE (2018, 8, 1, "%OB", "août");
1611 TEST_PRINTF_DATE (2018, 9, 1, "%b", "sept.");
1612 TEST_PRINTF_DATE (2018, 10, 1, "%Ob", "oct.");
1613 TEST_PRINTF_DATE (2018, 11, 1, "%h", "nov.");
1614 TEST_PRINTF_DATE (2018, 12, 1, "%Oh", "déc.");
1616 else
1617 g_test_skip ("locale fr_FR not available, skipping French month names test");
1619 /* Make sure that there are visible changes in some European languages. */
1620 setlocale (LC_ALL, "el_GR.utf-8");
1621 if (strstr (setlocale (LC_ALL, NULL), "el_GR") != NULL)
1623 TEST_PRINTF_DATE (2018, 1, 1, "%B", "Ιανουαρίου");
1624 TEST_PRINTF_DATE (2018, 2, 1, "%B", "Φεβρουαρίου");
1625 TEST_PRINTF_DATE (2018, 3, 1, "%B", "Μαρτίου");
1626 TEST_PRINTF_DATE (2018, 4, 1, "%OB", "Απρίλιος");
1627 TEST_PRINTF_DATE (2018, 5, 1, "%OB", "Μάιος");
1628 TEST_PRINTF_DATE (2018, 6, 1, "%OB", "Ιούνιος");
1629 TEST_PRINTF_DATE (2018, 7, 1, "%b", "Ιούλ");
1630 TEST_PRINTF_DATE (2018, 8, 1, "%Ob", "Αύγ");
1632 else
1633 g_test_skip ("locale el_GR not available, skipping Greek month names test");
1635 setlocale (LC_ALL, "hr_HR.utf-8");
1636 if (strstr (setlocale (LC_ALL, NULL), "hr_HR") != NULL)
1638 TEST_PRINTF_DATE (2018, 5, 1, "%B", "svibnja");
1639 TEST_PRINTF_DATE (2018, 6, 1, "%B", "lipnja");
1640 TEST_PRINTF_DATE (2018, 7, 1, "%B", "srpnja");
1641 TEST_PRINTF_DATE (2018, 8, 1, "%OB", "Kolovoz");
1642 TEST_PRINTF_DATE (2018, 9, 1, "%OB", "Rujan");
1643 TEST_PRINTF_DATE (2018, 10, 1, "%OB", "Listopad");
1644 TEST_PRINTF_DATE (2018, 11, 1, "%b", "Stu");
1645 TEST_PRINTF_DATE (2018, 12, 1, "%Ob", "Pro");
1647 else
1648 g_test_skip ("locale hr_HR not available, skipping Croatian month names test");
1650 setlocale (LC_ALL, "lt_LT.utf-8");
1651 if (strstr (setlocale (LC_ALL, NULL), "lt_LT") != NULL)
1653 TEST_PRINTF_DATE (2018, 1, 1, "%B", "sausio");
1654 TEST_PRINTF_DATE (2018, 2, 1, "%B", "vasario");
1655 TEST_PRINTF_DATE (2018, 3, 1, "%B", "kovo");
1656 TEST_PRINTF_DATE (2018, 4, 1, "%OB", "balandis");
1657 TEST_PRINTF_DATE (2018, 5, 1, "%OB", "gegužė");
1658 TEST_PRINTF_DATE (2018, 6, 1, "%OB", "birželis");
1659 TEST_PRINTF_DATE (2018, 7, 1, "%b", "Lie");
1660 TEST_PRINTF_DATE (2018, 8, 1, "%Ob", "Rgp");
1662 else
1663 g_test_skip ("locale lt_LT not available, skipping Lithuanian month names test");
1665 setlocale (LC_ALL, "pl_PL.utf-8");
1666 if (strstr (setlocale (LC_ALL, NULL), "pl_PL") != NULL)
1668 TEST_PRINTF_DATE (2018, 5, 1, "%B", "maja");
1669 TEST_PRINTF_DATE (2018, 6, 1, "%B", "czerwca");
1670 TEST_PRINTF_DATE (2018, 7, 1, "%B", "lipca");
1671 TEST_PRINTF_DATE (2018, 8, 1, "%OB", "sierpień");
1672 TEST_PRINTF_DATE (2018, 9, 1, "%OB", "wrzesień");
1673 TEST_PRINTF_DATE (2018, 10, 1, "%OB", "październik");
1674 TEST_PRINTF_DATE (2018, 11, 1, "%b", "lis");
1675 TEST_PRINTF_DATE (2018, 12, 1, "%Ob", "gru");
1677 else
1678 g_test_skip ("locale pl_PL not available, skipping Polish month names test");
1680 setlocale (LC_ALL, "ru_RU.utf-8");
1681 if (strstr (setlocale (LC_ALL, NULL), "ru_RU") != NULL)
1683 TEST_PRINTF_DATE (2018, 1, 1, "%B", "января");
1684 TEST_PRINTF_DATE (2018, 2, 1, "%B", "февраля");
1685 TEST_PRINTF_DATE (2018, 3, 1, "%B", "марта");
1686 TEST_PRINTF_DATE (2018, 4, 1, "%OB", "Апрель");
1687 TEST_PRINTF_DATE (2018, 5, 1, "%OB", "Май");
1688 TEST_PRINTF_DATE (2018, 6, 1, "%OB", "Июнь");
1689 TEST_PRINTF_DATE (2018, 7, 1, "%b", "июл");
1690 TEST_PRINTF_DATE (2018, 8, 1, "%Ob", "авг");
1691 /* This difference is very important in Russian: */
1692 TEST_PRINTF_DATE (2018, 5, 1, "%b", "мая");
1693 TEST_PRINTF_DATE (2018, 5, 1, "%Ob", "май");
1695 else
1696 g_test_skip ("locale ru_RU not available, skipping Russian month names test");
1698 setlocale (LC_ALL, oldlocale);
1699 g_free (oldlocale);
1702 static void
1703 test_GDateTime_dst (void)
1705 GDateTime *dt1, *dt2;
1706 GTimeZone *tz;
1708 /* this date has the DST state set for Europe/London */
1709 #ifdef G_OS_UNIX
1710 tz = g_time_zone_new ("Europe/London");
1711 #elif defined G_OS_WIN32
1712 tz = g_time_zone_new ("GMT Standard Time");
1713 #endif
1714 dt1 = g_date_time_new (tz, 2009, 8, 15, 3, 0, 1);
1715 g_assert (g_date_time_is_daylight_savings (dt1));
1716 g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, 3600);
1717 g_assert_cmpint (g_date_time_get_hour (dt1), ==, 3);
1719 /* add 6 months to clear the DST flag but keep the same time */
1720 dt2 = g_date_time_add_months (dt1, 6);
1721 g_assert (!g_date_time_is_daylight_savings (dt2));
1722 g_assert_cmpint (g_date_time_get_utc_offset (dt2) / G_USEC_PER_SEC, ==, 0);
1723 g_assert_cmpint (g_date_time_get_hour (dt2), ==, 3);
1725 g_date_time_unref (dt2);
1726 g_date_time_unref (dt1);
1728 /* now do the reverse: start with a non-DST state and move to DST */
1729 dt1 = g_date_time_new (tz, 2009, 2, 15, 2, 0, 1);
1730 g_assert (!g_date_time_is_daylight_savings (dt1));
1731 g_assert_cmpint (g_date_time_get_hour (dt1), ==, 2);
1733 dt2 = g_date_time_add_months (dt1, 6);
1734 g_assert (g_date_time_is_daylight_savings (dt2));
1735 g_assert_cmpint (g_date_time_get_hour (dt2), ==, 2);
1737 g_date_time_unref (dt2);
1738 g_date_time_unref (dt1);
1739 g_time_zone_unref (tz);
1742 static inline gboolean
1743 is_leap_year (gint year)
1745 g_assert (1 <= year && year <= 9999);
1747 return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
1750 static inline gint
1751 days_in_month (gint year, gint month)
1753 const gint table[2][13] = {
1754 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
1755 {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
1758 g_assert (1 <= month && month <= 12);
1760 return table[is_leap_year (year)][month];
1763 static void
1764 test_all_dates (void)
1766 gint year, month, day;
1767 GTimeZone *timezone;
1768 gint64 unix_time;
1769 gint day_of_year;
1770 gint week_year;
1771 gint week_num;
1772 gint weekday;
1774 /* save some time by hanging on to this. */
1775 timezone = g_time_zone_new_utc ();
1777 unix_time = G_GINT64_CONSTANT(-62135596800);
1779 /* 0001-01-01 is 0001-W01-1 */
1780 week_year = 1;
1781 week_num = 1;
1782 weekday = 1;
1785 /* The calendar makes a full cycle every 400 years, so we could
1786 * theoretically just test years 1 through 400. That assumes that our
1787 * software has no bugs, so probably we should just test them all. :)
1789 for (year = 1; year <= 9999; year++)
1791 day_of_year = 1;
1793 for (month = 1; month <= 12; month++)
1794 for (day = 1; day <= days_in_month (year, month); day++)
1796 GDateTime *dt;
1798 dt = g_date_time_new (timezone, year, month, day, 0, 0, 0);
1800 #if 0
1801 g_printerr ("%04d-%02d-%02d = %04d-W%02d-%d = %04d-%03d\n",
1802 year, month, day,
1803 week_year, week_num, weekday,
1804 year, day_of_year);
1805 #endif
1807 /* sanity check */
1808 if G_UNLIKELY (g_date_time_get_year (dt) != year ||
1809 g_date_time_get_month (dt) != month ||
1810 g_date_time_get_day_of_month (dt) != day)
1811 g_error ("%04d-%02d-%02d comes out as %04d-%02d-%02d",
1812 year, month, day,
1813 g_date_time_get_year (dt),
1814 g_date_time_get_month (dt),
1815 g_date_time_get_day_of_month (dt));
1817 if G_UNLIKELY (g_date_time_get_week_numbering_year (dt) != week_year ||
1818 g_date_time_get_week_of_year (dt) != week_num ||
1819 g_date_time_get_day_of_week (dt) != weekday)
1820 g_error ("%04d-%02d-%02d should be %04d-W%02d-%d but "
1821 "comes out as %04d-W%02d-%d", year, month, day,
1822 week_year, week_num, weekday,
1823 g_date_time_get_week_numbering_year (dt),
1824 g_date_time_get_week_of_year (dt),
1825 g_date_time_get_day_of_week (dt));
1827 if G_UNLIKELY (g_date_time_to_unix (dt) != unix_time)
1828 g_error ("%04d-%02d-%02d 00:00:00 UTC should have unix time %"
1829 G_GINT64_FORMAT " but comes out as %"G_GINT64_FORMAT,
1830 year, month, day, unix_time, g_date_time_to_unix (dt));
1832 if G_UNLIKELY (g_date_time_get_day_of_year (dt) != day_of_year)
1833 g_error ("%04d-%02d-%02d should be day of year %d"
1834 " but comes out as %d", year, month, day,
1835 day_of_year, g_date_time_get_day_of_year (dt));
1837 if G_UNLIKELY (g_date_time_get_hour (dt) != 0 ||
1838 g_date_time_get_minute (dt) != 0 ||
1839 g_date_time_get_seconds (dt) != 0)
1840 g_error ("%04d-%02d-%02d 00:00:00 UTC comes out "
1841 "as %02d:%02d:%02.6f", year, month, day,
1842 g_date_time_get_hour (dt),
1843 g_date_time_get_minute (dt),
1844 g_date_time_get_seconds (dt));
1845 /* done */
1847 /* add 24 hours to unix time */
1848 unix_time += 24 * 60 * 60;
1850 /* move day of year forward */
1851 day_of_year++;
1853 /* move the week date forward */
1854 if (++weekday == 8)
1856 weekday = 1; /* Sunday -> Monday */
1858 /* NOTE: year/month/day is the final day of the week we
1859 * just finished.
1861 * If we just finished the last week of last year then
1862 * we are definitely starting the first week of this
1863 * year.
1865 * Otherwise, if we're still in this year, but Sunday
1866 * fell on or after December 28 then December 29, 30, 31
1867 * could be days within the next year's first year.
1869 if (year != week_year || (month == 12 && day >= 28))
1871 /* first week of the new year */
1872 week_num = 1;
1873 week_year++;
1875 else
1876 week_num++;
1879 g_date_time_unref (dt);
1883 g_time_zone_unref (timezone);
1886 static void
1887 test_z (void)
1889 GTimeZone *tz;
1890 GDateTime *dt;
1891 gchar *p;
1893 g_test_bug ("642935");
1895 tz = g_time_zone_new ("-08:00");
1896 dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1898 p = g_date_time_format (dt, "%z");
1899 g_assert_cmpstr (p, ==, "-0800");
1900 g_free (p);
1902 p = g_date_time_format (dt, "%:z");
1903 g_assert_cmpstr (p, ==, "-08:00");
1904 g_free (p);
1906 p = g_date_time_format (dt, "%::z");
1907 g_assert_cmpstr (p, ==, "-08:00:00");
1908 g_free (p);
1910 p = g_date_time_format (dt, "%:::z");
1911 g_assert_cmpstr (p, ==, "-08");
1912 g_free (p);
1914 g_date_time_unref (dt);
1915 g_time_zone_unref (tz);
1917 tz = g_time_zone_new ("+00:00");
1918 dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1919 p = g_date_time_format (dt, "%:::z");
1920 g_assert_cmpstr (p, ==, "+00");
1921 g_free (p);
1922 g_date_time_unref (dt);
1923 g_time_zone_unref (tz);
1925 tz = g_time_zone_new ("+08:23");
1926 dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1927 p = g_date_time_format (dt, "%:::z");
1928 g_assert_cmpstr (p, ==, "+08:23");
1929 g_free (p);
1930 g_date_time_unref (dt);
1931 g_time_zone_unref (tz);
1933 tz = g_time_zone_new ("+08:23:45");
1934 dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1935 p = g_date_time_format (dt, "%:::z");
1936 g_assert_cmpstr (p, ==, "+08:23:45");
1937 g_free (p);
1938 g_date_time_unref (dt);
1939 g_time_zone_unref (tz);
1942 #pragma GCC diagnostic push
1943 #pragma GCC diagnostic ignored "-Wformat-y2k"
1944 static void
1945 test_strftime (void)
1947 #ifdef __linux__
1948 #define TEST_FORMAT \
1949 "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 " \
1950 "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 %%"
1951 time_t t;
1953 /* 127997 is prime, 1315005118 is now */
1954 for (t = 0; t < 1315005118; t += 127997)
1956 GDateTime *date_time;
1957 gchar c_str[1000];
1958 gchar *dt_str;
1960 date_time = g_date_time_new_from_unix_local (t);
1961 dt_str = g_date_time_format (date_time, TEST_FORMAT);
1962 strftime (c_str, sizeof c_str, TEST_FORMAT, localtime (&t));
1963 g_assert_cmpstr (c_str, ==, dt_str);
1964 g_date_time_unref (date_time);
1965 g_free (dt_str);
1967 #endif
1969 #pragma GCC diagnostic pop
1971 /* Check that g_date_time_format() correctly returns %NULL for format
1972 * placeholders which are not supported in the current locale. */
1973 static void
1974 test_GDateTime_strftime_error_handling (void)
1976 gchar *oldlocale;
1978 oldlocale = g_strdup (setlocale (LC_ALL, NULL));
1979 setlocale (LC_ALL, "de_DE.utf-8");
1980 if (strstr (setlocale (LC_ALL, NULL), "de_DE") != NULL)
1982 /* de_DE doesn’t ever write time in 12-hour notation, so %r is
1983 * unsupported for it. */
1984 TEST_PRINTF_TIME (23, 0, 0, "%r", NULL);
1986 else
1987 g_test_skip ("locale de_DE not available, skipping error handling tests");
1988 setlocale (LC_ALL, oldlocale);
1989 g_free (oldlocale);
1992 static void
1993 test_find_interval (void)
1995 GTimeZone *tz;
1996 GDateTime *dt;
1997 gint64 u;
1998 gint i1, i2;
2000 #ifdef G_OS_UNIX
2001 tz = g_time_zone_new ("America/Toronto");
2002 #elif defined G_OS_WIN32
2003 tz = g_time_zone_new ("Eastern Standard Time");
2004 #endif
2005 dt = g_date_time_new_utc (2010, 11, 7, 1, 30, 0);
2006 u = g_date_time_to_unix (dt);
2008 i1 = g_time_zone_find_interval (tz, G_TIME_TYPE_STANDARD, u);
2009 i2 = g_time_zone_find_interval (tz, G_TIME_TYPE_DAYLIGHT, u);
2011 g_assert_cmpint (i1, !=, i2);
2013 g_date_time_unref (dt);
2015 dt = g_date_time_new_utc (2010, 3, 14, 2, 0, 0);
2016 u = g_date_time_to_unix (dt);
2018 i1 = g_time_zone_find_interval (tz, G_TIME_TYPE_STANDARD, u);
2019 g_assert_cmpint (i1, ==, -1);
2021 g_date_time_unref (dt);
2022 g_time_zone_unref (tz);
2025 static void
2026 test_adjust_time (void)
2028 GTimeZone *tz;
2029 GDateTime *dt;
2030 gint64 u, u2;
2031 gint i1, i2;
2033 #ifdef G_OS_UNIX
2034 tz = g_time_zone_new ("America/Toronto");
2035 #elif defined G_OS_WIN32
2036 tz = g_time_zone_new ("Eastern Standard Time");
2037 #endif
2038 dt = g_date_time_new_utc (2010, 11, 7, 1, 30, 0);
2039 u = g_date_time_to_unix (dt);
2040 u2 = u;
2042 i1 = g_time_zone_find_interval (tz, G_TIME_TYPE_DAYLIGHT, u);
2043 i2 = g_time_zone_adjust_time (tz, G_TIME_TYPE_DAYLIGHT, &u2);
2045 g_assert_cmpint (i1, ==, i2);
2046 g_assert (u == u2);
2048 g_date_time_unref (dt);
2050 dt = g_date_time_new_utc (2010, 3, 14, 2, 30, 0);
2051 u2 = g_date_time_to_unix (dt);
2052 g_date_time_unref (dt);
2054 dt = g_date_time_new_utc (2010, 3, 14, 3, 0, 0);
2055 u = g_date_time_to_unix (dt);
2056 g_date_time_unref (dt);
2058 i1 = g_time_zone_adjust_time (tz, G_TIME_TYPE_DAYLIGHT, &u2);
2059 g_assert (u == u2);
2061 g_time_zone_unref (tz);
2064 static void
2065 test_no_header (void)
2067 GTimeZone *tz;
2069 tz = g_time_zone_new ("blabla");
2071 g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "UTC");
2072 g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 0);
2073 g_assert (!g_time_zone_is_dst (tz, 0));
2075 g_time_zone_unref (tz);
2078 static void
2079 test_posix_parse (void)
2081 GTimeZone *tz;
2082 GDateTime *gdt1, *gdt2;
2084 tz = g_time_zone_new ("PST");
2085 g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "UTC");
2086 g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 0);
2087 g_assert (!g_time_zone_is_dst (tz, 0));
2088 g_time_zone_unref (tz);
2090 tz = g_time_zone_new ("PST8");
2091 g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "PST");
2092 g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, - 8 * 3600);
2093 g_assert (!g_time_zone_is_dst (tz, 0));
2094 g_time_zone_unref (tz);
2096 /* This fails rules_from_identifier on Unix (though not on Windows)
2097 * but passes anyway because PST8PDT is a zone name.
2099 tz = g_time_zone_new ("PST8PDT");
2100 g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "PST");
2101 g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, - 8 * 3600);
2102 g_assert (!g_time_zone_is_dst (tz, 0));
2103 g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "PDT");
2104 g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==,- 7 * 3600);
2105 g_assert (g_time_zone_is_dst (tz, 1));
2106 g_time_zone_unref (tz);
2108 tz = g_time_zone_new ("PST8PDT6:32:15");
2109 #ifdef G_OS_WIN32
2110 g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "PST");
2111 g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, - 8 * 3600);
2112 g_assert (!g_time_zone_is_dst (tz, 0));
2113 g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "PDT");
2114 g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, - 6 * 3600 - 32 *60 - 15);
2115 g_assert (g_time_zone_is_dst (tz, 1));
2116 gdt1 = g_date_time_new (tz, 2012, 12, 6, 11, 15, 23.0);
2117 gdt2 = g_date_time_new (tz, 2012, 6, 6, 11, 15, 23.0);
2118 g_assert (!g_date_time_is_daylight_savings (gdt1));
2119 g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, -28800);
2120 g_assert (g_date_time_is_daylight_savings (gdt2));
2121 g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, -23535);
2122 g_date_time_unref (gdt1);
2123 g_date_time_unref (gdt2);
2124 #else
2125 g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "UTC");
2126 g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 0);
2127 g_assert (!g_time_zone_is_dst (tz, 0));
2128 #endif
2129 g_time_zone_unref (tz);
2131 tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,M10.1.0,M3.3.0");
2132 g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
2133 g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
2134 g_assert (!g_time_zone_is_dst (tz, 0));
2135 g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
2136 g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
2137 g_assert (g_time_zone_is_dst (tz, 1));
2138 gdt1 = g_date_time_new (tz, 2012, 3, 18, 0, 15, 23.0);
2139 gdt2 = g_date_time_new (tz, 2012, 3, 18, 3, 15, 23.0);
2140 g_assert (g_date_time_is_daylight_savings (gdt1));
2141 g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
2142 g_assert (!g_date_time_is_daylight_savings (gdt2));
2143 g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2144 g_date_time_unref (gdt1);
2145 g_date_time_unref (gdt2);
2146 gdt1 = g_date_time_new (tz, 2012, 10, 7, 3, 15, 23.0);
2147 gdt2 = g_date_time_new (tz, 2012, 10, 7, 1, 15, 23.0);
2148 g_assert (g_date_time_is_daylight_savings (gdt1));
2149 g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
2150 g_assert (!g_date_time_is_daylight_savings (gdt2));
2151 g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2152 g_date_time_unref (gdt1);
2153 g_date_time_unref (gdt2);
2154 g_time_zone_unref (tz);
2156 tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,280,77");
2157 g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
2158 g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
2159 g_assert (!g_time_zone_is_dst (tz, 0));
2160 g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
2161 g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
2162 g_assert (g_time_zone_is_dst (tz, 1));
2163 gdt1 = g_date_time_new (tz, 2012, 3, 18, 0, 15, 23.0);
2164 gdt2 = g_date_time_new (tz, 2012, 3, 18, 3, 15, 23.0);
2165 g_assert (g_date_time_is_daylight_savings (gdt1));
2166 g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
2167 g_assert (!g_date_time_is_daylight_savings (gdt2));
2168 g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2169 g_date_time_unref (gdt1);
2170 g_date_time_unref (gdt2);
2171 gdt1 = g_date_time_new (tz, 2012, 10, 7, 3, 15, 23.0);
2172 gdt2 = g_date_time_new (tz, 2012, 10, 7, 1, 15, 23.0);
2173 g_assert (g_date_time_is_daylight_savings (gdt1));
2174 g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
2175 g_assert (!g_date_time_is_daylight_savings (gdt2));
2176 g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2177 g_date_time_unref (gdt1);
2178 g_date_time_unref (gdt2);
2179 g_time_zone_unref (tz);
2181 tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,J279,J76");
2182 g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
2183 g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
2184 g_assert (!g_time_zone_is_dst (tz, 0));
2185 g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
2186 g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
2187 g_assert (g_time_zone_is_dst (tz, 1));
2188 gdt1 = g_date_time_new (tz, 2012, 3, 18, 0, 15, 23.0);
2189 gdt2 = g_date_time_new (tz, 2012, 3, 18, 3, 15, 23.0);
2190 g_assert (g_date_time_is_daylight_savings (gdt1));
2191 g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
2192 g_assert (!g_date_time_is_daylight_savings (gdt2));
2193 g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2194 g_date_time_unref (gdt1);
2195 g_date_time_unref (gdt2);
2196 gdt1 = g_date_time_new (tz, 2012, 10, 7, 3, 15, 23.0);
2197 gdt2 = g_date_time_new (tz, 2012, 10, 7, 1, 15, 23.0);
2198 g_assert (g_date_time_is_daylight_savings (gdt1));
2199 g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
2200 g_assert (!g_date_time_is_daylight_savings (gdt2));
2201 g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2202 g_date_time_unref (gdt1);
2203 g_date_time_unref (gdt2);
2204 g_time_zone_unref (tz);
2206 tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,M10.1.0/07:00,M3.3.0/07:00");
2207 g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
2208 g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
2209 g_assert (!g_time_zone_is_dst (tz, 0));
2210 g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
2211 g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
2212 g_assert (g_time_zone_is_dst (tz, 1));
2213 gdt1 = g_date_time_new (tz, 2012, 3, 18, 5, 15, 23.0);
2214 gdt2 = g_date_time_new (tz, 2012, 3, 18, 8, 15, 23.0);
2215 g_assert (g_date_time_is_daylight_savings (gdt1));
2216 g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
2217 g_assert (!g_date_time_is_daylight_savings (gdt2));
2218 g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2219 g_date_time_unref (gdt1);
2220 g_date_time_unref (gdt2);
2221 gdt1 = g_date_time_new (tz, 2012, 10, 7, 8, 15, 23.0);
2222 gdt2 = g_date_time_new (tz, 2012, 10, 7, 6, 15, 23.0);
2223 g_assert (g_date_time_is_daylight_savings (gdt1));
2224 g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
2225 g_assert (!g_date_time_is_daylight_savings (gdt2));
2226 g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2227 g_date_time_unref (gdt1);
2228 g_date_time_unref (gdt2);
2229 gdt1 = g_date_time_new (tz, 1902, 10, 7, 8, 15, 23.0);
2230 gdt2 = g_date_time_new (tz, 1902, 10, 7, 6, 15, 23.0);
2231 g_assert (!g_date_time_is_daylight_savings (gdt1));
2232 g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 43200);
2233 g_assert (!g_date_time_is_daylight_savings (gdt2));
2234 g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2235 g_date_time_unref (gdt1);
2236 g_date_time_unref (gdt2);
2237 gdt1 = g_date_time_new (tz, 2142, 10, 7, 8, 15, 23.0);
2238 gdt2 = g_date_time_new (tz, 2142, 10, 7, 6, 15, 23.0);
2239 g_assert (g_date_time_is_daylight_savings (gdt1));
2240 g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
2241 g_assert (!g_date_time_is_daylight_savings (gdt2));
2242 g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2243 g_date_time_unref (gdt1);
2244 g_date_time_unref (gdt2);
2245 gdt1 = g_date_time_new (tz, 3212, 10, 7, 8, 15, 23.0);
2246 gdt2 = g_date_time_new (tz, 3212, 10, 7, 6, 15, 23.0);
2247 g_assert (!g_date_time_is_daylight_savings (gdt1));
2248 g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 43200);
2249 g_assert (!g_date_time_is_daylight_savings (gdt2));
2250 g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
2251 g_date_time_unref (gdt1);
2252 g_date_time_unref (gdt2);
2253 g_time_zone_unref (tz);
2256 static void
2257 test_GDateTime_floating_point (void)
2259 GDateTime *dt;
2260 GTimeZone *tz;
2262 g_test_bug ("697715");
2264 tz = g_time_zone_new ("-03:00");
2265 dt = g_date_time_new (tz, 2010, 5, 24, 8, 0, 1.000001);
2266 g_time_zone_unref (tz);
2267 g_assert_cmpint (g_date_time_get_microsecond (dt), ==, 1);
2268 g_date_time_unref (dt);
2271 gint
2272 main (gint argc,
2273 gchar *argv[])
2275 g_test_init (&argc, &argv, NULL);
2276 g_test_bug_base ("http://bugzilla.gnome.org/");
2278 /* GDateTime Tests */
2280 g_test_add_func ("/GDateTime/invalid", test_GDateTime_invalid);
2281 g_test_add_func ("/GDateTime/add_days", test_GDateTime_add_days);
2282 g_test_add_func ("/GDateTime/add_full", test_GDateTime_add_full);
2283 g_test_add_func ("/GDateTime/add_hours", test_GDateTime_add_hours);
2284 g_test_add_func ("/GDateTime/add_minutes", test_GDateTime_add_minutes);
2285 g_test_add_func ("/GDateTime/add_months", test_GDateTime_add_months);
2286 g_test_add_func ("/GDateTime/add_seconds", test_GDateTime_add_seconds);
2287 g_test_add_func ("/GDateTime/add_weeks", test_GDateTime_add_weeks);
2288 g_test_add_func ("/GDateTime/add_years", test_GDateTime_add_years);
2289 g_test_add_func ("/GDateTime/compare", test_GDateTime_compare);
2290 g_test_add_func ("/GDateTime/diff", test_GDateTime_diff);
2291 g_test_add_func ("/GDateTime/equal", test_GDateTime_equal);
2292 g_test_add_func ("/GDateTime/get_day_of_week", test_GDateTime_get_day_of_week);
2293 g_test_add_func ("/GDateTime/get_day_of_month", test_GDateTime_get_day_of_month);
2294 g_test_add_func ("/GDateTime/get_day_of_year", test_GDateTime_get_day_of_year);
2295 g_test_add_func ("/GDateTime/get_hour", test_GDateTime_get_hour);
2296 g_test_add_func ("/GDateTime/get_microsecond", test_GDateTime_get_microsecond);
2297 g_test_add_func ("/GDateTime/get_minute", test_GDateTime_get_minute);
2298 g_test_add_func ("/GDateTime/get_month", test_GDateTime_get_month);
2299 g_test_add_func ("/GDateTime/get_second", test_GDateTime_get_second);
2300 g_test_add_func ("/GDateTime/get_utc_offset", test_GDateTime_get_utc_offset);
2301 g_test_add_func ("/GDateTime/get_year", test_GDateTime_get_year);
2302 g_test_add_func ("/GDateTime/hash", test_GDateTime_hash);
2303 g_test_add_func ("/GDateTime/new_from_unix", test_GDateTime_new_from_unix);
2304 g_test_add_func ("/GDateTime/new_from_unix_utc", test_GDateTime_new_from_unix_utc);
2305 g_test_add_func ("/GDateTime/new_from_unix/overflow", test_GDateTime_new_from_unix_overflow);
2306 g_test_add_func ("/GDateTime/new_from_timeval", test_GDateTime_new_from_timeval);
2307 g_test_add_func ("/GDateTime/new_from_timeval_utc", test_GDateTime_new_from_timeval_utc);
2308 g_test_add_func ("/GDateTime/new_from_timeval/overflow", test_GDateTime_new_from_timeval_overflow);
2309 g_test_add_func ("/GDateTime/new_from_iso8601", test_GDateTime_new_from_iso8601);
2310 g_test_add_func ("/GDateTime/new_full", test_GDateTime_new_full);
2311 g_test_add_func ("/GDateTime/now", test_GDateTime_now);
2312 g_test_add_func ("/GDateTime/printf", test_GDateTime_printf);
2313 g_test_add_func ("/GDateTime/non_utf8_printf", test_non_utf8_printf);
2314 g_test_add_func ("/GDateTime/strftime", test_strftime);
2315 g_test_add_func ("/GDateTime/strftime/error_handling", test_GDateTime_strftime_error_handling);
2316 g_test_add_func ("/GDateTime/modifiers", test_modifiers);
2317 g_test_add_func ("/GDateTime/month_names", test_month_names);
2318 g_test_add_func ("/GDateTime/to_local", test_GDateTime_to_local);
2319 g_test_add_func ("/GDateTime/to_unix", test_GDateTime_to_unix);
2320 g_test_add_func ("/GDateTime/to_timeval", test_GDateTime_to_timeval);
2321 g_test_add_func ("/GDateTime/to_utc", test_GDateTime_to_utc);
2322 g_test_add_func ("/GDateTime/now_utc", test_GDateTime_now_utc);
2323 g_test_add_func ("/GDateTime/dst", test_GDateTime_dst);
2324 g_test_add_func ("/GDateTime/test_z", test_z);
2325 g_test_add_func ("/GDateTime/test-all-dates", test_all_dates);
2326 g_test_add_func ("/GTimeZone/find-interval", test_find_interval);
2327 g_test_add_func ("/GTimeZone/adjust-time", test_adjust_time);
2328 g_test_add_func ("/GTimeZone/no-header", test_no_header);
2329 g_test_add_func ("/GTimeZone/posix-parse", test_posix_parse);
2330 g_test_add_func ("/GTimeZone/floating-point", test_GDateTime_floating_point);
2332 return g_test_run ();