Usermode/libc - C++ wrapping in headers, time rework
[tpg/acess2.git] / Usermode / Libraries / libc.so_src / time.c
index 412475b..330b6a7 100644 (file)
@@ -8,12 +8,34 @@
 #include <time.h>
 #include <acess/sys.h>
 #include <string.h>
+#include "timeconv.h"
+#include <errno.h>
+
+#define UNIX_TO_2K     ((30*365*3600*24) + (7*3600*24))        //Normal years + leap years
 
 clock_t clock(void)
 {
        return _SysTimestamp();
 }
 
+time_t mktime(struct tm *timeptr)
+{
+       time_t ret = seconds_since_y2k(
+               timeptr->tm_year - 2000,
+               timeptr->tm_mon,
+               timeptr->tm_mday,
+               timeptr->tm_hour,
+               timeptr->tm_min,
+               timeptr->tm_sec
+               );
+       if( ret == 0 && errno ) {
+               // Bad date
+       }
+       ret += UNIX_TO_2K;
+       
+       return ret;
+}
+
 time_t time(time_t *t)
 {
        time_t ret = _SysTimestamp() / 1000;
@@ -26,22 +48,26 @@ static struct tm    static_tm;
 
 struct tm *localtime(const time_t *timer)
 {
-       struct tm *ret = &static_tm;
+       return localtime_r(timer, &static_tm);
 
-       // TODO: This breaks on negative timestamps
-
-       int64_t day = *timer / (1000*60*60*24);
-       int64_t iday = *timer % (1000*60*60*24);        
+}
+struct tm *localtime_r(const time_t *timer, struct tm *ret)
+{
+       // Hours, Mins, Seconds
+       int64_t days = get_days_since_y2k(*timer, &ret->tm_hour, &ret->tm_min, &ret->tm_sec);
+       
+       // Week day
+       ret->tm_wday = (days + 6) % 7;  // Sun = 0, 1 Jan 2000 was Sat (6)
+       
+       // Year and Day of Year
+       bool    is_ly;
+       ret->tm_year = 2000 + get_years_since_y2k(days, &is_ly, &ret->tm_yday);
 
-       ret->tm_sec = (iday / 1000) % 60;;
-       ret->tm_min = (iday / (1000*60)) % 60;
-       ret->tm_hour = (iday / (1000*60*60));
-       ret->tm_year = 0;
-       ret->tm_mon = 0;
-       ret->tm_mday = 0;
-       ret->tm_wday = (day + 6) % 7;   // 1 Jan 2000 was a saturday
-       ret->tm_yday = 0;
+       // Month and Day of Month
+       get_month_day(ret->tm_yday, is_ly, &ret->tm_mon, &ret->tm_mday);
+       
        ret->tm_isdst = 0;      // Fuck DST
+       
        return ret;
 }
 

UCC git Repository :: git.ucc.asn.au