3 * - By John Hodge (thePowersGang)
6 * - POSIX/stdc time functions
14 #define UNIX_TO_2K ((30*365*3600*24) + (7*3600*24)) //Normal years + leap years
18 return _SysTimestamp();
21 time_t mktime(struct tm *timeptr)
23 time_t ret = seconds_since_y2k(
24 timeptr->tm_year - 2000,
31 if( ret == 0 && errno ) {
39 time_t time(time_t *t)
41 time_t ret = _SysTimestamp() / 1000;
47 static struct tm static_tm;
49 struct tm *localtime(const time_t *timer)
51 return localtime_r(timer, &static_tm);
54 struct tm *localtime_r(const time_t *timer, struct tm *ret)
56 // Hours, Mins, Seconds
57 int64_t days = get_days_since_y2k(*timer, &ret->tm_hour, &ret->tm_min, &ret->tm_sec);
60 ret->tm_wday = (days + 6) % 7; // Sun = 0, 1 Jan 2000 was Sat (6)
62 // Year and Day of Year
64 ret->tm_year = 2000 + get_years_since_y2k(days, &is_ly, &ret->tm_yday);
66 // Month and Day of Month
67 get_month_day(ret->tm_yday, is_ly, &ret->tm_mon, &ret->tm_mday);
69 ret->tm_isdst = 0; // Fuck DST
74 static inline size_t MIN(size_t a, size_t b) { return a < b ? a : b; }
75 static size_t _puts(char * restrict s, size_t maxsize, size_t ofs, const char *str, size_t len)
77 if( s && ofs < maxsize ) {
78 size_t addlen = MIN(len, maxsize-1-ofs);
79 memcpy(s+ofs, str, addlen);
84 size_t strftime(char*restrict s, size_t maxsize, const char*restrict format, const struct tm*restrict timeptr)
90 const char *restrict start = format;
91 while( *format && *format != '%' )
94 ofs += _puts(s, maxsize, ofs, start, format-start);
100 case 0: format--; break;
101 case '%': ofs += _puts(s, maxsize, ofs, format-1, 1); break;
102 case 'd': // The day of the month as a decimal number (range 01 to 31).
104 char tmp[2] = {'0','0'};
105 tmp[0] += (timeptr->tm_mday / 10) % 10;
106 tmp[1] += timeptr->tm_mday % 10;
107 ofs += _puts(s, maxsize, ofs, tmp, 2);
111 _SysDebug("TODO: strftime('...%%%c...')", format[-1]);