4 #include <opensync/opensync.h>
5 #include <opensync/opensync-time.h>
12 // this test assumes Eastern time, sorry.
13 START_TEST (time_timezone_diff
)
16 int zonediff_normal
, zonediff_day
, zonediff_month
;
18 // discover localtime zone
20 // localtime_r(&now, &local);
35 // t = mktime(&local);
36 // gmtime_r(&t, &utc);
38 zonediff_normal
= osync_time_timezone_diff(&local
);
43 zonediff_day
= osync_time_timezone_diff(&local
);
46 // test month straddle
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
);
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",
63 struct tm
*test
= osync_time_relative2tm(byday
, month
, year
);
65 printf(" Error in osync_time_relative2tm()\n");
66 return expected_day
== -1;
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
));
80 START_TEST (time_relative2tm
)
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
);
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
;
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
);
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
);
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
);
163 START_TEST (time_unix_converters
)
168 putenv("TZ=America/Montreal");
170 // simple test, no DST
176 base
.tm_year
= 2007 - 1900;
178 test_unix_converter(&base
, "20070101T050000Z");
180 // test dates that may straddle DST
184 test_unix_converter(&base
, "20070311T060000Z");
189 test_unix_converter(&base
, "20070311T080000Z");
191 // simple test, pure DST
195 test_unix_converter(&base
, "20070611T050000Z");
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
)