// There's no posix function that does a UTC version of mktime, // so annoyingly we need to implement this manually.. static inline int64 mktime_utc (const std::tm& t) noexcept { return 24 * 3600 * (daysFrom1970 (t.tm_year + 1900, t.tm_mon) + (t.tm_mday - 1)) + 3600 * t.tm_hour + 60 * t.tm_min + t.tm_sec; }
static inline int64 daysFrom1970 (int year, int month) noexcept { if (month > 11) { year += month / 12; month %= 12; } else if (month < 0) { const int numYears = (11 - month) / 12; year -= numYears; month += 12 * numYears; } return daysFrom1970 (year) + daysFromJan1 (year, month); }
time_t timegm(tm* t) { int year = t->tm_year + 1900; int month = t->tm_mon; if (month > 11) { year += month / 12; month %= 12; } else if (month < 0) { int years_diff = (-month + 11) / 12; year -= years_diff; month += 12 * years_diff; } month++; int day = t->tm_mday; int dayOfYear = daysFrom1Jan(year, month, day); int daysSinceEpoch = daysFrom1970(year) + dayOfYear; time_t seconds_in_day = 3600 * 24; time_t result = seconds_in_day * daysSinceEpoch + 3600 * t->tm_hour + 60 * t->tm_min + t->tm_sec; return result; }