summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNico Weber <thakis@chromium.org>2020-08-20 15:44:10 -0400
committerAndreas Kling <kling@serenityos.org>2020-08-21 12:11:48 +0200
commit459e4ace93fb3d0ed40b4faed9835f5ed57795e2 (patch)
treee14387ed1dbf8f8800d4d83f1585a57c42b688d9
parentcf8ce839dac3474bf0b1782323fb85822de15d3f (diff)
downloadserenity-459e4ace93fb3d0ed40b4faed9835f5ed57795e2.zip
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.
-rw-r--r--Libraries/LibC/time.cpp14
-rw-r--r--Libraries/LibC/time.h1
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();