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