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