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