From 459e4ace93fb3d0ed40b4faed9835f5ed57795e2 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Thu, 20 Aug 2020 15:44:10 -0400 Subject: [PATCH] LibC: Add timegm() timegm() is like mktime() in that it converts a struct tm to a timestamp, but it treats the struct tm as UTC instead of as local time. timegm() is nonstandard, but availabe in both Linux and BSD, and it's a useful function to have. --- Libraries/LibC/time.cpp | 14 ++++++++++++-- Libraries/LibC/time.h | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Libraries/LibC/time.cpp b/Libraries/LibC/time.cpp index fff2336a49..2d22f8c22c 100644 --- a/Libraries/LibC/time.cpp +++ b/Libraries/LibC/time.cpp @@ -93,7 +93,7 @@ static void time_to_tm(struct tm* tm, time_t t) tm->tm_mday += days; } -time_t mktime(struct tm* tm) +static time_t tm_to_time(struct tm* tm, long timezone_adjust) { int days = 0; int seconds = tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec; @@ -107,7 +107,12 @@ time_t mktime(struct tm* tm) ++tm->tm_yday; days += tm->tm_yday; - return days * __seconds_per_day + seconds + timezone; + return days * __seconds_per_day + seconds + timezone_adjust; +} + +time_t mktime(struct tm* tm) +{ + return tm_to_time(tm, timezone); } struct tm* localtime(const time_t* t) @@ -124,6 +129,11 @@ struct tm* localtime_r(const time_t* t, struct tm* tm) return tm; } +time_t timegm(struct tm* tm) +{ + return tm_to_time(tm, 0); +} + struct tm* gmtime(const time_t* t) { static struct tm tm_buf; diff --git a/Libraries/LibC/time.h b/Libraries/LibC/time.h index 4398dbaf3a..b120a03c01 100644 --- a/Libraries/LibC/time.h +++ b/Libraries/LibC/time.h @@ -54,6 +54,7 @@ typedef int64_t time_t; struct tm* localtime(const time_t*); struct tm* gmtime(const time_t*); time_t mktime(struct tm*); +time_t timegm(struct tm*); time_t time(time_t*); char* ctime(const time_t*); void tzset();