diff options
Diffstat (limited to 'src/util.cpp')
-rw-r--r-- | src/util.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/util.cpp b/src/util.cpp new file mode 100644 index 0000000..5952138 --- /dev/null +++ b/src/util.cpp | |||
@@ -0,0 +1,37 @@ | |||
1 | #include "util.h" | ||
2 | |||
3 | namespace twitter { | ||
4 | |||
5 | // Eric S Raymond wrote this | ||
6 | /* struct tm to seconds since Unix epoch */ | ||
7 | time_t timegm(struct tm * t) | ||
8 | { | ||
9 | long year; | ||
10 | time_t result; | ||
11 | #define MONTHSPERYEAR 12 /* months per calendar year */ | ||
12 | static const int cumdays[MONTHSPERYEAR] = | ||
13 | { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; | ||
14 | |||
15 | /*@ +matchanyintegral @*/ | ||
16 | year = 1900 + t->tm_year + t->tm_mon / MONTHSPERYEAR; | ||
17 | result = (year - 1970) * 365 + cumdays[t->tm_mon % MONTHSPERYEAR]; | ||
18 | result += (year - 1968) / 4; | ||
19 | result -= (year - 1900) / 100; | ||
20 | result += (year - 1600) / 400; | ||
21 | if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0) && | ||
22 | (t->tm_mon % MONTHSPERYEAR) < 2) | ||
23 | result--; | ||
24 | result += t->tm_mday - 1; | ||
25 | result *= 24; | ||
26 | result += t->tm_hour; | ||
27 | result *= 60; | ||
28 | result += t->tm_min; | ||
29 | result *= 60; | ||
30 | result += t->tm_sec; | ||
31 | if (t->tm_isdst == 1) | ||
32 | result -= 3600; | ||
33 | /*@ -matchanyintegral @*/ | ||
34 | return (result); | ||
35 | } | ||
36 | |||
37 | } | ||