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