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