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