mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:17:45 +00:00
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:
parent
cf8ce839da
commit
459e4ace93
2 changed files with 13 additions and 2 deletions
|
@ -93,7 +93,7 @@ static void time_to_tm(struct tm* tm, time_t t)
|
||||||
tm->tm_mday += days;
|
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 days = 0;
|
||||||
int seconds = tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
|
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;
|
++tm->tm_yday;
|
||||||
|
|
||||||
days += 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)
|
struct tm* localtime(const time_t* t)
|
||||||
|
@ -124,6 +129,11 @@ struct tm* localtime_r(const time_t* t, struct tm* tm)
|
||||||
return tm;
|
return tm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
time_t timegm(struct tm* tm)
|
||||||
|
{
|
||||||
|
return tm_to_time(tm, 0);
|
||||||
|
}
|
||||||
|
|
||||||
struct tm* gmtime(const time_t* t)
|
struct tm* gmtime(const time_t* t)
|
||||||
{
|
{
|
||||||
static struct tm tm_buf;
|
static struct tm tm_buf;
|
||||||
|
|
|
@ -54,6 +54,7 @@ typedef int64_t time_t;
|
||||||
struct tm* localtime(const time_t*);
|
struct tm* localtime(const time_t*);
|
||||||
struct tm* gmtime(const time_t*);
|
struct tm* gmtime(const time_t*);
|
||||||
time_t mktime(struct tm*);
|
time_t mktime(struct tm*);
|
||||||
|
time_t timegm(struct tm*);
|
||||||
time_t time(time_t*);
|
time_t time(time_t*);
|
||||||
char* ctime(const time_t*);
|
char* ctime(const time_t*);
|
||||||
void tzset();
|
void tzset();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue