-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.c
132 lines (97 loc) · 2.44 KB
/
time.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#pragma noroot
#pragma optimize -1
#pragma lint -1
#pragma debug 0x8000
#include <timetool.h>
#include <misctool.h>
#include <intmath.h>
/*
* From Silver Platter.
*
*/
// yyyy-mm-ddThh:mm:ssZ
void tiTimeRec2ISO8601(const TimeRecPtr t, char *str)
{
LongWord secs;
tiPrefRec tiPrefs;
TimeRec tr;
tiPrefs.pCount = 3;
tiGetTimePrefs(&tiPrefs);
secs = ConvSeconds(TimeRec2Secs, 0, (Pointer)t);
secs += tiPrefs.secOffset;
ConvSeconds(secs2TimeRec, secs, (Pointer)&tr);
str[0] = 20;
// yyyy-
Int2Dec(tr.year + 1900, &str[1], 4, 0);
str[5] = '-';
// mm-
Int2Dec(tr.month + 1, &str[6], 2, 0);
str[6] |= 0x10; // convert ' ' -> '0'
str[8] = '-';
// ddT
Int2Dec(tr.day + 1, &str[9], 2, 0);
str[9] |= 0x10; // convert ' ' -> '0'
str[11] = 'T';
// hh:
Int2Dec(tr.hour, &str[12], 2, 0);
str[12] |= 0x10; // convert ' ' -> '0'
str[14] = ':';
// mm:
Int2Dec(tr.minute, &str[15], 2, 0);
str[15] |= 0x10; // convert ' ' -> '0'
str[17] = ':';
// ss:
Int2Dec(tr.second, &str[18], 2, 0);
str[18] |= 0x10; // convert ' ' -> '0'
str[20] = 'Z';
}
void tiTimeRec2GMTString(const TimeRecPtr t, char *str)
{
static const char weekday[] = "Sun,Mon,Tue,Wed,Thu,Fri,Sat,";
static const char month[] = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ";
int i;
LongWord secs;
tiPrefRec tiPrefs;
TimeRec tr;
tiPrefs.pCount = 3;
tiGetTimePrefs(&tiPrefs);
secs = ConvSeconds(TimeRec2Secs, 0, (Pointer)t);
secs += tiPrefs.secOffset;
#if 0
//add daylight savings time...
if (ReadBParam(0x5e) & 0x02 == 0) secs += 3600;
#endif
ConvSeconds(secs2TimeRec, secs, (Pointer)&tr);
str[0] = 29;
i = (tr.weekDay - 1) << 2;
// Day of week
str[1] = weekday[i++];
str[2] = weekday[i++];
str[3] = weekday[i++];
str[4] = weekday[i++];
str[5] = ' ';
// day
Int2Dec(tr.day + 1, &str[6], 2, 0);
str[6] |= 0x10;
str[8] = ' ';
i = tr.month << 2;
str[9] = month[i++];
str[10] = month[i++];
str[11] = month[i++];
str[12] = month[i++];
// year
Int2Dec(tr.year + 1900, &str[13], 4, 0);
str[17] = ' ';
Int2Dec(tr.hour, &str[18], 2, 0);
str[18] |= 0x10;
str[20] = ':';
Int2Dec(tr.minute, &str[21], 2, 0);
str[21] |= 0x10;
str[23] = ':';
Int2Dec(tr.second, &str[24], 2, 0);
str[24] |= 0x10;
str[26] = ' ';
str[27] = 'G';
str[28] = 'M';
str[29] = 'T';
}