-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClock.hxx
381 lines (318 loc) · 10.3 KB
/
Clock.hxx
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright Kabuki Starship� <kabukistarship.com>.
#include "Clock.hpp"
#if SEAM >= SCRIPT2_CLOCK
//
#include <ctime>
#include "Uniprinter.hpp"
#if SEAM == SCRIPT2_CLOCK
#include "_Debug.hxx"
#else
#include "_Release.hxx"
#endif
using namespace _;
namespace _ {
const ISB* ClockLastDayOfMonth() {
static const ISB kMonthDayOfYear[12] = {31, 59, 90, 120, 151, 181,
212, 243, 273, 304, 334, 365};
return kMonthDayOfYear;
}
const ISB* ClockLastDayOfMonthLeapYear() {
static const ISB kMonthDayOfYearLeapYear[12] = {31, 60, 91, 121, 152, 182,
213, 244, 274, 305, 335, 366};
return kMonthDayOfYearLeapYear;
}
ISN MonthByDay(ISN day, ISN year) {
const ISB *cursor, *stop;
if (day < 1) return 0;
if (year & 3) { // 3 = 0b'11 which is a much faster way to do modulo 4.
cursor = ClockLastDayOfMonthLeapYear();
} else {
cursor = ClockLastDayOfMonth();
}
stop = cursor + 11;
while (cursor <= stop)
if (day < *cursor++) return (ISN)(stop - cursor);
return 0;
}
ISB ClockEpoch() { return AClockEpochInit; }
AClock* ClockInit(AClock& clock, ISC t) { return TClockInit<ISC>(clock, t); }
AClock* ClockInit(AClock& clock, ISD t) { return TClockInit<ISD>(clock, t); }
TMD& StopwatchInit(TMD& tss, ISC t, IUC ticks) {
tss.seconds = t;
tss.ticks = ticks;
return tss;
}
AClock* ClockInit(AClock& clock) {
time_t t;
time(&t);
return TClockInit<time_t>(clock, t);
}
void ClockEpochUpdate() {
// RoomLock();
// cClockEpochInit += 10;
// RoomUnlock();
}
ISD ClockNow() {
time_t t;
time(&t);
if (t > SecondsPerEpoch) ClockEpochUpdate();
return ISD(t);
}
ISC ClockSeconds(AClock& clock) {
return (clock.year - AClockEpochInit) * SecondsPerYear +
(clock.day - 1) * cSecondsPerDay + clock.hour * SecondsPerHour +
clock.minute * SecondsPerMinute + clock.second;
}
ISC ClockISC(AClock& clock) { return ClockSeconds(clock); }
ISD ClockISD(AClock& clock) { return ISD(ClockSeconds(clock)); }
ISN ClockMonthDayCount(ISC t) {
TClock<ISC> date(t);
static const CHA days_per_month[12] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
AClock& clock = date.clock;
if ((clock.year & 3) && (clock.month == 4)) {
// Then it's a leap year and April:
return 29;
}
return days_per_month[clock.month];
}
ISN ClockMonthDayCount(ISN month, ISN year) {
if (month < 1) return 0;
if (month > 12) return 0;
static const CHA days_per_month[12] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
if ((year & 0xC) && (month == 4)) // Then it's a leap year and April:
return 29;
return days_per_month[month];
}
const CHA* ClockWeekDay(ISN day_number) {
static const CHA* days[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
static const CHA cInvalidText[] = "Invalid\0";
if (day_number < 0) {
return cInvalidText;
}
if (day_number >= 7) {
return cInvalidText;
}
return days[day_number];
}
CHA ClockDayOfWeekInitial(ISN day_number) {
static const CHA days[] = "SMTWRFSU";
if (day_number < 0) return 'I';
if (day_number >= 7) return 'I';
return days[day_number];
}
ISN ClockCompare(const AClock& date_a, const AClock& date_b) {
D_COUT("\n Expecting Date:" << date_a << " and found:" << date_b);
if (date_a.year != date_b.year) {
D_COUT("\n year.a:" << date_a.year + ClockEpoch()
<< " != year.b:" << date_b.year + ClockEpoch());
return 1;
}
if (date_a.month != date_b.month) {
D_COUT("\n month.a:" << date_a.month
<< " != month.b:" << date_b.month + 1 << " ");
return 2;
}
if (date_a.day != date_b.day) {
D_COUT("\n day.a:" << date_a.day << " != day.b:" << date_b.day << " ");
return 3;
}
if (date_a.hour != date_b.hour) {
D_COUT("\n hour.a:" << date_a.hour << " != hour.b:" << date_b.hour
<< " ");
return 4;
}
if (date_a.minute != date_b.minute) {
D_COUT("\n minute.a:" << date_a.minute
<< " != minute.b:" << date_b.minute);
return 5;
}
if (date_a.second != date_b.second) {
D_COUT("\n second.a:" << date_a.second
<< " != second.b:" << date_b.second);
return 6;
}
return 0;
}
ISN ClockCompare(ISC time_a, ISC time_b) {
AClock a, b;
ClockInit(a, time_a);
ClockInit(b, time_b);
ISN result = ClockCompare(a, b);
D_COUT("\n Comparing time_a:" << time_a << " to time_b:" << time_b
<< " with result:" << result);
return result;
}
ISN ClockCompare(const AClock& clock, ISN year, ISN month, ISN day,
ISN hour = 0, ISN minute = 0, ISN second = 0) {
D_COUT("\n Expecting " << year << '/"' << month << '/"' << day << '@'
<< hour << ':' << minute << ':' << second
<< " and found " << clock);
if (year - ClockEpoch() != clock.year) {
D_COUT("\n Expecting year:" << year << " but found:"
<< clock.year + ClockEpoch() << '.');
return 1;
}
if (month != clock.month + 1) {
D_COUT("\n Expecting month:" << month << " but found:" << clock.month + 1
<< '.');
return 2;
}
if (day != clock.day) {
D_COUT("\n Expecting day:" << day << " but found:" << clock.day << '.');
return 3;
}
if (hour != clock.hour) {
D_COUT("\n Expecting hour:" << hour << " but found:" << clock.hour
<< '.');
return 4;
}
if (minute != clock.minute) {
D_COUT("\n Expecting minute:" << minute << " but found:" << clock.minute
<< '.');
return 5;
}
if (second != clock.second) {
D_COUT("\n Expecting second:" << second << " but found:" << clock.second
<< '.');
return 6;
}
return 0;
}
void ClockZeroTime(AClock& local_time) {
local_time.second = 0;
local_time.minute = 0;
local_time.hour = 0;
local_time.day = 0;
local_time.month = 0;
local_time.year = 0;
}
ISC TimeMake(AClock& time) { return (ISC)mktime(TPtr<tm>(&time)); }
const ISB* ClockDaysInMonth() {
static const ISB cDaysInMonth[12] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
return cDaysInMonth;
}
ISN ClockDaysInMonth(ISN month, ISN year) {
if ((year & 3) == 0) {
if (month == 4) return 29;
}
if (month < 1 || month > 12) return 0;
return ClockDaysInMonth()[month - 1];
}
ISN ClockDayOfYear(ISN year, ISN month, ISN day) {
if (day < 1 || day > ClockDaysInMonth(month, year) || month < 1 || month > 12)
return 0;
if (month == 1) {
return day;
}
if (year & 3) return ClockLastDayOfMonthLeapYear()[month - 2] + 1 + day;
return ClockLastDayOfMonth()[month - 2] + 1 + day;
}
ISC ClockTimeTMS(ISN year, ISN month, ISN day, ISN hour, ISN minute,
ISN second) {
return TClockTime<ISC>(year, month, day, hour, minute, second);
}
ISD ClockTimeTME(ISN year, ISN month, ISN day, ISN hour, ISN minute,
ISN second) {
return TClockTime<ISC>(year, month, day, hour, minute, second);
}
#if USING_UTF8 == YES_0
CHA* SPrint(CHA* origin, CHA* stop, const AClock& clock) {
return TSPrint<CHA>(origin, stop, clock);
}
CHA* SPrint(CHA* origin, CHA* stop, const TMD& t) {
return TSPrint<CHA>(origin, stop, t);
}
CHA* ClockPrint(CHA* origin, CHA* stop, ISC t) {
AClock clock;
ClockInit(clock, t);
return TSPrint<CHA>(origin, stop, clock);
}
CHA* ClockPrint(CHA* origin, CHA* stop, ISD t) {
AClock clock;
ClockInit(clock, t);
return TSPrint<CHA>(origin, stop, clock);
}
const CHA* ScanTime(const CHA* string, ISN& hour, ISN& minute, ISN& second) {
return TScanTime<CHA>(string, hour, minute, second);
}
const CHA* SScan(const CHA* string, AClock& clock) {
return TSScan<CHA>(string, clock);
}
const CHA* SScan(const CHA* string, TMD& t) { return TSScan<CHA>(string, t); }
const CHA* ScanTime(const CHA* string, ISC& t) {
return TScanTime<CHA, ISC>(string, t);
}
const CHA* ScanTime(const CHA* string, ISD& t) {
return TScanTime<CHA, ISD>(string, t);
}
#endif
#if USING_UTF16 == YES_0
CHB* SPrint(CHB* origin, CHB* stop, const AClock& clock) {
return TSPrint<CHB>(origin, stop, clock);
}
CHB* SPrint(CHB* origin, CHB* stop, const TMD& t) {
return TSPrint<CHB>(origin, stop, t);
}
CHB* ClockPrint(CHB* origin, CHB* stop, ISC t) {
return TClockPrint<CHB, ISC>(origin, stop, t);
}
CHB* ClockPrint(CHB* origin, CHB* stop, ISD t) {
return TClockPrint<CHB, ISD>(origin, stop, t);
}
CHC* SPrint(CHC* origin, CHC* stop, const AClock& clock) {
return TSPrint<CHC>(origin, stop, clock);
}
CHC* SPrint(CHC* origin, CHC* stop, const TMD& t) {
return TSPrint<CHC>(origin, stop, t);
}
CHC* ClockPrint(CHC* origin, CHC* stop, ISC t) {
return TClockPrint<CHC, ISC>(origin, stop, t);
}
CHC* ClockPrint(CHC* origin, CHC* stop, ISD t) {
return TClockPrint<CHC, ISD>(origin, stop, t);
}
const CHB* SScan(const CHB* string, AClock& clock) {
return TSScan<CHB>(string, clock);
}
const CHB* SScan(const CHB* string, TMD& result) {
return TSScan<CHB>(string, result);
}
const CHB* ScanTime(const CHB* string, ISN& hour, ISN& minute, ISN& second) {
return TScanTime<CHB>(string, hour, minute, second);
}
const CHB* ScanTime(const CHB* string, ISC& result) {
return TScanTime<CHB, ISC>(string, result);
}
const CHB* ScanTime(const CHB* string, ISD& result) {
return TScanTime<CHB, ISD>(string, result);
}
#endif
#if USING_UTF32 == YES_0
CHC* SPrint(CHC* origin, CHC* stop, AClock& clock) {
return TSPrint<CHC>(origin, stop, clock);
}
CHC* SPrint(CHC* origin, CHC* stop, ISC& t) {
return TSPrint<CHC>(origin, stop, t);
}
const CHC* ScanTime(const CHC* string, ISN& hour, ISN& minute, ISN& second) {
return TScanTime<CHC>(string, hour, minute, second);
}
const CHC* SScan(const CHC* string, AClock& time) {
return TSScan<CHC>(string, time);
}
const CHC* SScan(const CHC* string, TMD& result) {
return TSScan<CHC>(string, result);
}
const CHC* ScanTime(const CHC* string, ISC& result) {
return TScanTime<CHC, ISC>(string, result);
}
const CHC* ScanTime(const CHC* string, ISD& result) {
return TScanTime<CHC, ISD>(string, result);
}
#endif
} //< namespace _
#endif //< #if SEAM >= SCRIPT2_CLOCK