Inverse of gmtime: converts struct tm to time_t, assuming the data in tm is UTC rather than local timezone. This implementation returns the number of seconds since 1970-01-01, converted to time_t. @note this code adopted from http://osdir.com/ml/web.wget.patches/2005-07/msg00010.html Subject: A more robust timegm - msg#00010
| 182 | //! http://osdir.com/ml/web.wget.patches/2005-07/msg00010.html |
| 183 | //! Subject: A more robust timegm - msg#00010 |
| 184 | time_t TimeGM(const struct tm* t) { |
| 185 | // Only handles years after 1970 |
| 186 | if (Y_UNLIKELY(t->tm_year < 70)) { |
| 187 | return (time_t)-1; |
| 188 | } |
| 189 | |
| 190 | int days = 365 * (t->tm_year - 70); |
| 191 | // Take into account the leap days between 1970 and YEAR-1 |
| 192 | days += (t->tm_year - 1 - 68) / 4 - ((t->tm_year - 1) / 100) + ((t->tm_year - 1 + 300) / 400); |
| 193 | |
| 194 | if (Y_UNLIKELY(t->tm_mon < 0 || t->tm_mon >= 12)) { |
| 195 | return (time_t)-1; |
| 196 | } |
| 197 | if (IsLeapYear(1900 + t->tm_year)) { |
| 198 | days += MONTH_TO_DAYS_LEAP[t->tm_mon]; |
| 199 | } else { |
| 200 | days += MONTH_TO_DAYS[t->tm_mon]; |
| 201 | } |
| 202 | |
| 203 | days += t->tm_mday - 1; |
| 204 | |
| 205 | unsigned long secs = days * 86400ul + t->tm_hour * 3600 + t->tm_min * 60 + t->tm_sec; |
| 206 | return (time_t)secs; |
| 207 | } |
| 208 | |
| 209 | struct tm* GmTimeR(const time_t* timer, struct tm* tmbuf) { |
| 210 | i64 time = static_cast<i64>(*timer); |
no test coverage detected