From 2c164844f2e2116e1a315f235f56bb2f937dc212 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sat, 23 Mar 2013 02:41:28 +0100 Subject: [PATCH] Add mktime(3). --- libc/Makefile | 2 ++ libc/include/time.h | 2 ++ libc/time/gmtime_r.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++ libc/time/mktime.cpp | 31 ++++++++++++++++++++++++++ libc/time/timegm.cpp | 28 +++++++++++++++++++++++ 5 files changed, 113 insertions(+) create mode 100644 libc/time/mktime.cpp create mode 100644 libc/time/timegm.cpp diff --git a/libc/Makefile b/libc/Makefile index f8cd2c26..bd4467a3 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -136,8 +136,10 @@ time/gmtime.o \ time/gmtime_r.o \ time/localtime.o \ time/localtime_r.o \ +time/mktime.o \ timespec.o \ time/strftime.o \ +time/timegm.o \ ungetc.o \ vfscanf.o \ vsscanf.o \ diff --git a/libc/include/time.h b/libc/include/time.h index 42ce529c..c9dff34f 100644 --- a/libc/include/time.h +++ b/libc/include/time.h @@ -105,6 +105,8 @@ int timer_settime(timer_t, int, const struct itimerspec* __restrict, struct itimerspec* __restrict); void tzset(void); +/* TODO: This is some _MISC_SOURCE thing according to GNU, but I like it. */ +time_t timegm(struct tm*); #if defined(_SORTIX_SOURCE) int clock_gettimeres(clockid_t, struct timespec*, struct timespec*); diff --git a/libc/time/gmtime_r.cpp b/libc/time/gmtime_r.cpp index 9948b675..bbe76c6b 100644 --- a/libc/time/gmtime_r.cpp +++ b/libc/time/gmtime_r.cpp @@ -240,3 +240,53 @@ extern "C" struct tm* gmtime_r(const time_t* time_ptr, struct tm* ret) return ret; } + +extern "C" time_t timegm(struct tm* tm) +{ + time_t year = tm->tm_year + 1900; + time_t month = tm->tm_mon; + time_t day = tm->tm_mday - 1; + time_t hour = tm->tm_hour; + time_t minute = tm->tm_min; + time_t second = tm->tm_sec; + + time_t ret = 0; + for ( time_t y = 1970; y < year; y++ ) + { + time_t year_leaps = leap_seconds_in_year(y); + time_t year_days = days_in_year(y); + time_t year_seconds = year_days * 24 * 60 * 60 + year_leaps; + ret += year_seconds; + } + + int month_days_list[12] = + { + DAYS_JANUARY, + DAYS_FEBRUARY + (is_leap_year(year) ? 1 : 0), + DAYS_MARCH, + DAYS_APRIL, + DAYS_MAY, + DAYS_JUNE, + DAYS_JULY, + DAYS_AUGUST, + DAYS_SEPTEMBER, + DAYS_OCTOBER, + DAYS_NOVEMBER, + DAYS_DECEMBER, + }; + + for ( uint8_t m = 0; m < month; m++ ) + { + int month_leaps = get_leap_second(year, m); + int month_days = month_days_list[m]; + int month_seconds = month_days * 24 * 60 * 60 + month_leaps; + ret += month_seconds; + } + + ret += (time_t) day * 24 * 60 * 60; + ret += (time_t) hour * 60 * 60; + ret += (time_t) minute * 60; + ret += (time_t) second * 1; + + return ret; +} diff --git a/libc/time/mktime.cpp b/libc/time/mktime.cpp new file mode 100644 index 00000000..e1477184 --- /dev/null +++ b/libc/time/mktime.cpp @@ -0,0 +1,31 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + time/mktime.cpp + Create time stamp from broken time time format. + +*******************************************************************************/ + +#include +#include + +extern "C" time_t mktime(struct tm* tm) +{ + return timegm(tm); +} diff --git a/libc/time/timegm.cpp b/libc/time/timegm.cpp new file mode 100644 index 00000000..388d34d6 --- /dev/null +++ b/libc/time/timegm.cpp @@ -0,0 +1,28 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + time/timegm.cpp + Create time stamp from broken time time format. + +*******************************************************************************/ + +#include +#include + +// Note: timegm has been moved to gmtime_r.cpp because it made more sense there.