From b7bb942cadfe3d657895af1557b78acc2559947e Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 5 Aug 2018 15:58:14 -0400 Subject: Tweets store their created time C/C++ time handling is awful, pass it on --- src/util.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/util.cpp (limited to 'src/util.cpp') 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 @@ +#include "util.h" + +namespace twitter { + + // Eric S Raymond wrote this + /* struct tm to seconds since Unix epoch */ + time_t timegm(struct tm * t) + { + long year; + time_t result; + #define MONTHSPERYEAR 12 /* months per calendar year */ + static const int cumdays[MONTHSPERYEAR] = + { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; + + /*@ +matchanyintegral @*/ + year = 1900 + t->tm_year + t->tm_mon / MONTHSPERYEAR; + result = (year - 1970) * 365 + cumdays[t->tm_mon % MONTHSPERYEAR]; + result += (year - 1968) / 4; + result -= (year - 1900) / 100; + result += (year - 1600) / 400; + if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0) && + (t->tm_mon % MONTHSPERYEAR) < 2) + result--; + result += t->tm_mday - 1; + result *= 24; + result += t->tm_hour; + result *= 60; + result += t->tm_min; + result *= 60; + result += t->tm_sec; + if (t->tm_isdst == 1) + result -= 3600; + /*@ -matchanyintegral @*/ + return (result); + } + +} -- cgit 1.4.1