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 get_localtime_tm (t
, &tm
);
117 dt
= g_date_time_new_from_unix_local (t
);
118 g_assert_cmpint (g_date_time_get_year (dt
), ==, 1900 + tm
.tm_year
);
119 g_assert_cmpint (g_date_time_get_month (dt
), ==, 1 + tm
.tm_mon
);
120 g_assert_cmpint (g_date_time_get_day_of_month (dt
), ==, tm
.tm_mday
);
121 g_assert_cmpint (g_date_time_get_hour (dt
), ==, tm
.tm_hour
);
122 g_assert_cmpint (g_date_time_get_minute (dt
), ==, tm
.tm_min
);
123 g_assert_cmpint (g_date_time_get_second (dt
), ==, tm
.tm_sec
);
124 g_date_time_unref (dt
);
126 /* Choose 1990-01-01 04:00:00 because no DST leaps happened then. The more
127 * obvious 1990-01-01 00:00:00 was a DST leap in America/Lima (which has,
128 * confusingly, since stopped using DST). */
129 memset (&tm
, 0, sizeof (tm
));
139 dt
= g_date_time_new_from_unix_local (t
);
140 g_assert_cmpint (g_date_time_get_year (dt
), ==, 1990);
141 g_assert_cmpint (g_date_time_get_month (dt
), ==, 1);
142 g_assert_cmpint (g_date_time_get_day_of_month (dt
), ==, 1);
143 g_assert_cmpint (g_date_time_get_hour (dt
), ==, 4);
144 g_assert_cmpint (g_date_time_get_minute (dt
), ==, 0);
145 g_assert_cmpint (g_date_time_get_second (dt
), ==, 0);
146 g_date_time_unref (dt
);
149 /* Check that trying to create a #GDateTime too far in the future reliably
150 * fails. Previously, the checks for this overflowed and it silently returned
151 * an incorrect #GDateTime. */
153 test_GDateTime_new_from_unix_overflow (void)
157 g_test_bug ("782089");
159 dt
= g_date_time_new_from_unix_utc (G_MAXINT64
);
162 dt
= g_date_time_new_from_unix_local (G_MAXINT64
);
167 test_GDateTime_invalid (void)
171 g_test_bug ("702674");
173 dt
= g_date_time_new_utc (2013, -2147483647, 31, 17, 15, 48);
174 g_assert (dt
== NULL
);
178 test_GDateTime_compare (void)
180 GDateTime
*dt1
, *dt2
;
183 dt1
= g_date_time_new_utc (2000, 1, 1, 0, 0, 0);
185 for (i
= 1; i
< 2000; i
++)
187 dt2
= g_date_time_new_utc (i
, 12, 31, 0, 0, 0);
188 g_assert_cmpint (1, ==, g_date_time_compare (dt1
, dt2
));
189 g_date_time_unref (dt2
);
192 dt2
= g_date_time_new_utc (1999, 12, 31, 23, 59, 59);
193 g_assert_cmpint (1, ==, g_date_time_compare (dt1
, dt2
));
194 g_date_time_unref (dt2
);
196 dt2
= g_date_time_new_utc (2000, 1, 1, 0, 0, 1);
197 g_assert_cmpint (-1, ==, g_date_time_compare (dt1
, dt2
));
198 g_date_time_unref (dt2
);
200 dt2
= g_date_time_new_utc (2000, 1, 1, 0, 0, 0);
201 g_assert_cmpint (0, ==, g_date_time_compare (dt1
, dt2
));
202 g_date_time_unref (dt2
);
203 g_date_time_unref (dt1
);
207 test_GDateTime_equal (void)
209 GDateTime
*dt1
, *dt2
;
212 dt1
= g_date_time_new_local (2009, 10, 19, 0, 0, 0);
213 dt2
= g_date_time_new_local (2009, 10, 19, 0, 0, 0);
214 g_assert (g_date_time_equal (dt1
, dt2
));
215 g_date_time_unref (dt1
);
216 g_date_time_unref (dt2
);
218 dt1
= g_date_time_new_local (2009, 10, 18, 0, 0, 0);
219 dt2
= g_date_time_new_local (2009, 10, 19, 0, 0, 0);
220 g_assert (!g_date_time_equal (dt1
, dt2
));
221 g_date_time_unref (dt1
);
222 g_date_time_unref (dt2
);
224 /* UTC-0300 and not in DST */
225 tz
= g_time_zone_new ("-03:00");
226 dt1
= g_date_time_new (tz
, 2010, 5, 24, 8, 0, 0);
227 g_time_zone_unref (tz
);
228 g_assert_cmpint (g_date_time_get_utc_offset (dt1
) / G_USEC_PER_SEC
, ==, (-3 * 3600));
230 dt2
= g_date_time_new_utc (2010, 5, 24, 11, 0, 0);
231 g_assert_cmpint (g_date_time_get_utc_offset (dt2
), ==, 0);
233 g_assert (g_date_time_equal (dt1
, dt2
));
234 g_date_time_unref (dt1
);
236 /* America/Recife is in UTC-0300 */
238 tz
= g_time_zone_new ("America/Recife");
239 #elif defined G_OS_WIN32
240 tz
= g_time_zone_new ("E. South America Standard Time");
242 dt1
= g_date_time_new (tz
, 2010, 5, 24, 8, 0, 0);
243 g_time_zone_unref (tz
);
244 g_assert_cmpint (g_date_time_get_utc_offset (dt1
) / G_USEC_PER_SEC
, ==, (-3 * 3600));
245 g_assert (g_date_time_equal (dt1
, dt2
));
246 g_date_time_unref (dt1
);
247 g_date_time_unref (dt2
);
251 test_GDateTime_get_day_of_week (void)
255 dt
= g_date_time_new_local (2009, 10, 19, 0, 0, 0);
256 g_assert_cmpint (1, ==, g_date_time_get_day_of_week (dt
));
257 g_date_time_unref (dt
);
259 dt
= g_date_time_new_local (2000, 10, 1, 0, 0, 0);
260 g_assert_cmpint (7, ==, g_date_time_get_day_of_week (dt
));
261 g_date_time_unref (dt
);
265 test_GDateTime_get_day_of_month (void)
269 dt
= g_date_time_new_local (2009, 10, 19, 0, 0, 0);
270 g_assert_cmpint (g_date_time_get_day_of_month (dt
), ==, 19);
271 g_date_time_unref (dt
);
273 dt
= g_date_time_new_local (1400, 3, 12, 0, 0, 0);
274 g_assert_cmpint (g_date_time_get_day_of_month (dt
), ==, 12);
275 g_date_time_unref (dt
);
277 dt
= g_date_time_new_local (1800, 12, 31, 0, 0, 0);
278 g_assert_cmpint (g_date_time_get_day_of_month (dt
), ==, 31);
279 g_date_time_unref (dt
);
281 dt
= g_date_time_new_local (1000, 1, 1, 0, 0, 0);
282 g_assert_cmpint (g_date_time_get_day_of_month (dt
), ==, 1);
283 g_date_time_unref (dt
);
287 test_GDateTime_get_hour (void)
291 dt
= g_date_time_new_utc (2009, 10, 19, 15, 13, 11);
292 g_assert_cmpint (15, ==, g_date_time_get_hour (dt
));
293 g_date_time_unref (dt
);
295 dt
= g_date_time_new_utc (100, 10, 19, 1, 0, 0);
296 g_assert_cmpint (1, ==, g_date_time_get_hour (dt
));
297 g_date_time_unref (dt
);
299 dt
= g_date_time_new_utc (100, 10, 19, 0, 0, 0);
300 g_assert_cmpint (0, ==, g_date_time_get_hour (dt
));
301 g_date_time_unref (dt
);
303 dt
= g_date_time_new_utc (100, 10, 1, 23, 59, 59);
304 g_assert_cmpint (23, ==, g_date_time_get_hour (dt
));
305 g_date_time_unref (dt
);
309 test_GDateTime_get_microsecond (void)
314 g_get_current_time (&tv
);
315 dt
= g_date_time_new_from_timeval_local (&tv
);
316 g_assert_cmpint (tv
.tv_usec
, ==, g_date_time_get_microsecond (dt
));
317 g_date_time_unref (dt
);
321 test_GDateTime_get_year (void)
325 dt
= g_date_time_new_local (2009, 1, 1, 0, 0, 0);
326 g_assert_cmpint (2009, ==, g_date_time_get_year (dt
));
327 g_date_time_unref (dt
);
329 dt
= g_date_time_new_local (1, 1, 1, 0, 0, 0);
330 g_assert_cmpint (1, ==, g_date_time_get_year (dt
));
331 g_date_time_unref (dt
);
333 dt
= g_date_time_new_local (13, 1, 1, 0, 0, 0);
334 g_assert_cmpint (13, ==, g_date_time_get_year (dt
));
335 g_date_time_unref (dt
);
337 dt
= g_date_time_new_local (3000, 1, 1, 0, 0, 0);
338 g_assert_cmpint (3000, ==, g_date_time_get_year (dt
));
339 g_date_time_unref (dt
);
343 test_GDateTime_hash (void)
347 h
= g_hash_table_new_full (g_date_time_hash
, g_date_time_equal
,
348 (GDestroyNotify
)g_date_time_unref
,
350 g_hash_table_add (h
, g_date_time_new_now_local ());
351 g_hash_table_remove_all (h
);
352 g_hash_table_destroy (h
);
356 test_GDateTime_new_from_timeval (void)
361 g_get_current_time (&tv
);
362 dt
= g_date_time_new_from_timeval_local (&tv
);
364 if (g_test_verbose ())
365 g_printerr ("\nDT%04d-%02d-%02dT%02d:%02d:%02d%s\n",
366 g_date_time_get_year (dt
),
367 g_date_time_get_month (dt
),
368 g_date_time_get_day_of_month (dt
),
369 g_date_time_get_hour (dt
),
370 g_date_time_get_minute (dt
),
371 g_date_time_get_second (dt
),
372 g_date_time_get_timezone_abbreviation (dt
));
374 g_date_time_to_timeval (dt
, &tv2
);
375 g_assert_cmpint (tv
.tv_sec
, ==, tv2
.tv_sec
);
376 g_assert_cmpint (tv
.tv_usec
, ==, tv2
.tv_usec
);
377 g_date_time_unref (dt
);
381 find_maximum_supported_tv_sec (void)
383 glong highest_success
= 0, lowest_failure
= G_MAXLONG
;
385 GDateTime
*dt
= NULL
;
389 /* Corner case of all glong values being valid. */
390 tv
.tv_sec
= G_MAXLONG
;
391 dt
= g_date_time_new_from_timeval_utc (&tv
);
394 highest_success
= tv
.tv_sec
;
395 g_date_time_unref (dt
);
398 while (highest_success
< lowest_failure
- 1)
400 tv
.tv_sec
= highest_success
+ (lowest_failure
- highest_success
) / 2;
401 dt
= g_date_time_new_from_timeval_utc (&tv
);
405 highest_success
= tv
.tv_sec
;
406 g_date_time_unref (dt
);
410 lowest_failure
= tv
.tv_sec
;
414 return highest_success
;
417 /* Check that trying to create a #GDateTime too far in the future reliably
418 * fails. With a #GTimeVal, this is subtle, as the tv_usec are added into the
419 * calculation part-way through.
421 * This varies a bit between 32- and 64-bit architectures, due to the
422 * differences in the size of glong (tv.tv_sec). */
424 test_GDateTime_new_from_timeval_overflow (void)
429 g_test_bug ("782089");
431 tv
.tv_sec
= find_maximum_supported_tv_sec ();
432 tv
.tv_usec
= G_USEC_PER_SEC
- 1;
434 g_test_message ("Maximum supported GTimeVal.tv_sec = %lu", tv
.tv_sec
);
436 /* Sanity check: do we support the year 2000? */
437 g_assert_cmpint (tv
.tv_sec
, >=, 946684800);
439 dt
= g_date_time_new_from_timeval_utc (&tv
);
440 g_assert_nonnull (dt
);
441 g_date_time_unref (dt
);
443 if (tv
.tv_sec
< G_MAXLONG
)
448 dt
= g_date_time_new_from_timeval_utc (&tv
);
454 test_GDateTime_new_from_timeval_utc (void)
459 g_get_current_time (&tv
);
460 dt
= g_date_time_new_from_timeval_utc (&tv
);
462 if (g_test_verbose ())
463 g_printerr ("\nDT%04d-%02d-%02dT%02d:%02d:%02d%s\n",
464 g_date_time_get_year (dt
),
465 g_date_time_get_month (dt
),
466 g_date_time_get_day_of_month (dt
),
467 g_date_time_get_hour (dt
),
468 g_date_time_get_minute (dt
),
469 g_date_time_get_second (dt
),
470 g_date_time_get_timezone_abbreviation (dt
));
472 g_date_time_to_timeval (dt
, &tv2
);
473 g_assert_cmpint (tv
.tv_sec
, ==, tv2
.tv_sec
);
474 g_assert_cmpint (tv
.tv_usec
, ==, tv2
.tv_usec
);
475 g_date_time_unref (dt
);
479 test_GDateTime_new_from_iso8601 (void)
484 /* Need non-empty string */
485 dt
= g_date_time_new_from_iso8601 ("", NULL
);
488 /* Needs to be correctly formatted */
489 dt
= g_date_time_new_from_iso8601 ("not a date", NULL
);
492 /* Check common case */
493 dt
= g_date_time_new_from_iso8601 ("2016-08-24T22:10:42Z", NULL
);
494 ASSERT_DATE (dt
, 2016, 8, 24);
495 ASSERT_TIME (dt
, 22, 10, 42, 0);
496 g_date_time_unref (dt
);
498 /* Timezone is required in text or passed as arg */
499 tz
= g_time_zone_new_utc ();
500 dt
= g_date_time_new_from_iso8601 ("2016-08-24T22:10:42", tz
);
501 ASSERT_DATE (dt
, 2016, 8, 24);
502 g_date_time_unref (dt
);
503 g_time_zone_unref (tz
);
504 dt
= g_date_time_new_from_iso8601 ("2016-08-24T22:10:42", NULL
);
507 /* Can't have whitespace */
508 dt
= g_date_time_new_from_iso8601 ("2016 08 24T22:10:42Z", NULL
);
510 dt
= g_date_time_new_from_iso8601 ("2016-08-24T22:10:42Z ", NULL
);
512 dt
= g_date_time_new_from_iso8601 (" 2016-08-24T22:10:42Z", NULL
);
515 /* Check lowercase time separator or space allowed */
516 dt
= g_date_time_new_from_iso8601 ("2016-08-24t22:10:42Z", NULL
);
517 ASSERT_DATE (dt
, 2016, 8, 24);
518 ASSERT_TIME (dt
, 22, 10, 42, 0);
519 g_date_time_unref (dt
);
520 dt
= g_date_time_new_from_iso8601 ("2016-08-24 22:10:42Z", NULL
);
521 ASSERT_DATE (dt
, 2016, 8, 24);
522 ASSERT_TIME (dt
, 22, 10, 42, 0);
523 g_date_time_unref (dt
);
525 /* Check dates without separators allowed */
526 dt
= g_date_time_new_from_iso8601 ("20160824T22:10:42Z", NULL
);
527 ASSERT_DATE (dt
, 2016, 8, 24);
528 ASSERT_TIME (dt
, 22, 10, 42, 0);
529 g_date_time_unref (dt
);
531 /* Months are two digits */
532 dt
= g_date_time_new_from_iso8601 ("2016-1-01T22:10:42Z", NULL
);
535 /* Days are two digits */
536 dt
= g_date_time_new_from_iso8601 ("2016-01-1T22:10:42Z", NULL
);
539 /* Need consistent usage of separators */
540 dt
= g_date_time_new_from_iso8601 ("2016-0824T22:10:42Z", NULL
);
542 dt
= g_date_time_new_from_iso8601 ("201608-24T22:10:42Z", NULL
);
545 /* Check month within valid range */
546 dt
= g_date_time_new_from_iso8601 ("2016-00-13T22:10:42Z", NULL
);
548 dt
= g_date_time_new_from_iso8601 ("2016-13-13T22:10:42Z", NULL
);
551 /* Check day within valid range */
552 dt
= g_date_time_new_from_iso8601 ("2016-01-00T22:10:42Z", NULL
);
554 dt
= g_date_time_new_from_iso8601 ("2016-01-31T22:10:42Z", NULL
);
555 ASSERT_DATE (dt
, 2016, 1, 31);
556 g_date_time_unref (dt
);
557 dt
= g_date_time_new_from_iso8601 ("2016-01-32T22:10:42Z", NULL
);
559 dt
= g_date_time_new_from_iso8601 ("2016-02-29T22:10:42Z", NULL
);
560 ASSERT_DATE (dt
, 2016, 2, 29);
561 g_date_time_unref (dt
);
562 dt
= g_date_time_new_from_iso8601 ("2016-02-30T22:10:42Z", NULL
);
564 dt
= g_date_time_new_from_iso8601 ("2017-02-28T22:10:42Z", NULL
);
565 ASSERT_DATE (dt
, 2017, 2, 28);
566 g_date_time_unref (dt
);
567 dt
= g_date_time_new_from_iso8601 ("2017-02-29T22:10:42Z", NULL
);
569 dt
= g_date_time_new_from_iso8601 ("2016-03-31T22:10:42Z", NULL
);
570 ASSERT_DATE (dt
, 2016, 3, 31);
571 g_date_time_unref (dt
);
572 dt
= g_date_time_new_from_iso8601 ("2016-03-32T22:10:42Z", NULL
);
574 dt
= g_date_time_new_from_iso8601 ("2016-04-30T22:10:42Z", NULL
);
575 ASSERT_DATE (dt
, 2016, 4, 30);
576 g_date_time_unref (dt
);
577 dt
= g_date_time_new_from_iso8601 ("2016-04-31T22:10:42Z", NULL
);
579 dt
= g_date_time_new_from_iso8601 ("2016-05-31T22:10:42Z", NULL
);
580 ASSERT_DATE (dt
, 2016, 5, 31);
581 g_date_time_unref (dt
);
582 dt
= g_date_time_new_from_iso8601 ("2016-05-32T22:10:42Z", NULL
);
584 dt
= g_date_time_new_from_iso8601 ("2016-06-30T22:10:42Z", NULL
);
585 ASSERT_DATE (dt
, 2016, 6, 30);
586 g_date_time_unref (dt
);
587 dt
= g_date_time_new_from_iso8601 ("2016-06-31T22:10:42Z", NULL
);
589 dt
= g_date_time_new_from_iso8601 ("2016-07-31T22:10:42Z", NULL
);
590 ASSERT_DATE (dt
, 2016, 7, 31);
591 g_date_time_unref (dt
);
592 dt
= g_date_time_new_from_iso8601 ("2016-07-32T22:10:42Z", NULL
);
594 dt
= g_date_time_new_from_iso8601 ("2016-08-31T22:10:42Z", NULL
);
595 ASSERT_DATE (dt
, 2016, 8, 31);
596 g_date_time_unref (dt
);
597 dt
= g_date_time_new_from_iso8601 ("2016-08-32T22:10:42Z", NULL
);
599 dt
= g_date_time_new_from_iso8601 ("2016-09-30T22:10:42Z", NULL
);
600 ASSERT_DATE (dt
, 2016, 9, 30);
601 g_date_time_unref (dt
);
602 dt
= g_date_time_new_from_iso8601 ("2016-09-31T22:10:42Z", NULL
);
604 dt
= g_date_time_new_from_iso8601 ("2016-10-31T22:10:42Z", NULL
);
605 ASSERT_DATE (dt
, 2016, 10, 31);
606 g_date_time_unref (dt
);
607 dt
= g_date_time_new_from_iso8601 ("2016-10-32T22:10:42Z", NULL
);
609 dt
= g_date_time_new_from_iso8601 ("2016-11-30T22:10:42Z", NULL
);
610 ASSERT_DATE (dt
, 2016, 11, 30);
611 g_date_time_unref (dt
);
612 dt
= g_date_time_new_from_iso8601 ("2016-11-31T22:10:42Z", NULL
);
614 dt
= g_date_time_new_from_iso8601 ("2016-12-31T22:10:42Z", NULL
);
615 ASSERT_DATE (dt
, 2016, 12, 31);
616 g_date_time_unref (dt
);
617 dt
= g_date_time_new_from_iso8601 ("2016-12-32T22:10:42Z", NULL
);
620 /* Check ordinal days work */
621 dt
= g_date_time_new_from_iso8601 ("2016-237T22:10:42Z", NULL
);
622 ASSERT_DATE (dt
, 2016, 8, 24);
623 ASSERT_TIME (dt
, 22, 10, 42, 0);
624 g_date_time_unref (dt
);
625 dt
= g_date_time_new_from_iso8601 ("2016237T22:10:42Z", NULL
);
626 ASSERT_DATE (dt
, 2016, 8, 24);
627 ASSERT_TIME (dt
, 22, 10, 42, 0);
628 g_date_time_unref (dt
);
630 /* Check ordinal leap days */
631 dt
= g_date_time_new_from_iso8601 ("2016-366T22:10:42Z", NULL
);
632 ASSERT_DATE (dt
, 2016, 12, 31);
633 ASSERT_TIME (dt
, 22, 10, 42, 0);
634 g_date_time_unref (dt
);
635 dt
= g_date_time_new_from_iso8601 ("2017-365T22:10:42Z", NULL
);
636 ASSERT_DATE (dt
, 2017, 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-366T22:10:42Z", NULL
);
642 /* Days start at 1 */
643 dt
= g_date_time_new_from_iso8601 ("2016-000T22:10:42Z", NULL
);
646 /* Limited to number of days in the year (2016 is a leap year) */
647 dt
= g_date_time_new_from_iso8601 ("2016-367T22:10:42Z", NULL
);
650 /* Days are two digits */
651 dt
= g_date_time_new_from_iso8601 ("2016-1T22:10:42Z", NULL
);
653 dt
= g_date_time_new_from_iso8601 ("2016-12T22:10:42Z", NULL
);
656 /* Check week days work */
657 dt
= g_date_time_new_from_iso8601 ("2016-W34-3T22:10:42Z", NULL
);
658 ASSERT_DATE (dt
, 2016, 8, 24);
659 ASSERT_TIME (dt
, 22, 10, 42, 0);
660 g_date_time_unref (dt
);
661 dt
= g_date_time_new_from_iso8601 ("2016W343T22:10:42Z", NULL
);
662 ASSERT_DATE (dt
, 2016, 8, 24);
663 ASSERT_TIME (dt
, 22, 10, 42, 0);
664 g_date_time_unref (dt
);
666 /* We don't support weeks without weekdays (valid ISO 8601) */
667 dt
= g_date_time_new_from_iso8601 ("2016-W34T22:10:42Z", NULL
);
669 dt
= g_date_time_new_from_iso8601 ("2016W34T22:10:42Z", NULL
);
672 /* Weeks are two digits */
673 dt
= g_date_time_new_from_iso8601 ("2016-W3-1T22:10:42Z", NULL
);
676 /* Weeks start at 1 */
677 dt
= g_date_time_new_from_iso8601 ("2016-W00-1T22:10:42Z", NULL
);
680 /* Week one might be in the previous year */
681 dt
= g_date_time_new_from_iso8601 ("2015-W01-1T22:10:42Z", NULL
);
682 ASSERT_DATE (dt
, 2014, 12, 29);
683 g_date_time_unref (dt
);
685 /* Last week might be in next year */
686 dt
= g_date_time_new_from_iso8601 ("2015-W53-7T22:10:42Z", NULL
);
687 ASSERT_DATE (dt
, 2016, 1, 3);
688 g_date_time_unref (dt
);
690 /* Week 53 doesn't always exist */
691 dt
= g_date_time_new_from_iso8601 ("2016-W53-1T22:10:42Z", NULL
);
694 /* Limited to number of days in the week */
695 dt
= g_date_time_new_from_iso8601 ("2016-W34-0T22:10:42Z", NULL
);
697 dt
= g_date_time_new_from_iso8601 ("2016-W34-8T22:10:42Z", NULL
);
700 /* Days are one digit */
701 dt
= g_date_time_new_from_iso8601 ("2016-W34-99T22:10:42Z", NULL
);
704 /* Check week day changes depending on year */
705 dt
= g_date_time_new_from_iso8601 ("2017-W34-1T22:10:42Z", NULL
);
706 ASSERT_DATE (dt
, 2017, 8, 21);
707 g_date_time_unref (dt
);
709 /* Check week day changes depending on leap years */
710 dt
= g_date_time_new_from_iso8601 ("1900-W01-1T22:10:42Z", NULL
);
711 ASSERT_DATE (dt
, 1900, 1, 1);
712 g_date_time_unref (dt
);
714 /* YYYY-MM not allowed (NOT valid ISO 8601) */
715 dt
= g_date_time_new_from_iso8601 ("2016-08T22:10:42Z", NULL
);
718 /* We don't support omitted year (valid ISO 8601) */
719 dt
= g_date_time_new_from_iso8601 ("--08-24T22:10:42Z", NULL
);
721 dt
= g_date_time_new_from_iso8601 ("--0824T22:10:42Z", NULL
);
724 /* Check subseconds work */
725 dt
= g_date_time_new_from_iso8601 ("2016-08-24T22:10:42.123456Z", NULL
);
726 ASSERT_DATE (dt
, 2016, 8, 24);
727 ASSERT_TIME (dt
, 22, 10, 42, 123456);
728 g_date_time_unref (dt
);
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.Z", NULL
);
735 dt
= g_date_time_new_from_iso8601 ("2016-08-24T221042.123456Z", NULL
);
736 ASSERT_DATE (dt
, 2016, 8, 24);
737 ASSERT_TIME (dt
, 22, 10, 42, 123456);
738 g_date_time_unref (dt
);
740 /* We don't support times without minutes / seconds (valid ISO 8601) */
741 dt
= g_date_time_new_from_iso8601 ("2016-08-24T22Z", NULL
);
743 dt
= g_date_time_new_from_iso8601 ("2016-08-24T22:10Z", NULL
);
745 dt
= g_date_time_new_from_iso8601 ("2016-08-24T2210Z", NULL
);
748 /* UTC time uses 'Z' */
749 dt
= g_date_time_new_from_iso8601 ("2016-08-24T22:10:42Z", NULL
);
750 ASSERT_DATE (dt
, 2016, 8, 24);
751 ASSERT_TIME (dt
, 22, 10, 42, 0);
752 g_assert_cmpint (g_date_time_get_utc_offset (dt
), ==, 0);
753 g_date_time_unref (dt
);
755 /* Check timezone works */
756 dt
= g_date_time_new_from_iso8601 ("2016-08-24T22:10:42+12:00", NULL
);
757 ASSERT_DATE (dt
, 2016, 8, 24);
758 ASSERT_TIME (dt
, 22, 10, 42, 0);
759 g_assert_cmpint (g_date_time_get_utc_offset (dt
), ==, 12 * G_TIME_SPAN_HOUR
);
760 g_date_time_unref (dt
);
761 dt
= g_date_time_new_from_iso8601 ("2016-08-24T22:10:42+12", NULL
);
762 ASSERT_DATE (dt
, 2016, 8, 24);
763 ASSERT_TIME (dt
, 22, 10, 42, 0);
764 g_assert_cmpint (g_date_time_get_utc_offset (dt
), ==, 12 * G_TIME_SPAN_HOUR
);
765 g_date_time_unref (dt
);
766 dt
= g_date_time_new_from_iso8601 ("2016-08-24T22:10:42-02", NULL
);
767 ASSERT_DATE (dt
, 2016, 8, 24);
768 ASSERT_TIME (dt
, 22, 10, 42, 0);
769 g_assert_cmpint (g_date_time_get_utc_offset (dt
), ==, -2 * G_TIME_SPAN_HOUR
);
770 g_date_time_unref (dt
);
772 /* Timezone seconds not allowed */
773 dt
= g_date_time_new_from_iso8601 ("2016-08-24T22-12:00:00", NULL
);
775 dt
= g_date_time_new_from_iso8601 ("2016-08-24T22-12:00:00.000", NULL
);
778 /* Timezone hours two digits */
779 dt
= g_date_time_new_from_iso8601 ("2016-08-24T22-2Z", NULL
);
784 test_GDateTime_to_unix (void)
790 dt
= g_date_time_new_from_unix_local (t
);
791 g_assert_cmpint (g_date_time_to_unix (dt
), ==, t
);
792 g_date_time_unref (dt
);
796 test_GDateTime_add_years (void)
800 dt
= g_date_time_new_local (2009, 10, 19, 0, 0, 0);
801 dt2
= g_date_time_add_years (dt
, 1);
802 g_assert_cmpint (2010, ==, g_date_time_get_year (dt2
));
803 g_date_time_unref (dt
);
804 g_date_time_unref (dt2
);
808 test_GDateTime_add_months (void)
810 #define TEST_ADD_MONTHS(y,m,d,a,ny,nm,nd) G_STMT_START { \
811 GDateTime *dt, *dt2; \
812 dt = g_date_time_new_utc (y, m, d, 0, 0, 0); \
813 dt2 = g_date_time_add_months (dt, a); \
814 ASSERT_DATE (dt2, ny, nm, nd); \
815 g_date_time_unref (dt); \
816 g_date_time_unref (dt2); \
819 TEST_ADD_MONTHS (2009, 12, 31, 1, 2010, 1, 31);
820 TEST_ADD_MONTHS (2009, 12, 31, 1, 2010, 1, 31);
821 TEST_ADD_MONTHS (2009, 6, 15, 1, 2009, 7, 15);
822 TEST_ADD_MONTHS (1400, 3, 1, 1, 1400, 4, 1);
823 TEST_ADD_MONTHS (1400, 1, 31, 1, 1400, 2, 28);
824 TEST_ADD_MONTHS (1400, 1, 31, 7200, 2000, 1, 31);
825 TEST_ADD_MONTHS (2008, 2, 29, 12, 2009, 2, 28);
826 TEST_ADD_MONTHS (2000, 8, 16, -5, 2000, 3, 16);
827 TEST_ADD_MONTHS (2000, 8, 16, -12, 1999, 8, 16);
828 TEST_ADD_MONTHS (2011, 2, 1, -13, 2010, 1, 1);
829 TEST_ADD_MONTHS (1776, 7, 4, 1200, 1876, 7, 4);
833 test_GDateTime_add_days (void)
835 #define TEST_ADD_DAYS(y,m,d,a,ny,nm,nd) G_STMT_START { \
836 GDateTime *dt, *dt2; \
837 dt = g_date_time_new_local (y, m, d, 0, 0, 0); \
838 dt2 = g_date_time_add_days (dt, a); \
839 g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
840 g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
841 g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
842 g_date_time_unref (dt); \
843 g_date_time_unref (dt2); \
846 TEST_ADD_DAYS (2009, 1, 31, 1, 2009, 2, 1);
847 TEST_ADD_DAYS (2009, 2, 1, -1, 2009, 1, 31);
848 TEST_ADD_DAYS (2008, 2, 28, 1, 2008, 2, 29);
849 TEST_ADD_DAYS (2008, 12, 31, 1, 2009, 1, 1);
850 TEST_ADD_DAYS (1, 1, 1, 1, 1, 1, 2);
851 TEST_ADD_DAYS (1955, 5, 24, 10, 1955, 6, 3);
852 TEST_ADD_DAYS (1955, 5, 24, -10, 1955, 5, 14);
856 test_GDateTime_add_weeks (void)
858 #define TEST_ADD_WEEKS(y,m,d,a,ny,nm,nd) G_STMT_START { \
859 GDateTime *dt, *dt2; \
860 dt = g_date_time_new_local (y, m, d, 0, 0, 0); \
861 dt2 = g_date_time_add_weeks (dt, a); \
862 g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
863 g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
864 g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
865 g_date_time_unref (dt); \
866 g_date_time_unref (dt2); \
869 TEST_ADD_WEEKS (2009, 1, 1, 1, 2009, 1, 8);
870 TEST_ADD_WEEKS (2009, 8, 30, 1, 2009, 9, 6);
871 TEST_ADD_WEEKS (2009, 12, 31, 1, 2010, 1, 7);
872 TEST_ADD_WEEKS (2009, 1, 1, -1, 2008, 12, 25);
876 test_GDateTime_add_hours (void)
878 #define TEST_ADD_HOURS(y,m,d,h,mi,s,a,ny,nm,nd,nh,nmi,ns) G_STMT_START { \
879 GDateTime *dt, *dt2; \
880 dt = g_date_time_new_utc (y, m, d, h, mi, s); \
881 dt2 = g_date_time_add_hours (dt, a); \
882 g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
883 g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
884 g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
885 g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
886 g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
887 g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
888 g_date_time_unref (dt); \
889 g_date_time_unref (dt2); \
892 TEST_ADD_HOURS (2009, 1, 1, 0, 0, 0, 1, 2009, 1, 1, 1, 0, 0);
893 TEST_ADD_HOURS (2008, 12, 31, 23, 0, 0, 1, 2009, 1, 1, 0, 0, 0);
897 test_GDateTime_add_full (void)
899 #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 { \
900 GDateTime *dt, *dt2; \
901 dt = g_date_time_new_utc (y, m, d, h, mi, s); \
902 dt2 = g_date_time_add_full (dt, ay, am, ad, ah, ami, as); \
903 g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
904 g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
905 g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
906 g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
907 g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
908 g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
909 g_date_time_unref (dt); \
910 g_date_time_unref (dt2); \
913 TEST_ADD_FULL (2009, 10, 21, 0, 0, 0,
915 2010, 11, 22, 1, 1, 1);
916 TEST_ADD_FULL (2000, 1, 1, 1, 1, 1,
918 2000, 2, 1, 1, 1, 1);
919 TEST_ADD_FULL (2000, 1, 1, 0, 0, 0,
921 1999, 2, 1, 0, 0, 0);
922 TEST_ADD_FULL (2010, 10, 31, 0, 0, 0,
924 2011, 2, 28, 0, 0, 0);
925 TEST_ADD_FULL (2010, 8, 25, 22, 45, 0,
927 2010, 10, 2, 0, 10, 0);
931 test_GDateTime_add_minutes (void)
933 #define TEST_ADD_MINUTES(i,o) G_STMT_START { \
934 GDateTime *dt, *dt2; \
935 dt = g_date_time_new_local (2000, 1, 1, 0, 0, 0); \
936 dt2 = g_date_time_add_minutes (dt, i); \
937 g_assert_cmpint (o, ==, g_date_time_get_minute (dt2)); \
938 g_date_time_unref (dt); \
939 g_date_time_unref (dt2); \
942 TEST_ADD_MINUTES (60, 0);
943 TEST_ADD_MINUTES (100, 40);
944 TEST_ADD_MINUTES (5, 5);
945 TEST_ADD_MINUTES (1441, 1);
946 TEST_ADD_MINUTES (-1441, 59);
950 test_GDateTime_add_seconds (void)
952 #define TEST_ADD_SECONDS(i,o) G_STMT_START { \
953 GDateTime *dt, *dt2; \
954 dt = g_date_time_new_local (2000, 1, 1, 0, 0, 0); \
955 dt2 = g_date_time_add_seconds (dt, i); \
956 g_assert_cmpint (o, ==, g_date_time_get_second (dt2)); \
957 g_date_time_unref (dt); \
958 g_date_time_unref (dt2); \
961 TEST_ADD_SECONDS (1, 1);
962 TEST_ADD_SECONDS (60, 0);
963 TEST_ADD_SECONDS (61, 1);
964 TEST_ADD_SECONDS (120, 0);
965 TEST_ADD_SECONDS (-61, 59);
966 TEST_ADD_SECONDS (86401, 1);
967 TEST_ADD_SECONDS (-86401, 59);
968 TEST_ADD_SECONDS (-31, 29);
969 TEST_ADD_SECONDS (13, 13);
973 test_GDateTime_diff (void)
975 #define TEST_DIFF(y,m,d,y2,m2,d2,u) G_STMT_START { \
976 GDateTime *dt1, *dt2; \
978 dt1 = g_date_time_new_utc (y, m, d, 0, 0, 0); \
979 dt2 = g_date_time_new_utc (y2, m2, d2, 0, 0, 0); \
980 ts = g_date_time_difference (dt2, dt1); \
981 g_assert_cmpint (ts, ==, u); \
982 g_date_time_unref (dt1); \
983 g_date_time_unref (dt2); \
986 TEST_DIFF (2009, 1, 1, 2009, 2, 1, G_TIME_SPAN_DAY
* 31);
987 TEST_DIFF (2009, 1, 1, 2010, 1, 1, G_TIME_SPAN_DAY
* 365);
988 TEST_DIFF (2008, 2, 28, 2008, 2, 29, G_TIME_SPAN_DAY
);
989 TEST_DIFF (2008, 2, 29, 2008, 2, 28, -G_TIME_SPAN_DAY
);
991 /* TODO: Add usec tests */
995 test_GDateTime_get_minute (void)
999 dt
= g_date_time_new_utc (2009, 12, 1, 1, 31, 0);
1000 g_assert_cmpint (31, ==, g_date_time_get_minute (dt
));
1001 g_date_time_unref (dt
);
1005 test_GDateTime_get_month (void)
1009 dt
= g_date_time_new_utc (2009, 12, 1, 1, 31, 0);
1010 g_assert_cmpint (12, ==, g_date_time_get_month (dt
));
1011 g_date_time_unref (dt
);
1015 test_GDateTime_get_second (void)
1019 dt
= g_date_time_new_utc (2009, 12, 1, 1, 31, 44);
1020 g_assert_cmpint (44, ==, g_date_time_get_second (dt
));
1021 g_date_time_unref (dt
);
1025 test_GDateTime_new_full (void)
1027 GTimeZone
*tz
, *dt_tz
;
1030 dt
= g_date_time_new_utc (2009, 12, 11, 12, 11, 10);
1031 g_assert_cmpint (2009, ==, g_date_time_get_year (dt
));
1032 g_assert_cmpint (12, ==, g_date_time_get_month (dt
));
1033 g_assert_cmpint (11, ==, g_date_time_get_day_of_month (dt
));
1034 g_assert_cmpint (12, ==, g_date_time_get_hour (dt
));
1035 g_assert_cmpint (11, ==, g_date_time_get_minute (dt
));
1036 g_assert_cmpint (10, ==, g_date_time_get_second (dt
));
1037 g_date_time_unref (dt
);
1040 tz
= g_time_zone_new ("America/Tijuana");
1041 #elif defined G_OS_WIN32
1042 tz
= g_time_zone_new ("Pacific Standard Time");
1044 dt
= g_date_time_new (tz
, 2010, 11, 24, 8, 4, 0);
1046 dt_tz
= g_date_time_get_timezone (dt
);
1047 g_assert_cmpstr (g_time_zone_get_identifier (dt_tz
), ==,
1048 g_time_zone_get_identifier (tz
));
1050 g_assert_cmpint (2010, ==, g_date_time_get_year (dt
));
1051 g_assert_cmpint (11, ==, g_date_time_get_month (dt
));
1052 g_assert_cmpint (24, ==, g_date_time_get_day_of_month (dt
));
1053 g_assert_cmpint (8, ==, g_date_time_get_hour (dt
));
1054 g_assert_cmpint (4, ==, g_date_time_get_minute (dt
));
1055 g_assert_cmpint (0, ==, g_date_time_get_second (dt
));
1057 g_assert_cmpstr ("PST", ==, g_date_time_get_timezone_abbreviation (dt
));
1058 g_assert_cmpstr ("America/Tijuana", ==, g_time_zone_get_identifier (dt_tz
));
1059 #elif defined G_OS_WIN32
1060 g_assert_cmpstr ("Pacific Standard Time", ==,
1061 g_date_time_get_timezone_abbreviation (dt
));
1062 g_assert_cmpstr ("Pacific Standard Time", ==,
1063 g_time_zone_get_identifier (dt_tz
));
1065 g_assert (!g_date_time_is_daylight_savings (dt
));
1066 g_date_time_unref (dt
);
1067 g_time_zone_unref (tz
);
1069 /* Check month limits */
1070 dt
= g_date_time_new_utc (2016, 1, 31, 22, 10, 42);
1071 ASSERT_DATE (dt
, 2016, 1, 31);
1072 g_date_time_unref (dt
);
1073 dt
= g_date_time_new_utc (2016, 1, 32, 22, 10, 42);
1075 dt
= g_date_time_new_utc (2016, 2, 29, 22, 10, 42);
1076 ASSERT_DATE (dt
, 2016, 2, 29);
1077 g_date_time_unref (dt
);
1078 dt
= g_date_time_new_utc (2016, 2, 30, 22, 10, 42);
1080 dt
= g_date_time_new_utc (2017, 2, 28, 22, 10, 42);
1081 ASSERT_DATE (dt
, 2017, 2, 28);
1082 g_date_time_unref (dt
);
1083 dt
= g_date_time_new_utc (2017, 2, 29, 22, 10, 42);
1085 dt
= g_date_time_new_utc (2016, 3, 31, 22, 10, 42);
1086 ASSERT_DATE (dt
, 2016, 3, 31);
1087 g_date_time_unref (dt
);
1088 dt
= g_date_time_new_utc (2016, 3, 32, 22, 10, 42);
1090 dt
= g_date_time_new_utc (2016, 4, 30, 22, 10, 42);
1091 ASSERT_DATE (dt
, 2016, 4, 30);
1092 g_date_time_unref (dt
);
1093 dt
= g_date_time_new_utc (2016, 4, 31, 22, 10, 42);
1095 dt
= g_date_time_new_utc (2016, 5, 31, 22, 10, 42);
1096 ASSERT_DATE (dt
, 2016, 5, 31);
1097 g_date_time_unref (dt
);
1098 dt
= g_date_time_new_utc (2016, 5, 32, 22, 10, 42);
1100 dt
= g_date_time_new_utc (2016, 6, 30, 22, 10, 42);
1101 ASSERT_DATE (dt
, 2016, 6, 30);
1102 g_date_time_unref (dt
);
1103 dt
= g_date_time_new_utc (2016, 6, 31, 22, 10, 42);
1105 dt
= g_date_time_new_utc (2016, 7, 31, 22, 10, 42);
1106 ASSERT_DATE (dt
, 2016, 7, 31);
1107 g_date_time_unref (dt
);
1108 dt
= g_date_time_new_utc (2016, 7, 32, 22, 10, 42);
1110 dt
= g_date_time_new_utc (2016, 8, 31, 22, 10, 42);
1111 ASSERT_DATE (dt
, 2016, 8, 31);
1112 g_date_time_unref (dt
);
1113 dt
= g_date_time_new_utc (2016, 8, 32, 22, 10, 42);
1115 dt
= g_date_time_new_utc (2016, 9, 30, 22, 10, 42);
1116 ASSERT_DATE (dt
, 2016, 9, 30);
1117 g_date_time_unref (dt
);
1118 dt
= g_date_time_new_utc (2016, 9, 31, 22, 10, 42);
1120 dt
= g_date_time_new_utc (2016, 10, 31, 22, 10, 42);
1121 ASSERT_DATE (dt
, 2016, 10, 31);
1122 g_date_time_unref (dt
);
1123 dt
= g_date_time_new_utc (2016, 10, 32, 22, 10, 42);
1125 dt
= g_date_time_new_utc (2016, 11, 30, 22, 10, 42);
1126 ASSERT_DATE (dt
, 2016, 11, 30);
1127 g_date_time_unref (dt
);
1128 dt
= g_date_time_new_utc (2016, 11, 31, 22, 10, 42);
1130 dt
= g_date_time_new_utc (2016, 12, 31, 22, 10, 42);
1131 ASSERT_DATE (dt
, 2016, 12, 31);
1132 g_date_time_unref (dt
);
1133 dt
= g_date_time_new_utc (2016, 12, 32, 22, 10, 42);
1138 test_GDateTime_now_utc (void)
1145 /* t <= dt.to_unix() <= after, but the inequalities might not be
1146 * equality if we're close to the boundary between seconds.
1147 * We loop until t == after (and hence dt.to_unix() should equal both)
1148 * to guard against that. */
1151 t
= g_get_real_time () / G_TIME_SPAN_SECOND
;
1152 #ifdef HAVE_GMTIME_R
1156 struct tm
*tmp
= gmtime (&t
);
1157 /* Assume gmtime() can't fail as we got t from time(NULL). (Note
1158 * that on Windows, gmtime() *is* MT-safe, it uses a thread-local
1161 memcpy (&tm
, tmp
, sizeof (struct tm
));
1164 dt
= g_date_time_new_now_utc ();
1166 after
= g_get_real_time () / G_TIME_SPAN_SECOND
;
1170 g_assert_cmpint (tm
.tm_year
+ 1900, ==, g_date_time_get_year (dt
));
1171 g_assert_cmpint (tm
.tm_mon
+ 1, ==, g_date_time_get_month (dt
));
1172 g_assert_cmpint (tm
.tm_mday
, ==, g_date_time_get_day_of_month (dt
));
1173 g_assert_cmpint (tm
.tm_hour
, ==, g_date_time_get_hour (dt
));
1174 g_assert_cmpint (tm
.tm_min
, ==, g_date_time_get_minute (dt
));
1175 g_assert_cmpint (tm
.tm_sec
, ==, g_date_time_get_second (dt
));
1176 g_date_time_unref (dt
);
1180 test_GDateTime_new_from_unix_utc (void)
1185 t
= g_get_real_time ();
1188 dt
= g_date_time_new_from_unix_utc (t
);
1189 g_assert (dt
== NULL
);
1192 t
= t
/ 1e6
; /* oops, this was microseconds */
1194 dt
= g_date_time_new_from_unix_utc (t
);
1195 g_assert (dt
!= NULL
);
1197 g_assert (dt
== g_date_time_ref (dt
));
1198 g_date_time_unref (dt
);
1199 g_assert_cmpint (g_date_time_to_unix (dt
), ==, t
);
1200 g_date_time_unref (dt
);
1204 test_GDateTime_get_utc_offset (void)
1206 #if defined (HAVE_STRUCT_TM_TM_GMTOFF) || defined (HAVE_STRUCT_TM___TM_GMTOFF)
1211 memset (&tm
, 0, sizeof (tm
));
1212 get_localtime_tm (g_get_real_time () / G_TIME_SPAN_SECOND
, &tm
);
1214 dt
= g_date_time_new_now_local ();
1215 ts
= g_date_time_get_utc_offset (dt
);
1216 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
1217 g_assert_cmpint (ts
, ==, (tm
.tm_gmtoff
* G_TIME_SPAN_SECOND
));
1219 #ifdef HAVE_STRUCT_TM___TM_GMTOFF
1220 g_assert_cmpint (ts
, ==, (tm
.__tm_gmtoff
* G_TIME_SPAN_SECOND
));
1222 g_date_time_unref (dt
);
1227 test_GDateTime_to_timeval (void)
1232 memset (&tv1
, 0, sizeof (tv1
));
1233 memset (&tv2
, 0, sizeof (tv2
));
1235 g_get_current_time (&tv1
);
1236 dt
= g_date_time_new_from_timeval_local (&tv1
);
1237 g_date_time_to_timeval (dt
, &tv2
);
1238 g_assert_cmpint (tv1
.tv_sec
, ==, tv2
.tv_sec
);
1239 g_assert_cmpint (tv1
.tv_usec
, ==, tv2
.tv_usec
);
1240 g_date_time_unref (dt
);
1244 test_GDateTime_to_local (void)
1246 GDateTime
*utc
= NULL
, *now
= NULL
, *dt
;
1247 time_t before
, after
;
1249 /* before <= utc.to_unix() <= now.to_unix() <= after, but the inequalities
1250 * might not be equality if we're close to the boundary between seconds.
1251 * We loop until before == after (and hence the GDateTimes should match)
1252 * to guard against that. */
1255 before
= g_get_real_time () / G_TIME_SPAN_SECOND
;
1256 g_clear_pointer (&utc
, g_date_time_unref
);
1257 g_clear_pointer (&now
, g_date_time_unref
);
1258 utc
= g_date_time_new_now_utc ();
1259 now
= g_date_time_new_now_local ();
1260 after
= g_get_real_time () / G_TIME_SPAN_SECOND
;
1262 while (before
!= after
);
1264 dt
= g_date_time_to_local (utc
);
1266 g_assert_cmpint (g_date_time_get_year (now
), ==, g_date_time_get_year (dt
));
1267 g_assert_cmpint (g_date_time_get_month (now
), ==, g_date_time_get_month (dt
));
1268 g_assert_cmpint (g_date_time_get_day_of_month (now
), ==, g_date_time_get_day_of_month (dt
));
1269 g_assert_cmpint (g_date_time_get_hour (now
), ==, g_date_time_get_hour (dt
));
1270 g_assert_cmpint (g_date_time_get_minute (now
), ==, g_date_time_get_minute (dt
));
1271 g_assert_cmpint (g_date_time_get_second (now
), ==, g_date_time_get_second (dt
));
1273 g_date_time_unref (now
);
1274 g_date_time_unref (utc
);
1275 g_date_time_unref (dt
);
1279 test_GDateTime_to_utc (void)
1281 GDateTime
*dt
, *dt2
;
1286 #ifdef HAVE_GMTIME_R
1290 struct tm
*tmp
= gmtime (&t
);
1291 memcpy (&tm
, tmp
, sizeof (struct tm
));
1294 dt2
= g_date_time_new_from_unix_local (t
);
1295 dt
= g_date_time_to_utc (dt2
);
1296 g_assert_cmpint (tm
.tm_year
+ 1900, ==, g_date_time_get_year (dt
));
1297 g_assert_cmpint (tm
.tm_mon
+ 1, ==, g_date_time_get_month (dt
));
1298 g_assert_cmpint (tm
.tm_mday
, ==, g_date_time_get_day_of_month (dt
));
1299 g_assert_cmpint (tm
.tm_hour
, ==, g_date_time_get_hour (dt
));
1300 g_assert_cmpint (tm
.tm_min
, ==, g_date_time_get_minute (dt
));
1301 g_assert_cmpint (tm
.tm_sec
, ==, g_date_time_get_second (dt
));
1302 g_date_time_unref (dt
);
1303 g_date_time_unref (dt2
);
1307 test_GDateTime_get_day_of_year (void)
1309 #define TEST_DAY_OF_YEAR(y,m,d,o) G_STMT_START { \
1310 GDateTime *__dt = g_date_time_new_local ((y), (m), (d), 0, 0, 0); \
1311 g_assert_cmpint ((o), ==, g_date_time_get_day_of_year (__dt)); \
1312 g_date_time_unref (__dt); } G_STMT_END
1314 TEST_DAY_OF_YEAR (2009, 1, 1, 1);
1315 TEST_DAY_OF_YEAR (2009, 2, 1, 32);
1316 TEST_DAY_OF_YEAR (2009, 8, 16, 228);
1317 TEST_DAY_OF_YEAR (2008, 8, 16, 229);
1321 test_GDateTime_printf (void)
1323 /* 64 seems big, but one zoneinfo file, Factory, has an abbreviation
1324 * that long, and it will cause the test to fail if dst isn't big
1327 gchar
*old_lc_messages
;
1332 #define TEST_PRINTF(f,o) G_STMT_START { \
1333 GDateTime *__dt = g_date_time_new_local (2009, 10, 24, 0, 0, 0);\
1334 gchar *__p = g_date_time_format (__dt, (f)); \
1335 g_assert_cmpstr (__p, ==, (o)); \
1336 g_date_time_unref (__dt); \
1337 g_free (__p); } G_STMT_END
1339 #define TEST_PRINTF_DATE(y,m,d,f,o) G_STMT_START { \
1340 GDateTime *dt = g_date_time_new_local (y, m, d, 0, 0, 0); \
1341 gchar *p = g_date_time_format (dt, (f)); \
1342 gchar *o_casefold = g_utf8_casefold (o, -1); \
1343 gchar *p_casefold = g_utf8_casefold (p, -1); \
1344 g_assert_cmpstr (p_casefold, ==, (o_casefold)); \
1345 g_date_time_unref (dt); \
1346 g_free (p_casefold); \
1347 g_free (o_casefold); \
1348 g_free (p); } G_STMT_END
1350 #define TEST_PRINTF_TIME(h,m,s,f,o) G_STMT_START { \
1351 GDateTime *dt = g_date_time_new_local (2009, 10, 24, (h), (m), (s)); \
1352 gchar *p = g_date_time_format (dt, (f)); \
1353 g_assert_cmpstr (p, ==, (o)); \
1354 g_date_time_unref (dt); \
1355 g_free (p); } G_STMT_END
1357 old_lc_messages
= g_strdup (g_getenv ("LC_MESSAGES"));
1358 g_setenv ("LC_MESSAGES", "C", TRUE
);
1361 * This is a little helper to make sure we can compare timezones to
1362 * that of the generated timezone.
1365 memset (&tt
, 0, sizeof(tt
));
1366 get_localtime_tm (t
, &tt
);
1367 tt
.tm_year
= 2009 - 1900;
1368 tt
.tm_mon
= 9; /* 0 indexed */
1371 memset (&tt
, 0, sizeof(tt
));
1372 get_localtime_tm (t
, &tt
);
1373 strftime (dst
, sizeof(dst
), "%Z", &tt
);
1375 /* get current time_t for 20090924 in the local timezone */
1381 TEST_PRINTF ("%a", "Sat");
1382 TEST_PRINTF ("%A", "Saturday");
1383 TEST_PRINTF ("%b", "Oct");
1384 TEST_PRINTF ("%B", "October");
1385 TEST_PRINTF ("%d", "24");
1386 TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
1387 TEST_PRINTF ("%e", "24"); // fixme
1388 TEST_PRINTF ("%h", "Oct");
1389 TEST_PRINTF ("%H", "00");
1390 TEST_PRINTF_TIME (15, 0, 0, "%H", "15");
1391 TEST_PRINTF ("%I", "12");
1392 TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
1393 TEST_PRINTF_TIME (15, 0, 0, "%I", "03");
1394 TEST_PRINTF ("%j", "297");
1395 TEST_PRINTF ("%k", " 0");
1396 TEST_PRINTF_TIME (13, 13, 13, "%k", "13");
1397 TEST_PRINTF ("%l", "12");
1398 TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
1399 TEST_PRINTF_TIME (13, 13, 13, "%l", " 1");
1400 TEST_PRINTF_TIME (10, 13, 13, "%l", "10");
1401 TEST_PRINTF ("%m", "10");
1402 TEST_PRINTF ("%M", "00");
1403 TEST_PRINTF ("%p", "AM");
1404 TEST_PRINTF_TIME (13, 13, 13, "%p", "PM");
1405 TEST_PRINTF ("%P", "am");
1406 TEST_PRINTF_TIME (13, 13, 13, "%P", "pm");
1407 TEST_PRINTF ("%r", "12:00:00 AM");
1408 TEST_PRINTF_TIME (13, 13, 13, "%r", "01:13:13 PM");
1409 TEST_PRINTF ("%R", "00:00");
1410 TEST_PRINTF_TIME (13, 13, 31, "%R", "13:13");
1411 TEST_PRINTF ("%S", "00");
1412 TEST_PRINTF ("%t", " ");
1413 TEST_PRINTF ("%u", "6");
1414 TEST_PRINTF ("%x", "10/24/09");
1415 TEST_PRINTF ("%X", "00:00:00");
1416 TEST_PRINTF_TIME (13, 14, 15, "%X", "13:14:15");
1417 TEST_PRINTF ("%y", "09");
1418 TEST_PRINTF ("%Y", "2009");
1419 TEST_PRINTF ("%%", "%");
1420 TEST_PRINTF ("%", "");
1421 TEST_PRINTF ("%9", NULL
);
1423 TEST_PRINTF ("%Z", dst
);
1424 #elif defined G_OS_WIN32
1425 TEST_PRINTF ("%Z", "Pacific Standard Time");
1428 if (old_lc_messages
!= NULL
)
1429 g_setenv ("LC_MESSAGES", old_lc_messages
, TRUE
);
1431 g_unsetenv ("LC_MESSAGES");
1432 g_free (old_lc_messages
);
1436 test_non_utf8_printf (void)
1440 oldlocale
= g_strdup (setlocale (LC_ALL
, NULL
));
1441 setlocale (LC_ALL
, "ja_JP.eucjp");
1442 if (strstr (setlocale (LC_ALL
, NULL
), "ja_JP") == NULL
)
1444 g_test_skip ("locale ja_JP.eucjp not available, skipping non-UTF8 tests");
1448 if (g_get_charset (NULL
))
1450 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");
1451 setlocale (LC_ALL
, oldlocale
);
1456 /* These are the outputs that ja_JP.UTF-8 generates; if everything
1457 * is working then ja_JP.eucjp should generate the same.
1459 TEST_PRINTF ("%a", "\345\234\237");
1460 TEST_PRINTF ("%A", "\345\234\237\346\233\234\346\227\245");
1461 #ifndef __APPLE__ /* OSX just returns the number */
1462 TEST_PRINTF ("%b", "10\346\234\210");
1464 TEST_PRINTF ("%B", "10\346\234\210");
1465 TEST_PRINTF ("%d", "24");
1466 TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
1467 TEST_PRINTF ("%e", "24"); // fixme
1468 #ifndef __APPLE__ /* OSX just returns the number */
1469 TEST_PRINTF ("%h", "10\346\234\210");
1471 TEST_PRINTF ("%H", "00");
1472 TEST_PRINTF_TIME (15, 0, 0, "%H", "15");
1473 TEST_PRINTF ("%I", "12");
1474 TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
1475 TEST_PRINTF_TIME (15, 0, 0, "%I", "03");
1476 TEST_PRINTF ("%j", "297");
1477 TEST_PRINTF ("%k", " 0");
1478 TEST_PRINTF_TIME (13, 13, 13, "%k", "13");
1479 TEST_PRINTF ("%l", "12");
1480 TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
1481 TEST_PRINTF_TIME (13, 13, 13, "%l", " 1");
1482 TEST_PRINTF_TIME (10, 13, 13, "%l", "10");
1483 TEST_PRINTF ("%m", "10");
1484 TEST_PRINTF ("%M", "00");
1485 #ifndef __APPLE__ /* OSX returns latin "AM", not japanese */
1486 TEST_PRINTF ("%p", "\345\215\210\345\211\215");
1487 TEST_PRINTF_TIME (13, 13, 13, "%p", "\345\215\210\345\276\214");
1488 TEST_PRINTF ("%P", "\345\215\210\345\211\215");
1489 TEST_PRINTF_TIME (13, 13, 13, "%P", "\345\215\210\345\276\214");
1490 TEST_PRINTF ("%r", "\345\215\210\345\211\21512\346\231\20200\345\210\20600\347\247\222");
1491 TEST_PRINTF_TIME (13, 13, 13, "%r", "\345\215\210\345\276\21401\346\231\20213\345\210\20613\347\247\222");
1493 TEST_PRINTF ("%R", "00:00");
1494 TEST_PRINTF_TIME (13, 13, 31, "%R", "13:13");
1495 TEST_PRINTF ("%S", "00");
1496 TEST_PRINTF ("%t", " ");
1497 TEST_PRINTF ("%u", "6");
1498 #ifndef __APPLE__ /* OSX returns YYYY/MM/DD in ASCII */
1499 TEST_PRINTF ("%x", "2009\345\271\26410\346\234\21024\346\227\245");
1501 TEST_PRINTF ("%X", "00\346\231\20200\345\210\20600\347\247\222");
1502 TEST_PRINTF_TIME (13, 14, 15, "%X", "13\346\231\20214\345\210\20615\347\247\222");
1503 TEST_PRINTF ("%y", "09");
1504 TEST_PRINTF ("%Y", "2009");
1505 TEST_PRINTF ("%%", "%");
1506 TEST_PRINTF ("%", "");
1507 TEST_PRINTF ("%9", NULL
);
1509 setlocale (LC_ALL
, oldlocale
);
1514 test_modifiers (void)
1518 TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
1519 TEST_PRINTF_DATE (2009, 1, 1, "%_d", " 1");
1520 TEST_PRINTF_DATE (2009, 1, 1, "%-d", "1");
1521 TEST_PRINTF_DATE (2009, 1, 1, "%0d", "01");
1522 TEST_PRINTF_DATE (2009, 1, 21, "%d", "21");
1523 TEST_PRINTF_DATE (2009, 1, 21, "%_d", "21");
1524 TEST_PRINTF_DATE (2009, 1, 21, "%-d", "21");
1525 TEST_PRINTF_DATE (2009, 1, 21, "%0d", "21");
1527 TEST_PRINTF_DATE (2009, 1, 1, "%e", " 1");
1528 TEST_PRINTF_DATE (2009, 1, 1, "%_e", " 1");
1529 TEST_PRINTF_DATE (2009, 1, 1, "%-e", "1");
1530 TEST_PRINTF_DATE (2009, 1, 1, "%0e", "01");
1531 TEST_PRINTF_DATE (2009, 1, 21, "%e", "21");
1532 TEST_PRINTF_DATE (2009, 1, 21, "%_e", "21");
1533 TEST_PRINTF_DATE (2009, 1, 21, "%-e", "21");
1534 TEST_PRINTF_DATE (2009, 1, 21, "%0e", "21");
1536 TEST_PRINTF_TIME ( 1, 0, 0, "%H", "01");
1537 TEST_PRINTF_TIME ( 1, 0, 0, "%_H", " 1");
1538 TEST_PRINTF_TIME ( 1, 0, 0, "%-H", "1");
1539 TEST_PRINTF_TIME ( 1, 0, 0, "%0H", "01");
1540 TEST_PRINTF_TIME (21, 0, 0, "%H", "21");
1541 TEST_PRINTF_TIME (21, 0, 0, "%_H", "21");
1542 TEST_PRINTF_TIME (21, 0, 0, "%-H", "21");
1543 TEST_PRINTF_TIME (21, 0, 0, "%0H", "21");
1545 TEST_PRINTF_TIME ( 1, 0, 0, "%I", "01");
1546 TEST_PRINTF_TIME ( 1, 0, 0, "%_I", " 1");
1547 TEST_PRINTF_TIME ( 1, 0, 0, "%-I", "1");
1548 TEST_PRINTF_TIME ( 1, 0, 0, "%0I", "01");
1549 TEST_PRINTF_TIME (23, 0, 0, "%I", "11");
1550 TEST_PRINTF_TIME (23, 0, 0, "%_I", "11");
1551 TEST_PRINTF_TIME (23, 0, 0, "%-I", "11");
1552 TEST_PRINTF_TIME (23, 0, 0, "%0I", "11");
1554 TEST_PRINTF_TIME ( 1, 0, 0, "%k", " 1");
1555 TEST_PRINTF_TIME ( 1, 0, 0, "%_k", " 1");
1556 TEST_PRINTF_TIME ( 1, 0, 0, "%-k", "1");
1557 TEST_PRINTF_TIME ( 1, 0, 0, "%0k", "01");
1559 oldlocale
= g_strdup (setlocale (LC_ALL
, NULL
));
1560 setlocale (LC_ALL
, "fa_IR.utf-8");
1561 if (strstr (setlocale (LC_ALL
, NULL
), "fa_IR") != NULL
)
1563 TEST_PRINTF_TIME (23, 0, 0, "%OH", "\333\262\333\263"); /* '23' */
1564 TEST_PRINTF_TIME (23, 0, 0, "%OI", "\333\261\333\261"); /* '11' */
1565 TEST_PRINTF_TIME (23, 0, 0, "%OM", "\333\260\333\260"); /* '00' */
1567 TEST_PRINTF_DATE (2011, 7, 1, "%Om", "\333\260\333\267"); /* '07' */
1568 TEST_PRINTF_DATE (2011, 7, 1, "%0Om", "\333\260\333\267"); /* '07' */
1569 TEST_PRINTF_DATE (2011, 7, 1, "%-Om", "\333\267"); /* '7' */
1570 TEST_PRINTF_DATE (2011, 7, 1, "%_Om", " \333\267"); /* ' 7' */
1573 g_test_skip ("locale fa_IR not available, skipping O modifier tests");
1574 setlocale (LC_ALL
, oldlocale
);
1578 /* Test that the `O` modifier for g_date_time_format() works with %B, %b and %h;
1579 * i.e. whether genitive month names are supported. */
1581 test_month_names (void)
1585 g_test_bug ("749206");
1587 /* If running uninstalled (G_TEST_BUILDDIR is set), skip this test, since we
1588 * need the translations to be installed. We can’t mess around with
1589 * bindtextdomain() here, as the compiled .gmo files in po/ are not in the
1590 * right installed directory hierarchy to be successfully loaded by gettext. */
1591 if (g_getenv ("G_TEST_BUILDDIR") != NULL
)
1593 g_test_skip ("Skipping due to running uninstalled. "
1594 "This test can only be run when the translations are installed.");
1598 oldlocale
= g_strdup (setlocale (LC_ALL
, NULL
));
1600 /* Make sure that nothing has been changed in western European languages. */
1601 setlocale (LC_ALL
, "en_GB.utf-8");
1602 if (strstr (setlocale (LC_ALL
, NULL
), "en_GB") != NULL
)
1604 TEST_PRINTF_DATE (2018, 1, 1, "%B", "January");
1605 TEST_PRINTF_DATE (2018, 2, 1, "%OB", "February");
1606 TEST_PRINTF_DATE (2018, 3, 1, "%b", "Mar");
1607 TEST_PRINTF_DATE (2018, 4, 1, "%Ob", "Apr");
1608 TEST_PRINTF_DATE (2018, 5, 1, "%h", "May");
1609 TEST_PRINTF_DATE (2018, 6, 1, "%Oh", "Jun");
1612 g_test_skip ("locale en_GB not available, skipping English month names test");
1614 setlocale (LC_ALL
, "de_DE.utf-8");
1615 if (strstr (setlocale (LC_ALL
, NULL
), "de_DE") != NULL
)
1617 TEST_PRINTF_DATE (2018, 7, 1, "%B", "Juli");
1618 TEST_PRINTF_DATE (2018, 8, 1, "%OB", "August");
1619 TEST_PRINTF_DATE (2018, 9, 1, "%b", "Sep");
1620 TEST_PRINTF_DATE (2018, 10, 1, "%Ob", "Okt");
1621 TEST_PRINTF_DATE (2018, 11, 1, "%h", "Nov");
1622 TEST_PRINTF_DATE (2018, 12, 1, "%Oh", "Dez");
1625 g_test_skip ("locale de_DE not available, skipping German month names test");
1627 setlocale (LC_ALL
, "es_ES.utf-8");
1628 if (strstr (setlocale (LC_ALL
, NULL
), "es_ES") != NULL
)
1630 TEST_PRINTF_DATE (2018, 1, 1, "%B", "enero");
1631 TEST_PRINTF_DATE (2018, 2, 1, "%OB", "febrero");
1632 TEST_PRINTF_DATE (2018, 3, 1, "%b", "mar");
1633 TEST_PRINTF_DATE (2018, 4, 1, "%Ob", "abr");
1634 TEST_PRINTF_DATE (2018, 5, 1, "%h", "may");
1635 TEST_PRINTF_DATE (2018, 6, 1, "%Oh", "jun");
1638 g_test_skip ("locale es_ES not available, skipping Spanish month names test");
1640 setlocale (LC_ALL
, "fr_FR.utf-8");
1641 if (strstr (setlocale (LC_ALL
, NULL
), "fr_FR") != NULL
)
1643 TEST_PRINTF_DATE (2018, 7, 1, "%B", "juillet");
1644 TEST_PRINTF_DATE (2018, 8, 1, "%OB", "août");
1645 TEST_PRINTF_DATE (2018, 9, 1, "%b", "sept.");
1646 TEST_PRINTF_DATE (2018, 10, 1, "%Ob", "oct.");
1647 TEST_PRINTF_DATE (2018, 11, 1, "%h", "nov.");
1648 TEST_PRINTF_DATE (2018, 12, 1, "%Oh", "déc.");
1651 g_test_skip ("locale fr_FR not available, skipping French month names test");
1653 /* Make sure that there are visible changes in some European languages. */
1654 setlocale (LC_ALL
, "el_GR.utf-8");
1655 if (strstr (setlocale (LC_ALL
, NULL
), "el_GR") != NULL
)
1657 TEST_PRINTF_DATE (2018, 1, 1, "%B", "Ιανουαρίου");
1658 TEST_PRINTF_DATE (2018, 2, 1, "%B", "Φεβρουαρίου");
1659 TEST_PRINTF_DATE (2018, 3, 1, "%B", "Μαρτίου");
1660 TEST_PRINTF_DATE (2018, 4, 1, "%OB", "Απρίλιος");
1661 TEST_PRINTF_DATE (2018, 5, 1, "%OB", "Μάιος");
1662 TEST_PRINTF_DATE (2018, 6, 1, "%OB", "Ιούνιος");
1663 TEST_PRINTF_DATE (2018, 7, 1, "%b", "Ιουλ");
1664 TEST_PRINTF_DATE (2018, 8, 1, "%Ob", "Αύγ");
1667 g_test_skip ("locale el_GR not available, skipping Greek month names test");
1669 setlocale (LC_ALL
, "hr_HR.utf-8");
1670 if (strstr (setlocale (LC_ALL
, NULL
), "hr_HR") != NULL
)
1672 TEST_PRINTF_DATE (2018, 5, 1, "%B", "svibnja");
1673 TEST_PRINTF_DATE (2018, 6, 1, "%B", "lipnja");
1674 TEST_PRINTF_DATE (2018, 7, 1, "%B", "srpnja");
1675 TEST_PRINTF_DATE (2018, 8, 1, "%OB", "Kolovoz");
1676 TEST_PRINTF_DATE (2018, 9, 1, "%OB", "Rujan");
1677 TEST_PRINTF_DATE (2018, 10, 1, "%OB", "Listopad");
1678 TEST_PRINTF_DATE (2018, 11, 1, "%b", "Stu");
1679 TEST_PRINTF_DATE (2018, 12, 1, "%Ob", "Pro");
1682 g_test_skip ("locale hr_HR not available, skipping Croatian month names test");
1684 setlocale (LC_ALL
, "lt_LT.utf-8");
1685 if (strstr (setlocale (LC_ALL
, NULL
), "lt_LT") != NULL
)
1687 TEST_PRINTF_DATE (2018, 1, 1, "%B", "sausio");
1688 TEST_PRINTF_DATE (2018, 2, 1, "%B", "vasario");
1689 TEST_PRINTF_DATE (2018, 3, 1, "%B", "kovo");
1690 TEST_PRINTF_DATE (2018, 4, 1, "%OB", "balandis");
1691 TEST_PRINTF_DATE (2018, 5, 1, "%OB", "gegužė");
1692 TEST_PRINTF_DATE (2018, 6, 1, "%OB", "birželis");
1693 TEST_PRINTF_DATE (2018, 7, 1, "%b", "liep.");
1694 TEST_PRINTF_DATE (2018, 8, 1, "%Ob", "rugp.");
1697 g_test_skip ("locale lt_LT not available, skipping Lithuanian month names test");
1699 setlocale (LC_ALL
, "pl_PL.utf-8");
1700 if (strstr (setlocale (LC_ALL
, NULL
), "pl_PL") != NULL
)
1702 TEST_PRINTF_DATE (2018, 5, 1, "%B", "maja");
1703 TEST_PRINTF_DATE (2018, 6, 1, "%B", "czerwca");
1704 TEST_PRINTF_DATE (2018, 7, 1, "%B", "lipca");
1705 TEST_PRINTF_DATE (2018, 8, 1, "%OB", "sierpień");
1706 TEST_PRINTF_DATE (2018, 9, 1, "%OB", "wrzesień");
1707 TEST_PRINTF_DATE (2018, 10, 1, "%OB", "październik");
1708 TEST_PRINTF_DATE (2018, 11, 1, "%b", "lis");
1709 TEST_PRINTF_DATE (2018, 12, 1, "%Ob", "gru");
1712 g_test_skip ("locale pl_PL not available, skipping Polish month names test");
1714 setlocale (LC_ALL
, "ru_RU.utf-8");
1715 if (strstr (setlocale (LC_ALL
, NULL
), "ru_RU") != NULL
)
1717 TEST_PRINTF_DATE (2018, 1, 1, "%B", "января");
1718 TEST_PRINTF_DATE (2018, 2, 1, "%B", "февраля");
1719 TEST_PRINTF_DATE (2018, 3, 1, "%B", "марта");
1720 TEST_PRINTF_DATE (2018, 4, 1, "%OB", "Апрель");
1721 TEST_PRINTF_DATE (2018, 5, 1, "%OB", "Май");
1722 TEST_PRINTF_DATE (2018, 6, 1, "%OB", "Июнь");
1723 TEST_PRINTF_DATE (2018, 7, 1, "%b", "июл");
1724 TEST_PRINTF_DATE (2018, 8, 1, "%Ob", "авг");
1725 /* This difference is very important in Russian: */
1726 TEST_PRINTF_DATE (2018, 5, 1, "%b", "мая");
1727 TEST_PRINTF_DATE (2018, 5, 1, "%Ob", "май");
1730 g_test_skip ("locale ru_RU not available, skipping Russian month names test");
1732 setlocale (LC_ALL
, oldlocale
);
1737 test_GDateTime_dst (void)
1739 GDateTime
*dt1
, *dt2
;
1742 /* this date has the DST state set for Europe/London */
1744 tz
= g_time_zone_new ("Europe/London");
1745 #elif defined G_OS_WIN32
1746 tz
= g_time_zone_new ("GMT Standard Time");
1748 dt1
= g_date_time_new (tz
, 2009, 8, 15, 3, 0, 1);
1749 g_assert (g_date_time_is_daylight_savings (dt1
));
1750 g_assert_cmpint (g_date_time_get_utc_offset (dt1
) / G_USEC_PER_SEC
, ==, 3600);
1751 g_assert_cmpint (g_date_time_get_hour (dt1
), ==, 3);
1753 /* add 6 months to clear the DST flag but keep the same time */
1754 dt2
= g_date_time_add_months (dt1
, 6);
1755 g_assert (!g_date_time_is_daylight_savings (dt2
));
1756 g_assert_cmpint (g_date_time_get_utc_offset (dt2
) / G_USEC_PER_SEC
, ==, 0);
1757 g_assert_cmpint (g_date_time_get_hour (dt2
), ==, 3);
1759 g_date_time_unref (dt2
);
1760 g_date_time_unref (dt1
);
1762 /* now do the reverse: start with a non-DST state and move to DST */
1763 dt1
= g_date_time_new (tz
, 2009, 2, 15, 2, 0, 1);
1764 g_assert (!g_date_time_is_daylight_savings (dt1
));
1765 g_assert_cmpint (g_date_time_get_hour (dt1
), ==, 2);
1767 dt2
= g_date_time_add_months (dt1
, 6);
1768 g_assert (g_date_time_is_daylight_savings (dt2
));
1769 g_assert_cmpint (g_date_time_get_hour (dt2
), ==, 2);
1771 g_date_time_unref (dt2
);
1772 g_date_time_unref (dt1
);
1773 g_time_zone_unref (tz
);
1776 static inline gboolean
1777 is_leap_year (gint year
)
1779 g_assert (1 <= year
&& year
<= 9999);
1781 return year
% 400 == 0 || (year
% 4 == 0 && year
% 100 != 0);
1785 days_in_month (gint year
, gint month
)
1787 const gint table
[2][13] = {
1788 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
1789 {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
1792 g_assert (1 <= month
&& month
<= 12);
1794 return table
[is_leap_year (year
)][month
];
1798 test_all_dates (void)
1800 gint year
, month
, day
;
1801 GTimeZone
*timezone
;
1808 /* save some time by hanging on to this. */
1809 timezone
= g_time_zone_new_utc ();
1811 unix_time
= G_GINT64_CONSTANT(-62135596800);
1813 /* 0001-01-01 is 0001-W01-1 */
1819 /* The calendar makes a full cycle every 400 years, so we could
1820 * theoretically just test years 1 through 400. That assumes that our
1821 * software has no bugs, so probably we should just test them all. :)
1823 for (year
= 1; year
<= 9999; year
++)
1827 for (month
= 1; month
<= 12; month
++)
1828 for (day
= 1; day
<= days_in_month (year
, month
); day
++)
1832 dt
= g_date_time_new (timezone
, year
, month
, day
, 0, 0, 0);
1835 g_printerr ("%04d-%02d-%02d = %04d-W%02d-%d = %04d-%03d\n",
1837 week_year
, week_num
, weekday
,
1842 if G_UNLIKELY (g_date_time_get_year (dt
) != year
||
1843 g_date_time_get_month (dt
) != month
||
1844 g_date_time_get_day_of_month (dt
) != day
)
1845 g_error ("%04d-%02d-%02d comes out as %04d-%02d-%02d",
1847 g_date_time_get_year (dt
),
1848 g_date_time_get_month (dt
),
1849 g_date_time_get_day_of_month (dt
));
1851 if G_UNLIKELY (g_date_time_get_week_numbering_year (dt
) != week_year
||
1852 g_date_time_get_week_of_year (dt
) != week_num
||
1853 g_date_time_get_day_of_week (dt
) != weekday
)
1854 g_error ("%04d-%02d-%02d should be %04d-W%02d-%d but "
1855 "comes out as %04d-W%02d-%d", year
, month
, day
,
1856 week_year
, week_num
, weekday
,
1857 g_date_time_get_week_numbering_year (dt
),
1858 g_date_time_get_week_of_year (dt
),
1859 g_date_time_get_day_of_week (dt
));
1861 if G_UNLIKELY (g_date_time_to_unix (dt
) != unix_time
)
1862 g_error ("%04d-%02d-%02d 00:00:00 UTC should have unix time %"
1863 G_GINT64_FORMAT
" but comes out as %"G_GINT64_FORMAT
,
1864 year
, month
, day
, unix_time
, g_date_time_to_unix (dt
));
1866 if G_UNLIKELY (g_date_time_get_day_of_year (dt
) != day_of_year
)
1867 g_error ("%04d-%02d-%02d should be day of year %d"
1868 " but comes out as %d", year
, month
, day
,
1869 day_of_year
, g_date_time_get_day_of_year (dt
));
1871 if G_UNLIKELY (g_date_time_get_hour (dt
) != 0 ||
1872 g_date_time_get_minute (dt
) != 0 ||
1873 g_date_time_get_seconds (dt
) != 0)
1874 g_error ("%04d-%02d-%02d 00:00:00 UTC comes out "
1875 "as %02d:%02d:%02.6f", year
, month
, day
,
1876 g_date_time_get_hour (dt
),
1877 g_date_time_get_minute (dt
),
1878 g_date_time_get_seconds (dt
));
1881 /* add 24 hours to unix time */
1882 unix_time
+= 24 * 60 * 60;
1884 /* move day of year forward */
1887 /* move the week date forward */
1890 weekday
= 1; /* Sunday -> Monday */
1892 /* NOTE: year/month/day is the final day of the week we
1895 * If we just finished the last week of last year then
1896 * we are definitely starting the first week of this
1899 * Otherwise, if we're still in this year, but Sunday
1900 * fell on or after December 28 then December 29, 30, 31
1901 * could be days within the next year's first year.
1903 if (year
!= week_year
|| (month
== 12 && day
>= 28))
1905 /* first week of the new year */
1913 g_date_time_unref (dt
);
1917 g_time_zone_unref (timezone
);
1927 g_test_bug ("642935");
1929 tz
= g_time_zone_new ("-08:00");
1930 dt
= g_date_time_new (tz
, 1, 1, 1, 0, 0, 0);
1932 p
= g_date_time_format (dt
, "%z");
1933 g_assert_cmpstr (p
, ==, "-0800");
1936 p
= g_date_time_format (dt
, "%:z");
1937 g_assert_cmpstr (p
, ==, "-08:00");
1940 p
= g_date_time_format (dt
, "%::z");
1941 g_assert_cmpstr (p
, ==, "-08:00:00");
1944 p
= g_date_time_format (dt
, "%:::z");
1945 g_assert_cmpstr (p
, ==, "-08");
1948 g_date_time_unref (dt
);
1949 g_time_zone_unref (tz
);
1951 tz
= g_time_zone_new ("+00:00");
1952 dt
= g_date_time_new (tz
, 1, 1, 1, 0, 0, 0);
1953 p
= g_date_time_format (dt
, "%:::z");
1954 g_assert_cmpstr (p
, ==, "+00");
1956 g_date_time_unref (dt
);
1957 g_time_zone_unref (tz
);
1959 tz
= g_time_zone_new ("+08:23");
1960 dt
= g_date_time_new (tz
, 1, 1, 1, 0, 0, 0);
1961 p
= g_date_time_format (dt
, "%:::z");
1962 g_assert_cmpstr (p
, ==, "+08:23");
1964 g_date_time_unref (dt
);
1965 g_time_zone_unref (tz
);
1967 tz
= g_time_zone_new ("+08:23:45");
1968 dt
= g_date_time_new (tz
, 1, 1, 1, 0, 0, 0);
1969 p
= g_date_time_format (dt
, "%:::z");
1970 g_assert_cmpstr (p
, ==, "+08:23:45");
1972 g_date_time_unref (dt
);
1973 g_time_zone_unref (tz
);
1976 #pragma GCC diagnostic push
1977 #pragma GCC diagnostic ignored "-Wformat-y2k"
1979 test_strftime (void)
1982 #define TEST_FORMAT \
1983 "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 " \
1984 "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 %%"
1987 /* 127997 is prime, 1315005118 is now */
1988 for (t
= 0; t
< 1315005118; t
+= 127997)
1990 GDateTime
*date_time
;
1994 date_time
= g_date_time_new_from_unix_local (t
);
1995 dt_str
= g_date_time_format (date_time
, TEST_FORMAT
);
1996 strftime (c_str
, sizeof c_str
, TEST_FORMAT
, localtime (&t
));
1997 g_assert_cmpstr (c_str
, ==, dt_str
);
1998 g_date_time_unref (date_time
);
2003 #pragma GCC diagnostic pop
2005 /* Check that g_date_time_format() correctly returns %NULL for format
2006 * placeholders which are not supported in the current locale. */
2008 test_GDateTime_strftime_error_handling (void)
2012 oldlocale
= g_strdup (setlocale (LC_ALL
, NULL
));
2013 setlocale (LC_ALL
, "de_DE.utf-8");
2014 if (strstr (setlocale (LC_ALL
, NULL
), "de_DE") != NULL
)
2016 /* de_DE doesn’t ever write time in 12-hour notation, so %r is
2017 * unsupported for it. */
2018 TEST_PRINTF_TIME (23, 0, 0, "%r", NULL
);
2021 g_test_skip ("locale de_DE not available, skipping error handling tests");
2022 setlocale (LC_ALL
, oldlocale
);
2027 test_find_interval (void)
2035 tz
= g_time_zone_new ("America/Toronto");
2036 #elif defined G_OS_WIN32
2037 tz
= g_time_zone_new ("Eastern Standard Time");
2039 dt
= g_date_time_new_utc (2010, 11, 7, 1, 30, 0);
2040 u
= g_date_time_to_unix (dt
);
2042 i1
= g_time_zone_find_interval (tz
, G_TIME_TYPE_STANDARD
, u
);
2043 i2
= g_time_zone_find_interval (tz
, G_TIME_TYPE_DAYLIGHT
, u
);
2045 g_assert_cmpint (i1
, !=, i2
);
2047 g_date_time_unref (dt
);
2049 dt
= g_date_time_new_utc (2010, 3, 14, 2, 0, 0);
2050 u
= g_date_time_to_unix (dt
);
2052 i1
= g_time_zone_find_interval (tz
, G_TIME_TYPE_STANDARD
, u
);
2053 g_assert_cmpint (i1
, ==, -1);
2055 g_date_time_unref (dt
);
2056 g_time_zone_unref (tz
);
2060 test_adjust_time (void)
2068 tz
= g_time_zone_new ("America/Toronto");
2069 #elif defined G_OS_WIN32
2070 tz
= g_time_zone_new ("Eastern Standard Time");
2072 dt
= g_date_time_new_utc (2010, 11, 7, 1, 30, 0);
2073 u
= g_date_time_to_unix (dt
);
2076 i1
= g_time_zone_find_interval (tz
, G_TIME_TYPE_DAYLIGHT
, u
);
2077 i2
= g_time_zone_adjust_time (tz
, G_TIME_TYPE_DAYLIGHT
, &u2
);
2079 g_assert_cmpint (i1
, ==, i2
);
2082 g_date_time_unref (dt
);
2084 dt
= g_date_time_new_utc (2010, 3, 14, 2, 30, 0);
2085 u2
= g_date_time_to_unix (dt
);
2086 g_date_time_unref (dt
);
2088 dt
= g_date_time_new_utc (2010, 3, 14, 3, 0, 0);
2089 u
= g_date_time_to_unix (dt
);
2090 g_date_time_unref (dt
);
2092 i1
= g_time_zone_adjust_time (tz
, G_TIME_TYPE_DAYLIGHT
, &u2
);
2095 g_time_zone_unref (tz
);
2099 test_no_header (void)
2103 tz
= g_time_zone_new ("blabla");
2105 g_assert_cmpstr (g_time_zone_get_identifier (tz
), ==, "UTC");
2106 g_assert_cmpstr (g_time_zone_get_abbreviation (tz
, 0), ==, "UTC");
2107 g_assert_cmpint (g_time_zone_get_offset (tz
, 0), ==, 0);
2108 g_assert (!g_time_zone_is_dst (tz
, 0));
2110 g_time_zone_unref (tz
);
2114 test_posix_parse (void)
2117 GDateTime
*gdt1
, *gdt2
;
2119 tz
= g_time_zone_new ("PST");
2120 g_assert_cmpstr (g_time_zone_get_identifier (tz
), ==, "UTC");
2121 g_assert_cmpstr (g_time_zone_get_abbreviation (tz
, 0), ==, "UTC");
2122 g_assert_cmpint (g_time_zone_get_offset (tz
, 0), ==, 0);
2123 g_assert (!g_time_zone_is_dst (tz
, 0));
2124 g_time_zone_unref (tz
);
2126 tz
= g_time_zone_new ("PST8");
2127 g_assert_cmpstr (g_time_zone_get_identifier (tz
), ==, "PST8");
2128 g_assert_cmpstr (g_time_zone_get_abbreviation (tz
, 0), ==, "PST");
2129 g_assert_cmpint (g_time_zone_get_offset (tz
, 0), ==, - 8 * 3600);
2130 g_assert (!g_time_zone_is_dst (tz
, 0));
2131 g_time_zone_unref (tz
);
2133 /* This fails rules_from_identifier on Unix (though not on Windows)
2134 * but passes anyway because PST8PDT is a zone name.
2136 tz
= g_time_zone_new ("PST8PDT");
2137 g_assert_cmpstr (g_time_zone_get_identifier (tz
), ==, "PST8PDT");
2138 g_assert_cmpstr (g_time_zone_get_abbreviation (tz
, 0), ==, "PST");
2139 g_assert_cmpint (g_time_zone_get_offset (tz
, 0), ==, - 8 * 3600);
2140 g_assert (!g_time_zone_is_dst (tz
, 0));
2141 g_assert_cmpstr (g_time_zone_get_abbreviation (tz
, 1), ==, "PDT");
2142 g_assert_cmpint (g_time_zone_get_offset (tz
, 1), ==,- 7 * 3600);
2143 g_assert (g_time_zone_is_dst (tz
, 1));
2144 g_time_zone_unref (tz
);
2146 tz
= g_time_zone_new ("PST8PDT6:32:15");
2148 g_assert_cmpstr (g_time_zone_get_identifier (tz
), ==, "PST8PDT6:32:15");
2149 g_assert_cmpstr (g_time_zone_get_abbreviation (tz
, 0), ==, "PST");
2150 g_assert_cmpint (g_time_zone_get_offset (tz
, 0), ==, - 8 * 3600);
2151 g_assert (!g_time_zone_is_dst (tz
, 0));
2152 g_assert_cmpstr (g_time_zone_get_abbreviation (tz
, 1), ==, "PDT");
2153 g_assert_cmpint (g_time_zone_get_offset (tz
, 1), ==, - 6 * 3600 - 32 *60 - 15);
2154 g_assert (g_time_zone_is_dst (tz
, 1));
2155 gdt1
= g_date_time_new (tz
, 2012, 12, 6, 11, 15, 23.0);
2156 gdt2
= g_date_time_new (tz
, 2012, 6, 6, 11, 15, 23.0);
2157 g_assert (!g_date_time_is_daylight_savings (gdt1
));
2158 g_assert_cmpint (g_date_time_get_utc_offset (gdt1
) / 1000000, ==, -28800);
2159 g_assert (g_date_time_is_daylight_savings (gdt2
));
2160 g_assert_cmpint (g_date_time_get_utc_offset (gdt2
) / 1000000, ==, -23535);
2161 g_date_time_unref (gdt1
);
2162 g_date_time_unref (gdt2
);
2164 g_assert_cmpstr (g_time_zone_get_identifier (tz
), ==, "UTC");
2165 g_assert_cmpstr (g_time_zone_get_abbreviation (tz
, 0), ==, "UTC");
2166 g_assert_cmpint (g_time_zone_get_offset (tz
, 0), ==, 0);
2167 g_assert (!g_time_zone_is_dst (tz
, 0));
2169 g_time_zone_unref (tz
);
2171 tz
= g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,M10.1.0,M3.3.0");
2172 g_assert_cmpstr (g_time_zone_get_identifier (tz
), ==, "NZST-12:00:00NZDT-13:00:00,M10.1.0,M3.3.0");
2173 g_assert_cmpstr (g_time_zone_get_abbreviation (tz
, 0), ==, "NZST");
2174 g_assert_cmpint (g_time_zone_get_offset (tz
, 0), ==, 12 * 3600);
2175 g_assert (!g_time_zone_is_dst (tz
, 0));
2176 g_assert_cmpstr (g_time_zone_get_abbreviation (tz
, 1), ==, "NZDT");
2177 g_assert_cmpint (g_time_zone_get_offset (tz
, 1), ==, 13 * 3600);
2178 g_assert (g_time_zone_is_dst (tz
, 1));
2179 gdt1
= g_date_time_new (tz
, 2012, 3, 18, 0, 15, 23.0);
2180 gdt2
= g_date_time_new (tz
, 2012, 3, 18, 3, 15, 23.0);
2181 g_assert (g_date_time_is_daylight_savings (gdt1
));
2182 g_assert_cmpint (g_date_time_get_utc_offset (gdt1
) / 1000000, ==, 46800);
2183 g_assert (!g_date_time_is_daylight_savings (gdt2
));
2184 g_assert_cmpint (g_date_time_get_utc_offset (gdt2
) / 1000000, ==, 43200);
2185 g_date_time_unref (gdt1
);
2186 g_date_time_unref (gdt2
);
2187 gdt1
= g_date_time_new (tz
, 2012, 10, 7, 3, 15, 23.0);
2188 gdt2
= g_date_time_new (tz
, 2012, 10, 7, 1, 15, 23.0);
2189 g_assert (g_date_time_is_daylight_savings (gdt1
));
2190 g_assert_cmpint (g_date_time_get_utc_offset (gdt1
) / 1000000, ==, 46800);
2191 g_assert (!g_date_time_is_daylight_savings (gdt2
));
2192 g_assert_cmpint (g_date_time_get_utc_offset (gdt2
) / 1000000, ==, 43200);
2193 g_date_time_unref (gdt1
);
2194 g_date_time_unref (gdt2
);
2195 g_time_zone_unref (tz
);
2197 tz
= g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,280,77");
2198 g_assert_cmpstr (g_time_zone_get_identifier (tz
), ==, "NZST-12:00:00NZDT-13:00:00,280,77");
2199 g_assert_cmpstr (g_time_zone_get_abbreviation (tz
, 0), ==, "NZST");
2200 g_assert_cmpint (g_time_zone_get_offset (tz
, 0), ==, 12 * 3600);
2201 g_assert (!g_time_zone_is_dst (tz
, 0));
2202 g_assert_cmpstr (g_time_zone_get_abbreviation (tz
, 1), ==, "NZDT");
2203 g_assert_cmpint (g_time_zone_get_offset (tz
, 1), ==, 13 * 3600);
2204 g_assert (g_time_zone_is_dst (tz
, 1));
2205 gdt1
= g_date_time_new (tz
, 2012, 3, 18, 0, 15, 23.0);
2206 gdt2
= g_date_time_new (tz
, 2012, 3, 18, 3, 15, 23.0);
2207 g_assert (g_date_time_is_daylight_savings (gdt1
));
2208 g_assert_cmpint (g_date_time_get_utc_offset (gdt1
) / 1000000, ==, 46800);
2209 g_assert (!g_date_time_is_daylight_savings (gdt2
));
2210 g_assert_cmpint (g_date_time_get_utc_offset (gdt2
) / 1000000, ==, 43200);
2211 g_date_time_unref (gdt1
);
2212 g_date_time_unref (gdt2
);
2213 gdt1
= g_date_time_new (tz
, 2012, 10, 7, 3, 15, 23.0);
2214 gdt2
= g_date_time_new (tz
, 2012, 10, 7, 1, 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 g_time_zone_unref (tz
);
2223 tz
= g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,J279,J76");
2224 g_assert_cmpstr (g_time_zone_get_identifier (tz
), ==, "NZST-12:00:00NZDT-13:00:00,J279,J76");
2225 g_assert_cmpstr (g_time_zone_get_abbreviation (tz
, 0), ==, "NZST");
2226 g_assert_cmpint (g_time_zone_get_offset (tz
, 0), ==, 12 * 3600);
2227 g_assert (!g_time_zone_is_dst (tz
, 0));
2228 g_assert_cmpstr (g_time_zone_get_abbreviation (tz
, 1), ==, "NZDT");
2229 g_assert_cmpint (g_time_zone_get_offset (tz
, 1), ==, 13 * 3600);
2230 g_assert (g_time_zone_is_dst (tz
, 1));
2231 gdt1
= g_date_time_new (tz
, 2012, 3, 18, 0, 15, 23.0);
2232 gdt2
= g_date_time_new (tz
, 2012, 3, 18, 3, 15, 23.0);
2233 g_assert (g_date_time_is_daylight_savings (gdt1
));
2234 g_assert_cmpint (g_date_time_get_utc_offset (gdt1
) / 1000000, ==, 46800);
2235 g_assert (!g_date_time_is_daylight_savings (gdt2
));
2236 g_assert_cmpint (g_date_time_get_utc_offset (gdt2
) / 1000000, ==, 43200);
2237 g_date_time_unref (gdt1
);
2238 g_date_time_unref (gdt2
);
2239 gdt1
= g_date_time_new (tz
, 2012, 10, 7, 3, 15, 23.0);
2240 gdt2
= g_date_time_new (tz
, 2012, 10, 7, 1, 15, 23.0);
2241 g_assert (g_date_time_is_daylight_savings (gdt1
));
2242 g_assert_cmpint (g_date_time_get_utc_offset (gdt1
) / 1000000, ==, 46800);
2243 g_assert (!g_date_time_is_daylight_savings (gdt2
));
2244 g_assert_cmpint (g_date_time_get_utc_offset (gdt2
) / 1000000, ==, 43200);
2245 g_date_time_unref (gdt1
);
2246 g_date_time_unref (gdt2
);
2247 g_time_zone_unref (tz
);
2249 tz
= g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,M10.1.0/07:00,M3.3.0/07:00");
2250 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");
2251 g_assert_cmpstr (g_time_zone_get_abbreviation (tz
, 0), ==, "NZST");
2252 g_assert_cmpint (g_time_zone_get_offset (tz
, 0), ==, 12 * 3600);
2253 g_assert (!g_time_zone_is_dst (tz
, 0));
2254 g_assert_cmpstr (g_time_zone_get_abbreviation (tz
, 1), ==, "NZDT");
2255 g_assert_cmpint (g_time_zone_get_offset (tz
, 1), ==, 13 * 3600);
2256 g_assert (g_time_zone_is_dst (tz
, 1));
2257 gdt1
= g_date_time_new (tz
, 2012, 3, 18, 5, 15, 23.0);
2258 gdt2
= g_date_time_new (tz
, 2012, 3, 18, 8, 15, 23.0);
2259 g_assert (g_date_time_is_daylight_savings (gdt1
));
2260 g_assert_cmpint (g_date_time_get_utc_offset (gdt1
) / 1000000, ==, 46800);
2261 g_assert (!g_date_time_is_daylight_savings (gdt2
));
2262 g_assert_cmpint (g_date_time_get_utc_offset (gdt2
) / 1000000, ==, 43200);
2263 g_date_time_unref (gdt1
);
2264 g_date_time_unref (gdt2
);
2265 gdt1
= g_date_time_new (tz
, 2012, 10, 7, 8, 15, 23.0);
2266 gdt2
= g_date_time_new (tz
, 2012, 10, 7, 6, 15, 23.0);
2267 g_assert (g_date_time_is_daylight_savings (gdt1
));
2268 g_assert_cmpint (g_date_time_get_utc_offset (gdt1
) / 1000000, ==, 46800);
2269 g_assert (!g_date_time_is_daylight_savings (gdt2
));
2270 g_assert_cmpint (g_date_time_get_utc_offset (gdt2
) / 1000000, ==, 43200);
2271 g_date_time_unref (gdt1
);
2272 g_date_time_unref (gdt2
);
2273 gdt1
= g_date_time_new (tz
, 1902, 10, 7, 8, 15, 23.0);
2274 gdt2
= g_date_time_new (tz
, 1902, 10, 7, 6, 15, 23.0);
2275 g_assert (!g_date_time_is_daylight_savings (gdt1
));
2276 g_assert_cmpint (g_date_time_get_utc_offset (gdt1
) / 1000000, ==, 43200);
2277 g_assert (!g_date_time_is_daylight_savings (gdt2
));
2278 g_assert_cmpint (g_date_time_get_utc_offset (gdt2
) / 1000000, ==, 43200);
2279 g_date_time_unref (gdt1
);
2280 g_date_time_unref (gdt2
);
2281 gdt1
= g_date_time_new (tz
, 2142, 10, 7, 8, 15, 23.0);
2282 gdt2
= g_date_time_new (tz
, 2142, 10, 7, 6, 15, 23.0);
2283 g_assert (g_date_time_is_daylight_savings (gdt1
));
2284 g_assert_cmpint (g_date_time_get_utc_offset (gdt1
) / 1000000, ==, 46800);
2285 g_assert (!g_date_time_is_daylight_savings (gdt2
));
2286 g_assert_cmpint (g_date_time_get_utc_offset (gdt2
) / 1000000, ==, 43200);
2287 g_date_time_unref (gdt1
);
2288 g_date_time_unref (gdt2
);
2289 gdt1
= g_date_time_new (tz
, 3212, 10, 7, 8, 15, 23.0);
2290 gdt2
= g_date_time_new (tz
, 3212, 10, 7, 6, 15, 23.0);
2291 g_assert (!g_date_time_is_daylight_savings (gdt1
));
2292 g_assert_cmpint (g_date_time_get_utc_offset (gdt1
) / 1000000, ==, 43200);
2293 g_assert (!g_date_time_is_daylight_savings (gdt2
));
2294 g_assert_cmpint (g_date_time_get_utc_offset (gdt2
) / 1000000, ==, 43200);
2295 g_date_time_unref (gdt1
);
2296 g_date_time_unref (gdt2
);
2297 g_time_zone_unref (tz
);
2301 test_GDateTime_floating_point (void)
2306 g_test_bug ("697715");
2308 tz
= g_time_zone_new ("-03:00");
2309 g_assert_cmpstr (g_time_zone_get_identifier (tz
), ==, "-03:00");
2310 dt
= g_date_time_new (tz
, 2010, 5, 24, 8, 0, 1.000001);
2311 g_time_zone_unref (tz
);
2312 g_assert_cmpint (g_date_time_get_microsecond (dt
), ==, 1);
2313 g_date_time_unref (dt
);
2316 /* Check that g_time_zone_get_identifier() returns the identifier given to
2317 * g_time_zone_new(), or "UTC" if loading the timezone failed. */
2319 test_identifier (void)
2322 gchar
*old_tz
= g_strdup (g_getenv ("TZ"));
2324 tz
= g_time_zone_new ("UTC");
2325 g_assert_cmpstr (g_time_zone_get_identifier (tz
), ==, "UTC");
2326 g_time_zone_unref (tz
);
2328 tz
= g_time_zone_new_utc ();
2329 g_assert_cmpstr (g_time_zone_get_identifier (tz
), ==, "UTC");
2330 g_time_zone_unref (tz
);
2332 tz
= g_time_zone_new ("some rubbish");
2333 g_assert_cmpstr (g_time_zone_get_identifier (tz
), ==, "UTC");
2334 g_time_zone_unref (tz
);
2336 tz
= g_time_zone_new ("Z");
2337 g_assert_cmpstr (g_time_zone_get_identifier (tz
), ==, "Z");
2338 g_time_zone_unref (tz
);
2340 tz
= g_time_zone_new ("+03:15");
2341 g_assert_cmpstr (g_time_zone_get_identifier (tz
), ==, "+03:15");
2342 g_time_zone_unref (tz
);
2344 /* System timezone. We can’t change this, but we can at least assert that
2345 * the identifier is non-NULL and doesn’t start with a slash. */
2346 tz
= g_time_zone_new (NULL
);
2347 g_assert_nonnull (g_time_zone_get_identifier (tz
));
2348 g_assert_cmpstr (g_time_zone_get_identifier (tz
), !=, "");
2349 g_assert_true (*g_time_zone_get_identifier (tz
) != '/');
2350 g_time_zone_unref (tz
);
2352 /* Local timezone tests. */
2353 if (g_setenv ("TZ", "America/Recife", TRUE
))
2355 tz
= g_time_zone_new_local ();
2356 g_assert_cmpstr (g_time_zone_get_identifier (tz
), ==, "America/Recife");
2357 g_time_zone_unref (tz
);
2360 if (g_setenv ("TZ", "some rubbish", TRUE
))
2362 tz
= g_time_zone_new_local ();
2363 g_assert_cmpstr (g_time_zone_get_identifier (tz
), ==, "UTC");
2364 g_time_zone_unref (tz
);
2368 g_assert_true (g_setenv ("TZ", old_tz
, TRUE
));
2375 /* Test various calls to g_time_zone_new_offset(). */
2377 test_new_offset (void)
2379 const gint32 vectors
[] =
2395 for (i
= 0; i
< G_N_ELEMENTS (vectors
); i
++)
2397 GTimeZone
*tz
= NULL
;
2399 g_test_message ("Vector %" G_GSIZE_FORMAT
": %d", i
, vectors
[i
]);
2401 tz
= g_time_zone_new_offset (vectors
[i
]);
2402 g_assert_nonnull (tz
);
2403 g_assert_cmpstr (g_time_zone_get_identifier (tz
), !=, "UTC");
2404 g_assert_cmpint (g_time_zone_get_offset (tz
, 0), ==, vectors
[i
]);
2405 g_time_zone_unref (tz
);
2413 g_test_init (&argc
, &argv
, NULL
);
2414 g_test_bug_base ("http://bugzilla.gnome.org/");
2416 /* GDateTime Tests */
2417 bind_textdomain_codeset ("glib20", "UTF-8");
2419 g_test_add_func ("/GDateTime/invalid", test_GDateTime_invalid
);
2420 g_test_add_func ("/GDateTime/add_days", test_GDateTime_add_days
);
2421 g_test_add_func ("/GDateTime/add_full", test_GDateTime_add_full
);
2422 g_test_add_func ("/GDateTime/add_hours", test_GDateTime_add_hours
);
2423 g_test_add_func ("/GDateTime/add_minutes", test_GDateTime_add_minutes
);
2424 g_test_add_func ("/GDateTime/add_months", test_GDateTime_add_months
);
2425 g_test_add_func ("/GDateTime/add_seconds", test_GDateTime_add_seconds
);
2426 g_test_add_func ("/GDateTime/add_weeks", test_GDateTime_add_weeks
);
2427 g_test_add_func ("/GDateTime/add_years", test_GDateTime_add_years
);
2428 g_test_add_func ("/GDateTime/compare", test_GDateTime_compare
);
2429 g_test_add_func ("/GDateTime/diff", test_GDateTime_diff
);
2430 g_test_add_func ("/GDateTime/equal", test_GDateTime_equal
);
2431 g_test_add_func ("/GDateTime/get_day_of_week", test_GDateTime_get_day_of_week
);
2432 g_test_add_func ("/GDateTime/get_day_of_month", test_GDateTime_get_day_of_month
);
2433 g_test_add_func ("/GDateTime/get_day_of_year", test_GDateTime_get_day_of_year
);
2434 g_test_add_func ("/GDateTime/get_hour", test_GDateTime_get_hour
);
2435 g_test_add_func ("/GDateTime/get_microsecond", test_GDateTime_get_microsecond
);
2436 g_test_add_func ("/GDateTime/get_minute", test_GDateTime_get_minute
);
2437 g_test_add_func ("/GDateTime/get_month", test_GDateTime_get_month
);
2438 g_test_add_func ("/GDateTime/get_second", test_GDateTime_get_second
);
2439 g_test_add_func ("/GDateTime/get_utc_offset", test_GDateTime_get_utc_offset
);
2440 g_test_add_func ("/GDateTime/get_year", test_GDateTime_get_year
);
2441 g_test_add_func ("/GDateTime/hash", test_GDateTime_hash
);
2442 g_test_add_func ("/GDateTime/new_from_unix", test_GDateTime_new_from_unix
);
2443 g_test_add_func ("/GDateTime/new_from_unix_utc", test_GDateTime_new_from_unix_utc
);
2444 g_test_add_func ("/GDateTime/new_from_unix/overflow", test_GDateTime_new_from_unix_overflow
);
2445 g_test_add_func ("/GDateTime/new_from_timeval", test_GDateTime_new_from_timeval
);
2446 g_test_add_func ("/GDateTime/new_from_timeval_utc", test_GDateTime_new_from_timeval_utc
);
2447 g_test_add_func ("/GDateTime/new_from_timeval/overflow", test_GDateTime_new_from_timeval_overflow
);
2448 g_test_add_func ("/GDateTime/new_from_iso8601", test_GDateTime_new_from_iso8601
);
2449 g_test_add_func ("/GDateTime/new_full", test_GDateTime_new_full
);
2450 g_test_add_func ("/GDateTime/now", test_GDateTime_now
);
2451 g_test_add_func ("/GDateTime/printf", test_GDateTime_printf
);
2452 g_test_add_func ("/GDateTime/non_utf8_printf", test_non_utf8_printf
);
2453 g_test_add_func ("/GDateTime/strftime", test_strftime
);
2454 g_test_add_func ("/GDateTime/strftime/error_handling", test_GDateTime_strftime_error_handling
);
2455 g_test_add_func ("/GDateTime/modifiers", test_modifiers
);
2456 g_test_add_func ("/GDateTime/month_names", test_month_names
);
2457 g_test_add_func ("/GDateTime/to_local", test_GDateTime_to_local
);
2458 g_test_add_func ("/GDateTime/to_unix", test_GDateTime_to_unix
);
2459 g_test_add_func ("/GDateTime/to_timeval", test_GDateTime_to_timeval
);
2460 g_test_add_func ("/GDateTime/to_utc", test_GDateTime_to_utc
);
2461 g_test_add_func ("/GDateTime/now_utc", test_GDateTime_now_utc
);
2462 g_test_add_func ("/GDateTime/dst", test_GDateTime_dst
);
2463 g_test_add_func ("/GDateTime/test_z", test_z
);
2464 g_test_add_func ("/GDateTime/test-all-dates", test_all_dates
);
2465 g_test_add_func ("/GTimeZone/find-interval", test_find_interval
);
2466 g_test_add_func ("/GTimeZone/adjust-time", test_adjust_time
);
2467 g_test_add_func ("/GTimeZone/no-header", test_no_header
);
2468 g_test_add_func ("/GTimeZone/posix-parse", test_posix_parse
);
2469 g_test_add_func ("/GTimeZone/floating-point", test_GDateTime_floating_point
);
2470 g_test_add_func ("/GTimeZone/identifier", test_identifier
);
2471 g_test_add_func ("/GTimeZone/new-offset", test_new_offset
);
2473 return g_test_run ();