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
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <glib/gstdio.h>
28 #define ASSERT_DATE(dt,y,m,d) G_STMT_START { \
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))); \
33 #define ASSERT_TIME(dt,H,M,S) G_STMT_START { \
34 g_assert_cmpint ((H), ==, g_date_time_get_hour ((dt))); \
35 g_assert_cmpint ((M), ==, g_date_time_get_minute ((dt))); \
36 g_assert_cmpint ((S), ==, g_date_time_get_second ((dt))); \
40 get_localtime_tm (time_t time_,
43 #ifdef HAVE_LOCALTIME_R
44 localtime_r (&time_, retval);
47 struct tm *ptm = localtime (&time_);
51 /* Happens at least in Microsoft's C library if you pass a
52 * negative time_t. Use 2000-01-01 as default date.
54 #ifndef G_DISABLE_CHECKS
55 g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, "ptm != NULL");
60 retval->tm_year = 100;
63 memcpy ((void *) retval, (void *) ptm, sizeof (struct tm));
65 #endif /* HAVE_LOCALTIME_R */
69 test_GDateTime_now (void)
74 memset (&tm, 0, sizeof (tm));
75 get_localtime_tm (time (NULL), &tm);
77 dt = g_date_time_new_now_local ();
79 g_assert_cmpint (g_date_time_get_year (dt), ==, 1900 + tm.tm_year);
80 g_assert_cmpint (g_date_time_get_month (dt), ==, 1 + tm.tm_mon);
81 g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, tm.tm_mday);
82 g_assert_cmpint (g_date_time_get_hour (dt), ==, tm.tm_hour);
83 g_assert_cmpint (g_date_time_get_minute (dt), ==, tm.tm_min);
84 /* XXX we need some fuzzyness here */
85 g_assert_cmpint (g_date_time_get_second (dt), >=, tm.tm_sec);
87 g_date_time_unref (dt);
91 test_GDateTime_new_from_unix (void)
97 memset (&tm, 0, sizeof (tm));
99 get_localtime_tm (t, &tm);
101 dt = g_date_time_new_from_unix_local (t);
102 g_assert_cmpint (g_date_time_get_year (dt), ==, 1900 + tm.tm_year);
103 g_assert_cmpint (g_date_time_get_month (dt), ==, 1 + tm.tm_mon);
104 g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, tm.tm_mday);
105 g_assert_cmpint (g_date_time_get_hour (dt), ==, tm.tm_hour);
106 g_assert_cmpint (g_date_time_get_minute (dt), ==, tm.tm_min);
107 g_assert_cmpint (g_date_time_get_second (dt), ==, tm.tm_sec);
108 g_date_time_unref (dt);
110 memset (&tm, 0, sizeof (tm));
119 dt = g_date_time_new_from_unix_local (t);
120 g_assert_cmpint (g_date_time_get_year (dt), ==, 1990);
121 g_assert_cmpint (g_date_time_get_month (dt), ==, 1);
122 g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 1);
123 g_assert_cmpint (g_date_time_get_hour (dt), ==, 0);
124 g_assert_cmpint (g_date_time_get_minute (dt), ==, 0);
125 g_assert_cmpint (g_date_time_get_second (dt), ==, 0);
126 g_date_time_unref (dt);
130 test_GDateTime_compare (void)
132 GDateTime *dt1, *dt2;
135 dt1 = g_date_time_new_utc (2000, 1, 1, 0, 0, 0);
137 for (i = 1; i < 2000; i++)
139 dt2 = g_date_time_new_utc (i, 12, 31, 0, 0, 0);
140 g_assert_cmpint (1, ==, g_date_time_compare (dt1, dt2));
141 g_date_time_unref (dt2);
144 dt2 = g_date_time_new_utc (1999, 12, 31, 23, 59, 59);
145 g_assert_cmpint (1, ==, g_date_time_compare (dt1, dt2));
146 g_date_time_unref (dt2);
148 dt2 = g_date_time_new_utc (2000, 1, 1, 0, 0, 1);
149 g_assert_cmpint (-1, ==, g_date_time_compare (dt1, dt2));
150 g_date_time_unref (dt2);
152 dt2 = g_date_time_new_utc (2000, 1, 1, 0, 0, 0);
153 g_assert_cmpint (0, ==, g_date_time_compare (dt1, dt2));
154 g_date_time_unref (dt2);
155 g_date_time_unref (dt1);
159 test_GDateTime_equal (void)
161 GDateTime *dt1, *dt2;
164 dt1 = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
165 dt2 = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
166 g_assert (g_date_time_equal (dt1, dt2));
167 g_date_time_unref (dt1);
168 g_date_time_unref (dt2);
170 dt1 = g_date_time_new_local (2009, 10, 18, 0, 0, 0);
171 dt2 = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
172 g_assert (!g_date_time_equal (dt1, dt2));
173 g_date_time_unref (dt1);
174 g_date_time_unref (dt2);
176 /* UTC-0300 and not in DST */
177 tz = g_time_zone_new ("-03:00");
178 dt1 = g_date_time_new (tz, 2010, 5, 24, 8, 0, 0);
179 g_time_zone_unref (tz);
180 g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, (-3 * 3600));
182 dt2 = g_date_time_new_utc (2010, 5, 24, 11, 0, 0);
183 g_assert_cmpint (g_date_time_get_utc_offset (dt2), ==, 0);
185 g_assert (g_date_time_equal (dt1, dt2));
186 g_date_time_unref (dt1);
188 /* America/Recife is in UTC-0300 */
189 tz = g_time_zone_new ("America/Recife");
190 dt1 = g_date_time_new (tz, 2010, 5, 24, 8, 0, 0);
191 g_time_zone_unref (tz);
192 g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, (-3 * 3600));
193 g_assert (g_date_time_equal (dt1, dt2));
194 g_date_time_unref (dt1);
195 g_date_time_unref (dt2);
199 test_GDateTime_get_day_of_week (void)
203 dt = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
204 g_assert_cmpint (1, ==, g_date_time_get_day_of_week (dt));
205 g_date_time_unref (dt);
207 dt = g_date_time_new_local (2000, 10, 1, 0, 0, 0);
208 g_assert_cmpint (7, ==, g_date_time_get_day_of_week (dt));
209 g_date_time_unref (dt);
213 test_GDateTime_get_day_of_month (void)
217 dt = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
218 g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 19);
219 g_date_time_unref (dt);
221 dt = g_date_time_new_local (1400, 3, 12, 0, 0, 0);
222 g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 12);
223 g_date_time_unref (dt);
225 dt = g_date_time_new_local (1800, 12, 31, 0, 0, 0);
226 g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 31);
227 g_date_time_unref (dt);
229 dt = g_date_time_new_local (1000, 1, 1, 0, 0, 0);
230 g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 1);
231 g_date_time_unref (dt);
235 test_GDateTime_get_hour (void)
239 dt = g_date_time_new_utc (2009, 10, 19, 15, 13, 11);
240 g_assert_cmpint (15, ==, g_date_time_get_hour (dt));
241 g_date_time_unref (dt);
243 dt = g_date_time_new_utc (100, 10, 19, 1, 0, 0);
244 g_assert_cmpint (1, ==, g_date_time_get_hour (dt));
245 g_date_time_unref (dt);
247 dt = g_date_time_new_utc (100, 10, 19, 0, 0, 0);
248 g_assert_cmpint (0, ==, g_date_time_get_hour (dt));
249 g_date_time_unref (dt);
251 dt = g_date_time_new_utc (100, 10, 1, 23, 59, 59);
252 g_assert_cmpint (23, ==, g_date_time_get_hour (dt));
253 g_date_time_unref (dt);
257 test_GDateTime_get_microsecond (void)
262 g_get_current_time (&tv);
263 dt = g_date_time_new_from_timeval_local (&tv);
264 g_assert_cmpint (tv.tv_usec, ==, g_date_time_get_microsecond (dt));
265 g_date_time_unref (dt);
269 test_GDateTime_get_year (void)
273 dt = g_date_time_new_local (2009, 1, 1, 0, 0, 0);
274 g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
275 g_date_time_unref (dt);
277 dt = g_date_time_new_local (1, 1, 1, 0, 0, 0);
278 g_assert_cmpint (1, ==, g_date_time_get_year (dt));
279 g_date_time_unref (dt);
281 dt = g_date_time_new_local (13, 1, 1, 0, 0, 0);
282 g_assert_cmpint (13, ==, g_date_time_get_year (dt));
283 g_date_time_unref (dt);
285 dt = g_date_time_new_local (3000, 1, 1, 0, 0, 0);
286 g_assert_cmpint (3000, ==, g_date_time_get_year (dt));
287 g_date_time_unref (dt);
291 test_GDateTime_hash (void)
295 h = g_hash_table_new_full (g_date_time_hash, g_date_time_equal,
296 (GDestroyNotify)g_date_time_unref,
298 g_hash_table_insert (h, g_date_time_new_now_local (), NULL);
299 g_hash_table_remove_all (h);
300 g_hash_table_destroy (h);
304 test_GDateTime_new_from_timeval (void)
309 g_get_current_time (&tv);
310 dt = g_date_time_new_from_timeval_local (&tv);
312 if (g_test_verbose ())
313 g_print ("\nDT%04d-%02d-%02dT%02d:%02d:%02d%s\n",
314 g_date_time_get_year (dt),
315 g_date_time_get_month (dt),
316 g_date_time_get_day_of_month (dt),
317 g_date_time_get_hour (dt),
318 g_date_time_get_minute (dt),
319 g_date_time_get_second (dt),
320 g_date_time_get_timezone_abbreviation (dt));
322 g_date_time_to_timeval (dt, &tv2);
323 g_assert_cmpint (tv.tv_sec, ==, tv2.tv_sec);
324 g_assert_cmpint (tv.tv_usec, ==, tv2.tv_usec);
325 g_date_time_unref (dt);
329 test_GDateTime_to_unix (void)
335 dt = g_date_time_new_from_unix_local (t);
336 g_assert_cmpint (g_date_time_to_unix (dt), ==, t);
337 g_date_time_unref (dt);
341 test_GDateTime_add_years (void)
345 dt = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
346 dt2 = g_date_time_add_years (dt, 1);
347 g_assert_cmpint (2010, ==, g_date_time_get_year (dt2));
348 g_date_time_unref (dt);
349 g_date_time_unref (dt2);
353 test_GDateTime_add_months (void)
355 #define TEST_ADD_MONTHS(y,m,d,a,ny,nm,nd) G_STMT_START { \
356 GDateTime *dt, *dt2; \
357 dt = g_date_time_new_utc (y, m, d, 0, 0, 0); \
358 dt2 = g_date_time_add_months (dt, a); \
359 ASSERT_DATE (dt2, ny, nm, nd); \
360 g_date_time_unref (dt); \
361 g_date_time_unref (dt2); \
364 TEST_ADD_MONTHS (2009, 12, 31, 1, 2010, 1, 31);
365 TEST_ADD_MONTHS (2009, 12, 31, 1, 2010, 1, 31);
366 TEST_ADD_MONTHS (2009, 6, 15, 1, 2009, 7, 15);
367 TEST_ADD_MONTHS (1400, 3, 1, 1, 1400, 4, 1);
368 TEST_ADD_MONTHS (1400, 1, 31, 1, 1400, 2, 28);
369 TEST_ADD_MONTHS (1400, 1, 31, 7200, 2000, 1, 31);
370 TEST_ADD_MONTHS (2008, 2, 29, 12, 2009, 2, 28);
371 TEST_ADD_MONTHS (2000, 8, 16, -5, 2000, 3, 16);
372 TEST_ADD_MONTHS (2000, 8, 16, -12, 1999, 8, 16);
373 TEST_ADD_MONTHS (2011, 2, 1, -13, 2010, 1, 1);
374 TEST_ADD_MONTHS (1776, 7, 4, 1200, 1876, 7, 4);
378 test_GDateTime_add_days (void)
380 #define TEST_ADD_DAYS(y,m,d,a,ny,nm,nd) G_STMT_START { \
381 GDateTime *dt, *dt2; \
382 dt = g_date_time_new_local (y, m, d, 0, 0, 0); \
383 dt2 = g_date_time_add_days (dt, a); \
384 g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
385 g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
386 g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
387 g_date_time_unref (dt); \
388 g_date_time_unref (dt2); \
391 TEST_ADD_DAYS (2009, 1, 31, 1, 2009, 2, 1);
392 TEST_ADD_DAYS (2009, 2, 1, -1, 2009, 1, 31);
393 TEST_ADD_DAYS (2008, 2, 28, 1, 2008, 2, 29);
394 TEST_ADD_DAYS (2008, 12, 31, 1, 2009, 1, 1);
395 TEST_ADD_DAYS (1, 1, 1, 1, 1, 1, 2);
396 TEST_ADD_DAYS (1955, 5, 24, 10, 1955, 6, 3);
397 TEST_ADD_DAYS (1955, 5, 24, -10, 1955, 5, 14);
401 test_GDateTime_add_weeks (void)
403 #define TEST_ADD_WEEKS(y,m,d,a,ny,nm,nd) G_STMT_START { \
404 GDateTime *dt, *dt2; \
405 dt = g_date_time_new_local (y, m, d, 0, 0, 0); \
406 dt2 = g_date_time_add_weeks (dt, a); \
407 g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
408 g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
409 g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
410 g_date_time_unref (dt); \
411 g_date_time_unref (dt2); \
414 TEST_ADD_WEEKS (2009, 1, 1, 1, 2009, 1, 8);
415 TEST_ADD_WEEKS (2009, 8, 30, 1, 2009, 9, 6);
416 TEST_ADD_WEEKS (2009, 12, 31, 1, 2010, 1, 7);
417 TEST_ADD_WEEKS (2009, 1, 1, -1, 2008, 12, 25);
421 test_GDateTime_add_hours (void)
423 #define TEST_ADD_HOURS(y,m,d,h,mi,s,a,ny,nm,nd,nh,nmi,ns) G_STMT_START { \
424 GDateTime *dt, *dt2; \
425 dt = g_date_time_new_utc (y, m, d, h, mi, s); \
426 dt2 = g_date_time_add_hours (dt, a); \
427 g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
428 g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
429 g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
430 g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
431 g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
432 g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
433 g_date_time_unref (dt); \
434 g_date_time_unref (dt2); \
437 TEST_ADD_HOURS (2009, 1, 1, 0, 0, 0, 1, 2009, 1, 1, 1, 0, 0);
438 TEST_ADD_HOURS (2008, 12, 31, 23, 0, 0, 1, 2009, 1, 1, 0, 0, 0);
442 test_GDateTime_add_full (void)
444 #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 { \
445 GDateTime *dt, *dt2; \
446 dt = g_date_time_new_utc (y, m, d, h, mi, s); \
447 dt2 = g_date_time_add_full (dt, ay, am, ad, ah, ami, as); \
448 g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
449 g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
450 g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
451 g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
452 g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
453 g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
454 g_date_time_unref (dt); \
455 g_date_time_unref (dt2); \
458 TEST_ADD_FULL (2009, 10, 21, 0, 0, 0,
460 2010, 11, 22, 1, 1, 1);
461 TEST_ADD_FULL (2000, 1, 1, 1, 1, 1,
463 2000, 2, 1, 1, 1, 1);
464 TEST_ADD_FULL (2000, 1, 1, 0, 0, 0,
466 1999, 2, 1, 0, 0, 0);
467 TEST_ADD_FULL (2010, 10, 31, 0, 0, 0,
469 2011, 2, 28, 0, 0, 0);
470 TEST_ADD_FULL (2010, 8, 25, 22, 45, 0,
472 2010, 10, 2, 0, 10, 0);
476 test_GDateTime_add_minutes (void)
478 #define TEST_ADD_MINUTES(i,o) G_STMT_START { \
479 GDateTime *dt, *dt2; \
480 dt = g_date_time_new_local (2000, 1, 1, 0, 0, 0); \
481 dt2 = g_date_time_add_minutes (dt, i); \
482 g_assert_cmpint (o, ==, g_date_time_get_minute (dt2)); \
483 g_date_time_unref (dt); \
484 g_date_time_unref (dt2); \
487 TEST_ADD_MINUTES (60, 0);
488 TEST_ADD_MINUTES (100, 40);
489 TEST_ADD_MINUTES (5, 5);
490 TEST_ADD_MINUTES (1441, 1);
491 TEST_ADD_MINUTES (-1441, 59);
495 test_GDateTime_add_seconds (void)
497 #define TEST_ADD_SECONDS(i,o) G_STMT_START { \
498 GDateTime *dt, *dt2; \
499 dt = g_date_time_new_local (2000, 1, 1, 0, 0, 0); \
500 dt2 = g_date_time_add_seconds (dt, i); \
501 g_assert_cmpint (o, ==, g_date_time_get_second (dt2)); \
502 g_date_time_unref (dt); \
503 g_date_time_unref (dt2); \
506 TEST_ADD_SECONDS (1, 1);
507 TEST_ADD_SECONDS (60, 0);
508 TEST_ADD_SECONDS (61, 1);
509 TEST_ADD_SECONDS (120, 0);
510 TEST_ADD_SECONDS (-61, 59);
511 TEST_ADD_SECONDS (86401, 1);
512 TEST_ADD_SECONDS (-86401, 59);
513 TEST_ADD_SECONDS (-31, 29);
514 TEST_ADD_SECONDS (13, 13);
518 test_GDateTime_diff (void)
520 #define TEST_DIFF(y,m,d,y2,m2,d2,u) G_STMT_START { \
521 GDateTime *dt1, *dt2; \
523 dt1 = g_date_time_new_local (y, m, d, 0, 0, 0); \
524 dt2 = g_date_time_new_local (y2, m2, d2, 0, 0, 0); \
525 ts = g_date_time_difference (dt2, dt1); \
526 g_assert_cmpint (ts, ==, u); \
527 g_date_time_unref (dt1); \
528 g_date_time_unref (dt2); \
531 TEST_DIFF (2009, 1, 1, 2009, 2, 1, G_TIME_SPAN_DAY * 31);
532 TEST_DIFF (2009, 1, 1, 2010, 1, 1, G_TIME_SPAN_DAY * 365);
533 TEST_DIFF (2008, 2, 28, 2008, 2, 29, G_TIME_SPAN_DAY);
534 TEST_DIFF (2008, 2, 29, 2008, 2, 28, -G_TIME_SPAN_DAY);
536 /* TODO: Add usec tests */
540 test_GDateTime_get_minute (void)
544 dt = g_date_time_new_utc (2009, 12, 1, 1, 31, 0);
545 g_assert_cmpint (31, ==, g_date_time_get_minute (dt));
546 g_date_time_unref (dt);
550 test_GDateTime_get_month (void)
554 dt = g_date_time_new_utc (2009, 12, 1, 1, 31, 0);
555 g_assert_cmpint (12, ==, g_date_time_get_month (dt));
556 g_date_time_unref (dt);
560 test_GDateTime_get_second (void)
564 dt = g_date_time_new_utc (2009, 12, 1, 1, 31, 44);
565 g_assert_cmpint (44, ==, g_date_time_get_second (dt));
566 g_date_time_unref (dt);
570 test_GDateTime_new_full (void)
575 dt = g_date_time_new_utc (2009, 12, 11, 12, 11, 10);
576 g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
577 g_assert_cmpint (12, ==, g_date_time_get_month (dt));
578 g_assert_cmpint (11, ==, g_date_time_get_day_of_month (dt));
579 g_assert_cmpint (12, ==, g_date_time_get_hour (dt));
580 g_assert_cmpint (11, ==, g_date_time_get_minute (dt));
581 g_assert_cmpint (10, ==, g_date_time_get_second (dt));
582 g_date_time_unref (dt);
584 tz = g_time_zone_new ("America/Recife");
585 dt = g_date_time_new (tz, 2010, 5, 24, 8, 4, 0);
586 g_time_zone_unref (tz);
587 g_assert_cmpint (2010, ==, g_date_time_get_year (dt));
588 g_assert_cmpint (5, ==, g_date_time_get_month (dt));
589 g_assert_cmpint (24, ==, g_date_time_get_day_of_month (dt));
590 g_assert_cmpint (8, ==, g_date_time_get_hour (dt));
591 g_assert_cmpint (4, ==, g_date_time_get_minute (dt));
592 g_assert_cmpint (0, ==, g_date_time_get_second (dt));
593 g_assert_cmpstr ("BRT", ==, g_date_time_get_timezone_abbreviation (dt));
594 g_assert (!g_date_time_is_daylight_savings (dt));
595 g_date_time_unref (dt);
599 test_GDateTime_now_utc (void)
610 struct tm *tmp = gmtime (&t);
611 /* Assume gmtime() can't fail as we got t from time(NULL). (Note
612 * that on Windows, gmtime() *is* MT-safe, it uses a thread-local
615 memcpy (&tm, tmp, sizeof (struct tm));
618 dt = g_date_time_new_now_utc ();
619 g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
620 g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
621 g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
622 g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
623 g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
624 g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
625 g_date_time_unref (dt);
629 test_GDateTime_new_from_unix_utc (void)
634 t = g_get_real_time ();
637 dt = g_date_time_new_from_unix_utc (t);
638 g_assert (dt == NULL);
641 t = t / 1e6; /* oops, this was microseconds */
643 dt = g_date_time_new_from_unix_utc (t);
644 g_assert (dt != NULL);
646 g_assert (dt == g_date_time_ref (dt));
647 g_date_time_unref (dt);
648 g_assert_cmpint (g_date_time_to_unix (dt), ==, t);
649 g_date_time_unref (dt);
653 test_GDateTime_get_utc_offset (void)
659 memset (&tm, 0, sizeof (tm));
660 get_localtime_tm (time (NULL), &tm);
662 dt = g_date_time_new_now_local ();
663 ts = g_date_time_get_utc_offset (dt);
664 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
665 g_assert_cmpint (ts, ==, (tm.tm_gmtoff * G_TIME_SPAN_SECOND));
667 #ifdef HAVE_STRUCT_TM___TM_GMTOFF
668 g_assert_cmpint (ts, ==, (tm.__tm_gmtoff * G_TIME_SPAN_SECOND));
670 g_date_time_unref (dt);
674 test_GDateTime_to_timeval (void)
679 memset (&tv1, 0, sizeof (tv1));
680 memset (&tv2, 0, sizeof (tv2));
682 g_get_current_time (&tv1);
683 dt = g_date_time_new_from_timeval_local (&tv1);
684 g_date_time_to_timeval (dt, &tv2);
685 g_assert_cmpint (tv1.tv_sec, ==, tv2.tv_sec);
686 g_assert_cmpint (tv1.tv_usec, ==, tv2.tv_usec);
687 g_date_time_unref (dt);
691 test_GDateTime_to_local (void)
693 GDateTime *utc, *now, *dt;
695 utc = g_date_time_new_now_utc ();
696 now = g_date_time_new_now_local ();
697 dt = g_date_time_to_local (utc);
699 g_assert_cmpint (g_date_time_get_year (now), ==, g_date_time_get_year (dt));
700 g_assert_cmpint (g_date_time_get_month (now), ==, g_date_time_get_month (dt));
701 g_assert_cmpint (g_date_time_get_day_of_month (now), ==, g_date_time_get_day_of_month (dt));
702 g_assert_cmpint (g_date_time_get_hour (now), ==, g_date_time_get_hour (dt));
703 g_assert_cmpint (g_date_time_get_minute (now), ==, g_date_time_get_minute (dt));
704 g_assert_cmpint (g_date_time_get_second (now), ==, g_date_time_get_second (dt));
706 g_date_time_unref (now);
707 g_date_time_unref (utc);
708 g_date_time_unref (dt);
712 test_GDateTime_to_utc (void)
723 struct tm *tmp = gmtime (&t);
724 memcpy (&tm, tmp, sizeof (struct tm));
727 dt2 = g_date_time_new_now_local ();
728 dt = g_date_time_to_utc (dt2);
729 g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
730 g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
731 g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
732 g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
733 g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
734 g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
735 g_date_time_unref (dt);
736 g_date_time_unref (dt2);
740 test_GDateTime_get_day_of_year (void)
742 #define TEST_DAY_OF_YEAR(y,m,d,o) G_STMT_START { \
743 GDateTime *__dt = g_date_time_new_local ((y), (m), (d), 0, 0, 0); \
744 g_assert_cmpint ((o), ==, g_date_time_get_day_of_year (__dt)); \
745 g_date_time_unref (__dt); } G_STMT_END
747 TEST_DAY_OF_YEAR (2009, 1, 1, 1);
748 TEST_DAY_OF_YEAR (2009, 2, 1, 32);
749 TEST_DAY_OF_YEAR (2009, 8, 16, 228);
750 TEST_DAY_OF_YEAR (2008, 8, 16, 229);
754 test_GDateTime_printf (void)
761 #define TEST_PRINTF(f,o) G_STMT_START { \
762 GDateTime *__dt = g_date_time_new_local (2009, 10, 24, 0, 0, 0);\
763 gchar *__p = g_date_time_format (__dt, (f)); \
764 g_assert_cmpstr (__p, ==, (o)); \
765 g_date_time_unref (__dt); \
766 g_free (__p); } G_STMT_END
768 #define TEST_PRINTF_DATE(y,m,d,f,o) G_STMT_START { \
769 GDateTime *dt = g_date_time_new_local (y, m, d, 0, 0, 0); \
770 gchar *p = g_date_time_format (dt, (f)); \
771 g_assert_cmpstr (p, ==, (o)); \
772 g_date_time_unref (dt); \
773 g_free (p); } G_STMT_END
775 #define TEST_PRINTF_TIME(h,m,s,f,o) G_STMT_START { \
776 GDateTime *dt = g_date_time_new_local (2009, 10, 24, (h), (m), (s)); \
777 gchar *p = g_date_time_format (dt, (f)); \
778 g_assert_cmpstr (p, ==, (o)); \
779 g_date_time_unref (dt); \
780 g_free (p); } G_STMT_END
783 * This is a little helper to make sure we can compare timezones to
784 * that of the generated timezone.
787 memset (&tt, 0, sizeof(tt));
788 get_localtime_tm (t, &tt);
789 tt.tm_year = 2009 - 1900;
790 tt.tm_mon = 9; /* 0 indexed */
793 memset (&tt, 0, sizeof(tt));
794 get_localtime_tm (t, &tt);
795 strftime (dst, sizeof(dst), "%Z", &tt);
797 /* get current time_t for 20090924 in the local timezone */
802 g_sprintf (t_str, "%ld", t);
804 TEST_PRINTF ("%a", "Sat");
805 TEST_PRINTF ("%A", "Saturday");
806 TEST_PRINTF ("%b", "Oct");
807 TEST_PRINTF ("%B", "October");
808 TEST_PRINTF ("%d", "24");
809 TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
810 TEST_PRINTF ("%e", "24"); // fixme
811 TEST_PRINTF ("%h", "Oct");
812 TEST_PRINTF ("%H", "00");
813 TEST_PRINTF_TIME (15, 0, 0, "%H", "15");
814 TEST_PRINTF ("%I", "12");
815 TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
816 TEST_PRINTF_TIME (15, 0, 0, "%I", "03");
817 TEST_PRINTF ("%j", "297");
818 TEST_PRINTF ("%k", " 0");
819 TEST_PRINTF_TIME (13, 13, 13, "%k", "13");
820 TEST_PRINTF ("%l", "12");
821 TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
822 TEST_PRINTF_TIME (13, 13, 13, "%l", " 1");
823 TEST_PRINTF_TIME (10, 13, 13, "%l", "10");
824 TEST_PRINTF ("%m", "10");
825 TEST_PRINTF ("%M", "00");
826 TEST_PRINTF ("%p", "AM");
827 TEST_PRINTF_TIME (13, 13, 13, "%p", "PM");
828 TEST_PRINTF ("%P", "am");
829 TEST_PRINTF_TIME (13, 13, 13, "%P", "pm");
830 TEST_PRINTF ("%r", "12:00:00 AM");
831 TEST_PRINTF_TIME (13, 13, 13, "%r", "01:13:13 PM");
832 TEST_PRINTF ("%R", "00:00");
833 TEST_PRINTF_TIME (13, 13, 31, "%R", "13:13");
834 //TEST_PRINTF ("%s", t_str);
835 TEST_PRINTF ("%S", "00");
836 TEST_PRINTF ("%t", " ");
837 TEST_PRINTF ("%u", "6");
838 TEST_PRINTF ("%x", "10/24/09");
839 TEST_PRINTF ("%X", "00:00:00");
840 TEST_PRINTF_TIME (13, 14, 15, "%X", "13:14:15");
841 TEST_PRINTF ("%y", "09");
842 TEST_PRINTF ("%Y", "2009");
843 TEST_PRINTF ("%%", "%");
844 TEST_PRINTF ("%", "");
845 TEST_PRINTF ("%9", NULL);
846 TEST_PRINTF ("%Z", dst);
850 test_modifiers (void)
854 TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
855 TEST_PRINTF_DATE (2009, 1, 1, "%_d", " 1");
856 TEST_PRINTF_DATE (2009, 1, 1, "%-d", "1");
857 TEST_PRINTF_DATE (2009, 1, 1, "%0d", "01");
858 TEST_PRINTF_DATE (2009, 1, 21, "%d", "21");
859 TEST_PRINTF_DATE (2009, 1, 21, "%_d", "21");
860 TEST_PRINTF_DATE (2009, 1, 21, "%-d", "21");
861 TEST_PRINTF_DATE (2009, 1, 21, "%0d", "21");
863 TEST_PRINTF_DATE (2009, 1, 1, "%e", " 1");
864 TEST_PRINTF_DATE (2009, 1, 1, "%_e", " 1");
865 TEST_PRINTF_DATE (2009, 1, 1, "%-e", "1");
866 TEST_PRINTF_DATE (2009, 1, 1, "%0e", "01");
867 TEST_PRINTF_DATE (2009, 1, 21, "%e", "21");
868 TEST_PRINTF_DATE (2009, 1, 21, "%_e", "21");
869 TEST_PRINTF_DATE (2009, 1, 21, "%-e", "21");
870 TEST_PRINTF_DATE (2009, 1, 21, "%0e", "21");
872 TEST_PRINTF_TIME ( 1, 0, 0, "%H", "01");
873 TEST_PRINTF_TIME ( 1, 0, 0, "%_H", " 1");
874 TEST_PRINTF_TIME ( 1, 0, 0, "%-H", "1");
875 TEST_PRINTF_TIME ( 1, 0, 0, "%0H", "01");
876 TEST_PRINTF_TIME (21, 0, 0, "%H", "21");
877 TEST_PRINTF_TIME (21, 0, 0, "%_H", "21");
878 TEST_PRINTF_TIME (21, 0, 0, "%-H", "21");
879 TEST_PRINTF_TIME (21, 0, 0, "%0H", "21");
881 TEST_PRINTF_TIME ( 1, 0, 0, "%I", "01");
882 TEST_PRINTF_TIME ( 1, 0, 0, "%_I", " 1");
883 TEST_PRINTF_TIME ( 1, 0, 0, "%-I", "1");
884 TEST_PRINTF_TIME ( 1, 0, 0, "%0I", "01");
885 TEST_PRINTF_TIME (23, 0, 0, "%I", "11");
886 TEST_PRINTF_TIME (23, 0, 0, "%_I", "11");
887 TEST_PRINTF_TIME (23, 0, 0, "%-I", "11");
888 TEST_PRINTF_TIME (23, 0, 0, "%0I", "11");
890 TEST_PRINTF_TIME ( 1, 0, 0, "%k", " 1");
891 TEST_PRINTF_TIME ( 1, 0, 0, "%_k", " 1");
892 TEST_PRINTF_TIME ( 1, 0, 0, "%-k", "1");
893 TEST_PRINTF_TIME ( 1, 0, 0, "%0k", "01");
895 oldlocale = g_strdup (setlocale (LC_ALL, NULL));
896 setlocale (LC_ALL, "fa_IR.utf-8");
897 if (strstr (setlocale (LC_ALL, NULL), "fa_IR") != NULL)
899 TEST_PRINTF_TIME (23, 0, 0, "%OH", "\333\262\333\263");
900 TEST_PRINTF_TIME (23, 0, 0, "%OI", "\333\261\333\261");
901 TEST_PRINTF_TIME (23, 0, 0, "%OM", "\333\260");
903 TEST_PRINTF_DATE (2011, 7, 1, "%Om", "\333\267");
904 TEST_PRINTF_DATE (2011, 7, 1, "%-Om", "\333\267");
905 /* These do not currently work as expected, since glib's printf
906 counts arabic digits as two characters for some reason
907 TEST_PRINTF_DATE (2011, 7, 1, "%0Om", "0\333\267");
908 TEST_PRINTF_DATE (2011, 7, 1, "%_Om", " \333\267");
912 g_test_message ("locale fa_IR not available, skipping O modifier tests");
913 setlocale (LC_ALL, oldlocale);
918 test_GDateTime_dst (void)
920 GDateTime *dt1, *dt2;
923 /* this date has the DST state set for Europe/London */
924 tz = g_time_zone_new ("Europe/London");
925 dt1 = g_date_time_new (tz, 2009, 8, 15, 3, 0, 1);
926 g_assert (g_date_time_is_daylight_savings (dt1));
927 g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, 3600);
928 g_assert_cmpint (g_date_time_get_hour (dt1), ==, 3);
930 /* add 6 months to clear the DST flag but keep the same time */
931 dt2 = g_date_time_add_months (dt1, 6);
932 g_assert (!g_date_time_is_daylight_savings (dt2));
933 g_assert_cmpint (g_date_time_get_utc_offset (dt2) / G_USEC_PER_SEC, ==, 0);
934 g_assert_cmpint (g_date_time_get_hour (dt2), ==, 3);
936 g_date_time_unref (dt2);
937 g_date_time_unref (dt1);
939 /* now do the reverse: start with a non-DST state and move to DST */
940 dt1 = g_date_time_new (tz, 2009, 2, 15, 2, 0, 1);
941 g_assert (!g_date_time_is_daylight_savings (dt1));
942 g_assert_cmpint (g_date_time_get_hour (dt1), ==, 2);
944 dt2 = g_date_time_add_months (dt1, 6);
945 g_assert (g_date_time_is_daylight_savings (dt2));
946 g_assert_cmpint (g_date_time_get_hour (dt2), ==, 2);
948 g_date_time_unref (dt2);
949 g_date_time_unref (dt1);
950 g_time_zone_unref (tz);
953 static inline gboolean
954 is_leap_year (gint year)
956 g_assert (1 <= year && year <= 9999);
958 return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
962 days_in_month (gint year, gint month)
964 const gint table[2][13] = {
965 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
966 {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
969 g_assert (1 <= month && month <= 12);
971 return table[is_leap_year (year)][month];
975 test_all_dates (void)
977 gint year, month, day;
985 /* save some time by hanging on to this. */
986 timezone = g_time_zone_new_utc ();
988 unix_time = G_GINT64_CONSTANT(-62135596800);
990 /* 0001-01-01 is 0001-W01-1 */
996 /* The calendar makes a full cycle every 400 years, so we could
997 * theoretically just test years 1 through 400. That assumes that our
998 * software has no bugs, so probably we should just test them all. :)
1000 for (year = 1; year <= 9999; year++)
1004 for (month = 1; month <= 12; month++)
1005 for (day = 1; day <= days_in_month (year, month); day++)
1009 dt = g_date_time_new (timezone, year, month, day, 0, 0, 0);
1012 g_print ("%04d-%02d-%02d = %04d-W%02d-%d = %04d-%03d\n",
1014 week_year, week_num, weekday,
1019 if G_UNLIKELY (g_date_time_get_year (dt) != year ||
1020 g_date_time_get_month (dt) != month ||
1021 g_date_time_get_day_of_month (dt) != day)
1022 g_error ("%04d-%02d-%02d comes out as %04d-%02d-%02d",
1024 g_date_time_get_year (dt),
1025 g_date_time_get_month (dt),
1026 g_date_time_get_day_of_month (dt));
1028 if G_UNLIKELY (g_date_time_get_week_numbering_year (dt) != week_year ||
1029 g_date_time_get_week_of_year (dt) != week_num ||
1030 g_date_time_get_day_of_week (dt) != weekday)
1031 g_error ("%04d-%02d-%02d should be %04d-W%02d-%d but "
1032 "comes out as %04d-W%02d-%d", year, month, day,
1033 week_year, week_num, weekday,
1034 g_date_time_get_week_numbering_year (dt),
1035 g_date_time_get_week_of_year (dt),
1036 g_date_time_get_day_of_week (dt));
1038 if G_UNLIKELY (g_date_time_to_unix (dt) != unix_time)
1039 g_error ("%04d-%02d-%02d 00:00:00 UTC should have unix time %"
1040 G_GINT64_FORMAT " but comes out as %"G_GINT64_FORMAT,
1041 year, month, day, unix_time, g_date_time_to_unix (dt));
1043 if G_UNLIKELY (g_date_time_get_day_of_year (dt) != day_of_year)
1044 g_error ("%04d-%02d-%02d should be day of year %d"
1045 " but comes out as %d", year, month, day,
1046 day_of_year, g_date_time_get_day_of_year (dt));
1048 if G_UNLIKELY (g_date_time_get_hour (dt) != 0 ||
1049 g_date_time_get_minute (dt) != 0 ||
1050 g_date_time_get_seconds (dt) != 0)
1051 g_error ("%04d-%02d-%02d 00:00:00 UTC comes out "
1052 "as %02d:%02d:%02.6f", year, month, day,
1053 g_date_time_get_hour (dt),
1054 g_date_time_get_minute (dt),
1055 g_date_time_get_seconds (dt));
1058 /* add 24 hours to unix time */
1059 unix_time += 24 * 60 * 60;
1061 /* move day of year forward */
1064 /* move the week date forward */
1067 weekday = 1; /* Sunday -> Monday */
1069 /* NOTE: year/month/day is the final day of the week we
1072 * If we just finished the last week of last year then
1073 * we are definitely starting the first week of this
1076 * Otherwise, if we're still in this year, but Sunday
1077 * fell on or after December 28 then December 29, 30, 31
1078 * could be days within the next year's first year.
1080 if (year != week_year || (month == 12 && day >= 28))
1082 /* first week of the new year */
1090 g_date_time_unref (dt);
1094 g_time_zone_unref (timezone);
1103 g_test_bug ("642935");
1105 tz = g_time_zone_new ("-08:00");
1106 dt = g_date_time_new (tz, 0, 0, 0, 0, 0, 0);
1107 gchar *p = g_date_time_format (dt, "%z");
1108 g_assert_cmpstr (p, ==, "-0800");
1109 g_date_time_unref (dt);
1110 g_time_zone_unref (tz);
1115 test_strftime (void)
1117 /* this is probably going to cause various buggy libcs to explode... */
1118 #define TEST_FORMAT \
1119 "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 " \
1120 "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 %%"
1123 /* 127997 is prime, 1315005118 is now */
1124 for (t = 0; t < 1315005118; t += 127997)
1126 GDateTime *date_time;
1130 date_time = g_date_time_new_from_unix_local (t);
1131 dt_str = g_date_time_format (date_time, TEST_FORMAT);
1132 strftime (c_str, sizeof c_str, TEST_FORMAT, localtime (&t));
1133 g_assert_cmpstr (c_str, ==, dt_str);
1134 g_date_time_unref (date_time);
1143 g_test_init (&argc, &argv, NULL);
1144 g_test_bug_base ("http://bugzilla.gnome.org/");
1146 /* GDateTime Tests */
1148 g_test_add_func ("/GDateTime/add_days", test_GDateTime_add_days);
1149 g_test_add_func ("/GDateTime/add_full", test_GDateTime_add_full);
1150 g_test_add_func ("/GDateTime/add_hours", test_GDateTime_add_hours);
1151 g_test_add_func ("/GDateTime/add_minutes", test_GDateTime_add_minutes);
1152 g_test_add_func ("/GDateTime/add_months", test_GDateTime_add_months);
1153 g_test_add_func ("/GDateTime/add_seconds", test_GDateTime_add_seconds);
1154 g_test_add_func ("/GDateTime/add_weeks", test_GDateTime_add_weeks);
1155 g_test_add_func ("/GDateTime/add_years", test_GDateTime_add_years);
1156 g_test_add_func ("/GDateTime/compare", test_GDateTime_compare);
1157 g_test_add_func ("/GDateTime/diff", test_GDateTime_diff);
1158 g_test_add_func ("/GDateTime/equal", test_GDateTime_equal);
1159 g_test_add_func ("/GDateTime/get_day_of_week", test_GDateTime_get_day_of_week);
1160 g_test_add_func ("/GDateTime/get_day_of_month", test_GDateTime_get_day_of_month);
1161 g_test_add_func ("/GDateTime/get_day_of_year", test_GDateTime_get_day_of_year);
1162 g_test_add_func ("/GDateTime/get_hour", test_GDateTime_get_hour);
1163 g_test_add_func ("/GDateTime/get_microsecond", test_GDateTime_get_microsecond);
1164 g_test_add_func ("/GDateTime/get_minute", test_GDateTime_get_minute);
1165 g_test_add_func ("/GDateTime/get_month", test_GDateTime_get_month);
1166 g_test_add_func ("/GDateTime/get_second", test_GDateTime_get_second);
1167 g_test_add_func ("/GDateTime/get_utc_offset", test_GDateTime_get_utc_offset);
1168 g_test_add_func ("/GDateTime/get_year", test_GDateTime_get_year);
1169 g_test_add_func ("/GDateTime/hash", test_GDateTime_hash);
1170 g_test_add_func ("/GDateTime/new_from_unix", test_GDateTime_new_from_unix);
1171 g_test_add_func ("/GDateTime/new_from_unix_utc", test_GDateTime_new_from_unix_utc);
1172 g_test_add_func ("/GDateTime/new_from_timeval", test_GDateTime_new_from_timeval);
1173 g_test_add_func ("/GDateTime/new_full", test_GDateTime_new_full);
1174 g_test_add_func ("/GDateTime/now", test_GDateTime_now);
1175 g_test_add_func ("/GDateTime/printf", test_GDateTime_printf);
1176 g_test_add_func ("/GDateTime/strftime", test_strftime);
1177 g_test_add_func ("/GDateTime/modifiers", test_modifiers);
1178 g_test_add_func ("/GDateTime/to_local", test_GDateTime_to_local);
1179 g_test_add_func ("/GDateTime/to_unix", test_GDateTime_to_unix);
1180 g_test_add_func ("/GDateTime/to_timeval", test_GDateTime_to_timeval);
1181 g_test_add_func ("/GDateTime/to_utc", test_GDateTime_to_utc);
1182 g_test_add_func ("/GDateTime/now_utc", test_GDateTime_now_utc);
1183 g_test_add_func ("/GDateTime/dst", test_GDateTime_dst);
1184 g_test_add_func ("/GDateTime/test_z", test_z);
1185 g_test_add_func ("/GDateTime/test-all-dates", test_all_dates);
1187 return g_test_run ();