Browse Source

[core] move timegm() impl inline in sys-time.h

(for systems without timegm())
master
Glenn Strauss 1 year ago
parent
commit
6b6252a3ba
  1. 23
      src/http_date.c
  2. 8
      src/http_date.h
  3. 3
      src/mod_openssl.c
  4. 27
      src/sys-time.h

23
src/http_date.c

@ -276,27 +276,6 @@ http_date_time_to_str (char * const s, const size_t sz, const time_t t)
}
#if !defined(HAVE_TIMEGM) && !defined(_WIN32)
time_t
http_date_timegm (const struct tm * const tm)
{
int y = tm->tm_year + 1900;
int m = tm->tm_mon + 1;
int d = tm->tm_mday;
/* days_from_civil() http://howardhinnant.github.io/date_algorithms.html */
y -= m <= 2;
int era = y / 400;
int yoe = y - era * 400; // [0, 399]
int doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1; // [0, 365]
int doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096]
int days_since_1970 = era * 146097 + doe - 719468;
return 60*(60*(24L*days_since_1970+tm->tm_hour)+tm->tm_min)+tm->tm_sec;
}
#endif
int
http_date_if_modified_since (const char * const ifmod, const uint32_t ifmodlen,
const time_t lmtime)
@ -304,7 +283,7 @@ http_date_if_modified_since (const char * const ifmod, const uint32_t ifmodlen,
struct tm ifmodtm;
if (NULL == http_date_str_to_tm(ifmod, ifmodlen, &ifmodtm))
return 1; /* date parse error */
const time_t ifmtime = http_date_timegm(&ifmodtm);
const time_t ifmtime = timegm(&ifmodtm);
return (lmtime > ifmtime);
/* returns 0 if not modified since,
* returns 1 if modified since or date parse error */

8
src/http_date.h

@ -22,14 +22,6 @@ uint32_t http_date_time_to_str (char *s, size_t sz, time_t t);
int http_date_if_modified_since (const char *ifmod, uint32_t ifmodlen, time_t lmtime);
#ifdef HAVE_TIMEGM
#define http_date_timegm(tm) timegm(tm)
#elif defined(_WIN32)
#define http_date_timegm(tm) _mkgmtime(tm)
#else
time_t http_date_timegm (const struct tm *tm);
#endif
#ifdef __cplusplus
}

3
src/mod_openssl.c

@ -30,6 +30,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include "sys-time.h"
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
@ -1471,7 +1472,7 @@ mod_openssl_asn1_time_to_posix (ASN1_TIME *asn1time)
x.tm_year-= 1900;
x.tm_mon -= 1;
time_t t = http_date_timegm(&x);
time_t t = timegm(&x);
return (t != (time_t)-1) ? t + offset : t;
#else

27
src/sys-time.h

@ -28,4 +28,31 @@
#define gmtime_r(timep,result) ((*(result) = *(gmtime(timep))), (result))
#endif
#ifndef HAVE_TIMEGM
#ifdef _WIN32
#define timegm(tm) _mkgmtime(tm)
#else
__attribute_pure__
static inline time_t
timegm (const struct tm * const tm);
static inline time_t
timegm (const struct tm * const tm)
{
int y = tm->tm_year + 1900;
int m = tm->tm_mon + 1;
int d = tm->tm_mday;
/* days_from_civil() http://howardhinnant.github.io/date_algorithms.html */
y -= m <= 2;
int era = y / 400;
int yoe = y - era * 400; // [0, 399]
int doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1; // [0, 365]
int doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096]
int days_since_1970 = era * 146097 + doe - 719468;
return 60*(60*(24L*days_since_1970+tm->tm_hour)+tm->tm_min)+tm->tm_sec;
}
#endif
#endif
#endif

Loading…
Cancel
Save