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.
This commit is contained in:
Nico Weber 2020-08-20 15:44:10 -04:00 committed by Andreas Kling
parent cf8ce839da
commit 459e4ace93
Notes: sideshowbarker 2024-07-19 03:22:50 +09:00
2 changed files with 13 additions and 2 deletions
Libraries/LibC

View file

@ -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;

View file

@ -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();