diff --git a/mongoose.c b/mongoose.c index d7599397638a664fe2f8f13114a01dd2893f0d6e..b3461bf72ac9a46f2bb9a91b7e4017635a2ff514 100644 --- a/mongoose.c +++ b/mongoose.c @@ -780,6 +780,41 @@ double cs_time(void) { #endif /* _WIN32 */ return now; } + +double cs_timegm(const struct tm *tm) { + /* Month-to-day offset for non-leap-years. */ + static const int month_day[12] = {0, 31, 59, 90, 120, 151, + 181, 212, 243, 273, 304, 334}; + + /* Most of the calculation is easy; leap years are the main difficulty. */ + int month = tm->tm_mon % 12; + int year = tm->tm_year + tm->tm_mon / 12; + int year_for_leap; + int64_t rt; + + if (month < 0) { /* Negative values % 12 are still negative. */ + month += 12; + --year; + } + + /* This is the number of Februaries since 1900. */ + year_for_leap = (month > 1) ? year + 1 : year; + + rt = + tm->tm_sec /* Seconds */ + + + 60 * + (tm->tm_min /* Minute = 60 seconds */ + + + 60 * (tm->tm_hour /* Hour = 60 minutes */ + + + 24 * (month_day[month] + tm->tm_mday - 1 /* Day = 24 hours */ + + 365 * (year - 70) /* Year = 365 days */ + + (year_for_leap - 69) / 4 /* Every 4 years is leap... */ + - (year_for_leap - 1) / 100 /* Except centuries... */ + + (year_for_leap + 299) / 400))); /* Except 400s. */ + return rt < 0 ? -1 : (double) rt; +} #ifdef MG_MODULE_LINES #line 1 "common/cs_endian.h" #endif diff --git a/mongoose.h b/mongoose.h index 78482c2e787a8ad7fc75c1556ba6b3346c72afb3..2822d9bc2fe8ba207664d86353a459ebf2d74853 100644 --- a/mongoose.h +++ b/mongoose.h @@ -1797,6 +1797,8 @@ void cs_hmac_sha1(const unsigned char *key, size_t key_len, #ifndef CS_COMMON_CS_TIME_H_ #define CS_COMMON_CS_TIME_H_ +#include <time.h> + /* Amalgamated: #include "common/platform.h" */ #ifdef __cplusplus @@ -1806,6 +1808,12 @@ extern "C" { /* Sub-second granularity time(). */ double cs_time(void); +/* + * Similar to (non-standard) timegm, converts broken-down time into the number + * of seconds since Unix Epoch. + */ +double cs_timegm(const struct tm *tm); + #ifdef __cplusplus } #endif /* __cplusplus */