changed indentation to tabs
[opensync.git] / tests / format-tests / check_time.c
blob174f30509ad7ba968250cd247a71cc6a275578cc
1 #include <check.h>
2 #include <glib.h>
3 #include <gmodule.h>
4 #include <opensync/opensync.h>
5 #include <opensync/opensync-time.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdio.h>
10 #include "support.h"
12 // this test assumes Eastern time, sorry.
13 START_TEST (time_timezone_diff)
15 struct tm local;
16 int zonediff_normal, zonediff_day, zonediff_month;
18 // discover localtime zone
19 // time_t t;
20 // localtime_r(&now, &local);
21 // gmtime_r
24 // test normal case
25 local.tm_sec = 0;
26 local.tm_min = 0;
27 local.tm_hour = 0;
28 local.tm_mday = 15;
29 local.tm_mon = 0;
30 local.tm_year = 107;
31 local.tm_wday = 0;
32 local.tm_yday = 0;
33 local.tm_isdst = 0;
35 // t = mktime(&local);
36 // gmtime_r(&t, &utc);
38 zonediff_normal = osync_time_timezone_diff(&local);
41 // test day straddle
42 local.tm_hour = 23;
43 zonediff_day = osync_time_timezone_diff(&local);
46 // test month straddle
47 local.tm_mday = 31;
48 zonediff_month = osync_time_timezone_diff(&local);
50 printf("normal = %d\nday = %d\nmonth = %d\n",
51 zonediff_normal, zonediff_day, zonediff_month);
53 fail_unless(zonediff_normal == zonediff_day && zonediff_day == zonediff_month, NULL);
55 END_TEST
57 static int test_relative2tm(const char *byday, int month, int year,
58 int expected_day, int expected_wday)
60 printf("Test parameters: %s, month: %d, year: %d\n",
61 byday, month, year);
63 struct tm *test = osync_time_relative2tm(byday, month, year);
64 if( !test ) {
65 printf(" Error in osync_time_relative2tm()\n");
66 return expected_day == -1;
68 else {
69 int ret = expected_day == test->tm_mday &&
70 expected_wday == test->tm_wday &&
71 (month-1) == test->tm_mon &&
72 (year-1900) == test->tm_year;
74 printf(" Result: %s", asctime(test));
75 g_free(test);
76 return ret;
80 START_TEST (time_relative2tm)
83 June 2007
84 Su Mo Tu We Th Fr Sa
85 1 2
86 3 4 5 6 7 8 9
87 10 11 12 13 14 15 16
88 17 18 19 20 21 22 23
89 24 25 26 27 28 29 30
92 fail_unless( test_relative2tm("+1MO,SU", 6, 2007, 4, 1), NULL);
93 fail_unless( test_relative2tm("+2TU,SU", 6, 2007, 12, 2), NULL);
94 fail_unless( test_relative2tm("+3WE,SU", 6, 2007, 20, 3), NULL);
95 fail_unless( test_relative2tm("+4TH,SU", 6, 2007, 28, 4), NULL);
96 fail_unless( test_relative2tm("+5SU,FR", 6, 2007, -1, -1), NULL);
98 fail_unless( test_relative2tm("-1MO,SU", 6, 2007, 25, 1), NULL);
99 fail_unless( test_relative2tm("-2TU,SU", 6, 2007, 19, 2), NULL);
100 fail_unless( test_relative2tm("-3WE,SU", 6, 2007, 13, 3), NULL);
101 fail_unless( test_relative2tm("-4TH,SU", 6, 2007, 7, 4), NULL);
102 fail_unless( test_relative2tm("-5FR,SU", 6, 2007, 1, 5), NULL);
103 fail_unless( test_relative2tm("-6FR,SU", 6, 2007, -1, -1), NULL);
105 END_TEST
107 // Returns nonzero if tm structs are substantially equal.
108 // Ignores wday and yday.
109 int tm_equal(struct tm *a, struct tm *b)
111 return a->tm_sec == b->tm_sec &&
112 a->tm_min == b->tm_min &&
113 a->tm_hour == b->tm_hour &&
114 a->tm_mday == b->tm_mday &&
115 a->tm_mon == b->tm_mon &&
116 a->tm_year == b->tm_year &&
117 a->tm_isdst == b->tm_isdst;
120 void test_unix_converter(const struct tm *base, const char *vresult)
122 struct tm tm_first, tm_second, *tm_ptr = NULL;
123 time_t first, second;
124 char *vtime = NULL;
126 // test that osync_time_localtm2unix() behaves like mktime()
127 memcpy(&tm_first, base, sizeof(struct tm));
128 first = osync_time_localtm2unix(&tm_first);
129 tm_first.tm_isdst = -1;
130 second = mktime(&tm_first);
131 fail_unless( first == second, NULL );
133 // test that osync_time_unix2localtm() behaves like localtime()
134 tm_ptr = osync_time_unix2localtm(&first);
135 localtime_r(&first, &tm_second);
136 fail_unless( tm_equal(&tm_first, &tm_second), NULL );
137 fail_unless( tm_equal(tm_ptr, &tm_second), NULL );
138 g_free(tm_ptr);
139 tm_ptr = NULL;
141 // test that osync_time_unix2utctm() behaves like gmtime_r()
142 tm_ptr = osync_time_unix2utctm(&first);
143 gmtime_r(&first, &tm_second);
144 fail_unless( tm_equal(tm_ptr, &tm_second), NULL );
145 g_free(tm_ptr);
146 tm_ptr = NULL;
148 // test that osync_time_utctm2unix() works correctly
149 tm_second.tm_isdst = 0; // make sure incorrect value is handled
150 second = osync_time_utctm2unix(&tm_second);
151 fail_unless( first == second, NULL );
153 // test vtime string converters, in both directions
154 vtime = osync_time_unix2vtime(&first);
155 fail_unless( vtime != NULL, NULL );
156 fail_unless( strcmp(vtime, vresult) == 0, NULL );
157 printf("osync_time_unix2vtime() returned: %s\n", vtime);
158 second = osync_time_vtime2unix(vtime, 0);
159 fail_unless( first == second );
160 g_free(vtime);
163 START_TEST (time_unix_converters)
165 struct tm base;
168 putenv("TZ=America/Montreal");
170 // simple test, no DST
171 base.tm_sec = 0;
172 base.tm_min = 0;
173 base.tm_hour = 0;
174 base.tm_mday = 1;
175 base.tm_mon = 0;
176 base.tm_year = 2007 - 1900;
177 base.tm_isdst = 0;
178 test_unix_converter(&base, "20070101T050000Z");
180 // test dates that may straddle DST
181 base.tm_hour = 1;
182 base.tm_mday = 11;
183 base.tm_mon = 2;
184 test_unix_converter(&base, "20070311T060000Z");
186 base.tm_hour = 4;
187 base.tm_mday = 11;
188 base.tm_mon = 2;
189 test_unix_converter(&base, "20070311T080000Z");
191 // simple test, pure DST
192 base.tm_hour = 1;
193 base.tm_mday = 11;
194 base.tm_mon = 5;
195 test_unix_converter(&base, "20070611T050000Z");
197 END_TEST
199 OSYNC_TESTCASE_START("time")
200 OSYNC_TESTCASE_ADD(time_timezone_diff)
201 OSYNC_TESTCASE_ADD(time_relative2tm)
202 OSYNC_TESTCASE_ADD(time_unix_converters)
203 OSYNC_TESTCASE_END