gdatetime test: don't assume that time stands still
[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   time_t before;
74   time_t after;
75
76   /* before <= dt.to_unix() <= after, but the inequalities might not be
77    * equality if we're close to the boundary between seconds.
78    * We loop until before == after (and hence dt.to_unix() should equal both)
79    * to guard against that. */
80   do
81     {
82       before = time (NULL);
83
84       memset (&tm, 0, sizeof (tm));
85       get_localtime_tm (before, &tm);
86
87       dt = g_date_time_new_now_local ();
88
89       after = time (NULL);
90     }
91   while (before != after);
92
93   g_assert_cmpint (g_date_time_get_year (dt), ==, 1900 + tm.tm_year);
94   g_assert_cmpint (g_date_time_get_month (dt), ==, 1 + tm.tm_mon);
95   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, tm.tm_mday);
96   g_assert_cmpint (g_date_time_get_hour (dt), ==, tm.tm_hour);
97   g_assert_cmpint (g_date_time_get_minute (dt), ==, tm.tm_min);
98   g_assert_cmpint (g_date_time_get_second (dt), ==, tm.tm_sec);
99
100   g_date_time_unref (dt);
101 }
102
103 static void
104 test_GDateTime_new_from_unix (void)
105 {
106   GDateTime *dt;
107   struct tm  tm;
108   time_t     t;
109
110   memset (&tm, 0, sizeof (tm));
111   t = time (NULL);
112   get_localtime_tm (t, &tm);
113
114   dt = g_date_time_new_from_unix_local (t);
115   g_assert_cmpint (g_date_time_get_year (dt), ==, 1900 + tm.tm_year);
116   g_assert_cmpint (g_date_time_get_month (dt), ==, 1 + tm.tm_mon);
117   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, tm.tm_mday);
118   g_assert_cmpint (g_date_time_get_hour (dt), ==, tm.tm_hour);
119   g_assert_cmpint (g_date_time_get_minute (dt), ==, tm.tm_min);
120   g_assert_cmpint (g_date_time_get_second (dt), ==, tm.tm_sec);
121   g_date_time_unref (dt);
122
123   memset (&tm, 0, sizeof (tm));
124   tm.tm_year = 90;
125   tm.tm_mday = 1;
126   tm.tm_mon = 0;
127   tm.tm_hour = 0;
128   tm.tm_min = 0;
129   tm.tm_sec = 0;
130   tm.tm_isdst = -1;
131   t = mktime (&tm);
132
133   dt = g_date_time_new_from_unix_local (t);
134   g_assert_cmpint (g_date_time_get_year (dt), ==, 1990);
135   g_assert_cmpint (g_date_time_get_month (dt), ==, 1);
136   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 1);
137   g_assert_cmpint (g_date_time_get_hour (dt), ==, 0);
138   g_assert_cmpint (g_date_time_get_minute (dt), ==, 0);
139   g_assert_cmpint (g_date_time_get_second (dt), ==, 0);
140   g_date_time_unref (dt);
141 }
142
143 static void
144 test_GDateTime_invalid (void)
145 {
146   GDateTime *dt;
147
148   g_test_bug ("702674");
149
150   dt = g_date_time_new_utc (2013, -2147483647, 31, 17, 15, 48);
151   g_assert (dt == NULL);
152 }
153
154 static void
155 test_GDateTime_compare (void)
156 {
157   GDateTime *dt1, *dt2;
158   gint       i;
159
160   dt1 = g_date_time_new_utc (2000, 1, 1, 0, 0, 0);
161
162   for (i = 1; i < 2000; i++)
163     {
164       dt2 = g_date_time_new_utc (i, 12, 31, 0, 0, 0);
165       g_assert_cmpint (1, ==, g_date_time_compare (dt1, dt2));
166       g_date_time_unref (dt2);
167     }
168
169   dt2 = g_date_time_new_utc (1999, 12, 31, 23, 59, 59);
170   g_assert_cmpint (1, ==, g_date_time_compare (dt1, dt2));
171   g_date_time_unref (dt2);
172
173   dt2 = g_date_time_new_utc (2000, 1, 1, 0, 0, 1);
174   g_assert_cmpint (-1, ==, g_date_time_compare (dt1, dt2));
175   g_date_time_unref (dt2);
176
177   dt2 = g_date_time_new_utc (2000, 1, 1, 0, 0, 0);
178   g_assert_cmpint (0, ==, g_date_time_compare (dt1, dt2));
179   g_date_time_unref (dt2);
180   g_date_time_unref (dt1);
181 }
182
183 static void
184 test_GDateTime_equal (void)
185 {
186   GDateTime *dt1, *dt2;
187   GTimeZone *tz;
188
189   dt1 = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
190   dt2 = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
191   g_assert (g_date_time_equal (dt1, dt2));
192   g_date_time_unref (dt1);
193   g_date_time_unref (dt2);
194
195   dt1 = g_date_time_new_local (2009, 10, 18, 0, 0, 0);
196   dt2 = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
197   g_assert (!g_date_time_equal (dt1, dt2));
198   g_date_time_unref (dt1);
199   g_date_time_unref (dt2);
200
201   /* UTC-0300 and not in DST */
202   tz = g_time_zone_new ("-03:00");
203   dt1 = g_date_time_new (tz, 2010, 5, 24,  8, 0, 0);
204   g_time_zone_unref (tz);
205   g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, (-3 * 3600));
206   /* UTC */
207   dt2 = g_date_time_new_utc (2010, 5, 24, 11, 0, 0);
208   g_assert_cmpint (g_date_time_get_utc_offset (dt2), ==, 0);
209
210   g_assert (g_date_time_equal (dt1, dt2));
211   g_date_time_unref (dt1);
212
213   /* America/Recife is in UTC-0300 */
214 #ifdef G_OS_UNIX
215   tz = g_time_zone_new ("America/Recife");
216 #elif defined G_OS_WIN32
217   tz = g_time_zone_new ("E. South America Standard Time");
218 #endif
219   dt1 = g_date_time_new (tz, 2010, 5, 24,  8, 0, 0);
220   g_time_zone_unref (tz);
221   g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, (-3 * 3600));
222   g_assert (g_date_time_equal (dt1, dt2));
223   g_date_time_unref (dt1);
224   g_date_time_unref (dt2);
225 }
226
227 static void
228 test_GDateTime_get_day_of_week (void)
229 {
230   GDateTime *dt;
231
232   dt = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
233   g_assert_cmpint (1, ==, g_date_time_get_day_of_week (dt));
234   g_date_time_unref (dt);
235
236   dt = g_date_time_new_local (2000, 10, 1, 0, 0, 0);
237   g_assert_cmpint (7, ==, g_date_time_get_day_of_week (dt));
238   g_date_time_unref (dt);
239 }
240
241 static void
242 test_GDateTime_get_day_of_month (void)
243 {
244   GDateTime *dt;
245
246   dt = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
247   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 19);
248   g_date_time_unref (dt);
249
250   dt = g_date_time_new_local (1400, 3, 12, 0, 0, 0);
251   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 12);
252   g_date_time_unref (dt);
253
254   dt = g_date_time_new_local (1800, 12, 31, 0, 0, 0);
255   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 31);
256   g_date_time_unref (dt);
257
258   dt = g_date_time_new_local (1000, 1, 1, 0, 0, 0);
259   g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 1);
260   g_date_time_unref (dt);
261 }
262
263 static void
264 test_GDateTime_get_hour (void)
265 {
266   GDateTime *dt;
267
268   dt = g_date_time_new_utc (2009, 10, 19, 15, 13, 11);
269   g_assert_cmpint (15, ==, g_date_time_get_hour (dt));
270   g_date_time_unref (dt);
271
272   dt = g_date_time_new_utc (100, 10, 19, 1, 0, 0);
273   g_assert_cmpint (1, ==, g_date_time_get_hour (dt));
274   g_date_time_unref (dt);
275
276   dt = g_date_time_new_utc (100, 10, 19, 0, 0, 0);
277   g_assert_cmpint (0, ==, g_date_time_get_hour (dt));
278   g_date_time_unref (dt);
279
280   dt = g_date_time_new_utc (100, 10, 1, 23, 59, 59);
281   g_assert_cmpint (23, ==, g_date_time_get_hour (dt));
282   g_date_time_unref (dt);
283 }
284
285 static void
286 test_GDateTime_get_microsecond (void)
287 {
288   GTimeVal   tv;
289   GDateTime *dt;
290
291   g_get_current_time (&tv);
292   dt = g_date_time_new_from_timeval_local (&tv);
293   g_assert_cmpint (tv.tv_usec, ==, g_date_time_get_microsecond (dt));
294   g_date_time_unref (dt);
295 }
296
297 static void
298 test_GDateTime_get_year (void)
299 {
300   GDateTime *dt;
301
302   dt = g_date_time_new_local (2009, 1, 1, 0, 0, 0);
303   g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
304   g_date_time_unref (dt);
305
306   dt = g_date_time_new_local (1, 1, 1, 0, 0, 0);
307   g_assert_cmpint (1, ==, g_date_time_get_year (dt));
308   g_date_time_unref (dt);
309
310   dt = g_date_time_new_local (13, 1, 1, 0, 0, 0);
311   g_assert_cmpint (13, ==, g_date_time_get_year (dt));
312   g_date_time_unref (dt);
313
314   dt = g_date_time_new_local (3000, 1, 1, 0, 0, 0);
315   g_assert_cmpint (3000, ==, g_date_time_get_year (dt));
316   g_date_time_unref (dt);
317 }
318
319 static void
320 test_GDateTime_hash (void)
321 {
322   GHashTable *h;
323
324   h = g_hash_table_new_full (g_date_time_hash, g_date_time_equal,
325                              (GDestroyNotify)g_date_time_unref,
326                              NULL);
327   g_hash_table_insert (h, g_date_time_new_now_local (), NULL);
328   g_hash_table_remove_all (h);
329   g_hash_table_destroy (h);
330 }
331
332 static void
333 test_GDateTime_new_from_timeval (void)
334 {
335   GDateTime *dt;
336   GTimeVal   tv, tv2;
337
338   g_get_current_time (&tv);
339   dt = g_date_time_new_from_timeval_local (&tv);
340
341   if (g_test_verbose ())
342     g_print ("\nDT%04d-%02d-%02dT%02d:%02d:%02d%s\n",
343              g_date_time_get_year (dt),
344              g_date_time_get_month (dt),
345              g_date_time_get_day_of_month (dt),
346              g_date_time_get_hour (dt),
347              g_date_time_get_minute (dt),
348              g_date_time_get_second (dt),
349              g_date_time_get_timezone_abbreviation (dt));
350
351   g_date_time_to_timeval (dt, &tv2);
352   g_assert_cmpint (tv.tv_sec, ==, tv2.tv_sec);
353   g_assert_cmpint (tv.tv_usec, ==, tv2.tv_usec);
354   g_date_time_unref (dt);
355 }
356
357 static void
358 test_GDateTime_new_from_timeval_utc (void)
359 {
360   GDateTime *dt;
361   GTimeVal   tv, tv2;
362
363   g_get_current_time (&tv);
364   dt = g_date_time_new_from_timeval_utc (&tv);
365
366   if (g_test_verbose ())
367     g_print ("\nDT%04d-%02d-%02dT%02d:%02d:%02d%s\n",
368              g_date_time_get_year (dt),
369              g_date_time_get_month (dt),
370              g_date_time_get_day_of_month (dt),
371              g_date_time_get_hour (dt),
372              g_date_time_get_minute (dt),
373              g_date_time_get_second (dt),
374              g_date_time_get_timezone_abbreviation (dt));
375
376   g_date_time_to_timeval (dt, &tv2);
377   g_assert_cmpint (tv.tv_sec, ==, tv2.tv_sec);
378   g_assert_cmpint (tv.tv_usec, ==, tv2.tv_usec);
379   g_date_time_unref (dt);
380 }
381
382 static void
383 test_GDateTime_to_unix (void)
384 {
385   GDateTime *dt;
386   time_t     t;
387
388   t = time (NULL);
389   dt = g_date_time_new_from_unix_local (t);
390   g_assert_cmpint (g_date_time_to_unix (dt), ==, t);
391   g_date_time_unref (dt);
392 }
393
394 static void
395 test_GDateTime_add_years (void)
396 {
397   GDateTime *dt, *dt2;
398
399   dt = g_date_time_new_local (2009, 10, 19, 0, 0, 0);
400   dt2 = g_date_time_add_years (dt, 1);
401   g_assert_cmpint (2010, ==, g_date_time_get_year (dt2));
402   g_date_time_unref (dt);
403   g_date_time_unref (dt2);
404 }
405
406 static void
407 test_GDateTime_add_months (void)
408 {
409 #define TEST_ADD_MONTHS(y,m,d,a,ny,nm,nd) G_STMT_START { \
410   GDateTime *dt, *dt2; \
411   dt = g_date_time_new_utc (y, m, d, 0, 0, 0); \
412   dt2 = g_date_time_add_months (dt, a); \
413   ASSERT_DATE (dt2, ny, nm, nd); \
414   g_date_time_unref (dt); \
415   g_date_time_unref (dt2); \
416 } G_STMT_END
417
418   TEST_ADD_MONTHS (2009, 12, 31,    1, 2010, 1, 31);
419   TEST_ADD_MONTHS (2009, 12, 31,    1, 2010, 1, 31);
420   TEST_ADD_MONTHS (2009,  6, 15,    1, 2009, 7, 15);
421   TEST_ADD_MONTHS (1400,  3,  1,    1, 1400, 4,  1);
422   TEST_ADD_MONTHS (1400,  1, 31,    1, 1400, 2, 28);
423   TEST_ADD_MONTHS (1400,  1, 31, 7200, 2000, 1, 31);
424   TEST_ADD_MONTHS (2008,  2, 29,   12, 2009, 2, 28);
425   TEST_ADD_MONTHS (2000,  8, 16,   -5, 2000, 3, 16);
426   TEST_ADD_MONTHS (2000,  8, 16,  -12, 1999, 8, 16);
427   TEST_ADD_MONTHS (2011,  2,  1,  -13, 2010, 1,  1);
428   TEST_ADD_MONTHS (1776,  7,  4, 1200, 1876, 7,  4);
429 }
430
431 static void
432 test_GDateTime_add_days (void)
433 {
434 #define TEST_ADD_DAYS(y,m,d,a,ny,nm,nd) G_STMT_START { \
435   GDateTime *dt, *dt2; \
436   dt = g_date_time_new_local (y, m, d, 0, 0, 0); \
437   dt2 = g_date_time_add_days (dt, a); \
438   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
439   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
440   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
441   g_date_time_unref (dt); \
442   g_date_time_unref (dt2); \
443 } G_STMT_END
444
445   TEST_ADD_DAYS (2009, 1, 31, 1, 2009, 2, 1);
446   TEST_ADD_DAYS (2009, 2, 1, -1, 2009, 1, 31);
447   TEST_ADD_DAYS (2008, 2, 28, 1, 2008, 2, 29);
448   TEST_ADD_DAYS (2008, 12, 31, 1, 2009, 1, 1);
449   TEST_ADD_DAYS (1, 1, 1, 1, 1, 1, 2);
450   TEST_ADD_DAYS (1955, 5, 24, 10, 1955, 6, 3);
451   TEST_ADD_DAYS (1955, 5, 24, -10, 1955, 5, 14);
452 }
453
454 static void
455 test_GDateTime_add_weeks (void)
456 {
457 #define TEST_ADD_WEEKS(y,m,d,a,ny,nm,nd) G_STMT_START { \
458   GDateTime *dt, *dt2; \
459   dt = g_date_time_new_local (y, m, d, 0, 0, 0); \
460   dt2 = g_date_time_add_weeks (dt, a); \
461   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
462   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
463   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
464   g_date_time_unref (dt); \
465   g_date_time_unref (dt2); \
466 } G_STMT_END
467
468   TEST_ADD_WEEKS (2009, 1, 1, 1, 2009, 1, 8);
469   TEST_ADD_WEEKS (2009, 8, 30, 1, 2009, 9, 6);
470   TEST_ADD_WEEKS (2009, 12, 31, 1, 2010, 1, 7);
471   TEST_ADD_WEEKS (2009, 1, 1, -1, 2008, 12, 25);
472 }
473
474 static void
475 test_GDateTime_add_hours (void)
476 {
477 #define TEST_ADD_HOURS(y,m,d,h,mi,s,a,ny,nm,nd,nh,nmi,ns) G_STMT_START { \
478   GDateTime *dt, *dt2; \
479   dt = g_date_time_new_utc (y, m, d, h, mi, s); \
480   dt2 = g_date_time_add_hours (dt, a); \
481   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
482   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
483   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
484   g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
485   g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
486   g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
487   g_date_time_unref (dt); \
488   g_date_time_unref (dt2); \
489 } G_STMT_END
490
491   TEST_ADD_HOURS (2009,  1,  1,  0, 0, 0, 1, 2009, 1, 1, 1, 0, 0);
492   TEST_ADD_HOURS (2008, 12, 31, 23, 0, 0, 1, 2009, 1, 1, 0, 0, 0);
493 }
494
495 static void
496 test_GDateTime_add_full (void)
497 {
498 #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 { \
499   GDateTime *dt, *dt2; \
500   dt = g_date_time_new_utc (y, m, d, h, mi, s); \
501   dt2 = g_date_time_add_full (dt, ay, am, ad, ah, ami, as); \
502   g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
503   g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
504   g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
505   g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
506   g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
507   g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
508   g_date_time_unref (dt); \
509   g_date_time_unref (dt2); \
510 } G_STMT_END
511
512   TEST_ADD_FULL (2009, 10, 21,  0,  0, 0,
513                     1,  1,  1,  1,  1, 1,
514                  2010, 11, 22,  1,  1, 1);
515   TEST_ADD_FULL (2000,  1,  1,  1,  1, 1,
516                     0,  1,  0,  0,  0, 0,
517                  2000,  2,  1,  1,  1, 1);
518   TEST_ADD_FULL (2000,  1,  1,  0,  0, 0,
519                    -1,  1,  0,  0,  0, 0,
520                  1999,  2,  1,  0,  0, 0);
521   TEST_ADD_FULL (2010, 10, 31,  0,  0, 0,
522                     0,  4,  0,  0,  0, 0,
523                  2011,  2, 28,  0,  0, 0);
524   TEST_ADD_FULL (2010,  8, 25, 22, 45, 0,
525                     0,  1,  6,  1, 25, 0,
526                  2010, 10,  2,  0, 10, 0);
527 }
528
529 static void
530 test_GDateTime_add_minutes (void)
531 {
532 #define TEST_ADD_MINUTES(i,o) G_STMT_START { \
533   GDateTime *dt, *dt2; \
534   dt = g_date_time_new_local (2000, 1, 1, 0, 0, 0); \
535   dt2 = g_date_time_add_minutes (dt, i); \
536   g_assert_cmpint (o, ==, g_date_time_get_minute (dt2)); \
537   g_date_time_unref (dt); \
538   g_date_time_unref (dt2); \
539 } G_STMT_END
540
541   TEST_ADD_MINUTES (60, 0);
542   TEST_ADD_MINUTES (100, 40);
543   TEST_ADD_MINUTES (5, 5);
544   TEST_ADD_MINUTES (1441, 1);
545   TEST_ADD_MINUTES (-1441, 59);
546 }
547
548 static void
549 test_GDateTime_add_seconds (void)
550 {
551 #define TEST_ADD_SECONDS(i,o) G_STMT_START { \
552   GDateTime *dt, *dt2; \
553   dt = g_date_time_new_local (2000, 1, 1, 0, 0, 0); \
554   dt2 = g_date_time_add_seconds (dt, i); \
555   g_assert_cmpint (o, ==, g_date_time_get_second (dt2)); \
556   g_date_time_unref (dt); \
557   g_date_time_unref (dt2); \
558 } G_STMT_END
559
560   TEST_ADD_SECONDS (1, 1);
561   TEST_ADD_SECONDS (60, 0);
562   TEST_ADD_SECONDS (61, 1);
563   TEST_ADD_SECONDS (120, 0);
564   TEST_ADD_SECONDS (-61, 59);
565   TEST_ADD_SECONDS (86401, 1);
566   TEST_ADD_SECONDS (-86401, 59);
567   TEST_ADD_SECONDS (-31, 29);
568   TEST_ADD_SECONDS (13, 13);
569 }
570
571 static void
572 test_GDateTime_diff (void)
573 {
574 #define TEST_DIFF(y,m,d,y2,m2,d2,u) G_STMT_START { \
575   GDateTime *dt1, *dt2; \
576   GTimeSpan  ts = 0; \
577   dt1 = g_date_time_new_utc (y, m, d, 0, 0, 0); \
578   dt2 = g_date_time_new_utc (y2, m2, d2, 0, 0, 0); \
579   ts = g_date_time_difference (dt2, dt1); \
580   g_assert_cmpint (ts, ==, u); \
581   g_date_time_unref (dt1); \
582   g_date_time_unref (dt2); \
583 } G_STMT_END
584
585   TEST_DIFF (2009, 1, 1, 2009, 2, 1, G_TIME_SPAN_DAY * 31);
586   TEST_DIFF (2009, 1, 1, 2010, 1, 1, G_TIME_SPAN_DAY * 365);
587   TEST_DIFF (2008, 2, 28, 2008, 2, 29, G_TIME_SPAN_DAY);
588   TEST_DIFF (2008, 2, 29, 2008, 2, 28, -G_TIME_SPAN_DAY);
589
590   /* TODO: Add usec tests */
591 }
592
593 static void
594 test_GDateTime_get_minute (void)
595 {
596   GDateTime *dt;
597
598   dt = g_date_time_new_utc (2009, 12, 1, 1, 31, 0);
599   g_assert_cmpint (31, ==, g_date_time_get_minute (dt));
600   g_date_time_unref (dt);
601 }
602
603 static void
604 test_GDateTime_get_month (void)
605 {
606   GDateTime *dt;
607
608   dt = g_date_time_new_utc (2009, 12, 1, 1, 31, 0);
609   g_assert_cmpint (12, ==, g_date_time_get_month (dt));
610   g_date_time_unref (dt);
611 }
612
613 static void
614 test_GDateTime_get_second (void)
615 {
616   GDateTime *dt;
617
618   dt = g_date_time_new_utc (2009, 12, 1, 1, 31, 44);
619   g_assert_cmpint (44, ==, g_date_time_get_second (dt));
620   g_date_time_unref (dt);
621 }
622
623 static void
624 test_GDateTime_new_full (void)
625 {
626   GTimeZone *tz;
627   GDateTime *dt;
628
629   dt = g_date_time_new_utc (2009, 12, 11, 12, 11, 10);
630   g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
631   g_assert_cmpint (12, ==, g_date_time_get_month (dt));
632   g_assert_cmpint (11, ==, g_date_time_get_day_of_month (dt));
633   g_assert_cmpint (12, ==, g_date_time_get_hour (dt));
634   g_assert_cmpint (11, ==, g_date_time_get_minute (dt));
635   g_assert_cmpint (10, ==, g_date_time_get_second (dt));
636   g_date_time_unref (dt);
637
638 #ifdef G_OS_UNIX
639   tz = g_time_zone_new ("America/Recife");
640 #elif defined G_OS_WIN32
641   tz = g_time_zone_new ("E. South America Standard Time");
642 #endif
643   dt = g_date_time_new (tz, 2010, 5, 24, 8, 4, 0);
644   g_time_zone_unref (tz);
645   g_assert_cmpint (2010, ==, g_date_time_get_year (dt));
646   g_assert_cmpint (5, ==, g_date_time_get_month (dt));
647   g_assert_cmpint (24, ==, g_date_time_get_day_of_month (dt));
648   g_assert_cmpint (8, ==, g_date_time_get_hour (dt));
649   g_assert_cmpint (4, ==, g_date_time_get_minute (dt));
650   g_assert_cmpint (0, ==, g_date_time_get_second (dt));
651 #ifdef G_OS_UNIX
652   g_assert_cmpstr ("BRT", ==, g_date_time_get_timezone_abbreviation (dt));
653 #elif defined G_OS_WIN32
654   g_assert_cmpstr ("E. South America Standard Time", ==,
655                     g_date_time_get_timezone_abbreviation (dt));
656 #endif
657   g_assert (!g_date_time_is_daylight_savings (dt));
658   g_date_time_unref (dt);
659 }
660
661 static void
662 test_GDateTime_now_utc (void)
663 {
664   GDateTime *dt;
665   struct tm  tm;
666   time_t     t;
667   time_t     after;
668
669   /* t <= dt.to_unix() <= after, but the inequalities might not be
670    * equality if we're close to the boundary between seconds.
671    * We loop until t == after (and hence dt.to_unix() should equal both)
672    * to guard against that. */
673   do
674     {
675       t = time (NULL);
676 #ifdef HAVE_GMTIME_R
677       gmtime_r (&t, &tm);
678 #else
679       {
680         struct tm *tmp = gmtime (&t);
681         /* Assume gmtime() can't fail as we got t from time(NULL). (Note
682          * that on Windows, gmtime() *is* MT-safe, it uses a thread-local
683          * buffer.)
684          */
685         memcpy (&tm, tmp, sizeof (struct tm));
686       }
687 #endif
688       dt = g_date_time_new_now_utc ();
689
690       after = time (NULL);
691     }
692   while (t != after);
693
694   g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
695   g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
696   g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
697   g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
698   g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
699   g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
700   g_date_time_unref (dt);
701 }
702
703 static void
704 test_GDateTime_new_from_unix_utc (void)
705 {
706   GDateTime *dt;
707   gint64 t;
708
709   t = g_get_real_time ();
710
711 #if 0
712   dt = g_date_time_new_from_unix_utc (t);
713   g_assert (dt == NULL);
714 #endif
715
716   t = t / 1e6;  /* oops, this was microseconds */
717
718   dt = g_date_time_new_from_unix_utc (t);
719   g_assert (dt != NULL);
720
721   g_assert (dt == g_date_time_ref (dt));
722   g_date_time_unref (dt);
723   g_assert_cmpint (g_date_time_to_unix (dt), ==, t);
724   g_date_time_unref (dt);
725 }
726
727 static void
728 test_GDateTime_get_utc_offset (void)
729 {
730 #if defined (HAVE_STRUCT_TM_TM_GMTOFF) || defined (HAVE_STRUCT_TM___TM_GMTOFF)
731   GDateTime *dt;
732   GTimeSpan ts;
733   struct tm tm;
734
735   memset (&tm, 0, sizeof (tm));
736   get_localtime_tm (time (NULL), &tm);
737
738   dt = g_date_time_new_now_local ();
739   ts = g_date_time_get_utc_offset (dt);
740 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
741   g_assert_cmpint (ts, ==, (tm.tm_gmtoff * G_TIME_SPAN_SECOND));
742 #endif
743 #ifdef HAVE_STRUCT_TM___TM_GMTOFF
744   g_assert_cmpint (ts, ==, (tm.__tm_gmtoff * G_TIME_SPAN_SECOND));
745 #endif
746   g_date_time_unref (dt);
747 #endif
748 }
749
750 static void
751 test_GDateTime_to_timeval (void)
752 {
753   GTimeVal tv1, tv2;
754   GDateTime *dt;
755
756   memset (&tv1, 0, sizeof (tv1));
757   memset (&tv2, 0, sizeof (tv2));
758
759   g_get_current_time (&tv1);
760   dt = g_date_time_new_from_timeval_local (&tv1);
761   g_date_time_to_timeval (dt, &tv2);
762   g_assert_cmpint (tv1.tv_sec, ==, tv2.tv_sec);
763   g_assert_cmpint (tv1.tv_usec, ==, tv2.tv_usec);
764   g_date_time_unref (dt);
765 }
766
767 static void
768 test_GDateTime_to_local (void)
769 {
770   GDateTime *utc = NULL, *now = NULL, *dt;
771   time_t before, after;
772
773   /* before <= utc.to_unix() <= now.to_unix() <= after, but the inequalities
774    * might not be equality if we're close to the boundary between seconds.
775    * We loop until before == after (and hence the GDateTimes should match)
776    * to guard against that. */
777   do
778     {
779       before = time (NULL);
780       g_clear_pointer (&utc, g_date_time_unref);
781       g_clear_pointer (&now, g_date_time_unref);
782       utc = g_date_time_new_now_utc ();
783       now = g_date_time_new_now_local ();
784       after = time (NULL);
785     }
786   while (before != after);
787
788   dt = g_date_time_to_local (utc);
789
790   g_assert_cmpint (g_date_time_get_year (now), ==, g_date_time_get_year (dt));
791   g_assert_cmpint (g_date_time_get_month (now), ==, g_date_time_get_month (dt));
792   g_assert_cmpint (g_date_time_get_day_of_month (now), ==, g_date_time_get_day_of_month (dt));
793   g_assert_cmpint (g_date_time_get_hour (now), ==, g_date_time_get_hour (dt));
794   g_assert_cmpint (g_date_time_get_minute (now), ==, g_date_time_get_minute (dt));
795   g_assert_cmpint (g_date_time_get_second (now), ==, g_date_time_get_second (dt));
796
797   g_date_time_unref (now);
798   g_date_time_unref (utc);
799   g_date_time_unref (dt);
800 }
801
802 static void
803 test_GDateTime_to_utc (void)
804 {
805   GDateTime *dt, *dt2;
806   time_t     t;
807   struct tm  tm;
808
809   t = time (NULL);
810 #ifdef HAVE_GMTIME_R
811   gmtime_r (&t, &tm);
812 #else
813   {
814     struct tm *tmp = gmtime (&t);
815     memcpy (&tm, tmp, sizeof (struct tm));
816   }
817 #endif
818   dt2 = g_date_time_new_from_unix_local (t);
819   dt = g_date_time_to_utc (dt2);
820   g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
821   g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
822   g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
823   g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
824   g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
825   g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
826   g_date_time_unref (dt);
827   g_date_time_unref (dt2);
828 }
829
830 static void
831 test_GDateTime_get_day_of_year (void)
832 {
833 #define TEST_DAY_OF_YEAR(y,m,d,o)                       G_STMT_START {  \
834   GDateTime *__dt = g_date_time_new_local ((y), (m), (d), 0, 0, 0);     \
835   g_assert_cmpint ((o), ==, g_date_time_get_day_of_year (__dt));        \
836   g_date_time_unref (__dt);                             } G_STMT_END
837
838   TEST_DAY_OF_YEAR (2009, 1, 1, 1);
839   TEST_DAY_OF_YEAR (2009, 2, 1, 32);
840   TEST_DAY_OF_YEAR (2009, 8, 16, 228);
841   TEST_DAY_OF_YEAR (2008, 8, 16, 229);
842 }
843
844 static void
845 test_GDateTime_printf (void)
846 {
847 /* 64 seems big, but one zoneinfo file, Factory, has an abbreviation
848  * that long, and it will cause the test to fail if dst isn't big
849  * enough.
850  */
851   gchar dst[64];
852   struct tm tt;
853   time_t t;
854
855 #define TEST_PRINTF(f,o)                        G_STMT_START {  \
856 GDateTime *__dt = g_date_time_new_local (2009, 10, 24, 0, 0, 0);\
857   gchar *__p = g_date_time_format (__dt, (f));                  \
858   g_assert_cmpstr (__p, ==, (o));                               \
859   g_date_time_unref (__dt);                                     \
860   g_free (__p);                                 } G_STMT_END
861
862 #define TEST_PRINTF_DATE(y,m,d,f,o)             G_STMT_START {  \
863   GDateTime *dt = g_date_time_new_local (y, m, d, 0, 0, 0);     \
864   gchar *p = g_date_time_format (dt, (f));                      \
865   g_assert_cmpstr (p, ==, (o));                                 \
866   g_date_time_unref (dt);                                       \
867   g_free (p);                                   } G_STMT_END
868
869 #define TEST_PRINTF_TIME(h,m,s,f,o)             G_STMT_START { \
870   GDateTime *dt = g_date_time_new_local (2009, 10, 24, (h), (m), (s)); \
871   gchar *p = g_date_time_format (dt, (f));                      \
872   g_assert_cmpstr (p, ==, (o));                                 \
873   g_date_time_unref (dt);                                       \
874   g_free (p);                                   } G_STMT_END
875
876   /*
877    * This is a little helper to make sure we can compare timezones to
878    * that of the generated timezone.
879    */
880   t = time (NULL);
881   memset (&tt, 0, sizeof(tt));
882   get_localtime_tm (t, &tt);
883   tt.tm_year = 2009 - 1900;
884   tt.tm_mon = 9; /* 0 indexed */
885   tt.tm_mday = 24;
886   t = mktime (&tt);
887   memset (&tt, 0, sizeof(tt));
888   get_localtime_tm (t, &tt);
889   strftime (dst, sizeof(dst), "%Z", &tt);
890
891   /* get current time_t for 20090924 in the local timezone */
892   tt.tm_sec = 0;
893   tt.tm_min = 0;
894   tt.tm_hour = 0;
895   t = mktime (&tt);
896
897   TEST_PRINTF ("%a", "Sat");
898   TEST_PRINTF ("%A", "Saturday");
899   TEST_PRINTF ("%b", "Oct");
900   TEST_PRINTF ("%B", "October");
901   TEST_PRINTF ("%d", "24");
902   TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
903   TEST_PRINTF ("%e", "24"); // fixme
904   TEST_PRINTF ("%h", "Oct");
905   TEST_PRINTF ("%H", "00");
906   TEST_PRINTF_TIME (15, 0, 0, "%H", "15");
907   TEST_PRINTF ("%I", "12");
908   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
909   TEST_PRINTF_TIME (15, 0, 0, "%I", "03");
910   TEST_PRINTF ("%j", "297");
911   TEST_PRINTF ("%k", " 0");
912   TEST_PRINTF_TIME (13, 13, 13, "%k", "13");
913   TEST_PRINTF ("%l", "12");
914   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
915   TEST_PRINTF_TIME (13, 13, 13, "%l", " 1");
916   TEST_PRINTF_TIME (10, 13, 13, "%l", "10");
917   TEST_PRINTF ("%m", "10");
918   TEST_PRINTF ("%M", "00");
919   TEST_PRINTF ("%p", "AM");
920   TEST_PRINTF_TIME (13, 13, 13, "%p", "PM");
921   TEST_PRINTF ("%P", "am");
922   TEST_PRINTF_TIME (13, 13, 13, "%P", "pm");
923   TEST_PRINTF ("%r", "12:00:00 AM");
924   TEST_PRINTF_TIME (13, 13, 13, "%r", "01:13:13 PM");
925   TEST_PRINTF ("%R", "00:00");
926   TEST_PRINTF_TIME (13, 13, 31, "%R", "13:13");
927   TEST_PRINTF ("%S", "00");
928   TEST_PRINTF ("%t", "  ");
929   TEST_PRINTF ("%u", "6");
930   TEST_PRINTF ("%x", "10/24/09");
931   TEST_PRINTF ("%X", "00:00:00");
932   TEST_PRINTF_TIME (13, 14, 15, "%X", "13:14:15");
933   TEST_PRINTF ("%y", "09");
934   TEST_PRINTF ("%Y", "2009");
935   TEST_PRINTF ("%%", "%");
936   TEST_PRINTF ("%", "");
937   TEST_PRINTF ("%9", NULL);
938 #ifdef G_OS_UNIX
939   TEST_PRINTF ("%Z", dst);
940 #elif defined G_OS_WIN32
941   TEST_PRINTF ("%Z", "Pacific Standard Time");
942 #endif
943 }
944
945 static void
946 test_non_utf8_printf (void)
947 {
948   gchar *oldlocale;
949
950   oldlocale = g_strdup (setlocale (LC_ALL, NULL));
951   setlocale (LC_ALL, "ja_JP.eucjp");
952   if (strstr (setlocale (LC_ALL, NULL), "ja_JP") == NULL)
953     {
954       g_test_message ("locale ja_JP.eucjp not available, skipping non-UTF8 tests");
955       g_free (oldlocale);
956       return;
957     }
958   if (g_get_charset (NULL))
959     {
960       g_test_message ("locale ja_JP.eucjp may be available, but glib seems to think that it's equivalent to UTF-8, skipping non-UTF-8 tests.");
961       g_test_message ("This is a known issue on Darwin");
962       setlocale (LC_ALL, oldlocale);
963       g_free (oldlocale);
964       return;
965     }
966
967   /* These are the outputs that ja_JP.UTF-8 generates; if everything
968    * is working then ja_JP.eucjp should generate the same.
969    */
970   TEST_PRINTF ("%a", "\345\234\237");
971   TEST_PRINTF ("%A", "\345\234\237\346\233\234\346\227\245");
972 #ifndef HAVE_CARBON /* OSX just returns the number */
973   TEST_PRINTF ("%b", "10\346\234\210");
974 #endif
975   TEST_PRINTF ("%B", "10\346\234\210");
976   TEST_PRINTF ("%d", "24");
977   TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
978   TEST_PRINTF ("%e", "24"); // fixme
979 #ifndef HAVE_CARBON /* OSX just returns the number */
980   TEST_PRINTF ("%h", "10\346\234\210");
981 #endif
982   TEST_PRINTF ("%H", "00");
983   TEST_PRINTF_TIME (15, 0, 0, "%H", "15");
984   TEST_PRINTF ("%I", "12");
985   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
986   TEST_PRINTF_TIME (15, 0, 0, "%I", "03");
987   TEST_PRINTF ("%j", "297");
988   TEST_PRINTF ("%k", " 0");
989   TEST_PRINTF_TIME (13, 13, 13, "%k", "13");
990   TEST_PRINTF ("%l", "12");
991   TEST_PRINTF_TIME (12, 0, 0, "%I", "12");
992   TEST_PRINTF_TIME (13, 13, 13, "%l", " 1");
993   TEST_PRINTF_TIME (10, 13, 13, "%l", "10");
994   TEST_PRINTF ("%m", "10");
995   TEST_PRINTF ("%M", "00");
996 #ifndef HAVE_CARBON /* OSX returns latin "AM", not japanese */
997   TEST_PRINTF ("%p", "\345\215\210\345\211\215");
998   TEST_PRINTF_TIME (13, 13, 13, "%p", "\345\215\210\345\276\214");
999   TEST_PRINTF ("%P", "\345\215\210\345\211\215");
1000   TEST_PRINTF_TIME (13, 13, 13, "%P", "\345\215\210\345\276\214");
1001   TEST_PRINTF ("%r", "\345\215\210\345\211\21512\346\231\20200\345\210\20600\347\247\222");
1002   TEST_PRINTF_TIME (13, 13, 13, "%r", "\345\215\210\345\276\21401\346\231\20213\345\210\20613\347\247\222");
1003 #endif
1004   TEST_PRINTF ("%R", "00:00");
1005   TEST_PRINTF_TIME (13, 13, 31, "%R", "13:13");
1006   TEST_PRINTF ("%S", "00");
1007   TEST_PRINTF ("%t", "  ");
1008   TEST_PRINTF ("%u", "6");
1009 #ifndef HAVE_CARBON /* OSX returns YYYY/MM/DD in ASCII */
1010   TEST_PRINTF ("%x", "2009\345\271\26410\346\234\21024\346\227\245");
1011 #endif
1012   TEST_PRINTF ("%X", "00\346\231\20200\345\210\20600\347\247\222");
1013   TEST_PRINTF_TIME (13, 14, 15, "%X", "13\346\231\20214\345\210\20615\347\247\222");
1014   TEST_PRINTF ("%y", "09");
1015   TEST_PRINTF ("%Y", "2009");
1016   TEST_PRINTF ("%%", "%");
1017   TEST_PRINTF ("%", "");
1018   TEST_PRINTF ("%9", NULL);
1019
1020   setlocale (LC_ALL, oldlocale);
1021   g_free (oldlocale);
1022 }
1023
1024 static void
1025 test_modifiers (void)
1026 {
1027   gchar *oldlocale;
1028
1029   TEST_PRINTF_DATE (2009, 1,  1,  "%d", "01");
1030   TEST_PRINTF_DATE (2009, 1,  1, "%_d", " 1");
1031   TEST_PRINTF_DATE (2009, 1,  1, "%-d", "1");
1032   TEST_PRINTF_DATE (2009, 1,  1, "%0d", "01");
1033   TEST_PRINTF_DATE (2009, 1, 21,  "%d", "21");
1034   TEST_PRINTF_DATE (2009, 1, 21, "%_d", "21");
1035   TEST_PRINTF_DATE (2009, 1, 21, "%-d", "21");
1036   TEST_PRINTF_DATE (2009, 1, 21, "%0d", "21");
1037
1038   TEST_PRINTF_DATE (2009, 1,  1,  "%e", " 1");
1039   TEST_PRINTF_DATE (2009, 1,  1, "%_e", " 1");
1040   TEST_PRINTF_DATE (2009, 1,  1, "%-e", "1");
1041   TEST_PRINTF_DATE (2009, 1,  1, "%0e", "01");
1042   TEST_PRINTF_DATE (2009, 1, 21,  "%e", "21");
1043   TEST_PRINTF_DATE (2009, 1, 21, "%_e", "21");
1044   TEST_PRINTF_DATE (2009, 1, 21, "%-e", "21");
1045   TEST_PRINTF_DATE (2009, 1, 21, "%0e", "21");
1046
1047   TEST_PRINTF_TIME ( 1, 0, 0,  "%H", "01");
1048   TEST_PRINTF_TIME ( 1, 0, 0, "%_H", " 1");
1049   TEST_PRINTF_TIME ( 1, 0, 0, "%-H", "1");
1050   TEST_PRINTF_TIME ( 1, 0, 0, "%0H", "01");
1051   TEST_PRINTF_TIME (21, 0, 0,  "%H", "21");
1052   TEST_PRINTF_TIME (21, 0, 0, "%_H", "21");
1053   TEST_PRINTF_TIME (21, 0, 0, "%-H", "21");
1054   TEST_PRINTF_TIME (21, 0, 0, "%0H", "21");
1055
1056   TEST_PRINTF_TIME ( 1, 0, 0,  "%I", "01");
1057   TEST_PRINTF_TIME ( 1, 0, 0, "%_I", " 1");
1058   TEST_PRINTF_TIME ( 1, 0, 0, "%-I", "1");
1059   TEST_PRINTF_TIME ( 1, 0, 0, "%0I", "01");
1060   TEST_PRINTF_TIME (23, 0, 0,  "%I", "11");
1061   TEST_PRINTF_TIME (23, 0, 0, "%_I", "11");
1062   TEST_PRINTF_TIME (23, 0, 0, "%-I", "11");
1063   TEST_PRINTF_TIME (23, 0, 0, "%0I", "11");
1064
1065   TEST_PRINTF_TIME ( 1, 0, 0,  "%k", " 1");
1066   TEST_PRINTF_TIME ( 1, 0, 0, "%_k", " 1");
1067   TEST_PRINTF_TIME ( 1, 0, 0, "%-k", "1");
1068   TEST_PRINTF_TIME ( 1, 0, 0, "%0k", "01");
1069
1070   oldlocale = g_strdup (setlocale (LC_ALL, NULL));
1071   setlocale (LC_ALL, "fa_IR.utf-8");
1072   if (strstr (setlocale (LC_ALL, NULL), "fa_IR") != NULL)
1073     {
1074       TEST_PRINTF_TIME (23, 0, 0, "%OH", "\333\262\333\263");    /* '23' */
1075       TEST_PRINTF_TIME (23, 0, 0, "%OI", "\333\261\333\261");    /* '11' */
1076       TEST_PRINTF_TIME (23, 0, 0, "%OM", "\333\260\333\260");    /* '00' */
1077
1078       TEST_PRINTF_DATE (2011, 7, 1, "%Om", "\333\260\333\267");  /* '07' */
1079       TEST_PRINTF_DATE (2011, 7, 1, "%0Om", "\333\260\333\267"); /* '07' */
1080       TEST_PRINTF_DATE (2011, 7, 1, "%-Om", "\333\267");         /* '7' */
1081       TEST_PRINTF_DATE (2011, 7, 1, "%_Om", " \333\267");        /* ' 7' */
1082     }
1083   else
1084     g_test_message ("locale fa_IR not available, skipping O modifier tests");
1085   setlocale (LC_ALL, oldlocale);
1086   g_free (oldlocale);
1087 }
1088
1089 static void
1090 test_GDateTime_dst (void)
1091 {
1092   GDateTime *dt1, *dt2;
1093   GTimeZone *tz;
1094
1095   /* this date has the DST state set for Europe/London */
1096 #ifdef G_OS_UNIX
1097   tz = g_time_zone_new ("Europe/London");
1098 #elif defined G_OS_WIN32
1099   tz = g_time_zone_new ("GMT Standard Time");
1100 #endif
1101   dt1 = g_date_time_new (tz, 2009, 8, 15, 3, 0, 1);
1102   g_assert (g_date_time_is_daylight_savings (dt1));
1103   g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, 3600);
1104   g_assert_cmpint (g_date_time_get_hour (dt1), ==, 3);
1105
1106   /* add 6 months to clear the DST flag but keep the same time */
1107   dt2 = g_date_time_add_months (dt1, 6);
1108   g_assert (!g_date_time_is_daylight_savings (dt2));
1109   g_assert_cmpint (g_date_time_get_utc_offset (dt2) / G_USEC_PER_SEC, ==, 0);
1110   g_assert_cmpint (g_date_time_get_hour (dt2), ==, 3);
1111
1112   g_date_time_unref (dt2);
1113   g_date_time_unref (dt1);
1114
1115   /* now do the reverse: start with a non-DST state and move to DST */
1116   dt1 = g_date_time_new (tz, 2009, 2, 15, 2, 0, 1);
1117   g_assert (!g_date_time_is_daylight_savings (dt1));
1118   g_assert_cmpint (g_date_time_get_hour (dt1), ==, 2);
1119
1120   dt2 = g_date_time_add_months (dt1, 6);
1121   g_assert (g_date_time_is_daylight_savings (dt2));
1122   g_assert_cmpint (g_date_time_get_hour (dt2), ==, 2);
1123
1124   g_date_time_unref (dt2);
1125   g_date_time_unref (dt1);
1126   g_time_zone_unref (tz);
1127 }
1128
1129 static inline gboolean
1130 is_leap_year (gint year)
1131 {
1132   g_assert (1 <= year && year <= 9999);
1133
1134   return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
1135 }
1136
1137 static inline gint
1138 days_in_month (gint year, gint month)
1139 {
1140   const gint table[2][13] = {
1141     {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
1142     {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
1143   };
1144
1145   g_assert (1 <= month && month <= 12);
1146
1147   return table[is_leap_year (year)][month];
1148 }
1149
1150 static void
1151 test_all_dates (void)
1152 {
1153   gint year, month, day;
1154   GTimeZone *timezone;
1155   gint64 unix_time;
1156   gint day_of_year;
1157   gint week_year;
1158   gint week_num;
1159   gint weekday;
1160
1161   /* save some time by hanging on to this. */
1162   timezone = g_time_zone_new_utc ();
1163
1164   unix_time = G_GINT64_CONSTANT(-62135596800);
1165
1166   /* 0001-01-01 is 0001-W01-1 */
1167   week_year = 1;
1168   week_num = 1;
1169   weekday = 1;
1170
1171
1172   /* The calendar makes a full cycle every 400 years, so we could
1173    * theoretically just test years 1 through 400.  That assumes that our
1174    * software has no bugs, so probably we should just test them all. :)
1175    */
1176   for (year = 1; year <= 9999; year++)
1177     {
1178       day_of_year = 1;
1179
1180       for (month = 1; month <= 12; month++)
1181         for (day = 1; day <= days_in_month (year, month); day++)
1182           {
1183             GDateTime *dt;
1184
1185             dt = g_date_time_new (timezone, year, month, day, 0, 0, 0);
1186
1187 #if 0
1188             g_print ("%04d-%02d-%02d = %04d-W%02d-%d = %04d-%03d\n",
1189                      year, month, day,
1190                      week_year, week_num, weekday,
1191                      year, day_of_year);
1192 #endif
1193
1194             /* sanity check */
1195             if G_UNLIKELY (g_date_time_get_year (dt) != year ||
1196                            g_date_time_get_month (dt) != month ||
1197                            g_date_time_get_day_of_month (dt) != day)
1198               g_error ("%04d-%02d-%02d comes out as %04d-%02d-%02d",
1199                        year, month, day,
1200                        g_date_time_get_year (dt),
1201                        g_date_time_get_month (dt),
1202                        g_date_time_get_day_of_month (dt));
1203
1204             if G_UNLIKELY (g_date_time_get_week_numbering_year (dt) != week_year ||
1205                            g_date_time_get_week_of_year (dt) != week_num ||
1206                            g_date_time_get_day_of_week (dt) != weekday)
1207               g_error ("%04d-%02d-%02d should be %04d-W%02d-%d but "
1208                        "comes out as %04d-W%02d-%d", year, month, day,
1209                        week_year, week_num, weekday,
1210                        g_date_time_get_week_numbering_year (dt),
1211                        g_date_time_get_week_of_year (dt),
1212                        g_date_time_get_day_of_week (dt));
1213
1214             if G_UNLIKELY (g_date_time_to_unix (dt) != unix_time)
1215               g_error ("%04d-%02d-%02d 00:00:00 UTC should have unix time %"
1216                        G_GINT64_FORMAT " but comes out as %"G_GINT64_FORMAT,
1217                        year, month, day, unix_time, g_date_time_to_unix (dt));
1218
1219             if G_UNLIKELY (g_date_time_get_day_of_year (dt) != day_of_year)
1220               g_error ("%04d-%02d-%02d should be day of year %d"
1221                        " but comes out as %d", year, month, day,
1222                        day_of_year, g_date_time_get_day_of_year (dt));
1223
1224             if G_UNLIKELY (g_date_time_get_hour (dt) != 0 ||
1225                            g_date_time_get_minute (dt) != 0 ||
1226                            g_date_time_get_seconds (dt) != 0)
1227               g_error ("%04d-%02d-%02d 00:00:00 UTC comes out "
1228                        "as %02d:%02d:%02.6f", year, month, day,
1229                        g_date_time_get_hour (dt),
1230                        g_date_time_get_minute (dt),
1231                        g_date_time_get_seconds (dt));
1232             /* done */
1233
1234             /* add 24 hours to unix time */
1235             unix_time += 24 * 60 * 60;
1236
1237             /* move day of year forward */
1238             day_of_year++;
1239
1240             /* move the week date forward */
1241             if (++weekday == 8)
1242               {
1243                 weekday = 1; /* Sunday -> Monday */
1244
1245                 /* NOTE: year/month/day is the final day of the week we
1246                  * just finished.
1247                  *
1248                  * If we just finished the last week of last year then
1249                  * we are definitely starting the first week of this
1250                  * year.
1251                  *
1252                  * Otherwise, if we're still in this year, but Sunday
1253                  * fell on or after December 28 then December 29, 30, 31
1254                  * could be days within the next year's first year.
1255                  */
1256                 if (year != week_year || (month == 12 && day >= 28))
1257                   {
1258                     /* first week of the new year */
1259                     week_num = 1;
1260                     week_year++;
1261                   }
1262                 else
1263                   week_num++;
1264               }
1265
1266             g_date_time_unref (dt);
1267           }
1268     }
1269
1270   g_time_zone_unref (timezone);
1271 }
1272
1273 static void
1274 test_z (void)
1275 {
1276   GTimeZone *tz;
1277   GDateTime *dt;
1278   gchar *p;
1279
1280   g_test_bug ("642935");
1281
1282   tz = g_time_zone_new ("-08:00");
1283   dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1284
1285   p = g_date_time_format (dt, "%z");
1286   g_assert_cmpstr (p, ==, "-0800");
1287   g_free (p);
1288
1289   p = g_date_time_format (dt, "%:z");
1290   g_assert_cmpstr (p, ==, "-08:00");
1291   g_free (p);
1292
1293   p = g_date_time_format (dt, "%::z");
1294   g_assert_cmpstr (p, ==, "-08:00:00");
1295   g_free (p);
1296
1297   p = g_date_time_format (dt, "%:::z");
1298   g_assert_cmpstr (p, ==, "-08");
1299   g_free (p);
1300
1301   g_date_time_unref (dt);
1302   g_time_zone_unref (tz);
1303
1304   tz = g_time_zone_new ("+00:00");
1305   dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1306   p = g_date_time_format (dt, "%:::z");
1307   g_assert_cmpstr (p, ==, "+00");
1308   g_free (p);
1309   g_date_time_unref (dt);
1310   g_time_zone_unref (tz);
1311
1312   tz = g_time_zone_new ("+08:23");
1313   dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1314   p = g_date_time_format (dt, "%:::z");
1315   g_assert_cmpstr (p, ==, "+08:23");
1316   g_free (p);
1317   g_date_time_unref (dt);
1318   g_time_zone_unref (tz);
1319
1320   tz = g_time_zone_new ("+08:23:45");
1321   dt = g_date_time_new (tz, 1, 1, 1, 0, 0, 0);
1322   p = g_date_time_format (dt, "%:::z");
1323   g_assert_cmpstr (p, ==, "+08:23:45");
1324   g_free (p);
1325   g_date_time_unref (dt);
1326   g_time_zone_unref (tz);
1327 }
1328
1329 static void
1330 test_strftime (void)
1331 {
1332 #ifdef __linux__
1333 #define TEST_FORMAT \
1334   "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 " \
1335   "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 %%"
1336   time_t t;
1337
1338   /* 127997 is prime, 1315005118 is now */
1339   for (t = 0; t < 1315005118; t += 127997)
1340     {
1341       GDateTime *date_time;
1342       gchar c_str[1000];
1343       gchar *dt_str;
1344
1345       date_time = g_date_time_new_from_unix_local (t);
1346       dt_str = g_date_time_format (date_time, TEST_FORMAT);
1347       strftime (c_str, sizeof c_str, TEST_FORMAT, localtime (&t));
1348       g_assert_cmpstr (c_str, ==, dt_str);
1349       g_date_time_unref (date_time);
1350       g_free (dt_str);
1351     }
1352 #endif
1353 }
1354
1355 static void
1356 test_find_interval (void)
1357 {
1358   GTimeZone *tz;
1359   GDateTime *dt;
1360   gint64 u;
1361   gint i1, i2;
1362
1363 #ifdef G_OS_UNIX
1364   tz = g_time_zone_new ("America/Toronto");
1365 #elif defined G_OS_WIN32
1366   tz = g_time_zone_new ("Eastern Standard Time");
1367 #endif
1368   dt = g_date_time_new_utc (2010, 11, 7, 1, 30, 0);
1369   u = g_date_time_to_unix (dt);
1370
1371   i1 = g_time_zone_find_interval (tz, G_TIME_TYPE_STANDARD, u);
1372   i2 = g_time_zone_find_interval (tz, G_TIME_TYPE_DAYLIGHT, u);
1373
1374   g_assert_cmpint (i1, !=, i2);
1375
1376   g_date_time_unref (dt);
1377
1378   dt = g_date_time_new_utc (2010, 3, 14, 2, 0, 0);
1379   u = g_date_time_to_unix (dt);
1380
1381   i1 = g_time_zone_find_interval (tz, G_TIME_TYPE_STANDARD, u);
1382   g_assert_cmpint (i1, ==, -1);
1383
1384   g_date_time_unref (dt);
1385   g_time_zone_unref (tz);
1386 }
1387
1388 static void
1389 test_adjust_time (void)
1390 {
1391   GTimeZone *tz;
1392   GDateTime *dt;
1393   gint64 u, u2;
1394   gint i1, i2;
1395
1396 #ifdef G_OS_UNIX
1397   tz = g_time_zone_new ("America/Toronto");
1398 #elif defined G_OS_WIN32
1399   tz = g_time_zone_new ("Eastern Standard Time");
1400 #endif
1401   dt = g_date_time_new_utc (2010, 11, 7, 1, 30, 0);
1402   u = g_date_time_to_unix (dt);
1403   u2 = u;
1404
1405   i1 = g_time_zone_find_interval (tz, G_TIME_TYPE_DAYLIGHT, u);
1406   i2 = g_time_zone_adjust_time (tz, G_TIME_TYPE_DAYLIGHT, &u2);
1407
1408   g_assert_cmpint (i1, ==, i2);
1409   g_assert (u == u2);
1410
1411   g_date_time_unref (dt);
1412
1413   dt = g_date_time_new_utc (2010, 3, 14, 2, 30, 0);
1414   u2 = g_date_time_to_unix (dt);
1415   g_date_time_unref (dt);
1416
1417   dt = g_date_time_new_utc (2010, 3, 14, 3, 0, 0);
1418   u = g_date_time_to_unix (dt);
1419   g_date_time_unref (dt);
1420
1421   i1 = g_time_zone_adjust_time (tz, G_TIME_TYPE_DAYLIGHT, &u2);
1422   g_assert (u == u2);
1423
1424   g_time_zone_unref (tz);
1425 }
1426
1427 static void
1428 test_no_header (void)
1429 {
1430   GTimeZone *tz;
1431
1432   tz = g_time_zone_new ("blabla");
1433
1434   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "UTC");
1435   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 0);
1436   g_assert (!g_time_zone_is_dst (tz, 0));
1437
1438   g_time_zone_unref (tz);
1439 }
1440
1441 static void
1442 test_posix_parse (void)
1443 {
1444   GTimeZone *tz;
1445   GDateTime *gdt1, *gdt2;
1446
1447   tz = g_time_zone_new ("PST");
1448   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "UTC");
1449   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 0);
1450   g_assert (!g_time_zone_is_dst (tz, 0));
1451   g_time_zone_unref (tz);
1452
1453   tz = g_time_zone_new ("PST8");
1454   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "PST");
1455   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, - 8 * 3600);
1456   g_assert (!g_time_zone_is_dst (tz, 0));
1457   g_time_zone_unref (tz);
1458
1459 /* This fails rules_from_identifier on Unix (though not on Windows)
1460  * but passes anyway because PST8PDT is a zone name.
1461  */
1462   tz = g_time_zone_new ("PST8PDT");
1463   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "PST");
1464   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, - 8 * 3600);
1465   g_assert (!g_time_zone_is_dst (tz, 0));
1466   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "PDT");
1467   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==,- 7 * 3600);
1468   g_assert (g_time_zone_is_dst (tz, 1));
1469   g_time_zone_unref (tz);
1470
1471   tz = g_time_zone_new ("PST8PDT6:32:15");
1472 #ifdef G_OS_WIN32
1473   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "PST");
1474   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, - 8 * 3600);
1475   g_assert (!g_time_zone_is_dst (tz, 0));
1476   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "PDT");
1477   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, - 6 * 3600 - 32 *60 - 15);
1478   g_assert (g_time_zone_is_dst (tz, 1));
1479   gdt1 = g_date_time_new (tz, 2012, 12, 6, 11, 15, 23.0);
1480   gdt2 = g_date_time_new (tz, 2012, 6, 6, 11, 15, 23.0);
1481   g_assert (!g_date_time_is_daylight_savings (gdt1));
1482   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) /  1000000, ==, -28800);
1483   g_assert (g_date_time_is_daylight_savings (gdt2));
1484   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, -23535);
1485   g_date_time_unref (gdt1);
1486   g_date_time_unref (gdt2);
1487 #else
1488   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "UTC");
1489   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 0);
1490   g_assert (!g_time_zone_is_dst (tz, 0));
1491 #endif
1492   g_time_zone_unref (tz);
1493
1494   tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,M10.1.0,M3.3.0");
1495   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
1496   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
1497   g_assert (!g_time_zone_is_dst (tz, 0));
1498   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
1499   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
1500   g_assert (g_time_zone_is_dst (tz, 1));
1501   gdt1 = g_date_time_new (tz, 2012, 3, 18, 0, 15, 23.0);
1502   gdt2 = g_date_time_new (tz, 2012, 3, 18, 3, 15, 23.0);
1503   g_assert (g_date_time_is_daylight_savings (gdt1));
1504   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1505   g_assert (!g_date_time_is_daylight_savings (gdt2));
1506   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1507   g_date_time_unref (gdt1);
1508   g_date_time_unref (gdt2);
1509   gdt1 = g_date_time_new (tz, 2012, 10, 7, 3, 15, 23.0);
1510   gdt2 = g_date_time_new (tz, 2012, 10, 7, 1, 15, 23.0);
1511   g_assert (g_date_time_is_daylight_savings (gdt1));
1512   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1513   g_assert (!g_date_time_is_daylight_savings (gdt2));
1514   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1515   g_date_time_unref (gdt1);
1516   g_date_time_unref (gdt2);
1517   g_time_zone_unref (tz);
1518
1519   tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,280,77");
1520   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
1521   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
1522   g_assert (!g_time_zone_is_dst (tz, 0));
1523   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
1524   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
1525   g_assert (g_time_zone_is_dst (tz, 1));
1526   gdt1 = g_date_time_new (tz, 2012, 3, 18, 0, 15, 23.0);
1527   gdt2 = g_date_time_new (tz, 2012, 3, 18, 3, 15, 23.0);
1528   g_assert (g_date_time_is_daylight_savings (gdt1));
1529   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1530   g_assert (!g_date_time_is_daylight_savings (gdt2));
1531   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1532   g_date_time_unref (gdt1);
1533   g_date_time_unref (gdt2);
1534   gdt1 = g_date_time_new (tz, 2012, 10, 7, 3, 15, 23.0);
1535   gdt2 = g_date_time_new (tz, 2012, 10, 7, 1, 15, 23.0);
1536   g_assert (g_date_time_is_daylight_savings (gdt1));
1537   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1538   g_assert (!g_date_time_is_daylight_savings (gdt2));
1539   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1540   g_date_time_unref (gdt1);
1541   g_date_time_unref (gdt2);
1542   g_time_zone_unref (tz);
1543
1544   tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,J279,J76");
1545   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
1546   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
1547   g_assert (!g_time_zone_is_dst (tz, 0));
1548   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
1549   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
1550   g_assert (g_time_zone_is_dst (tz, 1));
1551   gdt1 = g_date_time_new (tz, 2012, 3, 18, 0, 15, 23.0);
1552   gdt2 = g_date_time_new (tz, 2012, 3, 18, 3, 15, 23.0);
1553   g_assert (g_date_time_is_daylight_savings (gdt1));
1554   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1555   g_assert (!g_date_time_is_daylight_savings (gdt2));
1556   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1557   g_date_time_unref (gdt1);
1558   g_date_time_unref (gdt2);
1559   gdt1 = g_date_time_new (tz, 2012, 10, 7, 3, 15, 23.0);
1560   gdt2 = g_date_time_new (tz, 2012, 10, 7, 1, 15, 23.0);
1561   g_assert (g_date_time_is_daylight_savings (gdt1));
1562   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1563   g_assert (!g_date_time_is_daylight_savings (gdt2));
1564   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1565   g_date_time_unref (gdt1);
1566   g_date_time_unref (gdt2);
1567   g_time_zone_unref (tz);
1568
1569   tz = g_time_zone_new ("NZST-12:00:00NZDT-13:00:00,M10.1.0/07:00,M3.3.0/07:00");
1570   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 0), ==, "NZST");
1571   g_assert_cmpint (g_time_zone_get_offset (tz, 0), ==, 12 * 3600);
1572   g_assert (!g_time_zone_is_dst (tz, 0));
1573   g_assert_cmpstr (g_time_zone_get_abbreviation (tz, 1), ==, "NZDT");
1574   g_assert_cmpint (g_time_zone_get_offset (tz, 1), ==, 13 * 3600);
1575   g_assert (g_time_zone_is_dst (tz, 1));
1576   gdt1 = g_date_time_new (tz, 2012, 3, 18, 5, 15, 23.0);
1577   gdt2 = g_date_time_new (tz, 2012, 3, 18, 8, 15, 23.0);
1578   g_assert (g_date_time_is_daylight_savings (gdt1));
1579   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1580   g_assert (!g_date_time_is_daylight_savings (gdt2));
1581   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1582   g_date_time_unref (gdt1);
1583   g_date_time_unref (gdt2);
1584   gdt1 = g_date_time_new (tz, 2012, 10, 7, 8, 15, 23.0);
1585   gdt2 = g_date_time_new (tz, 2012, 10, 7, 6, 15, 23.0);
1586   g_assert (g_date_time_is_daylight_savings (gdt1));
1587   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1588   g_assert (!g_date_time_is_daylight_savings (gdt2));
1589   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1590   g_date_time_unref (gdt1);
1591   g_date_time_unref (gdt2);
1592   gdt1 = g_date_time_new (tz, 1902, 10, 7, 8, 15, 23.0);
1593   gdt2 = g_date_time_new (tz, 1902, 10, 7, 6, 15, 23.0);
1594   g_assert (!g_date_time_is_daylight_savings (gdt1));
1595   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 43200);
1596   g_assert (!g_date_time_is_daylight_savings (gdt2));
1597   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1598   g_date_time_unref (gdt1);
1599   g_date_time_unref (gdt2);
1600   gdt1 = g_date_time_new (tz, 2142, 10, 7, 8, 15, 23.0);
1601   gdt2 = g_date_time_new (tz, 2142, 10, 7, 6, 15, 23.0);
1602   g_assert (g_date_time_is_daylight_savings (gdt1));
1603   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 46800);
1604   g_assert (!g_date_time_is_daylight_savings (gdt2));
1605   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1606   g_date_time_unref (gdt1);
1607   g_date_time_unref (gdt2);
1608   gdt1 = g_date_time_new (tz, 3212, 10, 7, 8, 15, 23.0);
1609   gdt2 = g_date_time_new (tz, 3212, 10, 7, 6, 15, 23.0);
1610   g_assert (!g_date_time_is_daylight_savings (gdt1));
1611   g_assert_cmpint (g_date_time_get_utc_offset (gdt1) / 1000000, ==, 43200);
1612   g_assert (!g_date_time_is_daylight_savings (gdt2));
1613   g_assert_cmpint (g_date_time_get_utc_offset (gdt2) / 1000000, ==, 43200);
1614   g_date_time_unref (gdt1);
1615   g_date_time_unref (gdt2);
1616   g_time_zone_unref (tz);
1617 }
1618
1619 gint
1620 main (gint   argc,
1621       gchar *argv[])
1622 {
1623   g_test_init (&argc, &argv, NULL);
1624   g_test_bug_base ("http://bugzilla.gnome.org/");
1625
1626   /* GDateTime Tests */
1627
1628   g_test_add_func ("/GDateTime/invalid", test_GDateTime_invalid);
1629   g_test_add_func ("/GDateTime/add_days", test_GDateTime_add_days);
1630   g_test_add_func ("/GDateTime/add_full", test_GDateTime_add_full);
1631   g_test_add_func ("/GDateTime/add_hours", test_GDateTime_add_hours);
1632   g_test_add_func ("/GDateTime/add_minutes", test_GDateTime_add_minutes);
1633   g_test_add_func ("/GDateTime/add_months", test_GDateTime_add_months);
1634   g_test_add_func ("/GDateTime/add_seconds", test_GDateTime_add_seconds);
1635   g_test_add_func ("/GDateTime/add_weeks", test_GDateTime_add_weeks);
1636   g_test_add_func ("/GDateTime/add_years", test_GDateTime_add_years);
1637   g_test_add_func ("/GDateTime/compare", test_GDateTime_compare);
1638   g_test_add_func ("/GDateTime/diff", test_GDateTime_diff);
1639   g_test_add_func ("/GDateTime/equal", test_GDateTime_equal);
1640   g_test_add_func ("/GDateTime/get_day_of_week", test_GDateTime_get_day_of_week);
1641   g_test_add_func ("/GDateTime/get_day_of_month", test_GDateTime_get_day_of_month);
1642   g_test_add_func ("/GDateTime/get_day_of_year", test_GDateTime_get_day_of_year);
1643   g_test_add_func ("/GDateTime/get_hour", test_GDateTime_get_hour);
1644   g_test_add_func ("/GDateTime/get_microsecond", test_GDateTime_get_microsecond);
1645   g_test_add_func ("/GDateTime/get_minute", test_GDateTime_get_minute);
1646   g_test_add_func ("/GDateTime/get_month", test_GDateTime_get_month);
1647   g_test_add_func ("/GDateTime/get_second", test_GDateTime_get_second);
1648   g_test_add_func ("/GDateTime/get_utc_offset", test_GDateTime_get_utc_offset);
1649   g_test_add_func ("/GDateTime/get_year", test_GDateTime_get_year);
1650   g_test_add_func ("/GDateTime/hash", test_GDateTime_hash);
1651   g_test_add_func ("/GDateTime/new_from_unix", test_GDateTime_new_from_unix);
1652   g_test_add_func ("/GDateTime/new_from_unix_utc", test_GDateTime_new_from_unix_utc);
1653   g_test_add_func ("/GDateTime/new_from_timeval", test_GDateTime_new_from_timeval);
1654   g_test_add_func ("/GDateTime/new_from_timeval_utc", test_GDateTime_new_from_timeval_utc);
1655   g_test_add_func ("/GDateTime/new_full", test_GDateTime_new_full);
1656   g_test_add_func ("/GDateTime/now", test_GDateTime_now);
1657   g_test_add_func ("/GDateTime/printf", test_GDateTime_printf);
1658   g_test_add_func ("/GDateTime/non_utf8_printf", test_non_utf8_printf);
1659   g_test_add_func ("/GDateTime/strftime", test_strftime);
1660   g_test_add_func ("/GDateTime/modifiers", test_modifiers);
1661   g_test_add_func ("/GDateTime/to_local", test_GDateTime_to_local);
1662   g_test_add_func ("/GDateTime/to_unix", test_GDateTime_to_unix);
1663   g_test_add_func ("/GDateTime/to_timeval", test_GDateTime_to_timeval);
1664   g_test_add_func ("/GDateTime/to_utc", test_GDateTime_to_utc);
1665   g_test_add_func ("/GDateTime/now_utc", test_GDateTime_now_utc);
1666   g_test_add_func ("/GDateTime/dst", test_GDateTime_dst);
1667   g_test_add_func ("/GDateTime/test_z", test_z);
1668   g_test_add_func ("/GDateTime/test-all-dates", test_all_dates);
1669   g_test_add_func ("/GTimeZone/find-interval", test_find_interval);
1670   g_test_add_func ("/GTimeZone/adjust-time", test_adjust_time);
1671   g_test_add_func ("/GTimeZone/no-header", test_no_header);
1672   g_test_add_func ("/GTimeZone/posix-parse", test_posix_parse);
1673
1674   return g_test_run ();
1675 }