gdatetime: Use proleptic gregorian
[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 ();
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_today (void)
91 {
92   GDateTime *dt;
93   struct tm tm;
94
95   memset (&tm, 0, sizeof (tm));
96   get_localtime_tm (time (NULL), &tm);
97
98   dt = g_date_time_new_today ();
99   g_assert_cmpint (g_date_time_get_year (dt), ==, 1900 + tm.tm_year);
100   g_assert_cmpint (g_date_time_get_month (dt), ==, 1 + tm.tm_mon);
101   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, tm.tm_mday);
102   g_assert_cmpint (g_date_time_get_hour (dt), ==, 0);
103   g_assert_cmpint (g_date_time_get_minute (dt), ==, 0);
104   g_assert_cmpint (g_date_time_get_second (dt), ==, 0);
105   g_assert_cmpint (g_date_time_get_millisecond (dt), ==, 0);
106   g_date_time_unref (dt);
107 }
108
109 static void
110 test_GDateTime_new_from_epoch (void)
111 {
112   GDateTime *dt;
113   struct tm  tm;
114   time_t     t;
115
116   memset (&tm, 0, sizeof (tm));
117   t = time (NULL);
118   get_localtime_tm (t, &tm);
119
120   dt = g_date_time_new_from_epoch (t);
121   g_assert_cmpint (g_date_time_get_year (dt), ==, 1900 + tm.tm_year);
122   g_assert_cmpint (g_date_time_get_month (dt), ==, 1 + tm.tm_mon);
123   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, tm.tm_mday);
124   g_assert_cmpint (g_date_time_get_hour (dt), ==, tm.tm_hour);
125   g_assert_cmpint (g_date_time_get_minute (dt), ==, tm.tm_min);
126   g_assert_cmpint (g_date_time_get_second (dt), ==, tm.tm_sec);
127   g_date_time_unref (dt);
128
129   memset (&tm, 0, sizeof (tm));
130   tm.tm_year = 70;
131   tm.tm_mday = 1;
132   tm.tm_mon = 0;
133   tm.tm_hour = 0;
134   tm.tm_min = 0;
135   tm.tm_sec = 0;
136   t = mktime (&tm);
137
138   dt = g_date_time_new_from_epoch (t);
139   g_assert_cmpint (g_date_time_get_year (dt), ==, 1970);
140   g_assert_cmpint (g_date_time_get_month (dt), ==, 1);
141   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 1);
142   g_assert_cmpint (g_date_time_get_hour (dt), ==, 0);
143   g_assert_cmpint (g_date_time_get_minute (dt), ==, 0);
144   g_assert_cmpint (g_date_time_get_second (dt), ==, 0);
145   g_date_time_unref (dt);
146 }
147
148 static void
149 test_GDateTime_is_leap_year (void)
150 {
151   GDateTime *dt;
152   gint       i;
153
154   for (i = 1; i <= 3000; i++)
155     {
156       dt = g_date_time_new_from_date (i, 1, 1);
157       if ((((i % 4) == 0) && ((i % 100) != 0)) || ((i % 400) == 0))
158         g_assert (g_date_time_is_leap_year (dt));
159       else
160         g_assert (!g_date_time_is_leap_year (dt));
161       g_date_time_unref (dt);
162     }
163 }
164
165 static void
166 test_GDateTime_compare (void)
167 {
168   GDateTime *dt1, *dt2;
169   gint       i;
170
171   dt1 = g_date_time_new_from_date (2000, 1, 1);
172
173   for (i = 1; i < 2000; i++)
174     {
175       dt2 = g_date_time_new_from_date (i, 12, 31);
176       g_assert_cmpint (1, ==, g_date_time_compare (dt1, dt2));
177       g_date_time_unref (dt2);
178     }
179
180   dt2 = g_date_time_new_full (1999, 12, 31, 23, 59, 59, NULL);
181   g_assert_cmpint (1, ==, g_date_time_compare (dt1, dt2));
182   g_date_time_unref (dt2);
183
184   dt2 = g_date_time_new_full (2000, 1, 1, 0, 0, 1, NULL);
185   g_assert_cmpint (-1, ==, g_date_time_compare (dt1, dt2));
186   g_date_time_unref (dt2);
187
188   dt2 = g_date_time_new_full (2000, 1, 1, 0, 0, 0, NULL);
189   g_assert_cmpint (0, ==, g_date_time_compare (dt1, dt2));
190   g_date_time_unref (dt2);
191
192   g_date_time_unref (dt1);
193 }
194
195 static void
196 test_GDateTime_copy (void)
197 {
198   GDateTime *dt1, *dt2;
199
200   dt1 = g_date_time_new_now ();
201   dt2 = g_date_time_copy (dt1);
202   g_assert (dt1 != NULL);
203   g_assert (dt2 != NULL);
204   g_assert_cmpint (0, ==, g_date_time_compare (dt1, dt2));
205   g_date_time_unref (dt1);
206   g_date_time_unref (dt2);
207 }
208
209 static void
210 test_GDateTime_date (void)
211 {
212   GDateTime *dt1, *dt2;
213
214   dt1 = g_date_time_new_full (2009, 10, 19, 13, 0, 55, NULL);
215   dt2 = g_date_time_day (dt1);
216   g_assert (dt1 != NULL);
217   g_assert (dt2 != NULL);
218   g_assert_cmpint (2009, ==, g_date_time_get_year (dt2));
219   g_assert_cmpint (10, ==, g_date_time_get_month (dt2));
220   g_assert_cmpint (19, ==, g_date_time_get_day_of_month (dt2));
221   g_assert_cmpint (0, ==, g_date_time_get_hour (dt2));
222   g_assert_cmpint (0, ==, g_date_time_get_minute (dt2));
223   g_assert_cmpint (0, ==, g_date_time_get_second (dt2));
224   g_date_time_unref (dt1);
225   g_date_time_unref (dt2);
226 }
227
228 static void
229 test_GDateTime_equal (void)
230 {
231   GDateTime *dt1, *dt2;
232
233   dt1 = g_date_time_new_from_date (2009, 10, 19);
234   dt2 = g_date_time_new_from_date (2009, 10, 19);
235   g_assert (g_date_time_equal (dt1, dt2));
236   g_date_time_unref (dt1);
237   g_date_time_unref (dt2);
238
239   dt1 = g_date_time_new_from_date (2009, 10, 18);
240   dt2 = g_date_time_new_from_date (2009, 10, 19);
241   g_assert (!g_date_time_equal (dt1, dt2));
242   g_date_time_unref (dt1);
243   g_date_time_unref (dt2);
244
245   /* America/Recife is GMT-3 and is not in DST for this time */
246   dt1 = g_date_time_new_full (2010, 5, 24, 8, 0, 0, "America/Recife");
247   dt2 = g_date_time_new_full (2010, 5, 24, 11, 0, 0, "UTC");
248   g_assert (g_date_time_equal (dt1, dt2));
249   g_date_time_unref (dt1);
250   g_date_time_unref (dt2);
251 }
252
253 static void
254 test_GDateTime_get_day_of_week (void)
255 {
256   GDateTime *dt;
257
258   dt = g_date_time_new_from_date (2009, 10, 19);
259   g_assert_cmpint (1, ==, g_date_time_get_day_of_week (dt));
260   g_date_time_unref (dt);
261
262   dt = g_date_time_new_from_date (2000, 10, 1);
263   g_assert_cmpint (7, ==, g_date_time_get_day_of_week (dt));
264   g_date_time_unref (dt);
265 }
266
267 static void
268 test_GDateTime_get_day_of_month (void)
269 {
270   GDateTime *dt;
271
272   dt = g_date_time_new_from_date (2009, 10, 19);
273   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 19);
274   g_date_time_unref (dt);
275
276   dt = g_date_time_new_from_date (1400, 3, 12);
277   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 12);
278   g_date_time_unref (dt);
279
280   dt = g_date_time_new_from_date (1800, 12, 31);
281   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 31);
282   g_date_time_unref (dt);
283
284   dt = g_date_time_new_from_date (1000, 1, 1);
285   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 1);
286   g_date_time_unref (dt);
287 }
288
289 static void
290 test_GDateTime_get_dmy (void)
291 {
292    GDateTime *dt;
293    struct tm tm;
294    time_t t;
295    gint d, m, y;
296    gint d2, m2, y2;
297    gint days[2][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
298                        {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
299
300    t = time (NULL);
301    memset (&tm, 0, sizeof (struct tm));
302    get_localtime_tm (t, &tm);
303
304    dt = g_date_time_new_from_epoch (t);
305    g_date_time_get_dmy(dt, &d, &m, &y);
306    g_assert_cmpint(y, ==, tm.tm_year + 1900);
307    g_assert_cmpint(m, ==, tm.tm_mon + 1);
308    g_assert_cmpint(d, ==, tm.tm_mday);
309
310    /* exaustive test */
311    for (y = 1750; y < 2250; y++)
312      {
313        gint leap = ((y % 4) == 0) && (!(((y % 100) == 0) && ((y % 400) != 0)))
314                  ? 1
315                  : 0;
316
317        for (m = 1; m <= 12; m++)
318          {
319            for (d = 1; d <= days[leap][m]; d++)
320              {
321                GDateTime *dt1 = g_date_time_new_from_date (y, m, d);
322
323                g_date_time_get_dmy (dt1, &d2, &m2, &y2);
324                g_assert_cmpint (y, ==, y2);
325                g_assert_cmpint (m, ==, m2);
326                g_assert_cmpint (d, ==, d2);
327                g_date_time_unref (dt1);
328              }
329          }
330      }
331 }
332
333 static void
334 test_GDateTime_get_hour (void)
335 {
336   GDateTime *dt;
337
338   dt = g_date_time_new_full (2009, 10, 19, 15, 13, 11, NULL);
339   g_assert_cmpint (15, ==, g_date_time_get_hour (dt));
340   g_date_time_unref (dt);
341
342   dt = g_date_time_new_full (100, 10, 19, 1, 0, 0, NULL);
343   g_assert_cmpint (1, ==, g_date_time_get_hour (dt));
344   g_date_time_unref (dt);
345
346   dt = g_date_time_new_full (100, 10, 19, 0, 0, 0, NULL);
347   g_assert_cmpint (0, ==, g_date_time_get_hour (dt));
348   g_date_time_unref (dt);
349
350   dt = g_date_time_new_full (100, 10, 1, 23, 59, 59, NULL);
351   g_assert_cmpint (23, ==, g_date_time_get_hour (dt));
352   g_date_time_unref (dt);
353 }
354
355 static void
356 test_GDateTime_get_julian (void)
357 {
358   GDateTime *dt;
359   gint period, julian, hour = 1, min = 1, sec = 1;
360
361   dt = g_date_time_new_from_date (1984, 8, 16);
362   g_date_time_get_julian (dt, &period, &julian, &hour, &min, &sec);
363   g_assert_cmpint (period, ==, 0);
364   g_assert_cmpint (julian, ==, 2445929);
365   g_assert_cmpint (hour, ==, 0);
366   g_assert_cmpint (min, ==, 0);
367   g_assert_cmpint (sec, ==, 0);
368   g_date_time_unref (dt);
369 }
370
371 static void
372 test_GDateTime_get_microsecond (void)
373 {
374   GTimeVal   tv;
375   GDateTime *dt;
376
377   g_get_current_time (&tv);
378   dt = g_date_time_new_from_timeval (&tv);
379   g_assert_cmpint (tv.tv_usec, ==, g_date_time_get_microsecond (dt));
380   g_date_time_unref (dt);
381 }
382
383 static void
384 test_GDateTime_get_millisecond (void)
385 {
386   GTimeVal   tv;
387   GDateTime *dt;
388
389   g_get_current_time (&tv);
390   dt = g_date_time_new_from_timeval (&tv);
391   g_assert_cmpint ((tv.tv_usec/1000), ==, g_date_time_get_millisecond (dt));
392   g_date_time_unref (dt);
393 }
394
395 static void
396 test_GDateTime_get_year (void)
397 {
398   GDateTime *dt;
399
400   dt = g_date_time_new_from_date (2009, 1, 1);
401   g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
402   g_date_time_unref (dt);
403
404   dt = g_date_time_new_from_date (1, 1, 1);
405   g_assert_cmpint (1, ==, g_date_time_get_year (dt));
406   g_date_time_unref (dt);
407
408   dt = g_date_time_new_from_date (13, 1, 1);
409   g_assert_cmpint (13, ==, g_date_time_get_year (dt));
410   g_date_time_unref (dt);
411
412   dt = g_date_time_new_from_date (3000, 1, 1);
413   g_assert_cmpint (3000, ==, g_date_time_get_year (dt));
414   g_date_time_unref (dt);
415 }
416
417 static void
418 test_GDateTime_hash (void)
419 {
420   GHashTable *h;
421
422   h = g_hash_table_new_full (g_date_time_hash, g_date_time_equal,
423                              (GDestroyNotify)g_date_time_unref,
424                              NULL);
425   g_hash_table_insert (h, g_date_time_new_now (), NULL);
426   g_hash_table_remove_all (h);
427   g_hash_table_destroy (h);
428 }
429
430 static void
431 test_GDateTime_new_from_timeval (void)
432 {
433   GDateTime *dt;
434   GTimeVal   tv, tv2;
435
436   g_get_current_time (&tv);
437   dt = g_date_time_new_from_timeval (&tv);
438
439   if (g_test_verbose ())
440     g_print ("\nDT%d/%d/%d\n",
441              g_date_time_get_year (dt),
442              g_date_time_get_month (dt),
443              g_date_time_get_day_of_month (dt));
444
445   g_date_time_to_timeval (dt, &tv2);
446   g_assert_cmpint (tv.tv_sec, ==, tv2.tv_sec);
447   g_assert_cmpint (tv.tv_usec, ==, tv2.tv_usec);
448   g_date_time_unref (dt);
449 }
450
451 static void
452 test_GDateTime_to_epoch (void)
453 {
454   GDateTime *dt;
455   time_t     t;
456
457   t = time (NULL);
458   dt = g_date_time_new_from_epoch (t);
459   g_assert_cmpint (g_date_time_to_epoch (dt), ==, t);
460   g_date_time_unref (dt);
461 }
462
463 static void
464 test_GDateTime_add_years (void)
465 {
466   GDateTime *dt, *dt2;
467
468   dt = g_date_time_new_from_date (2009, 10, 19);
469   dt2 = g_date_time_add_years (dt, 1);
470   g_assert_cmpint (2010, ==, g_date_time_get_year (dt2));
471   g_date_time_unref (dt);
472   g_date_time_unref (dt2);
473 }
474
475 static void
476 test_GDateTime_add_months (void)
477 {
478 #define TEST_ADD_MONTHS(y,m,d,a,ny,nm,nd) G_STMT_START { \
479   GDateTime *dt, *dt2; \
480   dt = g_date_time_new_from_date (y, m, d); \
481   dt2 = g_date_time_add_months (dt, a); \
482   ASSERT_DATE (dt2, ny, nm, nd); \
483   g_date_time_unref (dt); \
484   g_date_time_unref (dt2); \
485 } G_STMT_END
486
487   TEST_ADD_MONTHS (2009, 12, 31, 1, 2010, 1, 31);
488   TEST_ADD_MONTHS (2009, 12, 31, 1, 2010, 1, 31);
489   TEST_ADD_MONTHS (2009, 6, 15, 1, 2009, 7, 15);
490   TEST_ADD_MONTHS (1400, 3, 1, 1, 1400, 4, 1);
491   TEST_ADD_MONTHS (1400, 1, 31, 1, 1400, 2, 28);
492   TEST_ADD_MONTHS (1400, 1, 31, 7200, 2000, 1, 31);
493   TEST_ADD_MONTHS (2008, 2, 29, 12, 2009, 2, 28);
494   TEST_ADD_MONTHS (2000, 8, 16, -5, 2000, 3, 16);
495   TEST_ADD_MONTHS (2000, 8, 16, -12, 1999, 8, 16);
496   TEST_ADD_MONTHS (2011, 2, 1, -13, 2010, 1, 1);
497   TEST_ADD_MONTHS (1776, 7, 4, 1200, 1876, 7, 4);
498 }
499
500 static void
501 test_GDateTime_add_days (void)
502 {
503 #define TEST_ADD_DAYS(y,m,d,a,ny,nm,nd) G_STMT_START { \
504   GDateTime *dt, *dt2; \
505   dt = g_date_time_new_from_date (y, m, d); \
506   dt2 = g_date_time_add_days (dt, a); \
507   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
508   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
509   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
510   g_date_time_unref (dt); \
511   g_date_time_unref (dt2); \
512 } G_STMT_END
513
514   TEST_ADD_DAYS (2009, 1, 31, 1, 2009, 2, 1);
515   TEST_ADD_DAYS (2009, 2, 1, -1, 2009, 1, 31);
516   TEST_ADD_DAYS (2008, 2, 28, 1, 2008, 2, 29);
517   TEST_ADD_DAYS (2008, 12, 31, 1, 2009, 1, 1);
518   TEST_ADD_DAYS (1, 1, 1, 1, 1, 1, 2);
519   TEST_ADD_DAYS (1955, 5, 24, 10, 1955, 6, 3);
520   TEST_ADD_DAYS (1955, 5, 24, -10, 1955, 5, 14);
521 }
522
523 static void
524 test_GDateTime_add_weeks (void)
525 {
526 #define TEST_ADD_WEEKS(y,m,d,a,ny,nm,nd) G_STMT_START { \
527   GDateTime *dt, *dt2; \
528   dt = g_date_time_new_from_date (y, m, d); \
529   dt2 = g_date_time_add_weeks (dt, a); \
530   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
531   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
532   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
533   g_date_time_unref (dt); \
534   g_date_time_unref (dt2); \
535 } G_STMT_END
536
537   TEST_ADD_WEEKS (2009, 1, 1, 1, 2009, 1, 8);
538   TEST_ADD_WEEKS (2009, 8, 30, 1, 2009, 9, 6);
539   TEST_ADD_WEEKS (2009, 12, 31, 1, 2010, 1, 7);
540   TEST_ADD_WEEKS (2009, 1, 1, -1, 2008, 12, 25);
541 }
542
543 static void
544 test_GDateTime_add_hours (void)
545 {
546 #define TEST_ADD_HOURS(y,m,d,h,mi,s,a,ny,nm,nd,nh,nmi,ns) G_STMT_START { \
547   GDateTime *dt, *dt2; \
548   dt = g_date_time_new_full (y, m, d, h, mi, s, NULL); \
549   dt2 = g_date_time_add_hours (dt, a); \
550   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
551   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
552   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
553   g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
554   g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
555   g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
556   g_date_time_unref (dt); \
557   g_date_time_unref (dt2); \
558 } G_STMT_END
559
560   TEST_ADD_HOURS (2009,  1,  1,  0, 0, 0, 1, 2009, 1, 1, 1, 0, 0);
561   TEST_ADD_HOURS (2008, 12, 31, 23, 0, 0, 1, 2009, 1, 1, 0, 0, 0);
562 }
563
564 static void
565 test_GDateTime_add_full (void)
566 {
567 #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 { \
568   GDateTime *dt, *dt2; \
569   dt = g_date_time_new_full (y, m, d, h, mi, s, NULL); \
570   dt2 = g_date_time_add_full (dt, ay, am, ad, ah, ami, as); \
571   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
572   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
573   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
574   g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
575   g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
576   g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
577   g_date_time_unref (dt); \
578   g_date_time_unref (dt2); \
579 } G_STMT_END
580
581   TEST_ADD_FULL (2009, 10, 21,  0,  0, 0,
582                     1,  1,  1,  1,  1, 1,
583                  2010, 11, 22,  1,  1, 1);
584   TEST_ADD_FULL (2000,  1,  1,  1,  1, 1,
585                     0,  1,  0,  0,  0, 0,
586                  2000,  2,  1,  1,  1, 1);
587   TEST_ADD_FULL (2000,  1,  1,  0,  0, 0,
588                    -1,  1,  0,  0,  0, 0,
589                  1999,  2,  1,  0,  0, 0);
590   TEST_ADD_FULL (2010, 10, 31,  0,  0, 0,
591                     0,  4,  0,  0,  0, 0,
592                  2011,  2, 28,  0,  0, 0);
593   TEST_ADD_FULL (2010,  8, 25, 22, 45, 0,
594                     0,  1,  6,  1, 25, 0,
595                  2010, 10,  2,  0, 10, 0);
596 }
597
598 static void
599 test_GDateTime_add_milliseconds (void)
600 {
601 #define TEST_ADD_MILLISECOND(i,o) G_STMT_START { \
602   GDateTime *dt, *dt2; \
603   dt = g_date_time_new_from_date (2000, 1, 1); \
604   dt2 = g_date_time_add_milliseconds (dt, i); \
605   g_assert_cmpint (o, ==, g_date_time_get_millisecond (dt2)); \
606   g_date_time_unref (dt); \
607   g_date_time_unref (dt2); \
608 } G_STMT_END
609
610   TEST_ADD_MILLISECOND (199, 199);
611   TEST_ADD_MILLISECOND (10001, 1);
612   TEST_ADD_MILLISECOND (22201, 201);
613   TEST_ADD_MILLISECOND (-1000, 0);
614 }
615
616 static void
617 test_GDateTime_add_minutes (void)
618 {
619 #define TEST_ADD_MINUTES(i,o) G_STMT_START { \
620   GDateTime *dt, *dt2; \
621   dt = g_date_time_new_from_date (2000, 1, 1); \
622   dt2 = g_date_time_add_minutes (dt, i); \
623   g_assert_cmpint (o, ==, g_date_time_get_minute (dt2)); \
624   g_date_time_unref (dt); \
625   g_date_time_unref (dt2); \
626 } G_STMT_END
627
628   TEST_ADD_MINUTES (60, 0);
629   TEST_ADD_MINUTES (100, 40);
630   TEST_ADD_MINUTES (5, 5);
631   TEST_ADD_MINUTES (86401, 1);
632   TEST_ADD_MINUTES (-86401, 59);
633 }
634
635 static void
636 test_GDateTime_add_seconds (void)
637 {
638 #define TEST_ADD_SECONDS(i,o) G_STMT_START { \
639   GDateTime *dt, *dt2; \
640   dt = g_date_time_new_from_date (2000, 1, 1); \
641   dt2 = g_date_time_add_seconds (dt, i); \
642   g_assert_cmpint (o, ==, g_date_time_get_second (dt2)); \
643   g_date_time_unref (dt); \
644   g_date_time_unref (dt2); \
645 } G_STMT_END
646
647   TEST_ADD_SECONDS (1, 1);
648   TEST_ADD_SECONDS (60, 0);
649   TEST_ADD_SECONDS (61, 1);
650   TEST_ADD_SECONDS (120, 0);
651   TEST_ADD_SECONDS (-61, 59);
652   TEST_ADD_SECONDS (86401, 1);
653   TEST_ADD_SECONDS (-86401, 59);
654   TEST_ADD_SECONDS (-31, 29);
655   TEST_ADD_SECONDS (13, 13);
656 }
657
658 static void
659 test_GDateTime_diff (void)
660 {
661 #define TEST_DIFF(y,m,d,y2,m2,d2,u) G_STMT_START { \
662   GDateTime *dt1, *dt2; \
663   GTimeSpan  ts = 0; \
664   dt1 = g_date_time_new_from_date (y, m, d); \
665   dt2 = g_date_time_new_from_date (y2, m2, d2); \
666   ts = g_date_time_difference (dt1, dt2); \
667   g_assert_cmpint (ts, ==, u); \
668   g_date_time_unref (dt1); \
669   g_date_time_unref (dt2); \
670 } G_STMT_END
671
672   TEST_DIFF (2009, 1, 1, 2009, 2, 1, G_TIME_SPAN_DAY * 31);
673   TEST_DIFF (2009, 1, 1, 2010, 1, 1, G_TIME_SPAN_DAY * 365);
674   TEST_DIFF (2008, 2, 28, 2008, 2, 29, G_TIME_SPAN_DAY);
675   TEST_DIFF (2008, 2, 29, 2008, 2, 28, -G_TIME_SPAN_DAY);
676
677   /* TODO: Add usec tests */
678 }
679
680 static void
681 test_GDateTime_get_minute (void)
682 {
683   GDateTime *dt;
684
685   dt = g_date_time_new_full (2009, 12, 1, 1, 31, 0, NULL);
686   g_assert_cmpint (31, ==, g_date_time_get_minute (dt));
687   g_date_time_unref (dt);
688 }
689
690 static void
691 test_GDateTime_get_month (void)
692 {
693   GDateTime *dt;
694
695   dt = g_date_time_new_full (2009, 12, 1, 1, 31, 0, NULL);
696   g_assert_cmpint (12, ==, g_date_time_get_month (dt));
697   g_date_time_unref (dt);
698 }
699
700 static void
701 test_GDateTime_get_second (void)
702 {
703   GDateTime *dt;
704
705   dt = g_date_time_new_full (2009, 12, 1, 1, 31, 44, NULL);
706   g_assert_cmpint (44, ==, g_date_time_get_second (dt));
707   g_date_time_unref (dt);
708 }
709
710 static void
711 test_GDateTime_new_from_date (void)
712 {
713   GDateTime *dt;
714
715   dt = g_date_time_new_from_date (2009, 12, 11);
716   g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
717   g_assert_cmpint (12, ==, g_date_time_get_month (dt));
718   g_assert_cmpint (11, ==, g_date_time_get_day_of_month (dt));
719   g_date_time_unref (dt);
720 }
721
722 static void
723 test_GDateTime_new_full (void)
724 {
725   GDateTime *dt;
726
727   dt = g_date_time_new_full (2009, 12, 11, 12, 11, 10, NULL);
728   g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
729   g_assert_cmpint (12, ==, g_date_time_get_month (dt));
730   g_assert_cmpint (11, ==, g_date_time_get_day_of_month (dt));
731   g_assert_cmpint (12, ==, g_date_time_get_hour (dt));
732   g_assert_cmpint (11, ==, g_date_time_get_minute (dt));
733   g_assert_cmpint (10, ==, g_date_time_get_second (dt));
734   g_date_time_unref (dt);
735
736   dt = g_date_time_new_full (2010, 5, 24, 8, 4, 0, "America/Recife");
737   g_assert_cmpint (2010, ==, g_date_time_get_year (dt));
738   g_assert_cmpint (5, ==, g_date_time_get_month (dt));
739   g_assert_cmpint (24, ==, g_date_time_get_day_of_month (dt));
740   g_assert_cmpint (8, ==, g_date_time_get_hour (dt));
741   g_assert_cmpint (4, ==, g_date_time_get_minute (dt));
742   g_assert_cmpint (0, ==, g_date_time_get_second (dt));
743   g_assert_cmpstr ("BRT", ==, g_date_time_get_timezone_name (dt));
744   g_assert (!g_date_time_is_daylight_savings (dt));
745   g_date_time_unref (dt);
746 }
747
748 static void
749 test_GDateTime_utc_now (void)
750 {
751   GDateTime *dt;
752   time_t     t;
753   struct tm  tm;
754
755   t = time (NULL);
756 #ifdef HAVE_GMTIME_R
757   gmtime_r (&t, &tm);
758 #else
759   {
760     struct tm *tmp = gmtime (&t);
761     /* Assume gmtime() can't fail as we got t from time(NULL). (Note
762      * that on Windows, gmtime() *is* MT-safe, it uses a thread-local
763      * buffer.)
764      */
765     memcpy (&tm, tmp, sizeof (struct tm));
766   }
767 #endif
768   dt = g_date_time_new_utc_now ();
769   g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
770   g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
771   g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
772   g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
773   g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
774   g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
775   g_date_time_unref (dt);
776 }
777
778 static void
779 test_GDateTime_get_utc_offset (void)
780 {
781   GDateTime *dt;
782   GTimeSpan ts;
783   struct tm tm;
784
785   memset (&tm, 0, sizeof (tm));
786   get_localtime_tm (time (NULL), &tm);
787
788   dt = g_date_time_new_now ();
789   ts = g_date_time_get_utc_offset (dt);
790 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
791   g_assert_cmpint (ts, ==, (tm.tm_gmtoff * G_TIME_SPAN_SECOND));
792 #endif
793   g_date_time_unref (dt);
794 }
795
796 static void
797 test_GDateTime_to_timeval (void)
798 {
799   GTimeVal tv1, tv2;
800   GDateTime *dt;
801
802   memset (&tv1, 0, sizeof (tv1));
803   memset (&tv2, 0, sizeof (tv2));
804
805   g_get_current_time (&tv1);
806   dt = g_date_time_new_from_timeval (&tv1);
807   g_date_time_to_timeval (dt, &tv2);
808   g_assert_cmpint (tv1.tv_sec, ==, tv2.tv_sec);
809   g_assert_cmpint (tv1.tv_usec, ==, tv2.tv_usec);
810   g_date_time_unref (dt);
811 }
812
813 static void
814 test_GDateTime_to_local (void)
815 {
816   GDateTime *utc, *now, *dt;
817
818   utc = g_date_time_new_utc_now ();
819   now = g_date_time_new_now ();
820   dt = g_date_time_to_local (utc);
821
822   g_assert_cmpint (g_date_time_get_year (now), ==, g_date_time_get_year (dt));
823   g_assert_cmpint (g_date_time_get_month (now), ==, g_date_time_get_month (dt));
824   g_assert_cmpint (g_date_time_get_day_of_month (now), ==, g_date_time_get_day_of_month (dt));
825   g_assert_cmpint (g_date_time_get_hour (now), ==, g_date_time_get_hour (dt));
826   g_assert_cmpint (g_date_time_get_minute (now), ==, g_date_time_get_minute (dt));
827   g_assert_cmpint (g_date_time_get_second (now), ==, g_date_time_get_second (dt));
828
829   g_date_time_unref (now);
830   g_date_time_unref (utc);
831   g_date_time_unref (dt);
832 }
833
834 static void
835 test_GDateTime_to_utc (void)
836 {
837   GDateTime *dt, *dt2;
838   time_t     t;
839   struct tm  tm;
840
841   t = time (NULL);
842 #ifdef HAVE_GMTIME_R
843   gmtime_r (&t, &tm);
844 #else
845   {
846     struct tm *tmp = gmtime (&t);
847     memcpy (&tm, tmp, sizeof (struct tm));
848   }
849 #endif
850   dt2 = g_date_time_new_now ();
851   dt = g_date_time_to_utc (dt2);
852   g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
853   g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
854   g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
855   g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
856   g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
857   g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
858   g_date_time_unref (dt);
859   g_date_time_unref (dt2);
860 }
861
862 static void
863 test_GDateTime_get_day_of_year (void)
864 {
865 #define TEST_DAY_OF_YEAR(y,m,d,o)                       G_STMT_START {  \
866   GDateTime *__dt = g_date_time_new_from_date ((y), (m), (d));          \
867   g_assert_cmpint ((o), ==, g_date_time_get_day_of_year (__dt));        \
868   g_date_time_unref (__dt);                             } G_STMT_END
869
870   TEST_DAY_OF_YEAR (2009, 1, 1, 1);
871   TEST_DAY_OF_YEAR (2009, 2, 1, 32);
872   TEST_DAY_OF_YEAR (2009, 8, 16, 228);
873   TEST_DAY_OF_YEAR (2008, 8, 16, 229);
874 }
875
876 static void
877 test_GDateTime_printf (void)
878 {
879   gchar dst[16];
880   struct tm tt;
881   time_t t;
882   gchar t_str[16];
883
884 #define TEST_PRINTF(f,o)                        G_STMT_START {  \
885 GDateTime *__dt = g_date_time_new_from_date (2009, 10, 24);     \
886   gchar *__p = g_date_time_printf (__dt, (f));                  \
887   g_assert_cmpstr (__p, ==, (o));                               \
888   g_date_time_unref (__dt);                                     \
889   g_free (__p);                                 } G_STMT_END
890
891 #define TEST_PRINTF_DATE(y,m,d,f,o)             G_STMT_START {  \
892   GDateTime *dt = g_date_time_new_from_date ((y), (m), (d));    \
893   gchar *p = g_date_time_printf (dt, (f));                      \
894   g_assert_cmpstr (p, ==, (o));                                 \
895   g_date_time_unref (dt);                                       \
896   g_free (p);                                   } G_STMT_END
897
898 #define TEST_PRINTF_TIME(h,m,s,f,o)             G_STMT_START { \
899   GDateTime *dt = g_date_time_new_full (2009, 10, 24, (h), (m), (s), NULL); \
900   gchar *p = g_date_time_printf (dt, (f));                      \
901   g_assert_cmpstr (p, ==, (o));                                 \
902   g_date_time_unref (dt);                                       \
903   g_free (p);                                   } G_STMT_END
904
905   /*
906    * This is a little helper to make sure we can compare timezones to
907    * that of the generated timezone.
908    */
909   t = time (NULL);
910   memset (&tt, 0, sizeof(tt));
911   get_localtime_tm (t, &tt);
912   tt.tm_year = 2009 - 1900;
913   tt.tm_mon = 9; /* 0 indexed */
914   tt.tm_mday = 24;
915   t = mktime (&tt);
916   memset (&tt, 0, sizeof(tt));
917   get_localtime_tm (t, &tt);
918   strftime (dst, sizeof(dst), "%Z", &tt);
919
920   /* get current time_t for 20090924 in the local timezone */
921   tt.tm_sec = 0;
922   tt.tm_min = 0;
923   tt.tm_hour = 0;
924   t = mktime (&tt);
925   g_sprintf (t_str, "%ld", t);
926
927   TEST_PRINTF ("%a", "Sat");
928   TEST_PRINTF ("%A", "Saturday");
929   TEST_PRINTF ("%b", "Oct");
930   TEST_PRINTF ("%B", "October");
931   TEST_PRINTF ("%d", "24");
932   TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
933   TEST_PRINTF ("%e", "24"); // fixme
934   TEST_PRINTF ("%h", "Oct");
935   TEST_PRINTF ("%H", "00");
936   TEST_PRINTF_TIME (15, 0, 0, "%H", "15");
937   TEST_PRINTF ("%I", "12");
938   TEST_PRINTF_TIME (15, 0, 0, "%I", "03");
939   TEST_PRINTF ("%j", "297");
940   TEST_PRINTF ("%k", " 0");
941   TEST_PRINTF_TIME (13, 13, 13, "%k", "13");
942   TEST_PRINTF ("%l", "12");
943   TEST_PRINTF_TIME (13, 13, 13, "%l", " 1");
944   TEST_PRINTF_TIME (10, 13, 13, "%l", "10");
945   TEST_PRINTF ("%m", "10");
946   TEST_PRINTF ("%M", "00");
947   TEST_PRINTF ("%N", "0");
948   TEST_PRINTF ("%p", "AM");
949   TEST_PRINTF_TIME (13, 13, 13, "%p", "PM");
950   TEST_PRINTF ("%P", "am");
951   TEST_PRINTF_TIME (13, 13, 13, "%P", "pm");
952   TEST_PRINTF ("%r", "12:00:00 AM");
953   TEST_PRINTF_TIME (13, 13, 13, "%r", "01:13:13 PM");
954   TEST_PRINTF ("%R", "00:00");
955   TEST_PRINTF_TIME (13, 13, 31, "%R", "13:13");
956   TEST_PRINTF ("%s", t_str);
957   TEST_PRINTF ("%S", "00");
958   TEST_PRINTF ("%t", "  ");
959   TEST_PRINTF ("%W", "42");
960   TEST_PRINTF ("%u", "6");
961   TEST_PRINTF ("%x", "10/24/09");
962   TEST_PRINTF ("%X", "00:00:00");
963   TEST_PRINTF_TIME (13, 14, 15, "%X", "13:14:15");
964   TEST_PRINTF ("%y", "09");
965   TEST_PRINTF ("%Y", "2009");
966   TEST_PRINTF ("%%", "%");
967   TEST_PRINTF ("%", "");
968   TEST_PRINTF ("%9", NULL);
969   TEST_PRINTF ("%Z", dst);
970 }
971
972 gint
973 main (gint   argc,
974       gchar *argv[])
975 {
976   g_test_init (&argc, &argv, NULL);
977
978   /* GDateTime Tests */
979
980   g_test_add_func ("/GDateTime/add_days", test_GDateTime_add_days);
981   g_test_add_func ("/GDateTime/add_full", test_GDateTime_add_full);
982   g_test_add_func ("/GDateTime/add_hours", test_GDateTime_add_hours);
983   g_test_add_func ("/GDateTime/add_milliseconds", test_GDateTime_add_milliseconds);
984   g_test_add_func ("/GDateTime/add_minutes", test_GDateTime_add_minutes);
985   g_test_add_func ("/GDateTime/add_months", test_GDateTime_add_months);
986   g_test_add_func ("/GDateTime/add_seconds", test_GDateTime_add_seconds);
987   g_test_add_func ("/GDateTime/add_weeks", test_GDateTime_add_weeks);
988   g_test_add_func ("/GDateTime/add_years", test_GDateTime_add_years);
989   g_test_add_func ("/GDateTime/compare", test_GDateTime_compare);
990   g_test_add_func ("/GDateTime/copy", test_GDateTime_copy);
991   g_test_add_func ("/GDateTime/date", test_GDateTime_date);
992   g_test_add_func ("/GDateTime/diff", test_GDateTime_diff);
993   g_test_add_func ("/GDateTime/equal", test_GDateTime_equal);
994   g_test_add_func ("/GDateTime/get_day_of_week", test_GDateTime_get_day_of_week);
995   g_test_add_func ("/GDateTime/get_day_of_month", test_GDateTime_get_day_of_month);
996   g_test_add_func ("/GDateTime/get_day_of_year", test_GDateTime_get_day_of_year);
997   g_test_add_func ("/GDateTime/get_dmy", test_GDateTime_get_dmy);
998   g_test_add_func ("/GDateTime/get_hour", test_GDateTime_get_hour);
999   g_test_add_func ("/GDateTime/get_julian", test_GDateTime_get_julian);
1000   g_test_add_func ("/GDateTime/get_microsecond", test_GDateTime_get_microsecond);
1001   g_test_add_func ("/GDateTime/get_millisecond", test_GDateTime_get_millisecond);
1002   g_test_add_func ("/GDateTime/get_minute", test_GDateTime_get_minute);
1003   g_test_add_func ("/GDateTime/get_month", test_GDateTime_get_month);
1004   g_test_add_func ("/GDateTime/get_second", test_GDateTime_get_second);
1005   g_test_add_func ("/GDateTime/get_utc_offset", test_GDateTime_get_utc_offset);
1006   g_test_add_func ("/GDateTime/get_year", test_GDateTime_get_year);
1007   g_test_add_func ("/GDateTime/hash", test_GDateTime_hash);
1008   g_test_add_func ("/GDateTime/is_leap_year", test_GDateTime_is_leap_year);
1009   g_test_add_func ("/GDateTime/new_from_date", test_GDateTime_new_from_date);
1010   g_test_add_func ("/GDateTime/new_from_epoch", test_GDateTime_new_from_epoch);
1011   g_test_add_func ("/GDateTime/new_from_timeval", test_GDateTime_new_from_timeval);
1012   g_test_add_func ("/GDateTime/new_full", test_GDateTime_new_full);
1013   g_test_add_func ("/GDateTime/now", test_GDateTime_now);
1014   g_test_add_func ("/GDateTime/printf", test_GDateTime_printf);
1015   g_test_add_func ("/GDateTime/to_local", test_GDateTime_to_local);
1016   g_test_add_func ("/GDateTime/to_epoch", test_GDateTime_to_epoch);
1017   g_test_add_func ("/GDateTime/to_timeval", test_GDateTime_to_timeval);
1018   g_test_add_func ("/GDateTime/to_utc", test_GDateTime_to_utc);
1019   g_test_add_func ("/GDateTime/today", test_GDateTime_today);
1020   g_test_add_func ("/GDateTime/utc_now", test_GDateTime_utc_now);
1021
1022   return g_test_run ();
1023 }