Import Upstream version 20180207
[hcoop/debian/mlton.git] / runtime / basis / System / Date.c
1 #include "platform.h"
2
3 static struct tm Date_tmIn;
4 static struct tm *Date_tmOut;
5
6 C_Int_t Date_Tm_getHour(void) { return Date_tmOut->tm_hour; }
7 C_Int_t Date_Tm_getIsDst(void) { return Date_tmOut->tm_isdst; }
8 C_Int_t Date_Tm_getMDay(void) { return Date_tmOut->tm_mday; }
9 C_Int_t Date_Tm_getMin(void) { return Date_tmOut->tm_min; }
10 C_Int_t Date_Tm_getMon(void) { return Date_tmOut->tm_mon; }
11 C_Int_t Date_Tm_getSec(void) { return Date_tmOut->tm_sec; }
12 C_Int_t Date_Tm_getWDay(void) { return Date_tmOut->tm_wday; }
13 C_Int_t Date_Tm_getYDay(void) { return Date_tmOut->tm_yday; }
14 C_Int_t Date_Tm_getYear(void) { return Date_tmOut->tm_year; }
15
16 void Date_Tm_setHour(C_Int_t x) { Date_tmIn.tm_hour = x; }
17 void Date_Tm_setIsDst(C_Int_t x) { Date_tmIn.tm_isdst = x; }
18 void Date_Tm_setMDay(C_Int_t x) { Date_tmIn.tm_mday = x; }
19 void Date_Tm_setMin(C_Int_t x) { Date_tmIn.tm_min = x; }
20 void Date_Tm_setMon(C_Int_t x) { Date_tmIn.tm_mon = x; }
21 void Date_Tm_setSec(C_Int_t x) { Date_tmIn.tm_sec = x; }
22 void Date_Tm_setWDay(C_Int_t x) { Date_tmIn.tm_wday = x; }
23 void Date_Tm_setYDay(C_Int_t x) { Date_tmIn.tm_yday = x; }
24 void Date_Tm_setYear(C_Int_t x) { Date_tmIn.tm_year = x; }
25
26 C_Errno_t(C_Int_t) Date_gmTime(Ref(C_Time_t) p) {
27 Date_tmOut = gmtime((time_t*)p);
28 if (Date_tmOut == NULL) return -1;
29 return 0;
30 }
31
32 /* The idea for Date_localOffset comes from KitV3 src/Runtime/Time.c */
33 C_Double_t Date_localOffset(void) {
34 time_t t1, t2;
35
36 t1 = time(NULL);
37 t2 = mktime(gmtime(&t1));
38 return difftime(t2, t1);
39 }
40
41 C_Errno_t(C_Int_t) Date_localTime(Ref(C_Time_t) p) {
42 Date_tmOut = localtime((time_t*)p);
43 if (Date_tmOut == NULL) return -1;
44 return 0;
45 }
46
47 C_Errno_t(C_Time_t) Date_mkTime(void) {
48 return mktime(&Date_tmIn);
49 }
50
51 C_Size_t Date_strfTime(Array(Char8_t) buf, C_Size_t n, NullString8_t fmt) {
52 return strftime((char*)(buf), n, (const char*)(fmt), &Date_tmIn);
53 }