3 * - By John Hodge (thePowersGang)
6 * - POSIX/stdc time functions
13 #include <stdio.h> // sprintf
15 #define UNIX_TO_2K ((30*365*3600*24) + (7*3600*24)) //Normal years + leap years
19 return _SysTimestamp();
22 time_t mktime(struct tm *timeptr)
24 time_t ret = seconds_since_y2k(
25 timeptr->tm_year - 2000,
32 if( ret == 0 && errno ) {
40 time_t time(time_t *t)
42 time_t ret = _SysTimestamp() / 1000;
48 //! Convert the time structure into a string of form 'Sun Sep 16 01:03:52 1973\n\0'
49 char *asctime(const struct tm *timeptr)
51 static char staticbuf[sizeof("Sun Sep 16 01:03:52 1973\n")];
52 return asctime_r(timeptr, staticbuf);
54 char *asctime_r(const struct tm *timeptr, char *buf)
56 const char *WDAYS[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
57 const char *MONS[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
59 "%s %s %i %02i:%02i:%02i %4i\n",
60 WDAYS[timeptr->tm_wday],
61 MONS[timeptr->tm_mon],
71 //! asctime(localtime(timer))
72 char *ctime(const time_t *timer)
75 localtime_r(timer, &time_buf);
76 return asctime(&time_buf);
78 extern char *ctime_r(const time_t *timer, char *buf)
81 localtime_r(timer, &time_buf);
82 return asctime_r(&time_buf, buf);
85 //! Convert \a timter into UTC
86 struct tm *gmtime(const time_t *timer)
89 return localtime(timer);
91 struct tm *gmtime_r(const time_t *timer, struct tm *result)
93 return localtime_r(timer, result);
96 static struct tm static_tm;
98 struct tm *localtime(const time_t *timer)
100 return localtime_r(timer, &static_tm);
103 struct tm *localtime_r(const time_t *timer, struct tm *ret)
105 // Hours, Mins, Seconds
106 int64_t days = get_days_since_y2k(*timer, &ret->tm_hour, &ret->tm_min, &ret->tm_sec);
109 ret->tm_wday = (days + 6) % 7; // Sun = 0, 1 Jan 2000 was Sat (6)
111 // Year and Day of Year
113 ret->tm_year = 2000 + get_years_since_y2k(days, &is_ly, &ret->tm_yday);
115 // Month and Day of Month
116 get_month_day(ret->tm_yday, is_ly, &ret->tm_mon, &ret->tm_mday);
119 ret->tm_isdst = 0; // Fuck DST
124 static inline size_t MIN(size_t a, size_t b) { return a < b ? a : b; }
125 static size_t _puts(char * restrict s, size_t maxsize, size_t ofs, const char *str, size_t len)
127 if( s && ofs < maxsize ) {
128 size_t addlen = MIN(len, maxsize-1-ofs);
129 memcpy(s+ofs, str, addlen);
134 size_t strftime(char*restrict s, size_t maxsize, const char*restrict format, const struct tm*restrict timeptr)
140 const char *restrict start = format;
141 while( *format && *format != '%' )
143 if( format != start )
144 ofs += _puts(s, maxsize, ofs, start, format-start);
149 // If EOS is hit on a '%', break early
156 ofs += _puts(s, maxsize, ofs, format-1, 1);
158 // The day of the month as a decimal number (range 01 to 31).
161 char tmp[2] = {'0','0'};
162 tmp[0] += (timeptr->tm_mday / 10) % 10;
163 tmp[1] += timeptr->tm_mday % 10;
164 ofs += _puts(s, maxsize, ofs, tmp, 2);
170 char tmp[2] = {'0','0'};
171 tmp[0] += (timeptr->tm_hour / 10) % 10;
172 tmp[1] += timeptr->tm_hour % 10;
173 ofs += _puts(s, maxsize, ofs, tmp, 2);
179 char tmp[2] = {'0','0'};
180 tmp[0] += (timeptr->tm_min / 10) % 10;
181 tmp[1] += timeptr->tm_min % 10;
182 ofs += _puts(s, maxsize, ofs, tmp, 2);
186 _SysDebug("TODO: strftime('...%%%c...')", format[-1]);