Add mktime(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2013-03-23 02:41:28 +01:00
parent 519b054a24
commit 2c164844f2
5 changed files with 113 additions and 0 deletions

View File

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

View File

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

View File

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

31
libc/time/mktime.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
time/mktime.cpp
Create time stamp from broken time time format.
*******************************************************************************/
#include <stdlib.h>
#include <time.h>
extern "C" time_t mktime(struct tm* tm)
{
return timegm(tm);
}

28
libc/time/timegm.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
time/timegm.cpp
Create time stamp from broken time time format.
*******************************************************************************/
#include <stdlib.h>
#include <time.h>
// Note: timegm has been moved to gmtime_r.cpp because it made more sense there.