Improve GTimeZone test coverage
[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 #include <locale.h>
27
28 #define ASSERT_DATE(dt,y,m,d) G_STMT_START { \
29   g_assert_cmpint ((y), ==, g_date_time_get_year ((dt))); \
30   g_assert_cmpint ((m), ==, g_date_time_get_month ((dt))); \
31   g_assert_cmpint ((d), ==, g_date_time_get_day_of_month ((dt))); \
32 } G_STMT_END
33 #define ASSERT_TIME(dt,H,M,S) G_STMT_START { \
34   g_assert_cmpint ((H), ==, g_date_time_get_hour ((dt))); \
35   g_assert_cmpint ((M), ==, g_date_time_get_minute ((dt))); \
36   g_assert_cmpint ((S), ==, g_date_time_get_second ((dt))); \
37 } G_STMT_END
38
39 static void
40 get_localtime_tm (time_t     time_,
41                   struct tm *retval)
42 {
43 #ifdef HAVE_LOCALTIME_R
44   localtime_r (&time_, retval);
45 #else
46   {
47     struct tm *ptm = localtime (&time_);
48
49     if (ptm == NULL)
50       {
51         /* Happens at least in Microsoft's C library if you pass a
52          * negative time_t. Use 2000-01-01 as default date.
53          */
54 #ifndef G_DISABLE_CHECKS
55         g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, "ptm != NULL");
56 #endif
57
58         retval->tm_mon = 0;
59         retval->tm_mday = 1;
60         retval->tm_year = 100;
61       }
62     else
63       memcpy ((void *) retval, (void *) ptm, sizeof (struct tm));
64   }
65 #endif /* HAVE_LOCALTIME_R */
66 }
67
68 static void
69 test_GDateTime_now (void)
70 {
71   GDateTime *dt;
72   struct tm tm;
73
74   memset (&tm, 0, sizeof (tm));
75   get_localtime_tm (time (NULL), &tm);
76
77   dt = g_date_time_new_now_local ();
78
79   g_assert_cmpint (g_date_time_get_year (dt), ==, 1900 + tm.tm_year);
80   g_assert_cmpint (g_date_time_get_month (dt), ==, 1 + tm.tm_mon);
81   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, tm.tm_mday);
82   g_assert_cmpint (g_date_time_get_hour (dt), ==, tm.tm_hour);
83   g_assert_cmpint (g_date_time_get_minute (dt), ==, tm.tm_min);
84   /* XXX we need some fuzzyness here */
85   g_assert_cmpint (g_date_time_get_second (dt), >=, tm.tm_sec);
86
87   g_date_time_unref (dt);
88 }
89
90 static void
91 test_GDateTime_new_from_unix (void)
92 {
93   GDateTime *dt;
94   struct tm  tm;
95   time_t     t;
96
97   memset (&tm, 0, sizeof (tm));
98   t = time (NULL);
99   get_localtime_tm (t, &tm);
100
101   dt = g_date_time_new_from_unix_local (t);
102   g_assert_cmpint (g_date_time_get_year (dt), ==, 1900 + tm.tm_year);
103   g_assert_cmpint (g_date_time_get_month (dt), ==, 1 + tm.tm_mon);
104   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, tm.tm_mday);
105   g_assert_cmpint (g_date_time_get_hour (dt), ==, tm.tm_hour);
106   g_assert_cmpint (g_date_time_get_minute (dt), ==, tm.tm_min);
107   g_assert_cmpint (g_date_time_get_second (dt), ==, tm.tm_sec);
108   g_date_time_unref (dt);
109
110   memset (&tm, 0, sizeof (tm));
111   tm.tm_year = 90;
112   tm.tm_mday = 1;
113   tm.tm_mon = 0;
114   tm.tm_hour = 0;
115   tm.tm_min = 0;
116   tm.tm_sec = 0;
117   tm.tm_isdst = -1;
118   t = mktime (&tm);
119
120   dt = g_date_time_new_from_unix_local (t);
121   g_assert_cmpint (g_date_time_get_year (dt), ==, 1990);
122   g_assert_cmpint (g_date_time_get_month (dt), ==, 1);
123   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 1);
124   g_assert_cmpint (g_date_time_get_hour (dt), ==, 0);
125   g_assert_cmpint (g_date_time_get_minute (dt), ==, 0);
126   g_assert_cmpint (g_date_time_get_second (dt), ==, 0);
127   g_date_time_unref (dt);
128 }
129
130 static void
131 test_GDateTime_compare (void)
132 {
133   GDateTime *dt1, *dt2;
134   gint       i;
135
136   dt1 = g_date_time_new_utc (2000, 1, 1, 0, 0, 0);
137
138   for (i = 1; i < 2000; i++)
139     {
140       dt2 = g_date_time_new_utc (i, 12, 31, 0, 0, 0);
141       g_assert_cmpint (1, ==, g_date_time_compare (dt1, dt2));
142       g_date_time_unref (dt2);
143     }
144
145   dt2 = g_date_time_new_utc (1999, 12, 31, 23, 59, 59);
146   g_assert_cmpint (1, ==, g_date_time_compare (dt1, dt2));
147   g_date_time_unref (dt2);
148
149   dt2 = g_date_time_new_utc (2000, 1, 1, 0, 0, 1);
150   g_assert_cmpint (-1, ==, g_date_time_compare (dt1, dt2));
151   g_date_time_unref (dt2);
152
153   dt2 = g_date_time_new_utc (2000, 1, 1, 0, 0, 0);
154   g_assert_cmpint (0, ==, g_date_time_compare (dt1, dt2));
155   g_date_time_unref (dt2);
156   g_date_time_unref (dt1);
157 }
158
159 static void
160 test_GDateTime_equal (void)
161 {
162   GDateTime *dt1, *dt2;
163   GTimeZone *tz;
164
165   dt1 = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
166   dt2 = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
167   g_assert (g_date_time_equal (dt1, dt2));
168   g_date_time_unref (dt1);
169   g_date_time_unref (dt2);
170
171   dt1 = g_date_time_new_local (2009, 10, 18, 0, 0, 0);
172   dt2 = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
173   g_assert (!g_date_time_equal (dt1, dt2));
174   g_date_time_unref (dt1);
175   g_date_time_unref (dt2);
176
177   /* UTC-0300 and not in DST */
178   tz = g_time_zone_new ("-03:00");
179   dt1 = g_date_time_new (tz, 2010, 5, 24,  8, 0, 0);
180   g_time_zone_unref (tz);
181   g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, (-3 * 3600));
182   /* UTC */
183   dt2 = g_date_time_new_utc (2010, 5, 24, 11, 0, 0);
184   g_assert_cmpint (g_date_time_get_utc_offset (dt2), ==, 0);
185
186   g_assert (g_date_time_equal (dt1, dt2));
187   g_date_time_unref (dt1);
188
189   /* America/Recife is in UTC-0300 */
190   tz = g_time_zone_new ("America/Recife");
191   dt1 = g_date_time_new (tz, 2010, 5, 24,  8, 0, 0);
192   g_time_zone_unref (tz);
193   g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, (-3 * 3600));
194   g_assert (g_date_time_equal (dt1, dt2));
195   g_date_time_unref (dt1);
196   g_date_time_unref (dt2);
197 }
198
199 static void
200 test_GDateTime_get_day_of_week (void)
201 {
202   GDateTime *dt;
203
204   dt = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
205   g_assert_cmpint (1, ==, g_date_time_get_day_of_week (dt));
206   g_date_time_unref (dt);
207
208   dt = g_date_time_new_local (2000, 10, 1, 0, 0, 0);
209   g_assert_cmpint (7, ==, g_date_time_get_day_of_week (dt));
210   g_date_time_unref (dt);
211 }
212
213 static void
214 test_GDateTime_get_day_of_month (void)
215 {
216   GDateTime *dt;
217
218   dt = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
219   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 19);
220   g_date_time_unref (dt);
221
222   dt = g_date_time_new_local (1400, 3, 12, 0, 0, 0);
223   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 12);
224   g_date_time_unref (dt);
225
226   dt = g_date_time_new_local (1800, 12, 31, 0, 0, 0);
227   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 31);
228   g_date_time_unref (dt);
229
230   dt = g_date_time_new_local (1000, 1, 1, 0, 0, 0);
231   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 1);
232   g_date_time_unref (dt);
233 }
234
235 static void
236 test_GDateTime_get_hour (void)
237 {
238   GDateTime *dt;
239
240   dt = g_date_time_new_utc (2009, 10, 19, 15, 13, 11);
241   g_assert_cmpint (15, ==, g_date_time_get_hour (dt));
242   g_date_time_unref (dt);
243
244   dt = g_date_time_new_utc (100, 10, 19, 1, 0, 0);
245   g_assert_cmpint (1, ==, g_date_time_get_hour (dt));
246   g_date_time_unref (dt);
247
248   dt = g_date_time_new_utc (100, 10, 19, 0, 0, 0);
249   g_assert_cmpint (0, ==, g_date_time_get_hour (dt));
250   g_date_time_unref (dt);
251
252   dt = g_date_time_new_utc (100, 10, 1, 23, 59, 59);
253   g_assert_cmpint (23, ==, g_date_time_get_hour (dt));
254   g_date_time_unref (dt);
255 }
256
257 static void
258 test_GDateTime_get_microsecond (void)
259 {
260   GTimeVal   tv;
261   GDateTime *dt;
262
263   g_get_current_time (&tv);
264   dt = g_date_time_new_from_timeval_local (&tv);
265   g_assert_cmpint (tv.tv_usec, ==, g_date_time_get_microsecond (dt));
266   g_date_time_unref (dt);
267 }
268
269 static void
270 test_GDateTime_get_year (void)
271 {
272   GDateTime *dt;
273
274   dt = g_date_time_new_local (2009, 1, 1, 0, 0, 0);
275   g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
276   g_date_time_unref (dt);
277
278   dt = g_date_time_new_local (1, 1, 1, 0, 0, 0);
279   g_assert_cmpint (1, ==, g_date_time_get_year (dt));
280   g_date_time_unref (dt);
281
282   dt = g_date_time_new_local (13, 1, 1, 0, 0, 0);
283   g_assert_cmpint (13, ==, g_date_time_get_year (dt));
284   g_date_time_unref (dt);
285
286   dt = g_date_time_new_local (3000, 1, 1, 0, 0, 0);
287   g_assert_cmpint (3000, ==, g_date_time_get_year (dt));
288   g_date_time_unref (dt);
289 }
290
291 static void
292 test_GDateTime_hash (void)
293 {
294   GHashTable *h;
295
296   h = g_hash_table_new_full (g_date_time_hash, g_date_time_equal,
297                              (GDestroyNotify)g_date_time_unref,
298                              NULL);
299   g_hash_table_insert (h, g_date_time_new_now_local (), NULL);
300   g_hash_table_remove_all (h);
301   g_hash_table_destroy (h);
302 }
303
304 static void
305 test_GDateTime_new_from_timeval (void)
306 {
307   GDateTime *dt;
308   GTimeVal   tv, tv2;
309
310   g_get_current_time (&tv);
311   dt = g_date_time_new_from_timeval_local (&tv);
312
313   if (g_test_verbose ())
314     g_print ("\nDT%04d-%02d-%02dT%02d:%02d:%02d%s\n",
315              g_date_time_get_year (dt),
316              g_date_time_get_month (dt),
317              g_date_time_get_day_of_month (dt),
318              g_date_time_get_hour (dt),
319              g_date_time_get_minute (dt),
320              g_date_time_get_second (dt),
321              g_date_time_get_timezone_abbreviation (dt));
322
323   g_date_time_to_timeval (dt, &tv2);
324   g_assert_cmpint (tv.tv_sec, ==, tv2.tv_sec);
325   g_assert_cmpint (tv.tv_usec, ==, tv2.tv_usec);
326   g_date_time_unref (dt);
327 }
328
329 static void
330 test_GDateTime_new_from_timeval_utc (void)
331 {
332   GDateTime *dt;
333   GTimeVal   tv, tv2;
334
335   g_get_current_time (&tv);
336   dt = g_date_time_new_from_timeval_utc (&tv);
337
338   if (g_test_verbose ())
339     g_print ("\nDT%04d-%02d-%02dT%02d:%02d:%02d%s\n",
340              g_date_time_get_year (dt),
341              g_date_time_get_month (dt),
342              g_date_time_get_day_of_month (dt),
343              g_date_time_get_hour (dt),
344              g_date_time_get_minute (dt),
345              g_date_time_get_second (dt),
346              g_date_time_get_timezone_abbreviation (dt));
347
348   g_date_time_to_timeval (dt, &tv2);
349   g_assert_cmpint (tv.tv_sec, ==, tv2.tv_sec);
350   g_assert_cmpint (tv.tv_usec, ==, tv2.tv_usec);
351   g_date_time_unref (dt);
352 }
353
354 static void
355 test_GDateTime_to_unix (void)
356 {
357   GDateTime *dt;
358   time_t     t;
359
360   t = time (NULL);
361   dt = g_date_time_new_from_unix_local (t);
362   g_assert_cmpint (g_date_time_to_unix (dt), ==, t);
363   g_date_time_unref (dt);
364 }
365
366 static void
367 test_GDateTime_add_years (void)
368 {
369   GDateTime *dt, *dt2;
370
371   dt = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
372   dt2 = g_date_time_add_years (dt, 1);
373   g_assert_cmpint (2010, ==, g_date_time_get_year (dt2));
374   g_date_time_unref (dt);
375   g_date_time_unref (dt2);
376 }
377
378 static void
379 test_GDateTime_add_months (void)
380 {
381 #define TEST_ADD_MONTHS(y,m,d,a,ny,nm,nd) G_STMT_START { \
382   GDateTime *dt, *dt2; \
383   dt = g_date_time_new_utc (y, m, d, 0, 0, 0); \
384   dt2 = g_date_time_add_months (dt, a); \
385   ASSERT_DATE (dt2, ny, nm, nd); \
386   g_date_time_unref (dt); \
387   g_date_time_unref (dt2); \
388 } G_STMT_END
389
390   TEST_ADD_MONTHS (2009, 12, 31,    1, 2010, 1, 31);
391   TEST_ADD_MONTHS (2009, 12, 31,    1, 2010, 1, 31);
392   TEST_ADD_MONTHS (2009,  6, 15,    1, 2009, 7, 15);
393   TEST_ADD_MONTHS (1400,  3,  1,    1, 1400, 4,  1);
394   TEST_ADD_MONTHS (1400,  1, 31,    1, 1400, 2, 28);
395   TEST_ADD_MONTHS (1400,  1, 31, 7200, 2000, 1, 31);
396   TEST_ADD_MONTHS (2008,  2, 29,   12, 2009, 2, 28);
397   TEST_ADD_MONTHS (2000,  8, 16,   -5, 2000, 3, 16);
398   TEST_ADD_MONTHS (2000,  8, 16,  -12, 1999, 8, 16);
399   TEST_ADD_MONTHS (2011,  2,  1,  -13, 2010, 1,  1);
400   TEST_ADD_MONTHS (1776,  7,  4, 1200, 1876, 7,  4);
401 }
402
403 static void
404 test_GDateTime_add_days (void)
405 {
406 #define TEST_ADD_DAYS(y,m,d,a,ny,nm,nd) G_STMT_START { \
407   GDateTime *dt, *dt2; \
408   dt = g_date_time_new_local (y, m, d, 0, 0, 0); \
409   dt2 = g_date_time_add_days (dt, a); \
410   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
411   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
412   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
413   g_date_time_unref (dt); \
414   g_date_time_unref (dt2); \
415 } G_STMT_END
416
417   TEST_ADD_DAYS (2009, 1, 31, 1, 2009, 2, 1);
418   TEST_ADD_DAYS (2009, 2, 1, -1, 2009, 1, 31);
419   TEST_ADD_DAYS (2008, 2, 28, 1, 2008, 2, 29);
420   TEST_ADD_DAYS (2008, 12, 31, 1, 2009, 1, 1);
421   TEST_ADD_DAYS (1, 1, 1, 1, 1, 1, 2);
422   TEST_ADD_DAYS (1955, 5, 24, 10, 1955, 6, 3);
423   TEST_ADD_DAYS (1955, 5, 24, -10, 1955, 5, 14);
424 }
425
426 static void
427 test_GDateTime_add_weeks (void)
428 {
429 #define TEST_ADD_WEEKS(y,m,d,a,ny,nm,nd) G_STMT_START { \
430   GDateTime *dt, *dt2; \
431   dt = g_date_time_new_local (y, m, d, 0, 0, 0); \
432   dt2 = g_date_time_add_weeks (dt, a); \
433   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
434   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
435   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
436   g_date_time_unref (dt); \
437   g_date_time_unref (dt2); \
438 } G_STMT_END
439
440   TEST_ADD_WEEKS (2009, 1, 1, 1, 2009, 1, 8);
441   TEST_ADD_WEEKS (2009, 8, 30, 1, 2009, 9, 6);
442   TEST_ADD_WEEKS (2009, 12, 31, 1, 2010, 1, 7);
443   TEST_ADD_WEEKS (2009, 1, 1, -1, 2008, 12, 25);
444 }
445
446 static void
447 test_GDateTime_add_hours (void)
448 {
449 #define TEST_ADD_HOURS(y,m,d,h,mi,s,a,ny,nm,nd,nh,nmi,ns) G_STMT_START { \
450   GDateTime *dt, *dt2; \
451   dt = g_date_time_new_utc (y, m, d, h, mi, s); \
452   dt2 = g_date_time_add_hours (dt, a); \
453   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
454   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
455   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
456   g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
457   g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
458   g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
459   g_date_time_unref (dt); \
460   g_date_time_unref (dt2); \
461 } G_STMT_END
462
463   TEST_ADD_HOURS (2009,  1,  1,  0, 0, 0, 1, 2009, 1, 1, 1, 0, 0);
464   TEST_ADD_HOURS (2008, 12, 31, 23, 0, 0, 1, 2009, 1, 1, 0, 0, 0);
465 }
466
467 static void
468 test_GDateTime_add_full (void)
469 {
470 #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 { \
471   GDateTime *dt, *dt2; \
472   dt = g_date_time_new_utc (y, m, d, h, mi, s); \
473   dt2 = g_date_time_add_full (dt, ay, am, ad, ah, ami, as); \
474   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
475   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
476   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
477   g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
478   g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
479   g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
480   g_date_time_unref (dt); \
481   g_date_time_unref (dt2); \
482 } G_STMT_END
483
484   TEST_ADD_FULL (2009, 10, 21,  0,  0, 0,
485                     1,  1,  1,  1,  1, 1,
486                  2010, 11, 22,  1,  1, 1);
487   TEST_ADD_FULL (2000,  1,  1,  1,  1, 1,
488                     0,  1,  0,  0,  0, 0,
489                  2000,  2,  1,  1,  1, 1);
490   TEST_ADD_FULL (2000,  1,  1,  0,  0, 0,
491                    -1,  1,  0,  0,  0, 0,
492                  1999,  2,  1,  0,  0, 0);
493   TEST_ADD_FULL (2010, 10, 31,  0,  0, 0,
494                     0,  4,  0,  0,  0, 0,
495                  2011,  2, 28,  0,  0, 0);
496   TEST_ADD_FULL (2010,  8, 25, 22, 45, 0,
497                     0,  1,  6,  1, 25, 0,
498                  2010, 10,  2,  0, 10, 0);
499 }
500
501 static void
502 test_GDateTime_add_minutes (void)
503 {
504 #define TEST_ADD_MINUTES(i,o) G_STMT_START { \
505   GDateTime *dt, *dt2; \
506   dt = g_date_time_new_local (2000, 1, 1, 0, 0, 0); \
507   dt2 = g_date_time_add_minutes (dt, i); \
508   g_assert_cmpint (o, ==, g_date_time_get_minute (dt2)); \
509   g_date_time_unref (dt); \
510   g_date_time_unref (dt2); \
511 } G_STMT_END
512
513   TEST_ADD_MINUTES (60, 0);
514   TEST_ADD_MINUTES (100, 40);
515   TEST_ADD_MINUTES (5, 5);
516   TEST_ADD_MINUTES (1441, 1);
517   TEST_ADD_MINUTES (-1441, 59);
518 }
519
520 static void
521 test_GDateTime_add_seconds (void)
522 {
523 #define TEST_ADD_SECONDS(i,o) G_STMT_START { \
524   GDateTime *dt, *dt2; \
525   dt = g_date_time_new_local (2000, 1, 1, 0, 0, 0); \
526   dt2 = g_date_time_add_seconds (dt, i); \
527   g_assert_cmpint (o, ==, g_date_time_get_second (dt2)); \
528   g_date_time_unref (dt); \
529   g_date_time_unref (dt2); \
530 } G_STMT_END
531
532   TEST_ADD_SECONDS (1, 1);
533   TEST_ADD_SECONDS (60, 0);
534   TEST_ADD_SECONDS (61, 1);
535   TEST_ADD_SECONDS (120, 0);
536   TEST_ADD_SECONDS (-61, 59);
537   TEST_ADD_SECONDS (86401, 1);
538   TEST_ADD_SECONDS (-86401, 59);
539   TEST_ADD_SECONDS (-31, 29);
540   TEST_ADD_SECONDS (13, 13);
541 }
542
543 static void
544 test_GDateTime_diff (void)
545 {
546 #define TEST_DIFF(y,m,d,y2,m2,d2,u) G_STMT_START { \
547   GDateTime *dt1, *dt2; \
548   GTimeSpan  ts = 0; \
549   dt1 = g_date_time_new_local (y, m, d, 0, 0, 0); \
550   dt2 = g_date_time_new_local (y2, m2, d2, 0, 0, 0); \
551   ts = g_date_time_difference (dt2, dt1); \
552   g_assert_cmpint (ts, ==, u); \
553   g_date_time_unref (dt1); \
554   g_date_time_unref (dt2); \
555 } G_STMT_END
556
557   TEST_DIFF (2009, 1, 1, 2009, 2, 1, G_TIME_SPAN_DAY * 31);
558   TEST_DIFF (2009, 1, 1, 2010, 1, 1, G_TIME_SPAN_DAY * 365);
559   TEST_DIFF (2008, 2, 28, 2008, 2, 29, G_TIME_SPAN_DAY);
560   TEST_DIFF (2008, 2, 29, 2008, 2, 28, -G_TIME_SPAN_DAY);
561
562   /* TODO: Add usec tests */
563 }
564
565 static void
566 test_GDateTime_get_minute (void)
567 {
568   GDateTime *dt;
569
570   dt = g_date_time_new_utc (2009, 12, 1, 1, 31, 0);
571   g_assert_cmpint (31, ==, g_date_time_get_minute (dt));
572   g_date_time_unref (dt);
573 }
574
575 static void
576 test_GDateTime_get_month (void)
577 {
578   GDateTime *dt;
579
580   dt = g_date_time_new_utc (2009, 12, 1, 1, 31, 0);
581   g_assert_cmpint (12, ==, g_date_time_get_month (dt));
582   g_date_time_unref (dt);
583 }
584
585 static void
586 test_GDateTime_get_second (void)
587 {
588   GDateTime *dt;
589
590   dt = g_date_time_new_utc (2009, 12, 1, 1, 31, 44);
591   g_assert_cmpint (44, ==, g_date_time_get_second (dt));
592   g_date_time_unref (dt);
593 }
594
595 static void
596 test_GDateTime_new_full (void)
597 {
598   GTimeZone *tz;
599   GDateTime *dt;
600
601   dt = g_date_time_new_utc (2009, 12, 11, 12, 11, 10);
602   g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
603   g_assert_cmpint (12, ==, g_date_time_get_month (dt));
604   g_assert_cmpint (11, ==, g_date_time_get_day_of_month (dt));
605   g_assert_cmpint (12, ==, g_date_time_get_hour (dt));
606   g_assert_cmpint (11, ==, g_date_time_get_minute (dt));
607   g_assert_cmpint (10, ==, g_date_time_get_second (dt));
608   g_date_time_unref (dt);
609
610   tz = g_time_zone_new ("America/Recife");
611   dt = g_date_time_new (tz, 2010, 5, 24, 8, 4, 0);
612   g_time_zone_unref (tz);
613   g_assert_cmpint (2010, ==, g_date_time_get_year (dt));
614   g_assert_cmpint (5, ==, g_date_time_get_month (dt));
615   g_assert_cmpint (24, ==, g_date_time_get_day_of_month (dt));
616   g_assert_cmpint (8, ==, g_date_time_get_hour (dt));
617   g_assert_cmpint (4, ==, g_date_time_get_minute (dt));
618   g_assert_cmpint (0, ==, g_date_time_get_second (dt));
619   g_assert_cmpstr ("BRT", ==, g_date_time_get_timezone_abbreviation (dt));
620   g_assert (!g_date_time_is_daylight_savings (dt));
621   g_date_time_unref (dt);
622 }
623
624 static void
625 test_GDateTime_now_utc (void)
626 {
627   GDateTime *dt;
628   time_t     t;
629   struct tm  tm;
630
631   t = time (NULL);
632 #ifdef HAVE_GMTIME_R
633   gmtime_r (&t, &tm);
634 #else
635   {
636     struct tm *tmp = gmtime (&t);
637     /* Assume gmtime() can't fail as we got t from time(NULL). (Note
638      * that on Windows, gmtime() *is* MT-safe, it uses a thread-local
639      * buffer.)
640      */
641     memcpy (&tm, tmp, sizeof (struct tm));
642   }
643 #endif
644   dt = g_date_time_new_now_utc ();
645   g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
646   g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
647   g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
648   g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
649   g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
650   g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
651   g_date_time_unref (dt);
652 }
653
654 static void
655 test_GDateTime_new_from_unix_utc (void)
656 {
657   GDateTime *dt;
658   gint64 t;
659
660   t = g_get_real_time ();
661
662 #if 0
663   dt = g_date_time_new_from_unix_utc (t);
664   g_assert (dt == NULL);
665 #endif
666
667   t = t / 1e6;  /* oops, this was microseconds */
668
669   dt = g_date_time_new_from_unix_utc (t);
670   g_assert (dt != NULL);
671
672   g_assert (dt == g_date_time_ref (dt));
673   g_date_time_unref (dt);
674   g_assert_cmpint (g_date_time_to_unix (dt), ==, t);
675   g_date_time_unref (dt);
676 }
677
678 static void
679 test_GDateTime_get_utc_offset (void)
680 {
681   GDateTime *dt;
682   GTimeSpan ts;
683   struct tm tm;
684
685   memset (&tm, 0, sizeof (tm));
686   get_localtime_tm (time (NULL), &tm);
687
688   dt = g_date_time_new_now_local ();
689   ts = g_date_time_get_utc_offset (dt);
690 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
691   g_assert_cmpint (ts, ==, (tm.tm_gmtoff * G_TIME_SPAN_SECOND));
692 #endif
693 #ifdef HAVE_STRUCT_TM___TM_GMTOFF
694   g_assert_cmpint (ts, ==, (tm.__tm_gmtoff * G_TIME_SPAN_SECOND));
695 #endif
696   g_date_time_unref (dt);
697 }
698
699 static void
700 test_GDateTime_to_timeval (void)
701 {
702   GTimeVal tv1, tv2;
703   GDateTime *dt;
704
705   memset (&tv1, 0, sizeof (tv1));
706   memset (&tv2, 0, sizeof (tv2));
707
708   g_get_current_time (&tv1);
709   dt = g_date_time_new_from_timeval_local (&tv1);
710   g_date_time_to_timeval (dt, &tv2);
711   g_assert_cmpint (tv1.tv_sec, ==, tv2.tv_sec);
712   g_assert_cmpint (tv1.tv_usec, ==, tv2.tv_usec);
713   g_date_time_unref (dt);
714 }
715
716 static void
717 test_GDateTime_to_local (void)
718 {
719   GDateTime *utc, *now, *dt;
720
721   utc = g_date_time_new_now_utc ();
722   now = g_date_time_new_now_local ();
723   dt = g_date_time_to_local (utc);
724
725   g_assert_cmpint (g_date_time_get_year (now), ==, g_date_time_get_year (dt));
726   g_assert_cmpint (g_date_time_get_month (now), ==, g_date_time_get_month (dt));
727   g_assert_cmpint (g_date_time_get_day_of_month (now), ==, g_date_time_get_day_of_month (dt));
728   g_assert_cmpint (g_date_time_get_hour (now), ==, g_date_time_get_hour (dt));
729   g_assert_cmpint (g_date_time_get_minute (now), ==, g_date_time_get_minute (dt));
730   g_assert_cmpint (g_date_time_get_second (now), ==, g_date_time_get_second (dt));
731
732   g_date_time_unref (now);
733   g_date_time_unref (utc);
734   g_date_time_unref (dt);
735 }
736
737 static void
738 test_GDateTime_to_utc (void)
739 {
740   GDateTime *dt, *dt2;
741   time_t     t;
742   struct tm  tm;
743
744   t = time (NULL);
745 #ifdef HAVE_GMTIME_R
746   gmtime_r (&t, &tm);
747 #else
748   {
749     struct tm *tmp = gmtime (&t);
750     memcpy (&tm, tmp, sizeof (struct tm));
751   }
752 #endif
753   dt2 = g_date_time_new_from_unix_local (t);
754   dt = g_date_time_to_utc (dt2);
755   g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
756   g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
757   g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
758   g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
759   g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
760   g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
761   g_date_time_unref (dt);
762   g_date_time_unref (dt2);
763 }
764
765 static void
766 test_GDateTime_get_day_of_year (void)
767 {
768 #define TEST_DAY_OF_YEAR(y,m,d,o)                       G_STMT_START {  \
769   GDateTime *__dt = g_date_time_new_local ((y), (m), (d), 0, 0, 0);     \
770   g_assert_cmpint ((o), ==, g_date_time_get_day_of_year (__dt));        \
771   g_date_time_unref (__dt);                             } G_STMT_END
772
773   TEST_DAY_OF_YEAR (2009, 1, 1, 1);
774   TEST_DAY_OF_YEAR (2009, 2, 1, 32);
775   TEST_DAY_OF_YEAR (2009, 8, 16, 228);
776   TEST_DAY_OF_YEAR (2008, 8, 16, 229);
777 }
778
779 static void
780 test_GDateTime_printf (void)
781 {
782   gchar dst[16];
783   struct tm tt;
784   time_t t;
785   gchar t_str[16];
786
787 #define TEST_PRINTF(f,o)                        G_STMT_START {  \
788 GDateTime *__dt = g_date_time_new_local (2009, 10, 24, 0, 0, 0);\
789   gchar *__p = g_date_time_format (__dt, (f));                  \
790   g_assert_cmpstr (__p, ==, (o));                               \
791   g_date_time_unref (__dt);                                     \
792   g_free (__p);                                 } G_STMT_END
793
794 #define TEST_PRINTF_DATE(y,m,d,f,o)             G_STMT_START {  \
795   GDateTime *dt = g_date_time_new_local (y, m, d, 0, 0, 0);     \
796   gchar *p = g_date_time_format (dt, (f));                      \
797   g_assert_cmpstr (p, ==, (o));                                 \
798   g_date_time_unref (dt);                                       \
799   g_free (p);                                   } G_STMT_END
800
801 #define TEST_PRINTF_TIME(h,m,s,f,o)             G_STMT_START { \
802   GDateTime *dt = g_date_time_new_local (2009, 10, 24, (h), (m), (s)); \
803   gchar *p = g_date_time_format (dt, (f));                      \
804   g_assert_cmpstr (p, ==, (o));                                 \
805   g_date_time_unref (dt);                                       \
806   g_free (p);                                   } G_STMT_END
807
808   /*
809    * This is a little helper to make sure we can compare timezones to
810    * that of the generated timezone.
811    */
812   t = time (NULL);
813   memset (&tt, 0, sizeof(tt));
814   get_localtime_tm (t, &tt);
815   tt.tm_year = 2009 - 1900;
816   tt.tm_mon = 9; /* 0 indexed */
817   tt.tm_mday = 24;
818   t = mktime (&tt);
819   memset (&tt, 0, sizeof(tt));
820   get_localtime_tm (t, &tt);
821   strftime (dst, sizeof(dst), "%Z", &tt);
822
823   /* get current time_t for 20090924 in the local timezone */
824   tt.tm_sec = 0;
825   tt.tm_min = 0;
826   tt.tm_hour = 0;
827   t = mktime (&tt);
828   g_sprintf (t_str, "%ld", t);
829
830   TEST_PRINTF ("%a", "Sat");
831   TEST_PRINTF ("%A", "Saturday");
832   TEST_PRINTF ("%b", "Oct");
833   TEST_PRINTF ("%B", "October");
834   TEST_PRINTF ("%d", "24");
835   TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
836   TEST_PRINTF ("%e", "24"); // fixme
837   TEST_PRINTF ("%h", "Oct");
838   TEST_PRINTF ("%H", "00");
839   TEST_PRINTF_TIME (15, 0, 0, "%H", "15");
840   TEST_PRINTF ("%I", "12");
841   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
842   TEST_PRINTF_TIME (15, 0, 0, "%I", "03");
843   TEST_PRINTF ("%j", "297");
844   TEST_PRINTF ("%k", " 0");
845   TEST_PRINTF_TIME (13, 13, 13, "%k", "13");
846   TEST_PRINTF ("%l", "12");
847   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
848   TEST_PRINTF_TIME (13, 13, 13, "%l", " 1");
849   TEST_PRINTF_TIME (10, 13, 13, "%l", "10");
850   TEST_PRINTF ("%m", "10");
851   TEST_PRINTF ("%M", "00");
852   TEST_PRINTF ("%p", "AM");
853   TEST_PRINTF_TIME (13, 13, 13, "%p", "PM");
854   TEST_PRINTF ("%P", "am");
855   TEST_PRINTF_TIME (13, 13, 13, "%P", "pm");
856   TEST_PRINTF ("%r", "12:00:00 AM");
857   TEST_PRINTF_TIME (13, 13, 13, "%r", "01:13:13 PM");
858   TEST_PRINTF ("%R", "00:00");
859   TEST_PRINTF_TIME (13, 13, 31, "%R", "13:13");
860   //TEST_PRINTF ("%s", t_str);
861   TEST_PRINTF ("%S", "00");
862   TEST_PRINTF ("%t", "  ");
863   TEST_PRINTF ("%u", "6");
864   TEST_PRINTF ("%x", "10/24/09");
865   TEST_PRINTF ("%X", "00:00:00");
866   TEST_PRINTF_TIME (13, 14, 15, "%X", "13:14:15");
867   TEST_PRINTF ("%y", "09");
868   TEST_PRINTF ("%Y", "2009");
869   TEST_PRINTF ("%%", "%");
870   TEST_PRINTF ("%", "");
871   TEST_PRINTF ("%9", NULL);
872   TEST_PRINTF ("%Z", dst);
873 }
874
875 static void
876 test_non_utf8_printf (void)
877 {
878   gchar *oldlocale;
879
880   oldlocale = g_strdup (setlocale (LC_ALL, NULL));
881   setlocale (LC_ALL, "ja_JP.eucjp");
882   if (strstr (setlocale (LC_ALL, NULL), "ja_JP") == NULL)
883     {
884       g_test_message ("locale ja_JP.eucjp not available, skipping non-UTF8 tests");
885       g_free (oldlocale);
886       return;
887     }
888
889   /* These are the outputs that ja_JP.UTF-8 generates; if everything
890    * is working then ja_JP.eucjp should generate the same.
891    */
892   TEST_PRINTF ("%a", "\345\234\237");
893   TEST_PRINTF ("%A", "\345\234\237\346\233\234\346\227\245");
894   TEST_PRINTF ("%b", "10\346\234\210");
895   TEST_PRINTF ("%B", "10\346\234\210");
896   TEST_PRINTF ("%d", "24");
897   TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
898   TEST_PRINTF ("%e", "24"); // fixme
899   TEST_PRINTF ("%h", "10\346\234\210");
900   TEST_PRINTF ("%H", "00");
901   TEST_PRINTF_TIME (15, 0, 0, "%H", "15");
902   TEST_PRINTF ("%I", "12");
903   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
904   TEST_PRINTF_TIME (15, 0, 0, "%I", "03");
905   TEST_PRINTF ("%j", "297");
906   TEST_PRINTF ("%k", " 0");
907   TEST_PRINTF_TIME (13, 13, 13, "%k", "13");
908   TEST_PRINTF ("%l", "12");
909   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
910   TEST_PRINTF_TIME (13, 13, 13, "%l", " 1");
911   TEST_PRINTF_TIME (10, 13, 13, "%l", "10");
912   TEST_PRINTF ("%m", "10");
913   TEST_PRINTF ("%M", "00");
914   TEST_PRINTF ("%p", "\345\215\210\345\211\215");
915   TEST_PRINTF_TIME (13, 13, 13, "%p", "\345\215\210\345\276\214");
916   TEST_PRINTF ("%P", "\345\215\210\345\211\215");
917   TEST_PRINTF_TIME (13, 13, 13, "%P", "\345\215\210\345\276\214");
918   TEST_PRINTF ("%r", "\345\215\210\345\211\21512\346\231\20200\345\210\20600\347\247\222");
919   TEST_PRINTF_TIME (13, 13, 13, "%r", "\345\215\210\345\276\21401\346\231\20213\345\210\20613\347\247\222");
920   TEST_PRINTF ("%R", "00:00");
921   TEST_PRINTF_TIME (13, 13, 31, "%R", "13:13");
922   TEST_PRINTF ("%S", "00");
923   TEST_PRINTF ("%t", "  ");
924   TEST_PRINTF ("%u", "6");
925   TEST_PRINTF ("%x", "2009\345\271\26410\346\234\21024\346\227\245");
926   TEST_PRINTF ("%X", "00\346\231\20200\345\210\20600\347\247\222");
927   TEST_PRINTF_TIME (13, 14, 15, "%X", "13\346\231\20214\345\210\20615\347\247\222");
928   TEST_PRINTF ("%y", "09");
929   TEST_PRINTF ("%Y", "2009");
930   TEST_PRINTF ("%%", "%");
931   TEST_PRINTF ("%", "");
932   TEST_PRINTF ("%9", NULL);
933
934   setlocale (LC_ALL, oldlocale);
935   g_free (oldlocale);
936 }
937
938 static void
939 test_modifiers (void)
940 {
941   gchar *oldlocale;
942
943   TEST_PRINTF_DATE (2009, 1,  1,  "%d", "01");
944   TEST_PRINTF_DATE (2009, 1,  1, "%_d", " 1");
945   TEST_PRINTF_DATE (2009, 1,  1, "%-d", "1");
946   TEST_PRINTF_DATE (2009, 1,  1, "%0d", "01");
947   TEST_PRINTF_DATE (2009, 1, 21,  "%d", "21");
948   TEST_PRINTF_DATE (2009, 1, 21, "%_d", "21");
949   TEST_PRINTF_DATE (2009, 1, 21, "%-d", "21");
950   TEST_PRINTF_DATE (2009, 1, 21, "%0d", "21");
951
952   TEST_PRINTF_DATE (2009, 1,  1,  "%e", " 1");
953   TEST_PRINTF_DATE (2009, 1,  1, "%_e", " 1");
954   TEST_PRINTF_DATE (2009, 1,  1, "%-e", "1");
955   TEST_PRINTF_DATE (2009, 1,  1, "%0e", "01");
956   TEST_PRINTF_DATE (2009, 1, 21,  "%e", "21");
957   TEST_PRINTF_DATE (2009, 1, 21, "%_e", "21");
958   TEST_PRINTF_DATE (2009, 1, 21, "%-e", "21");
959   TEST_PRINTF_DATE (2009, 1, 21, "%0e", "21");
960
961   TEST_PRINTF_TIME ( 1, 0, 0,  "%H", "01");
962   TEST_PRINTF_TIME ( 1, 0, 0, "%_H", " 1");
963   TEST_PRINTF_TIME ( 1, 0, 0, "%-H", "1");
964   TEST_PRINTF_TIME ( 1, 0, 0, "%0H", "01");
965   TEST_PRINTF_TIME (21, 0, 0,  "%H", "21");
966   TEST_PRINTF_TIME (21, 0, 0, "%_H", "21");
967   TEST_PRINTF_TIME (21, 0, 0, "%-H", "21");
968   TEST_PRINTF_TIME (21, 0, 0, "%0H", "21");
969
970   TEST_PRINTF_TIME ( 1, 0, 0,  "%I", "01");
971   TEST_PRINTF_TIME ( 1, 0, 0, "%_I", " 1");
972   TEST_PRINTF_TIME ( 1, 0, 0, "%-I", "1");
973   TEST_PRINTF_TIME ( 1, 0, 0, "%0I", "01");
974   TEST_PRINTF_TIME (23, 0, 0,  "%I", "11");
975   TEST_PRINTF_TIME (23, 0, 0, "%_I", "11");
976   TEST_PRINTF_TIME (23, 0, 0, "%-I", "11");
977   TEST_PRINTF_TIME (23, 0, 0, "%0I", "11");
978
979   TEST_PRINTF_TIME ( 1, 0, 0,  "%k", " 1");
980   TEST_PRINTF_TIME ( 1, 0, 0, "%_k", " 1");
981   TEST_PRINTF_TIME ( 1, 0, 0, "%-k", "1");
982   TEST_PRINTF_TIME ( 1, 0, 0, "%0k", "01");
983
984   oldlocale = g_strdup (setlocale (LC_ALL, NULL));
985   setlocale (LC_ALL, "fa_IR.utf-8");
986   if (strstr (setlocale (LC_ALL, NULL), "fa_IR") != NULL)
987     {
988       TEST_PRINTF_TIME (23, 0, 0, "%OH", "\333\262\333\263");    /* '23' */
989       TEST_PRINTF_TIME (23, 0, 0, "%OI", "\333\261\333\261");    /* '11' */
990       TEST_PRINTF_TIME (23, 0, 0, "%OM", "\333\260\333\260");    /* '00' */
991
992       TEST_PRINTF_DATE (2011, 7, 1, "%Om", "\333\260\333\267");  /* '07' */
993       TEST_PRINTF_DATE (2011, 7, 1, "%0Om", "\333\260\333\267"); /* '07' */
994       TEST_PRINTF_DATE (2011, 7, 1, "%-Om", "\333\267");         /* '7' */
995       TEST_PRINTF_DATE (2011, 7, 1, "%_Om", " \333\267");        /* ' 7' */
996     }
997   else
998     g_test_message ("locale fa_IR not available, skipping O modifier tests");
999   setlocale (LC_ALL, oldlocale);
1000   g_free (oldlocale);
1001 }
1002
1003 static void
1004 test_GDateTime_dst (void)
1005 {
1006   GDateTime *dt1, *dt2;
1007   GTimeZone *tz;
1008
1009   /* this date has the DST state set for Europe/London */
1010   tz = g_time_zone_new ("Europe/London");
1011   dt1 = g_date_time_new (tz, 2009, 8, 15, 3, 0, 1);
1012   g_assert (g_date_time_is_daylight_savings (dt1));
1013   g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, 3600);
1014   g_assert_cmpint (g_date_time_get_hour (dt1), ==, 3);
1015
1016   /* add 6 months to clear the DST flag but keep the same time */
1017   dt2 = g_date_time_add_months (dt1, 6);
1018   g_assert (!g_date_time_is_daylight_savings (dt2));
1019   g_assert_cmpint (g_date_time_get_utc_offset (dt2) / G_USEC_PER_SEC, ==, 0);
1020   g_assert_cmpint (g_date_time_get_hour (dt2), ==, 3);
1021
1022   g_date_time_unref (dt2);
1023   g_date_time_unref (dt1);
1024
1025   /* now do the reverse: start with a non-DST state and move to DST */
1026   dt1 = g_date_time_new (tz, 2009, 2, 15, 2, 0, 1);
1027   g_assert (!g_date_time_is_daylight_savings (dt1));
1028   g_assert_cmpint (g_date_time_get_hour (dt1), ==, 2);
1029
1030   dt2 = g_date_time_add_months (dt1, 6);
1031   g_assert (g_date_time_is_daylight_savings (dt2));
1032   g_assert_cmpint (g_date_time_get_hour (dt2), ==, 2);
1033
1034   g_date_time_unref (dt2);
1035   g_date_time_unref (dt1);
1036   g_time_zone_unref (tz);
1037 }
1038
1039 static inline gboolean
1040 is_leap_year (gint year)
1041 {
1042   g_assert (1 <= year && year <= 9999);
1043
1044   return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
1045 }
1046
1047 static inline gint
1048 days_in_month (gint year, gint month)
1049 {
1050   const gint table[2][13] = {
1051     {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
1052     {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
1053   };
1054
1055   g_assert (1 <= month && month <= 12);
1056
1057   return table[is_leap_year (year)][month];
1058 }
1059
1060 static void
1061 test_all_dates (void)
1062 {
1063   gint year, month, day;
1064   GTimeZone *timezone;
1065   gint64 unix_time;
1066   gint day_of_year;
1067   gint week_year;
1068   gint week_num;
1069   gint weekday;
1070
1071   /* save some time by hanging on to this. */
1072   timezone = g_time_zone_new_utc ();
1073
1074   unix_time = G_GINT64_CONSTANT(-62135596800);
1075
1076   /* 0001-01-01 is 0001-W01-1 */
1077   week_year = 1;
1078   week_num = 1;
1079   weekday = 1;
1080
1081
1082   /* The calendar makes a full cycle every 400 years, so we could
1083    * theoretically just test years 1 through 400.  That assumes that our
1084    * software has no bugs, so probably we should just test them all. :)
1085    */
1086   for (year = 1; year <= 9999; year++)
1087     {
1088       day_of_year = 1;
1089
1090       for (month = 1; month <= 12; month++)
1091         for (day = 1; day <= days_in_month (year, month); day++)
1092           {
1093             GDateTime *dt;
1094
1095             dt = g_date_time_new (timezone, year, month, day, 0, 0, 0);
1096
1097 #if 0
1098             g_print ("%04d-%02d-%02d = %04d-W%02d-%d = %04d-%03d\n",
1099                      year, month, day,
1100                      week_year, week_num, weekday,
1101                      year, day_of_year);
1102 #endif
1103
1104             /* sanity check */
1105             if G_UNLIKELY (g_date_time_get_year (dt) != year ||
1106                            g_date_time_get_month (dt) != month ||
1107                            g_date_time_get_day_of_month (dt) != day)
1108               g_error ("%04d-%02d-%02d comes out as %04d-%02d-%02d",
1109                        year, month, day,
1110                        g_date_time_get_year (dt),
1111                        g_date_time_get_month (dt),
1112                        g_date_time_get_day_of_month (dt));
1113
1114             if G_UNLIKELY (g_date_time_get_week_numbering_year (dt) != week_year ||
1115                            g_date_time_get_week_of_year (dt) != week_num ||
1116                            g_date_time_get_day_of_week (dt) != weekday)
1117               g_error ("%04d-%02d-%02d should be %04d-W%02d-%d but "
1118                        "comes out as %04d-W%02d-%d", year, month, day,
1119                        week_year, week_num, weekday,
1120                        g_date_time_get_week_numbering_year (dt),
1121                        g_date_time_get_week_of_year (dt),
1122                        g_date_time_get_day_of_week (dt));
1123
1124             if G_UNLIKELY (g_date_time_to_unix (dt) != unix_time)
1125               g_error ("%04d-%02d-%02d 00:00:00 UTC should have unix time %"
1126                        G_GINT64_FORMAT " but comes out as %"G_GINT64_FORMAT,
1127                        year, month, day, unix_time, g_date_time_to_unix (dt));
1128
1129             if G_UNLIKELY (g_date_time_get_day_of_year (dt) != day_of_year)
1130               g_error ("%04d-%02d-%02d should be day of year %d"
1131                        " but comes out as %d", year, month, day,
1132                        day_of_year, g_date_time_get_day_of_year (dt));
1133
1134             if G_UNLIKELY (g_date_time_get_hour (dt) != 0 ||
1135                            g_date_time_get_minute (dt) != 0 ||
1136                            g_date_time_get_seconds (dt) != 0)
1137               g_error ("%04d-%02d-%02d 00:00:00 UTC comes out "
1138                        "as %02d:%02d:%02.6f", year, month, day,
1139                        g_date_time_get_hour (dt),
1140                        g_date_time_get_minute (dt),
1141                        g_date_time_get_seconds (dt));
1142             /* done */
1143
1144             /* add 24 hours to unix time */
1145             unix_time += 24 * 60 * 60;
1146
1147             /* move day of year forward */
1148             day_of_year++;
1149
1150             /* move the week date forward */
1151             if (++weekday == 8)
1152               {
1153                 weekday = 1; /* Sunday -> Monday */
1154
1155                 /* NOTE: year/month/day is the final day of the week we
1156                  * just finished.
1157                  *
1158                  * If we just finished the last week of last year then
1159                  * we are definitely starting the first week of this
1160                  * year.
1161                  *
1162                  * Otherwise, if we're still in this year, but Sunday
1163                  * fell on or after December 28 then December 29, 30, 31
1164                  * could be days within the next year's first year.
1165                  */
1166                 if (year != week_year || (month == 12 && day >= 28))
1167                   {
1168                     /* first week of the new year */
1169                     week_num = 1;
1170                     week_year++;
1171                   }
1172                 else
1173                   week_num++;
1174               }
1175
1176             g_date_time_unref (dt);
1177           }
1178     }
1179
1180   g_time_zone_unref (timezone);
1181 }
1182
1183 static void
1184 test_z (void)
1185 {
1186   GTimeZone *tz;
1187   GDateTime *dt;
1188
1189   g_test_bug ("642935");
1190
1191   tz = g_time_zone_new ("-08:00");
1192   dt = g_date_time_new (tz, 0, 0, 0, 0, 0, 0);
1193   gchar *p = g_date_time_format (dt, "%z");
1194   g_assert_cmpstr (p, ==, "-0800");
1195   g_date_time_unref (dt);
1196   g_time_zone_unref (tz);
1197   g_free (p);
1198 }
1199
1200 static void
1201 test_strftime (void)
1202 {
1203   /* this is probably going to cause various buggy libcs to explode... */
1204 #define TEST_FORMAT \
1205   "a%a A%A b%b B%B c%c C%C d%d e%e F%F g%g G%G h%h H%H I%I j%j m%m M%M " \
1206   "n%n p%p r%r R%R S%S t%t T%T u%u V%V w%w x%x X%X y%y Y%Y z%z Z%Z %%"
1207   time_t t;
1208
1209   /* 127997 is prime, 1315005118 is now */
1210   for (t = 0; t < 1315005118; t += 127997)
1211     {
1212       GDateTime *date_time;
1213       gchar c_str[1000];
1214       gchar *dt_str;
1215
1216       date_time = g_date_time_new_from_unix_local (t);
1217       dt_str = g_date_time_format (date_time, TEST_FORMAT);
1218       strftime (c_str, sizeof c_str, TEST_FORMAT, localtime (&t));
1219       g_assert_cmpstr (c_str, ==, dt_str);
1220       g_date_time_unref (date_time);
1221       g_free (dt_str);
1222     }
1223 }
1224
1225 static void
1226 test_find_interval (void)
1227 {
1228   GTimeZone *tz;
1229   GDateTime *dt;
1230   gint64 u;
1231   gint i1, i2;
1232
1233   tz = g_time_zone_new ("Canada/Eastern");
1234   dt = g_date_time_new_utc (2010, 11, 7, 1, 30, 0);
1235   u = g_date_time_to_unix (dt);
1236
1237   i1 = g_time_zone_find_interval (tz, G_TIME_TYPE_STANDARD, u);
1238   i2 = g_time_zone_find_interval (tz, G_TIME_TYPE_DAYLIGHT, u);
1239
1240   g_assert_cmpint (i1, !=, i2);
1241
1242   g_date_time_unref (dt);
1243
1244   dt = g_date_time_new_utc (2010, 3, 14, 2, 0, 0);
1245   u = g_date_time_to_unix (dt);
1246
1247   i1 = g_time_zone_find_interval (tz, G_TIME_TYPE_STANDARD, u);
1248   g_assert_cmpint (i1, ==, -1);
1249
1250   g_date_time_unref (dt);
1251   g_time_zone_unref (tz);
1252 }
1253
1254 static void
1255 test_adjust_time (void)
1256 {
1257   GTimeZone *tz;
1258   GDateTime *dt;
1259   gint64 u, u2;
1260   gint i1, i2;
1261
1262   tz = g_time_zone_new ("Canada/Eastern");
1263   dt = g_date_time_new_utc (2010, 11, 7, 1, 30, 0);
1264   u = g_date_time_to_unix (dt);
1265   u2 = u;
1266
1267   i1 = g_time_zone_find_interval (tz, G_TIME_TYPE_DAYLIGHT, u);
1268   i2 = g_time_zone_adjust_time (tz, G_TIME_TYPE_DAYLIGHT, &u2);
1269
1270   g_assert_cmpint (i1, ==, i2);
1271   g_assert (u == u2);
1272
1273   g_date_time_unref (dt);
1274
1275   dt = g_date_time_new_utc (2010, 3, 14, 2, 30, 0);
1276   u2 = g_date_time_to_unix (dt);
1277   g_date_time_unref (dt);
1278
1279   dt = g_date_time_new_utc (2010, 3, 14, 3, 0, 0);
1280   u = g_date_time_to_unix (dt);
1281   g_date_time_unref (dt);
1282
1283   i1 = g_time_zone_adjust_time (tz, G_TIME_TYPE_DAYLIGHT, &u2);
1284   g_assert (u == u2);
1285
1286   g_time_zone_unref (tz);
1287 }
1288
1289 gint
1290 main (gint   argc,
1291       gchar *argv[])
1292 {
1293   g_test_init (&argc, &argv, NULL);
1294   g_test_bug_base ("http://bugzilla.gnome.org/");
1295
1296   /* GDateTime Tests */
1297
1298   g_test_add_func ("/GDateTime/add_days", test_GDateTime_add_days);
1299   g_test_add_func ("/GDateTime/add_full", test_GDateTime_add_full);
1300   g_test_add_func ("/GDateTime/add_hours", test_GDateTime_add_hours);
1301   g_test_add_func ("/GDateTime/add_minutes", test_GDateTime_add_minutes);
1302   g_test_add_func ("/GDateTime/add_months", test_GDateTime_add_months);
1303   g_test_add_func ("/GDateTime/add_seconds", test_GDateTime_add_seconds);
1304   g_test_add_func ("/GDateTime/add_weeks", test_GDateTime_add_weeks);
1305   g_test_add_func ("/GDateTime/add_years", test_GDateTime_add_years);
1306   g_test_add_func ("/GDateTime/compare", test_GDateTime_compare);
1307   g_test_add_func ("/GDateTime/diff", test_GDateTime_diff);
1308   g_test_add_func ("/GDateTime/equal", test_GDateTime_equal);
1309   g_test_add_func ("/GDateTime/get_day_of_week", test_GDateTime_get_day_of_week);
1310   g_test_add_func ("/GDateTime/get_day_of_month", test_GDateTime_get_day_of_month);
1311   g_test_add_func ("/GDateTime/get_day_of_year", test_GDateTime_get_day_of_year);
1312   g_test_add_func ("/GDateTime/get_hour", test_GDateTime_get_hour);
1313   g_test_add_func ("/GDateTime/get_microsecond", test_GDateTime_get_microsecond);
1314   g_test_add_func ("/GDateTime/get_minute", test_GDateTime_get_minute);
1315   g_test_add_func ("/GDateTime/get_month", test_GDateTime_get_month);
1316   g_test_add_func ("/GDateTime/get_second", test_GDateTime_get_second);
1317   g_test_add_func ("/GDateTime/get_utc_offset", test_GDateTime_get_utc_offset);
1318   g_test_add_func ("/GDateTime/get_year", test_GDateTime_get_year);
1319   g_test_add_func ("/GDateTime/hash", test_GDateTime_hash);
1320   g_test_add_func ("/GDateTime/new_from_unix", test_GDateTime_new_from_unix);
1321   g_test_add_func ("/GDateTime/new_from_unix_utc", test_GDateTime_new_from_unix_utc);
1322   g_test_add_func ("/GDateTime/new_from_timeval", test_GDateTime_new_from_timeval);
1323   g_test_add_func ("/GDateTime/new_from_timeval_utc", test_GDateTime_new_from_timeval_utc);
1324   g_test_add_func ("/GDateTime/new_full", test_GDateTime_new_full);
1325   g_test_add_func ("/GDateTime/now", test_GDateTime_now);
1326   g_test_add_func ("/GDateTime/printf", test_GDateTime_printf);
1327   g_test_add_func ("/GDateTime/non_utf8_printf", test_non_utf8_printf);
1328   g_test_add_func ("/GDateTime/strftime", test_strftime);
1329   g_test_add_func ("/GDateTime/modifiers", test_modifiers);
1330   g_test_add_func ("/GDateTime/to_local", test_GDateTime_to_local);
1331   g_test_add_func ("/GDateTime/to_unix", test_GDateTime_to_unix);
1332   g_test_add_func ("/GDateTime/to_timeval", test_GDateTime_to_timeval);
1333   g_test_add_func ("/GDateTime/to_utc", test_GDateTime_to_utc);
1334   g_test_add_func ("/GDateTime/now_utc", test_GDateTime_now_utc);
1335   g_test_add_func ("/GDateTime/dst", test_GDateTime_dst);
1336   g_test_add_func ("/GDateTime/test_z", test_z);
1337   g_test_add_func ("/GDateTime/test-all-dates", test_all_dates);
1338   g_test_add_func ("/GDateTime/find-interval", test_find_interval);
1339   g_test_add_func ("/GDateTime/adjust-time", test_adjust_time);
1340
1341   return g_test_run ();
1342 }