datetime: Allow setting fractionary seconds in new_full()
[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   g_date_time_unref (dt1);
192 }
193
194 static void
195 test_GDateTime_date (void)
196 {
197   GDateTime *dt1, *dt2;
198
199   dt1 = g_date_time_new_full (2009, 10, 19, 13, 0, 55, NULL);
200   dt2 = g_date_time_day (dt1);
201   g_assert (dt1 != NULL);
202   g_assert (dt2 != NULL);
203   g_assert_cmpint (2009, ==, g_date_time_get_year (dt2));
204   g_assert_cmpint (10, ==, g_date_time_get_month (dt2));
205   g_assert_cmpint (19, ==, g_date_time_get_day_of_month (dt2));
206   g_assert_cmpint (0, ==, g_date_time_get_hour (dt2));
207   g_assert_cmpint (0, ==, g_date_time_get_minute (dt2));
208   g_assert_cmpint (0, ==, g_date_time_get_second (dt2));
209   g_date_time_unref (dt1);
210   g_date_time_unref (dt2);
211 }
212
213 static void
214 test_GDateTime_equal (void)
215 {
216   GDateTime *dt1, *dt2;
217   GTimeZone *time_zone;
218
219   dt1 = g_date_time_new_from_date (2009, 10, 19);
220   dt2 = g_date_time_new_from_date (2009, 10, 19);
221   g_assert (g_date_time_equal (dt1, dt2));
222   g_date_time_unref (dt1);
223   g_date_time_unref (dt2);
224
225   dt1 = g_date_time_new_from_date (2009, 10, 18);
226   dt2 = g_date_time_new_from_date (2009, 10, 19);
227   g_assert (!g_date_time_equal (dt1, dt2));
228   g_date_time_unref (dt1);
229   g_date_time_unref (dt2);
230
231   /* UTC-0300 and not in DST */
232   time_zone = g_time_zone_new (-3 * 3600, FALSE);
233   dt1 = g_date_time_new_full (2010, 5, 24,  8, 0, 0, time_zone);
234   g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, (-3 * 3600));
235   /* UTC */
236   dt2 = g_date_time_new_full (2010, 5, 24, 11, 0, 0, NULL);
237   g_assert_cmpint (g_date_time_get_utc_offset (dt2), ==, 0);
238
239   g_assert (g_date_time_equal (dt1, dt2));
240   g_date_time_unref (dt1);
241   g_time_zone_free (time_zone);
242
243   /* America/Recife is in UTC-0300 */
244   time_zone = g_time_zone_new_for_name ("America/Recife");
245   dt1 = g_date_time_new_full (2010, 5, 24,  8, 0, 0, time_zone);
246   g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, (-3 * 3600));
247   g_assert (g_date_time_equal (dt1, dt2));
248   g_date_time_unref (dt1);
249   g_date_time_unref (dt2);
250   g_time_zone_free (time_zone);
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   dt = g_date_time_new_full (2010, 9, 15, 12, 0, 0.1234, NULL);
395   g_assert_cmpint (123, ==, g_date_time_get_millisecond (dt));
396   g_assert_cmpint (123400, ==, g_date_time_get_microsecond (dt));
397   g_date_time_unref (dt);
398 }
399
400 static void
401 test_GDateTime_get_year (void)
402 {
403   GDateTime *dt;
404
405   dt = g_date_time_new_from_date (2009, 1, 1);
406   g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
407   g_date_time_unref (dt);
408
409   dt = g_date_time_new_from_date (1, 1, 1);
410   g_assert_cmpint (1, ==, g_date_time_get_year (dt));
411   g_date_time_unref (dt);
412
413   dt = g_date_time_new_from_date (13, 1, 1);
414   g_assert_cmpint (13, ==, g_date_time_get_year (dt));
415   g_date_time_unref (dt);
416
417   dt = g_date_time_new_from_date (3000, 1, 1);
418   g_assert_cmpint (3000, ==, g_date_time_get_year (dt));
419   g_date_time_unref (dt);
420 }
421
422 static void
423 test_GDateTime_hash (void)
424 {
425   GHashTable *h;
426
427   h = g_hash_table_new_full (g_date_time_hash, g_date_time_equal,
428                              (GDestroyNotify)g_date_time_unref,
429                              NULL);
430   g_hash_table_insert (h, g_date_time_new_now (), NULL);
431   g_hash_table_remove_all (h);
432   g_hash_table_destroy (h);
433 }
434
435 static void
436 test_GDateTime_new_from_timeval (void)
437 {
438   GDateTime *dt;
439   GTimeVal   tv, tv2;
440
441   g_get_current_time (&tv);
442   dt = g_date_time_new_from_timeval (&tv);
443
444   if (g_test_verbose ())
445     g_print ("\nDT%04d-%02d-%02dT%02d:%02d:%02d%s\n",
446              g_date_time_get_year (dt),
447              g_date_time_get_month (dt),
448              g_date_time_get_day_of_month (dt),
449              g_date_time_get_hour (dt),
450              g_date_time_get_minute (dt),
451              g_date_time_get_second (dt),
452              g_date_time_get_timezone_name (dt));
453
454   g_date_time_to_timeval (dt, &tv2);
455   g_assert_cmpint (tv.tv_sec, ==, tv2.tv_sec);
456   g_assert_cmpint (tv.tv_usec, ==, tv2.tv_usec);
457   g_date_time_unref (dt);
458 }
459
460 static void
461 test_GDateTime_to_epoch (void)
462 {
463   GDateTime *dt;
464   time_t     t;
465
466   t = time (NULL);
467   dt = g_date_time_new_from_epoch (t);
468   g_assert_cmpint (g_date_time_to_epoch (dt), ==, t);
469   g_date_time_unref (dt);
470 }
471
472 static void
473 test_GDateTime_add_years (void)
474 {
475   GDateTime *dt, *dt2;
476
477   dt = g_date_time_new_from_date (2009, 10, 19);
478   dt2 = g_date_time_add_years (dt, 1);
479   g_assert_cmpint (2010, ==, g_date_time_get_year (dt2));
480   g_date_time_unref (dt);
481   g_date_time_unref (dt2);
482 }
483
484 static void
485 test_GDateTime_add_months (void)
486 {
487 #define TEST_ADD_MONTHS(y,m,d,a,ny,nm,nd) G_STMT_START { \
488   GDateTime *dt, *dt2; \
489   dt = g_date_time_new_from_date (y, m, d); \
490   dt2 = g_date_time_add_months (dt, a); \
491   ASSERT_DATE (dt2, ny, nm, nd); \
492   g_date_time_unref (dt); \
493   g_date_time_unref (dt2); \
494 } G_STMT_END
495
496   TEST_ADD_MONTHS (2009, 12, 31, 1, 2010, 1, 31);
497   TEST_ADD_MONTHS (2009, 12, 31, 1, 2010, 1, 31);
498   TEST_ADD_MONTHS (2009, 6, 15, 1, 2009, 7, 15);
499   TEST_ADD_MONTHS (1400, 3, 1, 1, 1400, 4, 1);
500   TEST_ADD_MONTHS (1400, 1, 31, 1, 1400, 2, 28);
501   TEST_ADD_MONTHS (1400, 1, 31, 7200, 2000, 1, 31);
502   TEST_ADD_MONTHS (2008, 2, 29, 12, 2009, 2, 28);
503   TEST_ADD_MONTHS (2000, 8, 16, -5, 2000, 3, 16);
504   TEST_ADD_MONTHS (2000, 8, 16, -12, 1999, 8, 16);
505   TEST_ADD_MONTHS (2011, 2, 1, -13, 2010, 1, 1);
506   TEST_ADD_MONTHS (1776, 7, 4, 1200, 1876, 7, 4);
507 }
508
509 static void
510 test_GDateTime_add_days (void)
511 {
512 #define TEST_ADD_DAYS(y,m,d,a,ny,nm,nd) G_STMT_START { \
513   GDateTime *dt, *dt2; \
514   dt = g_date_time_new_from_date (y, m, d); \
515   dt2 = g_date_time_add_days (dt, a); \
516   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
517   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
518   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
519   g_date_time_unref (dt); \
520   g_date_time_unref (dt2); \
521 } G_STMT_END
522
523   TEST_ADD_DAYS (2009, 1, 31, 1, 2009, 2, 1);
524   TEST_ADD_DAYS (2009, 2, 1, -1, 2009, 1, 31);
525   TEST_ADD_DAYS (2008, 2, 28, 1, 2008, 2, 29);
526   TEST_ADD_DAYS (2008, 12, 31, 1, 2009, 1, 1);
527   TEST_ADD_DAYS (1, 1, 1, 1, 1, 1, 2);
528   TEST_ADD_DAYS (1955, 5, 24, 10, 1955, 6, 3);
529   TEST_ADD_DAYS (1955, 5, 24, -10, 1955, 5, 14);
530 }
531
532 static void
533 test_GDateTime_add_weeks (void)
534 {
535 #define TEST_ADD_WEEKS(y,m,d,a,ny,nm,nd) G_STMT_START { \
536   GDateTime *dt, *dt2; \
537   dt = g_date_time_new_from_date (y, m, d); \
538   dt2 = g_date_time_add_weeks (dt, a); \
539   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
540   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
541   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
542   g_date_time_unref (dt); \
543   g_date_time_unref (dt2); \
544 } G_STMT_END
545
546   TEST_ADD_WEEKS (2009, 1, 1, 1, 2009, 1, 8);
547   TEST_ADD_WEEKS (2009, 8, 30, 1, 2009, 9, 6);
548   TEST_ADD_WEEKS (2009, 12, 31, 1, 2010, 1, 7);
549   TEST_ADD_WEEKS (2009, 1, 1, -1, 2008, 12, 25);
550 }
551
552 static void
553 test_GDateTime_add_hours (void)
554 {
555 #define TEST_ADD_HOURS(y,m,d,h,mi,s,a,ny,nm,nd,nh,nmi,ns) G_STMT_START { \
556   GDateTime *dt, *dt2; \
557   dt = g_date_time_new_full (y, m, d, h, mi, s, NULL); \
558   dt2 = g_date_time_add_hours (dt, a); \
559   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
560   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
561   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
562   g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
563   g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
564   g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
565   g_date_time_unref (dt); \
566   g_date_time_unref (dt2); \
567 } G_STMT_END
568
569   TEST_ADD_HOURS (2009,  1,  1,  0, 0, 0, 1, 2009, 1, 1, 1, 0, 0);
570   TEST_ADD_HOURS (2008, 12, 31, 23, 0, 0, 1, 2009, 1, 1, 0, 0, 0);
571 }
572
573 static void
574 test_GDateTime_add_full (void)
575 {
576 #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 { \
577   GDateTime *dt, *dt2; \
578   dt = g_date_time_new_full (y, m, d, h, mi, s, NULL); \
579   dt2 = g_date_time_add_full (dt, ay, am, ad, ah, ami, as); \
580   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
581   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
582   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
583   g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
584   g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
585   g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
586   g_date_time_unref (dt); \
587   g_date_time_unref (dt2); \
588 } G_STMT_END
589
590   TEST_ADD_FULL (2009, 10, 21,  0,  0, 0,
591                     1,  1,  1,  1,  1, 1,
592                  2010, 11, 22,  1,  1, 1);
593   TEST_ADD_FULL (2000,  1,  1,  1,  1, 1,
594                     0,  1,  0,  0,  0, 0,
595                  2000,  2,  1,  1,  1, 1);
596   TEST_ADD_FULL (2000,  1,  1,  0,  0, 0,
597                    -1,  1,  0,  0,  0, 0,
598                  1999,  2,  1,  0,  0, 0);
599   TEST_ADD_FULL (2010, 10, 31,  0,  0, 0,
600                     0,  4,  0,  0,  0, 0,
601                  2011,  2, 28,  0,  0, 0);
602   TEST_ADD_FULL (2010,  8, 25, 22, 45, 0,
603                     0,  1,  6,  1, 25, 0,
604                  2010, 10,  2,  0, 10, 0);
605 }
606
607 static void
608 test_GDateTime_add_milliseconds (void)
609 {
610 #define TEST_ADD_MILLISECOND(i,o) G_STMT_START { \
611   GDateTime *dt, *dt2; \
612   dt = g_date_time_new_from_date (2000, 1, 1); \
613   dt2 = g_date_time_add_milliseconds (dt, i); \
614   g_assert_cmpint (o, ==, g_date_time_get_millisecond (dt2)); \
615   g_date_time_unref (dt); \
616   g_date_time_unref (dt2); \
617 } G_STMT_END
618
619   TEST_ADD_MILLISECOND (199, 199);
620   TEST_ADD_MILLISECOND (10001, 1);
621   TEST_ADD_MILLISECOND (22201, 201);
622   TEST_ADD_MILLISECOND (-1000, 0);
623 }
624
625 static void
626 test_GDateTime_add_minutes (void)
627 {
628 #define TEST_ADD_MINUTES(i,o) G_STMT_START { \
629   GDateTime *dt, *dt2; \
630   dt = g_date_time_new_from_date (2000, 1, 1); \
631   dt2 = g_date_time_add_minutes (dt, i); \
632   g_assert_cmpint (o, ==, g_date_time_get_minute (dt2)); \
633   g_date_time_unref (dt); \
634   g_date_time_unref (dt2); \
635 } G_STMT_END
636
637   TEST_ADD_MINUTES (60, 0);
638   TEST_ADD_MINUTES (100, 40);
639   TEST_ADD_MINUTES (5, 5);
640   TEST_ADD_MINUTES (86401, 1);
641   TEST_ADD_MINUTES (-86401, 59);
642 }
643
644 static void
645 test_GDateTime_add_seconds (void)
646 {
647 #define TEST_ADD_SECONDS(i,o) G_STMT_START { \
648   GDateTime *dt, *dt2; \
649   dt = g_date_time_new_from_date (2000, 1, 1); \
650   dt2 = g_date_time_add_seconds (dt, i); \
651   g_assert_cmpint (o, ==, g_date_time_get_second (dt2)); \
652   g_date_time_unref (dt); \
653   g_date_time_unref (dt2); \
654 } G_STMT_END
655
656   TEST_ADD_SECONDS (1, 1);
657   TEST_ADD_SECONDS (60, 0);
658   TEST_ADD_SECONDS (61, 1);
659   TEST_ADD_SECONDS (120, 0);
660   TEST_ADD_SECONDS (-61, 59);
661   TEST_ADD_SECONDS (86401, 1);
662   TEST_ADD_SECONDS (-86401, 59);
663   TEST_ADD_SECONDS (-31, 29);
664   TEST_ADD_SECONDS (13, 13);
665 }
666
667 static void
668 test_GDateTime_diff (void)
669 {
670 #define TEST_DIFF(y,m,d,y2,m2,d2,u) G_STMT_START { \
671   GDateTime *dt1, *dt2; \
672   GTimeSpan  ts = 0; \
673   dt1 = g_date_time_new_from_date (y, m, d); \
674   dt2 = g_date_time_new_from_date (y2, m2, d2); \
675   ts = g_date_time_difference (dt1, dt2); \
676   g_assert_cmpint (ts, ==, u); \
677   g_date_time_unref (dt1); \
678   g_date_time_unref (dt2); \
679 } G_STMT_END
680
681   TEST_DIFF (2009, 1, 1, 2009, 2, 1, G_TIME_SPAN_DAY * 31);
682   TEST_DIFF (2009, 1, 1, 2010, 1, 1, G_TIME_SPAN_DAY * 365);
683   TEST_DIFF (2008, 2, 28, 2008, 2, 29, G_TIME_SPAN_DAY);
684   TEST_DIFF (2008, 2, 29, 2008, 2, 28, -G_TIME_SPAN_DAY);
685
686   /* TODO: Add usec tests */
687 }
688
689 static void
690 test_GDateTime_get_minute (void)
691 {
692   GDateTime *dt;
693
694   dt = g_date_time_new_full (2009, 12, 1, 1, 31, 0, NULL);
695   g_assert_cmpint (31, ==, g_date_time_get_minute (dt));
696   g_date_time_unref (dt);
697 }
698
699 static void
700 test_GDateTime_get_month (void)
701 {
702   GDateTime *dt;
703
704   dt = g_date_time_new_full (2009, 12, 1, 1, 31, 0, NULL);
705   g_assert_cmpint (12, ==, g_date_time_get_month (dt));
706   g_date_time_unref (dt);
707 }
708
709 static void
710 test_GDateTime_get_second (void)
711 {
712   GDateTime *dt;
713
714   dt = g_date_time_new_full (2009, 12, 1, 1, 31, 44, NULL);
715   g_assert_cmpint (44, ==, g_date_time_get_second (dt));
716   g_date_time_unref (dt);
717 }
718
719 static void
720 test_GDateTime_new_from_date (void)
721 {
722   GDateTime *dt;
723
724   dt = g_date_time_new_from_date (2009, 12, 11);
725   g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
726   g_assert_cmpint (12, ==, g_date_time_get_month (dt));
727   g_assert_cmpint (11, ==, g_date_time_get_day_of_month (dt));
728   g_date_time_unref (dt);
729 }
730
731 static void
732 test_GDateTime_new_full (void)
733 {
734   GDateTime *dt;
735   GTimeZone *time_zone;
736
737   dt = g_date_time_new_full (2009, 12, 11, 12, 11, 10, NULL);
738   g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
739   g_assert_cmpint (12, ==, g_date_time_get_month (dt));
740   g_assert_cmpint (11, ==, g_date_time_get_day_of_month (dt));
741   g_assert_cmpint (12, ==, g_date_time_get_hour (dt));
742   g_assert_cmpint (11, ==, g_date_time_get_minute (dt));
743   g_assert_cmpint (10, ==, g_date_time_get_second (dt));
744   g_date_time_unref (dt);
745
746   time_zone = g_time_zone_new_for_name ("America/Recife");
747   dt = g_date_time_new_full (2010, 5, 24, 8, 4, 0, time_zone);
748   g_assert_cmpint (2010, ==, g_date_time_get_year (dt));
749   g_assert_cmpint (5, ==, g_date_time_get_month (dt));
750   g_assert_cmpint (24, ==, g_date_time_get_day_of_month (dt));
751   g_assert_cmpint (8, ==, g_date_time_get_hour (dt));
752   g_assert_cmpint (4, ==, g_date_time_get_minute (dt));
753   g_assert_cmpint (0, ==, g_date_time_get_second (dt));
754   g_assert_cmpstr ("BRT", ==, g_date_time_get_timezone_name (dt));
755   g_assert (!g_date_time_is_daylight_savings (dt));
756   g_date_time_unref (dt);
757   g_time_zone_free (time_zone);
758 }
759
760 static void
761 test_GDateTime_utc_now (void)
762 {
763   GDateTime *dt;
764   time_t     t;
765   struct tm  tm;
766
767   t = time (NULL);
768 #ifdef HAVE_GMTIME_R
769   gmtime_r (&t, &tm);
770 #else
771   {
772     struct tm *tmp = gmtime (&t);
773     /* Assume gmtime() can't fail as we got t from time(NULL). (Note
774      * that on Windows, gmtime() *is* MT-safe, it uses a thread-local
775      * buffer.)
776      */
777     memcpy (&tm, tmp, sizeof (struct tm));
778   }
779 #endif
780   dt = g_date_time_new_utc_now ();
781   g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
782   g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
783   g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
784   g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
785   g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
786   g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
787   g_date_time_unref (dt);
788 }
789
790 static void
791 test_GDateTime_get_utc_offset (void)
792 {
793   GDateTime *dt;
794   GTimeSpan ts;
795   struct tm tm;
796
797   memset (&tm, 0, sizeof (tm));
798   get_localtime_tm (time (NULL), &tm);
799
800   dt = g_date_time_new_now ();
801   ts = g_date_time_get_utc_offset (dt);
802 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
803   g_assert_cmpint (ts, ==, (tm.tm_gmtoff * G_TIME_SPAN_SECOND));
804 #endif
805   g_date_time_unref (dt);
806 }
807
808 static void
809 test_GDateTime_to_timeval (void)
810 {
811   GTimeVal tv1, tv2;
812   GDateTime *dt;
813
814   memset (&tv1, 0, sizeof (tv1));
815   memset (&tv2, 0, sizeof (tv2));
816
817   g_get_current_time (&tv1);
818   dt = g_date_time_new_from_timeval (&tv1);
819   g_date_time_to_timeval (dt, &tv2);
820   g_assert_cmpint (tv1.tv_sec, ==, tv2.tv_sec);
821   g_assert_cmpint (tv1.tv_usec, ==, tv2.tv_usec);
822   g_date_time_unref (dt);
823 }
824
825 static void
826 test_GDateTime_to_local (void)
827 {
828   GDateTime *utc, *now, *dt;
829
830   utc = g_date_time_new_utc_now ();
831   now = g_date_time_new_now ();
832   dt = g_date_time_to_local (utc);
833
834   g_assert_cmpint (g_date_time_get_year (now), ==, g_date_time_get_year (dt));
835   g_assert_cmpint (g_date_time_get_month (now), ==, g_date_time_get_month (dt));
836   g_assert_cmpint (g_date_time_get_day_of_month (now), ==, g_date_time_get_day_of_month (dt));
837   g_assert_cmpint (g_date_time_get_hour (now), ==, g_date_time_get_hour (dt));
838   g_assert_cmpint (g_date_time_get_minute (now), ==, g_date_time_get_minute (dt));
839   g_assert_cmpint (g_date_time_get_second (now), ==, g_date_time_get_second (dt));
840
841   g_date_time_unref (now);
842   g_date_time_unref (utc);
843   g_date_time_unref (dt);
844 }
845
846 static void
847 test_GDateTime_to_utc (void)
848 {
849   GDateTime *dt, *dt2;
850   time_t     t;
851   struct tm  tm;
852
853   t = time (NULL);
854 #ifdef HAVE_GMTIME_R
855   gmtime_r (&t, &tm);
856 #else
857   {
858     struct tm *tmp = gmtime (&t);
859     memcpy (&tm, tmp, sizeof (struct tm));
860   }
861 #endif
862   dt2 = g_date_time_new_now ();
863   dt = g_date_time_to_utc (dt2);
864   g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
865   g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
866   g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
867   g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
868   g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
869   g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
870   g_date_time_unref (dt);
871   g_date_time_unref (dt2);
872 }
873
874 static void
875 test_GDateTime_get_day_of_year (void)
876 {
877 #define TEST_DAY_OF_YEAR(y,m,d,o)                       G_STMT_START {  \
878   GDateTime *__dt = g_date_time_new_from_date ((y), (m), (d));          \
879   g_assert_cmpint ((o), ==, g_date_time_get_day_of_year (__dt));        \
880   g_date_time_unref (__dt);                             } G_STMT_END
881
882   TEST_DAY_OF_YEAR (2009, 1, 1, 1);
883   TEST_DAY_OF_YEAR (2009, 2, 1, 32);
884   TEST_DAY_OF_YEAR (2009, 8, 16, 228);
885   TEST_DAY_OF_YEAR (2008, 8, 16, 229);
886 }
887
888 static void
889 test_GDateTime_printf (void)
890 {
891   gchar dst[16];
892   struct tm tt;
893   time_t t;
894   gchar t_str[16];
895
896 #define TEST_PRINTF(f,o)                        G_STMT_START {  \
897 GDateTime *__dt = g_date_time_new_from_date (2009, 10, 24);     \
898   gchar *__p = g_date_time_printf (__dt, (f));                  \
899   g_assert_cmpstr (__p, ==, (o));                               \
900   g_date_time_unref (__dt);                                     \
901   g_free (__p);                                 } G_STMT_END
902
903 #define TEST_PRINTF_DATE(y,m,d,f,o)             G_STMT_START {  \
904   GDateTime *dt = g_date_time_new_from_date ((y), (m), (d));    \
905   gchar *p = g_date_time_printf (dt, (f));                      \
906   g_assert_cmpstr (p, ==, (o));                                 \
907   g_date_time_unref (dt);                                       \
908   g_free (p);                                   } G_STMT_END
909
910 #define TEST_PRINTF_TIME(h,m,s,f,o)             G_STMT_START { \
911   GDateTime *dt = g_date_time_new_full (2009, 10, 24, (h), (m), (s), NULL); \
912   gchar *p = g_date_time_printf (dt, (f));                      \
913   g_assert_cmpstr (p, ==, (o));                                 \
914   g_date_time_unref (dt);                                       \
915   g_free (p);                                   } G_STMT_END
916
917   /*
918    * This is a little helper to make sure we can compare timezones to
919    * that of the generated timezone.
920    */
921   t = time (NULL);
922   memset (&tt, 0, sizeof(tt));
923   get_localtime_tm (t, &tt);
924   tt.tm_year = 2009 - 1900;
925   tt.tm_mon = 9; /* 0 indexed */
926   tt.tm_mday = 24;
927   t = mktime (&tt);
928   memset (&tt, 0, sizeof(tt));
929   get_localtime_tm (t, &tt);
930   strftime (dst, sizeof(dst), "%Z", &tt);
931
932   /* get current time_t for 20090924 in the local timezone */
933   tt.tm_sec = 0;
934   tt.tm_min = 0;
935   tt.tm_hour = 0;
936   t = mktime (&tt);
937   g_sprintf (t_str, "%ld", t);
938
939   TEST_PRINTF ("%a", "Sat");
940   TEST_PRINTF ("%A", "Saturday");
941   TEST_PRINTF ("%b", "Oct");
942   TEST_PRINTF ("%B", "October");
943   TEST_PRINTF ("%d", "24");
944   TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
945   TEST_PRINTF ("%e", "24"); // fixme
946   TEST_PRINTF ("%h", "Oct");
947   TEST_PRINTF ("%H", "00");
948   TEST_PRINTF_TIME (15, 0, 0, "%H", "15");
949   TEST_PRINTF ("%I", "12");
950   TEST_PRINTF_TIME (15, 0, 0, "%I", "03");
951   TEST_PRINTF ("%j", "297");
952   TEST_PRINTF ("%k", " 0");
953   TEST_PRINTF_TIME (13, 13, 13, "%k", "13");
954   TEST_PRINTF ("%l", "12");
955   TEST_PRINTF_TIME (13, 13, 13, "%l", " 1");
956   TEST_PRINTF_TIME (10, 13, 13, "%l", "10");
957   TEST_PRINTF ("%m", "10");
958   TEST_PRINTF ("%M", "00");
959   TEST_PRINTF ("%N", "0");
960   TEST_PRINTF ("%p", "AM");
961   TEST_PRINTF_TIME (13, 13, 13, "%p", "PM");
962   TEST_PRINTF ("%P", "am");
963   TEST_PRINTF_TIME (13, 13, 13, "%P", "pm");
964   TEST_PRINTF ("%r", "12:00:00 AM");
965   TEST_PRINTF_TIME (13, 13, 13, "%r", "01:13:13 PM");
966   TEST_PRINTF ("%R", "00:00");
967   TEST_PRINTF_TIME (13, 13, 31, "%R", "13:13");
968   TEST_PRINTF ("%s", t_str);
969   TEST_PRINTF ("%S", "00");
970   TEST_PRINTF ("%t", "  ");
971   TEST_PRINTF ("%W", "42");
972   TEST_PRINTF ("%u", "6");
973   TEST_PRINTF ("%x", "10/24/09");
974   TEST_PRINTF ("%X", "00:00:00");
975   TEST_PRINTF_TIME (13, 14, 15, "%X", "13:14:15");
976   TEST_PRINTF ("%y", "09");
977   TEST_PRINTF ("%Y", "2009");
978   TEST_PRINTF ("%%", "%");
979   TEST_PRINTF ("%", "");
980   TEST_PRINTF ("%9", NULL);
981   TEST_PRINTF ("%Z", dst);
982 }
983
984 gint
985 main (gint   argc,
986       gchar *argv[])
987 {
988   g_test_init (&argc, &argv, NULL);
989
990   /* GDateTime Tests */
991
992   g_test_add_func ("/GDateTime/add_days", test_GDateTime_add_days);
993   g_test_add_func ("/GDateTime/add_full", test_GDateTime_add_full);
994   g_test_add_func ("/GDateTime/add_hours", test_GDateTime_add_hours);
995   g_test_add_func ("/GDateTime/add_milliseconds", test_GDateTime_add_milliseconds);
996   g_test_add_func ("/GDateTime/add_minutes", test_GDateTime_add_minutes);
997   g_test_add_func ("/GDateTime/add_months", test_GDateTime_add_months);
998   g_test_add_func ("/GDateTime/add_seconds", test_GDateTime_add_seconds);
999   g_test_add_func ("/GDateTime/add_weeks", test_GDateTime_add_weeks);
1000   g_test_add_func ("/GDateTime/add_years", test_GDateTime_add_years);
1001   g_test_add_func ("/GDateTime/compare", test_GDateTime_compare);
1002   g_test_add_func ("/GDateTime/date", test_GDateTime_date);
1003   g_test_add_func ("/GDateTime/diff", test_GDateTime_diff);
1004   g_test_add_func ("/GDateTime/equal", test_GDateTime_equal);
1005   g_test_add_func ("/GDateTime/get_day_of_week", test_GDateTime_get_day_of_week);
1006   g_test_add_func ("/GDateTime/get_day_of_month", test_GDateTime_get_day_of_month);
1007   g_test_add_func ("/GDateTime/get_day_of_year", test_GDateTime_get_day_of_year);
1008   g_test_add_func ("/GDateTime/get_dmy", test_GDateTime_get_dmy);
1009   g_test_add_func ("/GDateTime/get_hour", test_GDateTime_get_hour);
1010   g_test_add_func ("/GDateTime/get_julian", test_GDateTime_get_julian);
1011   g_test_add_func ("/GDateTime/get_microsecond", test_GDateTime_get_microsecond);
1012   g_test_add_func ("/GDateTime/get_millisecond", test_GDateTime_get_millisecond);
1013   g_test_add_func ("/GDateTime/get_minute", test_GDateTime_get_minute);
1014   g_test_add_func ("/GDateTime/get_month", test_GDateTime_get_month);
1015   g_test_add_func ("/GDateTime/get_second", test_GDateTime_get_second);
1016   g_test_add_func ("/GDateTime/get_utc_offset", test_GDateTime_get_utc_offset);
1017   g_test_add_func ("/GDateTime/get_year", test_GDateTime_get_year);
1018   g_test_add_func ("/GDateTime/hash", test_GDateTime_hash);
1019   g_test_add_func ("/GDateTime/is_leap_year", test_GDateTime_is_leap_year);
1020   g_test_add_func ("/GDateTime/new_from_date", test_GDateTime_new_from_date);
1021   g_test_add_func ("/GDateTime/new_from_epoch", test_GDateTime_new_from_epoch);
1022   g_test_add_func ("/GDateTime/new_from_timeval", test_GDateTime_new_from_timeval);
1023   g_test_add_func ("/GDateTime/new_full", test_GDateTime_new_full);
1024   g_test_add_func ("/GDateTime/now", test_GDateTime_now);
1025   g_test_add_func ("/GDateTime/printf", test_GDateTime_printf);
1026   g_test_add_func ("/GDateTime/to_local", test_GDateTime_to_local);
1027   g_test_add_func ("/GDateTime/to_epoch", test_GDateTime_to_epoch);
1028   g_test_add_func ("/GDateTime/to_timeval", test_GDateTime_to_timeval);
1029   g_test_add_func ("/GDateTime/to_utc", test_GDateTime_to_utc);
1030   g_test_add_func ("/GDateTime/today", test_GDateTime_today);
1031   g_test_add_func ("/GDateTime/utc_now", test_GDateTime_utc_now);
1032
1033   return g_test_run ();
1034 }