1 | #include <iostream> |
2 | #include <cstring> |
3 | |
4 | #include <Poco/Exception.h> |
5 | |
6 | #include <common/DateLUT.h> |
7 | |
8 | |
9 | static std::string toString(time_t Value) |
10 | { |
11 | struct tm tm; |
12 | char buf[96]; |
13 | |
14 | localtime_r(&Value, &tm); |
15 | snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02d" , |
16 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); |
17 | |
18 | return buf; |
19 | } |
20 | |
21 | static time_t orderedIdentifierToDate(unsigned value) |
22 | { |
23 | struct tm tm; |
24 | |
25 | memset(&tm, 0, sizeof(tm)); |
26 | |
27 | tm.tm_year = value / 10000 - 1900; |
28 | tm.tm_mon = (value % 10000) / 100 - 1; |
29 | tm.tm_mday = value % 100; |
30 | tm.tm_isdst = -1; |
31 | |
32 | return mktime(&tm); |
33 | } |
34 | |
35 | |
36 | void loop(time_t begin, time_t end, int step) |
37 | { |
38 | const auto & date_lut = DateLUT::instance(); |
39 | |
40 | for (time_t t = begin; t < end; t += step) |
41 | { |
42 | time_t t2 = date_lut.makeDateTime(date_lut.toYear(t), date_lut.toMonth(t), date_lut.toDayOfMonth(t), |
43 | date_lut.toHour(t), date_lut.toMinute(t), date_lut.toSecond(t)); |
44 | |
45 | std::string s1 = toString(t); |
46 | std::string s2 = toString(t2); |
47 | |
48 | std::cerr << s1 << ", " << s2 << std::endl; |
49 | |
50 | if (s1 != s2) |
51 | throw Poco::Exception("Test failed." ); |
52 | } |
53 | } |
54 | |
55 | |
56 | int main(int argc, char ** argv) |
57 | { |
58 | loop(orderedIdentifierToDate(20101031), orderedIdentifierToDate(20101101), 15 * 60); |
59 | loop(orderedIdentifierToDate(20100328), orderedIdentifierToDate(20100330), 15 * 60); |
60 | |
61 | return 0; |
62 | } |
63 | |