Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / glib / tests / gdatetime.c
1 /* gdatetime-tests.c
2  *
3  * Copyright (C) 2009-2010 Christian Hergert <chris@dronelabs.com>
4  *
5  * This is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
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
18  */
19
20 #include "config.h"
21
22 #include <string.h>
23 #include <time.h>
24 #include <glib.h>
25 #include <glib/gstdio.h>
26
27 #define ASSERT_DATE(dt,y,m,d) G_STMT_START { \
28   g_assert_cmpint ((y), ==, g_date_time_get_year ((dt))); \
29   g_assert_cmpint ((m), ==, g_date_time_get_month ((dt))); \
30   g_assert_cmpint ((d), ==, g_date_time_get_day_of_month ((dt))); \
31 } G_STMT_END
32 #define ASSERT_TIME(dt,H,M,S) G_STMT_START { \
33   g_assert_cmpint ((H), ==, g_date_time_get_hour ((dt))); \
34   g_assert_cmpint ((M), ==, g_date_time_get_minute ((dt))); \
35   g_assert_cmpint ((S), ==, g_date_time_get_second ((dt))); \
36 } G_STMT_END
37
38 static void
39 get_localtime_tm (time_t     time_,
40                   struct tm *retval)
41 {
42 #ifdef HAVE_LOCALTIME_R
43   localtime_r (&time_, retval);
44 #else
45   {
46     struct tm *ptm = localtime (&time_);
47
48     if (ptm == NULL)
49       {
50         /* Happens at least in Microsoft's C library if you pass a
51          * negative time_t. Use 2000-01-01 as default date.
52          */
53 #ifndef G_DISABLE_CHECKS
54         g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, "ptm != NULL");
55 #endif
56
57         retval->tm_mon = 0;
58         retval->tm_mday = 1;
59         retval->tm_year = 100;
60       }
61     else
62       memcpy ((void *) retval, (void *) ptm, sizeof (struct tm));
63   }
64 #endif /* HAVE_LOCALTIME_R */
65 }
66
67 static void
68 test_GDateTime_now (void)
69 {
70   GDateTime *dt;
71   struct tm tm;
72
73   memset (&tm, 0, sizeof (tm));
74   get_localtime_tm (time (NULL), &tm);
75
76   dt = g_date_time_new_now_local ();
77
78   g_assert_cmpint (g_date_time_get_year (dt), ==, 1900 + tm.tm_year);
79   g_assert_cmpint (g_date_time_get_month (dt), ==, 1 + tm.tm_mon);
80   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, tm.tm_mday);
81   g_assert_cmpint (g_date_time_get_hour (dt), ==, tm.tm_hour);
82   g_assert_cmpint (g_date_time_get_minute (dt), ==, tm.tm_min);
83   /* XXX we need some fuzzyness here */
84   g_assert_cmpint (g_date_time_get_second (dt), >=, tm.tm_sec);
85
86   g_date_time_unref (dt);
87 }
88
89 static void
90 test_GDateTime_new_from_unix (void)
91 {
92   GDateTime *dt;
93   struct tm  tm;
94   time_t     t;
95
96   memset (&tm, 0, sizeof (tm));
97   t = time (NULL);
98   get_localtime_tm (t, &tm);
99
100   dt = g_date_time_new_from_unix_local (t);
101   g_assert_cmpint (g_date_time_get_year (dt), ==, 1900 + tm.tm_year);
102   g_assert_cmpint (g_date_time_get_month (dt), ==, 1 + tm.tm_mon);
103   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, tm.tm_mday);
104   g_assert_cmpint (g_date_time_get_hour (dt), ==, tm.tm_hour);
105   g_assert_cmpint (g_date_time_get_minute (dt), ==, tm.tm_min);
106   g_assert_cmpint (g_date_time_get_second (dt), ==, tm.tm_sec);
107   g_date_time_unref (dt);
108
109   memset (&tm, 0, sizeof (tm));
110   tm.tm_year = 70;
111   tm.tm_mday = 1;
112   tm.tm_mon = 0;
113   tm.tm_hour = 0;
114   tm.tm_min = 0;
115   tm.tm_sec = 0;
116   t = mktime (&tm);
117
118   dt = g_date_time_new_from_unix_local (t);
119   g_assert_cmpint (g_date_time_get_year (dt), ==, 1970);
120   g_assert_cmpint (g_date_time_get_month (dt), ==, 1);
121   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 1);
122   g_assert_cmpint (g_date_time_get_hour (dt), ==, 0);
123   g_assert_cmpint (g_date_time_get_minute (dt), ==, 0);
124   g_assert_cmpint (g_date_time_get_second (dt), ==, 0);
125   g_date_time_unref (dt);
126 }
127
128 static void
129 test_GDateTime_compare (void)
130 {
131   GDateTime *dt1, *dt2;
132   gint       i;
133
134   dt1 = g_date_time_new_utc (2000, 1, 1, 0, 0, 0);
135
136   for (i = 1; i < 2000; i++)
137     {
138       dt2 = g_date_time_new_utc (i, 12, 31, 0, 0, 0);
139       g_assert_cmpint (1, ==, g_date_time_compare (dt1, dt2));
140       g_date_time_unref (dt2);
141     }
142
143   dt2 = g_date_time_new_utc (1999, 12, 31, 23, 59, 59);
144   g_assert_cmpint (1, ==, g_date_time_compare (dt1, dt2));
145   g_date_time_unref (dt2);
146
147   dt2 = g_date_time_new_utc (2000, 1, 1, 0, 0, 1);
148   g_assert_cmpint (-1, ==, g_date_time_compare (dt1, dt2));
149   g_date_time_unref (dt2);
150
151   dt2 = g_date_time_new_utc (2000, 1, 1, 0, 0, 0);
152   g_assert_cmpint (0, ==, g_date_time_compare (dt1, dt2));
153   g_date_time_unref (dt2);
154   g_date_time_unref (dt1);
155 }
156
157 static void
158 test_GDateTime_equal (void)
159 {
160   GDateTime *dt1, *dt2;
161   GTimeZone *tz;
162
163   dt1 = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
164   dt2 = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
165   g_assert (g_date_time_equal (dt1, dt2));
166   g_date_time_unref (dt1);
167   g_date_time_unref (dt2);
168
169   dt1 = g_date_time_new_local (2009, 10, 18, 0, 0, 0);
170   dt2 = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
171   g_assert (!g_date_time_equal (dt1, dt2));
172   g_date_time_unref (dt1);
173   g_date_time_unref (dt2);
174
175   /* UTC-0300 and not in DST */
176   tz = g_time_zone_new ("-03:00");
177   dt1 = g_date_time_new (tz, 2010, 5, 24,  8, 0, 0);
178   g_time_zone_unref (tz);
179   g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, (-3 * 3600));
180   /* UTC */
181   dt2 = g_date_time_new_utc (2010, 5, 24, 11, 0, 0);
182   g_assert_cmpint (g_date_time_get_utc_offset (dt2), ==, 0);
183
184   g_assert (g_date_time_equal (dt1, dt2));
185   g_date_time_unref (dt1);
186
187   /* America/Recife is in UTC-0300 */
188   tz = g_time_zone_new ("America/Recife");
189   dt1 = g_date_time_new (tz, 2010, 5, 24,  8, 0, 0);
190   g_time_zone_unref (tz);
191   g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, (-3 * 3600));
192   g_assert (g_date_time_equal (dt1, dt2));
193   g_date_time_unref (dt1);
194   g_date_time_unref (dt2);
195 }
196
197 static void
198 test_GDateTime_get_day_of_week (void)
199 {
200   GDateTime *dt;
201
202   dt = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
203   g_assert_cmpint (1, ==, g_date_time_get_day_of_week (dt));
204   g_date_time_unref (dt);
205
206   dt = g_date_time_new_local (2000, 10, 1, 0, 0, 0);
207   g_assert_cmpint (7, ==, g_date_time_get_day_of_week (dt));
208   g_date_time_unref (dt);
209 }
210
211 static void
212 test_GDateTime_get_day_of_month (void)
213 {
214   GDateTime *dt;
215
216   dt = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
217   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 19);
218   g_date_time_unref (dt);
219
220   dt = g_date_time_new_local (1400, 3, 12, 0, 0, 0);
221   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 12);
222   g_date_time_unref (dt);
223
224   dt = g_date_time_new_local (1800, 12, 31, 0, 0, 0);
225   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 31);
226   g_date_time_unref (dt);
227
228   dt = g_date_time_new_local (1000, 1, 1, 0, 0, 0);
229   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 1);
230   g_date_time_unref (dt);
231 }
232
233 static void
234 test_GDateTime_get_hour (void)
235 {
236   GDateTime *dt;
237
238   dt = g_date_time_new_utc (2009, 10, 19, 15, 13, 11);
239   g_assert_cmpint (15, ==, g_date_time_get_hour (dt));
240   g_date_time_unref (dt);
241
242   dt = g_date_time_new_utc (100, 10, 19, 1, 0, 0);
243   g_assert_cmpint (1, ==, g_date_time_get_hour (dt));
244   g_date_time_unref (dt);
245
246   dt = g_date_time_new_utc (100, 10, 19, 0, 0, 0);
247   g_assert_cmpint (0, ==, g_date_time_get_hour (dt));
248   g_date_time_unref (dt);
249
250   dt = g_date_time_new_utc (100, 10, 1, 23, 59, 59);
251   g_assert_cmpint (23, ==, g_date_time_get_hour (dt));
252   g_date_time_unref (dt);
253 }
254
255 static void
256 test_GDateTime_get_microsecond (void)
257 {
258   GTimeVal   tv;
259   GDateTime *dt;
260
261   g_get_current_time (&tv);
262   dt = g_date_time_new_from_timeval_local (&tv);
263   g_assert_cmpint (tv.tv_usec, ==, g_date_time_get_microsecond (dt));
264   g_date_time_unref (dt);
265 }
266
267 static void
268 test_GDateTime_get_year (void)
269 {
270   GDateTime *dt;
271
272   dt = g_date_time_new_local (2009, 1, 1, 0, 0, 0);
273   g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
274   g_date_time_unref (dt);
275
276   dt = g_date_time_new_local (1, 1, 1, 0, 0, 0);
277   g_assert_cmpint (1, ==, g_date_time_get_year (dt));
278   g_date_time_unref (dt);
279
280   dt = g_date_time_new_local (13, 1, 1, 0, 0, 0);
281   g_assert_cmpint (13, ==, g_date_time_get_year (dt));
282   g_date_time_unref (dt);
283
284   dt = g_date_time_new_local (3000, 1, 1, 0, 0, 0);
285   g_assert_cmpint (3000, ==, g_date_time_get_year (dt));
286   g_date_time_unref (dt);
287 }
288
289 static void
290 test_GDateTime_hash (void)
291 {
292   GHashTable *h;
293
294   h = g_hash_table_new_full (g_date_time_hash, g_date_time_equal,
295                              (GDestroyNotify)g_date_time_unref,
296                              NULL);
297   g_hash_table_insert (h, g_date_time_new_now_local (), NULL);
298   g_hash_table_remove_all (h);
299   g_hash_table_destroy (h);
300 }
301
302 static void
303 test_GDateTime_new_from_timeval (void)
304 {
305   GDateTime *dt;
306   GTimeVal   tv, tv2;
307
308   g_get_current_time (&tv);
309   dt = g_date_time_new_from_timeval_local (&tv);
310
311   if (g_test_verbose ())
312     g_print ("\nDT%04d-%02d-%02dT%02d:%02d:%02d%s\n",
313              g_date_time_get_year (dt),
314              g_date_time_get_month (dt),
315              g_date_time_get_day_of_month (dt),
316              g_date_time_get_hour (dt),
317              g_date_time_get_minute (dt),
318              g_date_time_get_second (dt),
319              g_date_time_get_timezone_abbreviation (dt));
320
321   g_date_time_to_timeval (dt, &tv2);
322   g_assert_cmpint (tv.tv_sec, ==, tv2.tv_sec);
323   g_assert_cmpint (tv.tv_usec, ==, tv2.tv_usec);
324   g_date_time_unref (dt);
325 }
326
327 static void
328 test_GDateTime_to_unix (void)
329 {
330   GDateTime *dt;
331   time_t     t;
332
333   t = time (NULL);
334   dt = g_date_time_new_from_unix_local (t);
335   g_assert_cmpint (g_date_time_to_unix (dt), ==, t);
336   g_date_time_unref (dt);
337 }
338
339 static void
340 test_GDateTime_add_years (void)
341 {
342   GDateTime *dt, *dt2;
343
344   dt = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
345   dt2 = g_date_time_add_years (dt, 1);
346   g_assert_cmpint (2010, ==, g_date_time_get_year (dt2));
347   g_date_time_unref (dt);
348   g_date_time_unref (dt2);
349 }
350
351 static void
352 test_GDateTime_add_months (void)
353 {
354 #define TEST_ADD_MONTHS(y,m,d,a,ny,nm,nd) G_STMT_START { \
355   GDateTime *dt, *dt2; \
356   dt = g_date_time_new_utc (y, m, d, 0, 0, 0); \
357   dt2 = g_date_time_add_months (dt, a); \
358   ASSERT_DATE (dt2, ny, nm, nd); \
359   g_date_time_unref (dt); \
360   g_date_time_unref (dt2); \
361 } G_STMT_END
362
363   TEST_ADD_MONTHS (2009, 12, 31,    1, 2010, 1, 31);
364   TEST_ADD_MONTHS (2009, 12, 31,    1, 2010, 1, 31);
365   TEST_ADD_MONTHS (2009,  6, 15,    1, 2009, 7, 15);
366   TEST_ADD_MONTHS (1400,  3,  1,    1, 1400, 4,  1);
367   TEST_ADD_MONTHS (1400,  1, 31,    1, 1400, 2, 28);
368   TEST_ADD_MONTHS (1400,  1, 31, 7200, 2000, 1, 31);
369   TEST_ADD_MONTHS (2008,  2, 29,   12, 2009, 2, 28);
370   TEST_ADD_MONTHS (2000,  8, 16,   -5, 2000, 3, 16);
371   TEST_ADD_MONTHS (2000,  8, 16,  -12, 1999, 8, 16);
372   TEST_ADD_MONTHS (2011,  2,  1,  -13, 2010, 1,  1);
373   TEST_ADD_MONTHS (1776,  7,  4, 1200, 1876, 7,  4);
374 }
375
376 static void
377 test_GDateTime_add_days (void)
378 {
379 #define TEST_ADD_DAYS(y,m,d,a,ny,nm,nd) G_STMT_START { \
380   GDateTime *dt, *dt2; \
381   dt = g_date_time_new_local (y, m, d, 0, 0, 0); \
382   dt2 = g_date_time_add_days (dt, a); \
383   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
384   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
385   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
386   g_date_time_unref (dt); \
387   g_date_time_unref (dt2); \
388 } G_STMT_END
389
390   TEST_ADD_DAYS (2009, 1, 31, 1, 2009, 2, 1);
391   TEST_ADD_DAYS (2009, 2, 1, -1, 2009, 1, 31);
392   TEST_ADD_DAYS (2008, 2, 28, 1, 2008, 2, 29);
393   TEST_ADD_DAYS (2008, 12, 31, 1, 2009, 1, 1);
394   TEST_ADD_DAYS (1, 1, 1, 1, 1, 1, 2);
395   TEST_ADD_DAYS (1955, 5, 24, 10, 1955, 6, 3);
396   TEST_ADD_DAYS (1955, 5, 24, -10, 1955, 5, 14);
397 }
398
399 static void
400 test_GDateTime_add_weeks (void)
401 {
402 #define TEST_ADD_WEEKS(y,m,d,a,ny,nm,nd) G_STMT_START { \
403   GDateTime *dt, *dt2; \
404   dt = g_date_time_new_local (y, m, d, 0, 0, 0); \
405   dt2 = g_date_time_add_weeks (dt, a); \
406   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
407   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
408   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
409   g_date_time_unref (dt); \
410   g_date_time_unref (dt2); \
411 } G_STMT_END
412
413   TEST_ADD_WEEKS (2009, 1, 1, 1, 2009, 1, 8);
414   TEST_ADD_WEEKS (2009, 8, 30, 1, 2009, 9, 6);
415   TEST_ADD_WEEKS (2009, 12, 31, 1, 2010, 1, 7);
416   TEST_ADD_WEEKS (2009, 1, 1, -1, 2008, 12, 25);
417 }
418
419 static void
420 test_GDateTime_add_hours (void)
421 {
422 #define TEST_ADD_HOURS(y,m,d,h,mi,s,a,ny,nm,nd,nh,nmi,ns) G_STMT_START { \
423   GDateTime *dt, *dt2; \
424   dt = g_date_time_new_utc (y, m, d, h, mi, s); \
425   dt2 = g_date_time_add_hours (dt, a); \
426   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
427   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
428   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
429   g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
430   g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
431   g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
432   g_date_time_unref (dt); \
433   g_date_time_unref (dt2); \
434 } G_STMT_END
435
436   TEST_ADD_HOURS (2009,  1,  1,  0, 0, 0, 1, 2009, 1, 1, 1, 0, 0);
437   TEST_ADD_HOURS (2008, 12, 31, 23, 0, 0, 1, 2009, 1, 1, 0, 0, 0);
438 }
439
440 static void
441 test_GDateTime_add_full (void)
442 {
443 #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 { \
444   GDateTime *dt, *dt2; \
445   dt = g_date_time_new_utc (y, m, d, h, mi, s); \
446   dt2 = g_date_time_add_full (dt, ay, am, ad, ah, ami, as); \
447   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
448   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
449   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
450   g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
451   g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
452   g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
453   g_date_time_unref (dt); \
454   g_date_time_unref (dt2); \
455 } G_STMT_END
456
457   TEST_ADD_FULL (2009, 10, 21,  0,  0, 0,
458                     1,  1,  1,  1,  1, 1,
459                  2010, 11, 22,  1,  1, 1);
460   TEST_ADD_FULL (2000,  1,  1,  1,  1, 1,
461                     0,  1,  0,  0,  0, 0,
462                  2000,  2,  1,  1,  1, 1);
463   TEST_ADD_FULL (2000,  1,  1,  0,  0, 0,
464                    -1,  1,  0,  0,  0, 0,
465                  1999,  2,  1,  0,  0, 0);
466   TEST_ADD_FULL (2010, 10, 31,  0,  0, 0,
467                     0,  4,  0,  0,  0, 0,
468                  2011,  2, 28,  0,  0, 0);
469   TEST_ADD_FULL (2010,  8, 25, 22, 45, 0,
470                     0,  1,  6,  1, 25, 0,
471                  2010, 10,  2,  0, 10, 0);
472 }
473
474 static void
475 test_GDateTime_add_minutes (void)
476 {
477 #define TEST_ADD_MINUTES(i,o) G_STMT_START { \
478   GDateTime *dt, *dt2; \
479   dt = g_date_time_new_local (2000, 1, 1, 0, 0, 0); \
480   dt2 = g_date_time_add_minutes (dt, i); \
481   g_assert_cmpint (o, ==, g_date_time_get_minute (dt2)); \
482   g_date_time_unref (dt); \
483   g_date_time_unref (dt2); \
484 } G_STMT_END
485
486   TEST_ADD_MINUTES (60, 0);
487   TEST_ADD_MINUTES (100, 40);
488   TEST_ADD_MINUTES (5, 5);
489   TEST_ADD_MINUTES (1441, 1);
490   TEST_ADD_MINUTES (-1441, 59);
491 }
492
493 static void
494 test_GDateTime_add_seconds (void)
495 {
496 #define TEST_ADD_SECONDS(i,o) G_STMT_START { \
497   GDateTime *dt, *dt2; \
498   dt = g_date_time_new_local (2000, 1, 1, 0, 0, 0); \
499   dt2 = g_date_time_add_seconds (dt, i); \
500   g_assert_cmpint (o, ==, g_date_time_get_second (dt2)); \
501   g_date_time_unref (dt); \
502   g_date_time_unref (dt2); \
503 } G_STMT_END
504
505   TEST_ADD_SECONDS (1, 1);
506   TEST_ADD_SECONDS (60, 0);
507   TEST_ADD_SECONDS (61, 1);
508   TEST_ADD_SECONDS (120, 0);
509   TEST_ADD_SECONDS (-61, 59);
510   TEST_ADD_SECONDS (86401, 1);
511   TEST_ADD_SECONDS (-86401, 59);
512   TEST_ADD_SECONDS (-31, 29);
513   TEST_ADD_SECONDS (13, 13);
514 }
515
516 static void
517 test_GDateTime_diff (void)
518 {
519 #define TEST_DIFF(y,m,d,y2,m2,d2,u) G_STMT_START { \
520   GDateTime *dt1, *dt2; \
521   GTimeSpan  ts = 0; \
522   dt1 = g_date_time_new_local (y, m, d, 0, 0, 0); \
523   dt2 = g_date_time_new_local (y2, m2, d2, 0, 0, 0); \
524   ts = g_date_time_difference (dt2, dt1); \
525   g_assert_cmpint (ts, ==, u); \
526   g_date_time_unref (dt1); \
527   g_date_time_unref (dt2); \
528 } G_STMT_END
529
530   TEST_DIFF (2009, 1, 1, 2009, 2, 1, G_TIME_SPAN_DAY * 31);
531   TEST_DIFF (2009, 1, 1, 2010, 1, 1, G_TIME_SPAN_DAY * 365);
532   TEST_DIFF (2008, 2, 28, 2008, 2, 29, G_TIME_SPAN_DAY);
533   TEST_DIFF (2008, 2, 29, 2008, 2, 28, -G_TIME_SPAN_DAY);
534
535   /* TODO: Add usec tests */
536 }
537
538 static void
539 test_GDateTime_get_minute (void)
540 {
541   GDateTime *dt;
542
543   dt = g_date_time_new_utc (2009, 12, 1, 1, 31, 0);
544   g_assert_cmpint (31, ==, g_date_time_get_minute (dt));
545   g_date_time_unref (dt);
546 }
547
548 static void
549 test_GDateTime_get_month (void)
550 {
551   GDateTime *dt;
552
553   dt = g_date_time_new_utc (2009, 12, 1, 1, 31, 0);
554   g_assert_cmpint (12, ==, g_date_time_get_month (dt));
555   g_date_time_unref (dt);
556 }
557
558 static void
559 test_GDateTime_get_second (void)
560 {
561   GDateTime *dt;
562
563   dt = g_date_time_new_utc (2009, 12, 1, 1, 31, 44);
564   g_assert_cmpint (44, ==, g_date_time_get_second (dt));
565   g_date_time_unref (dt);
566 }
567
568 static void
569 test_GDateTime_new_full (void)
570 {
571   GTimeZone *tz;
572   GDateTime *dt;
573
574   dt = g_date_time_new_utc (2009, 12, 11, 12, 11, 10);
575   g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
576   g_assert_cmpint (12, ==, g_date_time_get_month (dt));
577   g_assert_cmpint (11, ==, g_date_time_get_day_of_month (dt));
578   g_assert_cmpint (12, ==, g_date_time_get_hour (dt));
579   g_assert_cmpint (11, ==, g_date_time_get_minute (dt));
580   g_assert_cmpint (10, ==, g_date_time_get_second (dt));
581   g_date_time_unref (dt);
582
583   tz = g_time_zone_new ("America/Recife");
584   dt = g_date_time_new (tz, 2010, 5, 24, 8, 4, 0);
585   g_time_zone_unref (tz);
586   g_assert_cmpint (2010, ==, g_date_time_get_year (dt));
587   g_assert_cmpint (5, ==, g_date_time_get_month (dt));
588   g_assert_cmpint (24, ==, g_date_time_get_day_of_month (dt));
589   g_assert_cmpint (8, ==, g_date_time_get_hour (dt));
590   g_assert_cmpint (4, ==, g_date_time_get_minute (dt));
591   g_assert_cmpint (0, ==, g_date_time_get_second (dt));
592   g_assert_cmpstr ("BRT", ==, g_date_time_get_timezone_abbreviation (dt));
593   g_assert (!g_date_time_is_daylight_savings (dt));
594   g_date_time_unref (dt);
595 }
596
597 static void
598 test_GDateTime_now_utc (void)
599 {
600   GDateTime *dt;
601   time_t     t;
602   struct tm  tm;
603
604   t = time (NULL);
605 #ifdef HAVE_GMTIME_R
606   gmtime_r (&t, &tm);
607 #else
608   {
609     struct tm *tmp = gmtime (&t);
610     /* Assume gmtime() can't fail as we got t from time(NULL). (Note
611      * that on Windows, gmtime() *is* MT-safe, it uses a thread-local
612      * buffer.)
613      */
614     memcpy (&tm, tmp, sizeof (struct tm));
615   }
616 #endif
617   dt = g_date_time_new_now_utc ();
618   g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
619   g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
620   g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
621   g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
622   g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
623   g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
624   g_date_time_unref (dt);
625 }
626
627 static void
628 test_GDateTime_get_utc_offset (void)
629 {
630   GDateTime *dt;
631   GTimeSpan ts;
632   struct tm tm;
633
634   memset (&tm, 0, sizeof (tm));
635   get_localtime_tm (time (NULL), &tm);
636
637   dt = g_date_time_new_now_local ();
638   ts = g_date_time_get_utc_offset (dt);
639 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
640   g_assert_cmpint (ts, ==, (tm.tm_gmtoff * G_TIME_SPAN_SECOND));
641 #endif
642   g_date_time_unref (dt);
643 }
644
645 static void
646 test_GDateTime_to_timeval (void)
647 {
648   GTimeVal tv1, tv2;
649   GDateTime *dt;
650
651   memset (&tv1, 0, sizeof (tv1));
652   memset (&tv2, 0, sizeof (tv2));
653
654   g_get_current_time (&tv1);
655   dt = g_date_time_new_from_timeval_local (&tv1);
656   g_date_time_to_timeval (dt, &tv2);
657   g_assert_cmpint (tv1.tv_sec, ==, tv2.tv_sec);
658   g_assert_cmpint (tv1.tv_usec, ==, tv2.tv_usec);
659   g_date_time_unref (dt);
660 }
661
662 static void
663 test_GDateTime_to_local (void)
664 {
665   GDateTime *utc, *now, *dt;
666
667   utc = g_date_time_new_now_utc ();
668   now = g_date_time_new_now_local ();
669   dt = g_date_time_to_local (utc);
670
671   g_assert_cmpint (g_date_time_get_year (now), ==, g_date_time_get_year (dt));
672   g_assert_cmpint (g_date_time_get_month (now), ==, g_date_time_get_month (dt));
673   g_assert_cmpint (g_date_time_get_day_of_month (now), ==, g_date_time_get_day_of_month (dt));
674   g_assert_cmpint (g_date_time_get_hour (now), ==, g_date_time_get_hour (dt));
675   g_assert_cmpint (g_date_time_get_minute (now), ==, g_date_time_get_minute (dt));
676   g_assert_cmpint (g_date_time_get_second (now), ==, g_date_time_get_second (dt));
677
678   g_date_time_unref (now);
679   g_date_time_unref (utc);
680   g_date_time_unref (dt);
681 }
682
683 static void
684 test_GDateTime_to_utc (void)
685 {
686   GDateTime *dt, *dt2;
687   time_t     t;
688   struct tm  tm;
689
690   t = time (NULL);
691 #ifdef HAVE_GMTIME_R
692   gmtime_r (&t, &tm);
693 #else
694   {
695     struct tm *tmp = gmtime (&t);
696     memcpy (&tm, tmp, sizeof (struct tm));
697   }
698 #endif
699   dt2 = g_date_time_new_now_local ();
700   dt = g_date_time_to_utc (dt2);
701   g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
702   g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
703   g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
704   g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
705   g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
706   g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
707   g_date_time_unref (dt);
708   g_date_time_unref (dt2);
709 }
710
711 static void
712 test_GDateTime_get_day_of_year (void)
713 {
714 #define TEST_DAY_OF_YEAR(y,m,d,o)                       G_STMT_START {  \
715   GDateTime *__dt = g_date_time_new_local ((y), (m), (d), 0, 0, 0);     \
716   g_assert_cmpint ((o), ==, g_date_time_get_day_of_year (__dt));        \
717   g_date_time_unref (__dt);                             } G_STMT_END
718
719   TEST_DAY_OF_YEAR (2009, 1, 1, 1);
720   TEST_DAY_OF_YEAR (2009, 2, 1, 32);
721   TEST_DAY_OF_YEAR (2009, 8, 16, 228);
722   TEST_DAY_OF_YEAR (2008, 8, 16, 229);
723 }
724
725 static void
726 test_GDateTime_printf (void)
727 {
728   gchar dst[16];
729   struct tm tt;
730   time_t t;
731   gchar t_str[16];
732
733 #define TEST_PRINTF(f,o)                        G_STMT_START {  \
734 GDateTime *__dt = g_date_time_new_local (2009, 10, 24, 0, 0, 0);\
735   gchar *__p = g_date_time_format (__dt, (f));                  \
736   g_assert_cmpstr (__p, ==, (o));                               \
737   g_date_time_unref (__dt);                                     \
738   g_free (__p);                                 } G_STMT_END
739
740 #define TEST_PRINTF_DATE(y,m,d,f,o)             G_STMT_START {  \
741   GDateTime *dt = g_date_time_new_local (y, m, d, 0, 0, 0);     \
742   gchar *p = g_date_time_format (dt, (f));                      \
743   g_assert_cmpstr (p, ==, (o));                                 \
744   g_date_time_unref (dt);                                       \
745   g_free (p);                                   } G_STMT_END
746
747 #define TEST_PRINTF_TIME(h,m,s,f,o)             G_STMT_START { \
748   GDateTime *dt = g_date_time_new_local (2009, 10, 24, (h), (m), (s)); \
749   gchar *p = g_date_time_format (dt, (f));                      \
750   g_assert_cmpstr (p, ==, (o));                                 \
751   g_date_time_unref (dt);                                       \
752   g_free (p);                                   } G_STMT_END
753
754   /*
755    * This is a little helper to make sure we can compare timezones to
756    * that of the generated timezone.
757    */
758   t = time (NULL);
759   memset (&tt, 0, sizeof(tt));
760   get_localtime_tm (t, &tt);
761   tt.tm_year = 2009 - 1900;
762   tt.tm_mon = 9; /* 0 indexed */
763   tt.tm_mday = 24;
764   t = mktime (&tt);
765   memset (&tt, 0, sizeof(tt));
766   get_localtime_tm (t, &tt);
767   strftime (dst, sizeof(dst), "%Z", &tt);
768
769   /* get current time_t for 20090924 in the local timezone */
770   tt.tm_sec = 0;
771   tt.tm_min = 0;
772   tt.tm_hour = 0;
773   t = mktime (&tt);
774   g_sprintf (t_str, "%ld", t);
775
776   TEST_PRINTF ("%a", "Sat");
777   TEST_PRINTF ("%A", "Saturday");
778   TEST_PRINTF ("%b", "Oct");
779   TEST_PRINTF ("%B", "October");
780   TEST_PRINTF ("%d", "24");
781   TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
782   TEST_PRINTF ("%e", "24"); // fixme
783   TEST_PRINTF ("%h", "Oct");
784   TEST_PRINTF ("%H", "00");
785   TEST_PRINTF_TIME (15, 0, 0, "%H", "15");
786   TEST_PRINTF ("%I", "12");
787   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
788   TEST_PRINTF_TIME (15, 0, 0, "%I", "03");
789   TEST_PRINTF ("%j", "297");
790   TEST_PRINTF ("%k", " 0");
791   TEST_PRINTF_TIME (13, 13, 13, "%k", "13");
792   TEST_PRINTF ("%l", "12");
793   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
794   TEST_PRINTF_TIME (13, 13, 13, "%l", " 1");
795   TEST_PRINTF_TIME (10, 13, 13, "%l", "10");
796   TEST_PRINTF ("%m", "10");
797   TEST_PRINTF ("%M", "00");
798   TEST_PRINTF ("%N", "0");
799   TEST_PRINTF ("%p", "AM");
800   TEST_PRINTF_TIME (13, 13, 13, "%p", "PM");
801   TEST_PRINTF ("%P", "am");
802   TEST_PRINTF_TIME (13, 13, 13, "%P", "pm");
803   TEST_PRINTF ("%r", "12:00:00 AM");
804   TEST_PRINTF_TIME (13, 13, 13, "%r", "01:13:13 PM");
805   TEST_PRINTF ("%R", "00:00");
806   TEST_PRINTF_TIME (13, 13, 31, "%R", "13:13");
807   //TEST_PRINTF ("%s", t_str);
808   TEST_PRINTF ("%S", "00");
809   TEST_PRINTF ("%t", "  ");
810   TEST_PRINTF ("%W", "42");
811   TEST_PRINTF ("%u", "6");
812   TEST_PRINTF ("%x", "10/24/09");
813   TEST_PRINTF ("%X", "00:00:00");
814   TEST_PRINTF_TIME (13, 14, 15, "%X", "13:14:15");
815   TEST_PRINTF ("%y", "09");
816   TEST_PRINTF ("%Y", "2009");
817   TEST_PRINTF ("%%", "%");
818   TEST_PRINTF ("%", "");
819   TEST_PRINTF ("%9", NULL);
820   TEST_PRINTF ("%Z", dst);
821 }
822
823 static void
824 test_GDateTime_dst (void)
825 {
826   GDateTime *dt1, *dt2;
827   GTimeZone *tz;
828
829   /* this date has the DST state set for Europe/London */
830   tz = g_time_zone_new ("Europe/London");
831   dt1 = g_date_time_new (tz, 2009, 8, 15, 3, 0, 1);
832   g_assert (g_date_time_is_daylight_savings (dt1));
833   g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, 3600);
834   g_assert_cmpint (g_date_time_get_hour (dt1), ==, 3);
835
836   /* add 6 months to clear the DST flag but keep the same time */
837   dt2 = g_date_time_add_months (dt1, 6);
838   g_assert (!g_date_time_is_daylight_savings (dt2));
839   g_assert_cmpint (g_date_time_get_utc_offset (dt2) / G_USEC_PER_SEC, ==, 0);
840   g_assert_cmpint (g_date_time_get_hour (dt2), ==, 3);
841
842   g_date_time_unref (dt2);
843   g_date_time_unref (dt1);
844
845   /* now do the reverse: start with a non-DST state and move to DST */
846   dt1 = g_date_time_new (tz, 2009, 2, 15, 2, 0, 1);
847   g_assert (!g_date_time_is_daylight_savings (dt1));
848   g_assert_cmpint (g_date_time_get_hour (dt1), ==, 2);
849
850   dt2 = g_date_time_add_months (dt1, 6);
851   g_assert (g_date_time_is_daylight_savings (dt2));
852   g_assert_cmpint (g_date_time_get_hour (dt2), ==, 2);
853
854   g_date_time_unref (dt2);
855   g_date_time_unref (dt1);
856   g_time_zone_unref (tz);
857 }
858
859 static inline gboolean
860 is_leap_year (gint year)
861 {
862   g_assert (1 <= year && year <= 9999);
863
864   return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
865 }
866
867 static inline gint
868 days_in_month (gint year, gint month)
869 {
870   const gint table[2][13] = {
871     {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
872     {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
873   };
874
875   g_assert (1 <= month && month <= 12);
876
877   return table[is_leap_year (year)][month];
878 }
879
880 static void
881 test_all_dates (void)
882 {
883   gint year, month, day;
884   GTimeZone *timezone;
885   gint64 unix_time;
886   gint day_of_year;
887   gint week_year;
888   gint week_num;
889   gint weekday;
890
891   /* save some time by hanging on to this. */
892   timezone = g_time_zone_new_utc ();
893
894   unix_time = G_GINT64_CONSTANT(-62135596800);
895
896   /* 0001-01-01 is 0001-W01-1 */
897   week_year = 1;
898   week_num = 1;
899   weekday = 1;
900
901
902   /* The calendar makes a full cycle every 400 years, so we could
903    * theoretically just test years 1 through 400.  That assumes that our
904    * software has no bugs, so probably we should just test them all. :)
905    */
906   for (year = 1; year <= 9999; year++)
907     {
908       day_of_year = 1;
909
910       for (month = 1; month <= 12; month++)
911         for (day = 1; day <= days_in_month (year, month); day++)
912           {
913             GDateTime *dt;
914
915             dt = g_date_time_new (timezone, year, month, day, 0, 0, 0);
916
917 #if 0
918             g_print ("%04d-%02d-%02d = %04d-W%02d-%d = %04d-%03d\n",
919                      year, month, day,
920                      week_year, week_num, weekday,
921                      year, day_of_year);
922 #endif
923
924             /* sanity check */
925             if G_UNLIKELY (g_date_time_get_year (dt) != year ||
926                            g_date_time_get_month (dt) != month ||
927                            g_date_time_get_day_of_month (dt) != day)
928               g_error ("%04d-%02d-%02d comes out as %04d-%02d-%02d",
929                        year, month, day,
930                        g_date_time_get_year (dt),
931                        g_date_time_get_month (dt),
932                        g_date_time_get_day_of_month (dt));
933
934             if G_UNLIKELY (g_date_time_get_week_numbering_year (dt) != week_year ||
935                            g_date_time_get_week_of_year (dt) != week_num ||
936                            g_date_time_get_day_of_week (dt) != weekday)
937               g_error ("%04d-%02d-%02d should be %04d-W%02d-%d but "
938                        "comes out as %04d-W%02d-%d", year, month, day,
939                        week_year, week_num, weekday,
940                        g_date_time_get_week_numbering_year (dt),
941                        g_date_time_get_week_of_year (dt),
942                        g_date_time_get_day_of_week (dt));
943
944             if G_UNLIKELY (g_date_time_to_unix (dt) != unix_time)
945               g_error ("%04d-%02d-%02d 00:00:00 UTC should have unix time %"
946                        G_GINT64_FORMAT " but comes out as %"G_GINT64_FORMAT,
947                        year, month, day, unix_time, g_date_time_to_unix (dt));
948
949             if G_UNLIKELY (g_date_time_get_day_of_year (dt) != day_of_year)
950               g_error ("%04d-%02d-%02d should be day of year %d"
951                        " but comes out as %d", year, month, day,
952                        day_of_year, g_date_time_get_day_of_year (dt));
953
954             if G_UNLIKELY (g_date_time_get_hour (dt) != 0 ||
955                            g_date_time_get_minute (dt) != 0 ||
956                            g_date_time_get_seconds (dt) != 0)
957               g_error ("%04d-%02d-%02d 00:00:00 UTC comes out "
958                        "as %02d:%02d:%02.6f", year, month, day,
959                        g_date_time_get_hour (dt),
960                        g_date_time_get_minute (dt),
961                        g_date_time_get_seconds (dt));
962             /* done */
963
964             /* add 24 hours to unix time */
965             unix_time += 24 * 60 * 60;
966
967             /* move day of year forward */
968             day_of_year++;
969
970             /* move the week date forward */
971             if (++weekday == 8)
972               {
973                 weekday = 1; /* Sunday -> Monday */
974
975                 /* NOTE: year/month/day is the final day of the week we
976                  * just finished.
977                  *
978                  * If we just finished the last week of last year then
979                  * we are definitely starting the first week of this
980                  * year.
981                  *
982                  * Otherwise, if we're still in this year, but Sunday
983                  * fell on or after December 28 then December 29, 30, 31
984                  * could be days within the next year's first year.
985                  */
986                 if (year != week_year || (month == 12 && day >= 28))
987                   {
988                     /* first week of the new year */
989                     week_num = 1;
990                     week_year++;
991                   }
992                 else
993                   week_num++;
994               }
995           }
996     }
997
998   g_time_zone_unref (timezone);
999 }
1000
1001 gint
1002 main (gint   argc,
1003       gchar *argv[])
1004 {
1005   g_test_init (&argc, &argv, NULL);
1006
1007   /* GDateTime Tests */
1008
1009   g_test_add_func ("/GDateTime/add_days", test_GDateTime_add_days);
1010   g_test_add_func ("/GDateTime/add_full", test_GDateTime_add_full);
1011   g_test_add_func ("/GDateTime/add_hours", test_GDateTime_add_hours);
1012   g_test_add_func ("/GDateTime/add_minutes", test_GDateTime_add_minutes);
1013   g_test_add_func ("/GDateTime/add_months", test_GDateTime_add_months);
1014   g_test_add_func ("/GDateTime/add_seconds", test_GDateTime_add_seconds);
1015   g_test_add_func ("/GDateTime/add_weeks", test_GDateTime_add_weeks);
1016   g_test_add_func ("/GDateTime/add_years", test_GDateTime_add_years);
1017   g_test_add_func ("/GDateTime/compare", test_GDateTime_compare);
1018   g_test_add_func ("/GDateTime/diff", test_GDateTime_diff);
1019   g_test_add_func ("/GDateTime/equal", test_GDateTime_equal);
1020   g_test_add_func ("/GDateTime/get_day_of_week", test_GDateTime_get_day_of_week);
1021   g_test_add_func ("/GDateTime/get_day_of_month", test_GDateTime_get_day_of_month);
1022   g_test_add_func ("/GDateTime/get_day_of_year", test_GDateTime_get_day_of_year);
1023   g_test_add_func ("/GDateTime/get_hour", test_GDateTime_get_hour);
1024   g_test_add_func ("/GDateTime/get_microsecond", test_GDateTime_get_microsecond);
1025   g_test_add_func ("/GDateTime/get_minute", test_GDateTime_get_minute);
1026   g_test_add_func ("/GDateTime/get_month", test_GDateTime_get_month);
1027   g_test_add_func ("/GDateTime/get_second", test_GDateTime_get_second);
1028   g_test_add_func ("/GDateTime/get_utc_offset", test_GDateTime_get_utc_offset);
1029   g_test_add_func ("/GDateTime/get_year", test_GDateTime_get_year);
1030   g_test_add_func ("/GDateTime/hash", test_GDateTime_hash);
1031   g_test_add_func ("/GDateTime/new_from_unix", test_GDateTime_new_from_unix);
1032   g_test_add_func ("/GDateTime/new_from_timeval", test_GDateTime_new_from_timeval);
1033   g_test_add_func ("/GDateTime/new_full", test_GDateTime_new_full);
1034   g_test_add_func ("/GDateTime/now", test_GDateTime_now);
1035   g_test_add_func ("/GDateTime/printf", test_GDateTime_printf);
1036   g_test_add_func ("/GDateTime/to_local", test_GDateTime_to_local);
1037   g_test_add_func ("/GDateTime/to_unix", test_GDateTime_to_unix);
1038   g_test_add_func ("/GDateTime/to_timeval", test_GDateTime_to_timeval);
1039   g_test_add_func ("/GDateTime/to_utc", test_GDateTime_to_utc);
1040   g_test_add_func ("/GDateTime/now_utc", test_GDateTime_now_utc);
1041   g_test_add_func ("/GDateTime/dst", test_GDateTime_dst);
1042   g_test_add_func ("/GDateTime/test-all-dates", test_all_dates);
1043
1044   return g_test_run ();
1045 }