3 * - By John Hodge (thePowersGang)
6 * - POSIX/stdc time functions
14 return _SysTimestamp();
17 time_t time(time_t *t)
19 time_t ret = _SysTimestamp() / 1000;
25 static struct tm static_tm;
27 struct tm *localtime(const time_t *timer)
29 struct tm *ret = &static_tm;
31 // TODO: This breaks on negative timestamps
33 int64_t day = *timer / (1000*60*60*24);
34 int64_t iday = *timer % (1000*60*60*24);
36 ret->tm_sec = (iday / 1000) % 60;;
37 ret->tm_min = (iday / (1000*60)) % 60;
38 ret->tm_hour = (iday / (1000*60*60));
42 ret->tm_wday = (day + 6) % 7; // 1 Jan 2000 was a saturday
44 ret->tm_isdst = 0; // Fuck DST
48 static inline size_t MIN(size_t a, size_t b) { return a < b ? a : b; }
49 static size_t _puts(char * restrict s, size_t maxsize, size_t ofs, const char *str, size_t len)
51 if( s && ofs < maxsize ) {
52 size_t addlen = MIN(len, maxsize-1-ofs);
53 memcpy(s+ofs, str, addlen);
58 size_t strftime(char*restrict s, size_t maxsize, const char*restrict format, const struct tm*restrict timeptr)
64 const char *restrict start = format;
65 while( *format && *format != '%' )
68 ofs += _puts(s, maxsize, ofs, start, format-start);
74 case 0: format--; break;
75 case '%': ofs += _puts(s, maxsize, ofs, format-1, 1); break;
76 case 'd': // The day of the month as a decimal number (range 01 to 31).
78 char tmp[2] = {'0','0'};
79 tmp[0] += (timeptr->tm_mday / 10) % 10;
80 tmp[1] += timeptr->tm_mday % 10;
81 ofs += _puts(s, maxsize, ofs, tmp, 2);
85 _SysDebug("TODO: strftime('...%%%c...')", format[-1]);