/programs/develop/libraries/kolibri-libc/source/time/asctime.c |
---|
0,0 → 1,27 |
#include <errno.h> |
#include <stdlib.h> |
#include <time.h> |
#include <stdio.h> |
const char *wday_str[7]={"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; |
const char *mon_str[12]={"Jan", "Feb", "Mar", "Ap", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
#pragma GCC push_options |
#pragma GCC optimize("O0") |
char *asctime(const struct tm *tm){ |
static char time_str[30]; |
if(tm->tm_wday>7 || tm->tm_wday<1 || tm->tm_mon<1 || tm->tm_mon>12){ |
errno = EINVAL; |
return NULL; |
} |
snprintf(time_str, 26, "%.3s %.3s%3d %2d:%2d:%2d %d\n", |
wday_str[tm->tm_wday-1], |
mon_str[tm->tm_mon-1], |
tm->tm_mday, tm->tm_hour, |
tm->tm_min, tm->tm_sec, |
1900 + tm->tm_year |
); |
return time_str; |
} |
#pragma GCC pop_options |
/programs/develop/libraries/kolibri-libc/source/time/difftime.c |
---|
0,0 → 1,6 |
#include <time.h> |
double difftime (time_t end, time_t beginning) |
{ |
return end - beginning; |
} |
/programs/develop/libraries/kolibri-libc/source/time/localtime.c |
---|
0,0 → 1,108 |
#include <time.h> |
#include <sys/ksys.h> |
#include <ctype.h> |
#include <errno.h> |
#include <limits.h> |
#define LEAPOCH (946684800LL + 86400*(31+29)) |
#define DAYS_PER_400Y (365*400 + 97) |
#define DAYS_PER_100Y (365*100 + 24) |
#define DAYS_PER_4Y (365*4 + 1) |
static int __secs_to_tm(long long t, struct tm *tm) |
{ |
long long days, secs, years; |
int remdays, remsecs, remyears; |
int qc_cycles, c_cycles, q_cycles; |
int months; |
int wday, yday, leap; |
static const char days_in_month[] = {31,30,31,30,31,31,30,31,30,31,31,29}; |
// Reject time_t values whose year would overflow int |
if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL) |
return -1; |
secs = t - LEAPOCH; |
days = secs / 86400; |
remsecs = secs % 86400; |
if (remsecs < 0) { |
remsecs += 86400; |
days--; |
} |
wday = (3+days)%7; |
if (wday < 0) wday += 7; |
qc_cycles = days / DAYS_PER_400Y; |
remdays = days % DAYS_PER_400Y; |
if (remdays < 0) { |
remdays += DAYS_PER_400Y; |
qc_cycles--; |
} |
c_cycles = remdays / DAYS_PER_100Y; |
if (c_cycles == 4) c_cycles--; |
remdays -= c_cycles * DAYS_PER_100Y; |
q_cycles = remdays / DAYS_PER_4Y; |
if (q_cycles == 25) q_cycles--; |
remdays -= q_cycles * DAYS_PER_4Y; |
remyears = remdays / 365; |
if (remyears == 4) remyears--; |
remdays -= remyears * 365; |
leap = !remyears && (q_cycles || !c_cycles); |
yday = remdays + 31 + 28 + leap; |
if (yday >= 365+leap) yday -= 365+leap; |
years = remyears + 4*q_cycles + 100*c_cycles + 400LL*qc_cycles; |
for (months=0; days_in_month[months] <= remdays; months++) |
remdays -= days_in_month[months]; |
if (months >= 10) { |
months -= 12; |
years++; |
} |
if (years+100 > INT_MAX || years+100 < INT_MIN) |
return -1; |
tm->tm_year = years + 100; |
tm->tm_mon = months + 2; |
tm->tm_mday = remdays + 1; |
tm->tm_wday = wday; |
tm->tm_yday = yday; |
tm->tm_hour = remsecs / 3600; |
tm->tm_min = remsecs / 60 % 60; |
tm->tm_sec = remsecs % 60; |
return 0; |
} |
struct tm *__localtime_r(const time_t *restrict t, struct tm *restrict tm) |
{ |
// Reject time_t values whose year would overflow int because |
// __secs_to_zone cannot safely handle them. |
if (*t < INT_MIN * 31622400LL || *t > INT_MAX * 31622400LL) { |
errno = EOVERFLOW; |
return NULL; |
} |
//__secs_to_zone(*t, 0, &tm->tm_isdst, &tm->__tm_gmtoff, 0, &tm->__tm_zone); |
if (__secs_to_tm((long long)*t,tm) < 0) { |
errno = EOVERFLOW; |
return NULL; |
} |
return tm; |
} |
struct tm * localtime (const time_t * timer) |
{ |
static struct tm tm; |
return __localtime_r(timer, &tm); |
} |
/programs/develop/libraries/kolibri-libc/source/time/mktime.c |
---|
0,0 → 1,23 |
#include <time.h> |
time_t mktime (struct tm * timeptr) |
{ |
int utcdiff = -3; |
const int mon_days [] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
unsigned long int tyears, tdays, leaps, utc_hrs; |
int i; |
tyears = timeptr->tm_year - 70 ;// tm->tm_year is from 1900. |
leaps = (tyears + 2) / 4; // no of next two lines until year 2100. |
i = (timeptr->tm_year - 100) / 100; |
leaps -= ( (i/4)*3 + i%4 ); |
tdays = 0; |
for (i=0; i < timeptr->tm_mon; i++) tdays += mon_days[i]; |
tdays += timeptr->tm_mday-1; // days of month passed. |
tdays = tdays + (tyears * 365) + leaps; |
utc_hrs = timeptr->tm_hour + utcdiff; // for your time zone. |
return (tdays * 86400) + (utc_hrs * 3600) + (timeptr->tm_min * 60) + timeptr->tm_sec; |
} |
/programs/develop/libraries/kolibri-libc/source/time/time.c |
---|
0,0 → 1,34 |
#include <time.h> |
#include <sys/ksys.h> |
static struct tm __buffertime; |
time_t time(time_t *timer){ |
int kos_date, kos_time; |
kos_date = _ksys_get_date(); |
kos_time = _ksys_get_clock(); |
int bcd_day = (kos_date >> 16); |
int bcd_mon = ((kos_date & 0xFF00) >> 8); |
int bcd_year = (kos_date & 0xFF); |
__buffertime.tm_mday = ((bcd_day & 0xF0)>>4)*10 + (bcd_day & 0x0F); |
__buffertime.tm_mon = ((bcd_mon & 0xF0)>>4)*10 + (bcd_mon & 0x0F) - 1; |
__buffertime.tm_year = ((bcd_year & 0xF0)>>4)*10 + (bcd_year & 0x0F) + 100; |
__buffertime.tm_wday = __buffertime.tm_yday = __buffertime.tm_isdst = -1; /* temporary */ |
int bcd_sec = (kos_time >> 16); |
int bcd_min = ((kos_time & 0xFF00) >> 8); |
int bcd_hour = (kos_time & 0xFF); |
__buffertime.tm_sec = ((bcd_sec & 0xF0)>>4)*10 + (bcd_sec & 0x0F); |
__buffertime.tm_min = ((bcd_min & 0xF0)>>4)*10 + (bcd_min & 0x0F); |
__buffertime.tm_hour = ((bcd_hour & 0xF0)>>4)*10 + (bcd_hour & 0x0F); |
time_t ret = mktime(&__buffertime); |
if(timer){ |
*timer=ret; |
} |
return ret; |
} |