-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClock.hpp
602 lines (553 loc) · 15.9 KB
/
Clock.hpp
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
// Copyright Kabuki Starship� <kabukistarship.com>.
#pragma once
#include "Clock.h"
#if SEAM >= SCRIPT2_CLOCK
#ifndef INCLUDED_SCRIPT2_CLOCK_TEMPLATES
#define INCLUDED_SCRIPT2_CLOCK_TEMPLATES
#include "Uniprinter.hpp"
#include "Test.h"
#if SEAM == SCRIPT2_COUT
#include "_Debug.hxx"
#else
#include "_Release.hxx"
#endif
namespace _ {
template<typename IS>
AClock* TClockInit(AClock& clock, IS t) {
// Algorithm:
// 1. Using manual modulo convert in the following order:
// a. Year based on seconds per year.
// b. Day of year based on seconds per day.
// c. Month based on day of year and leap year.
// d. Hour.
// e. Minute.
// f. Second.
ISN value = (ISN)(t / SecondsPerYear);
t -= IS(value) * SecondsPerYear;
clock.year = value + ClockEpoch();
value = (ISN)(t / cSecondsPerDay);
t -= IS(value) * cSecondsPerDay;
clock.day = value;
value = (ISN)(t / SecondsPerHour);
t -= IS(value) * SecondsPerHour;
clock.hour = value;
value = (ISN)(t / SecondsPerMinute);
clock.minute = value;
clock.second = (ISN)(t - IS(value) * SecondsPerMinute);
return &clock;
}
template<typename IS>
IS TClockTime(ISN year, ISN month, ISN day, ISN hour, ISN minute, ISN second) {
if (year >= (ClockEpoch() + 10)) {
if (month >= 1 && day >= 19 && hour >= 3 && minute >= 14 && second >= 7)
return 0;
}
if (month < 1 || month >= 12 || hour >= 23 || minute >= 60 || second >= 60)
return 0;
return (IS)((year - ClockEpoch()) * SecondsPerYear +
ClockDayOfYear(year, month, day) * cSecondsPerDay +
hour * SecondsPerHour + minute * SecondsPerMinute + second);
}
/*
template<typename CHT = CHR>
CHT* Print(CHT* cursor, CHT* stop, TME& t) {
AClock c (t.seconds);
cursor = TSPrint<CHT>(cursor, stop, c);
cursor = TSPrint<CHT>(cursor, stop, ':');
return TSPrint<CHT>(cursor, stop, t.ticks);
}*/
#if USING_STR
template<typename CHT = CHR>
CHT* TSPrint(CHT* cursor, CHT* stop, const AClock& clock) {
// The way the utf functions are setup, we return a nil-term CHA so we
// don't have to check to write a single CHA in this
A_ASSERT(cursor);
A_ASSERT(cursor < stop);
cursor = TSPrint<CHT>(cursor, stop, clock.year + ClockEpoch());
if (!cursor) return nullptr;
*cursor++ = '-';
cursor = TSPrint<CHT>(cursor, stop, clock.month + 1);
if (!cursor) return nullptr;
*cursor++ = '-';
cursor = TSPrint<CHT>(cursor, stop, clock.day);
if (!cursor) return nullptr;
*cursor++ = '@';
cursor = TSPrint<CHT>(cursor, stop, clock.hour);
if (!cursor) return nullptr;
*cursor++ = ':';
cursor = TSPrint<CHT>(cursor, stop, clock.minute);
if (!cursor) return nullptr;
*cursor++ = ':';
cursor = TSPrint<CHT>(cursor, stop, clock.second);
if (!cursor) return nullptr;
return cursor;
}
template<typename Printer>
Printer& TSPrint(Printer& o, const _::AClock& clock) {
return o << clock.year + ClockEpoch() << '-' << clock.month + 1 << '-'
<< clock.day << '@' << clock.hour << ':' << clock.minute << ':'
<< clock.second;
}
template<typename CHT = CHR, typename IS = ISD>
CHT* TClockPrint(CHT* cursor, CHT* stop, IS t) {
AClock clock;
ClockInit(clock, t);
return TSPrint<CHT>(cursor, stop, clock);
}
template<typename CHT = CHR>
CHT* TSPrint(CHT* cursor, CHT* stop, const TMD& t) {
// The way the utf functions are setup, we return a nil-term CHA so we
// don't have to check to write a single CHA in this
A_ASSERT(cursor);
A_ASSERT(cursor < stop);
AClock clock;
ClockInit(clock, t.seconds);
cursor = TSPrint<CHT>(cursor, stop, clock);
*cursor++ = ':';
cursor = TSPrint<CHT>(cursor, stop, t.ticks);
if (!cursor) return nullptr;
return cursor;
}
template<typename Printer>
Printer& TSPrint(Printer& o, TMD& t) {
AClock clock;
ClockInit(clock, t.seconds);
return TSPrint<Printer>(o, clock) << ':' << t.ticks;
}
/* Scans a time in seconds from the given string. */
template<typename CHT = CHR>
const CHT* TScanTime(const CHT* string, ISC& hour, ISC& minute, ISC& second) {
if (string == nullptr) return nullptr;
D_COUT("\n\n Scanning time:\"" << string << '\"');
CHT c; //< The current CHT.
ISC h, //< Hour.
m, //< Minute.
s; //< Second.
if (!TScanSigned<ISC, IUC, CHT>(++string, h)) {
D_COUT("\nInvalid hour:" << h);
return nullptr;
}
string = TSTRSkipNumbers<CHT>(string);
if (h < 0) {
D_COUT("\nHours:" << h << " can't be negative.");
return nullptr;
}
if (h > 23) {
D_COUT("\nHours:" << h << " can't be > 23.");
return nullptr;
}
D_COUT(h);
c = *string++;
if (!c || TIsWhitespace<CHT>(c)) { // Case @HH
D_COUT(" HH ");
// Then it's a single number_, so create a ISC for the current
// user-time hour..
hour = h;
return string;
}
c = TToLower<CHT>(c);
if (c == 'a') { //
D_COUT("\nCase @HHAm\n HHam ");
c = *string++;
if (TToLower<CHT>(c) == 'm') c = *string++;
if (c && !TIsWhitespace<CHT>(c)) {
D_COUT("\nInvalid am format.");
return nullptr;
}
D_COUT(" @HHAM ");
hour = h;
return string;
}
if (c == 'p') {
D_COUT(" Case @HHpm ");
c = *string++;
if (TToLower<CHT>(c) == 'm') c = *string++;
if (c && !TIsWhitespace<CHT>(c)) {
D_COUT("\ninvalid pm format.");
return nullptr;
}
D_COUT("\nCase @HHPM " << h + 12 << ":00:00");
hour = h + 12;
return string;
}
if (c != ':') {
D_COUT("\nExpecting ':'.");
return nullptr;
}
D_COUT(
"\nCases HH:MM, HH::MMam, HH::MMpm, HH:MM:SS, HH:MM:SSam, and "
"HH:MM:SSpm");
if (!TScanSigned<ISC, IUC, CHT>(string, m)) return nullptr;
string = TSTRSkipNumbers<CHT>(string);
if (m < 0) {
D_COUT("\nMinutes:" << m << " can't be negative!");
return nullptr;
}
if (m >= 60) {
D_COUT("\nMinutes:" << m << " can't be >= 60!");
return nullptr; //< 60 minutes in an hour.
}
D_COUT(':' << m);
string = TSTRSkipNumbers<CHT>(string);
c = *string++;
if (!c || TIsWhitespace<CHT>(c)) {
D_COUT(" HH:MM ");
hour = h;
minute = m;
return string;
}
c = TToLower<CHT>(c);
if (c == 'a') {
D_COUT(" HH:MMam ");
c = *string++;
if (TToLower<CHT>(c) == 'm') { // The 'm' is optional.
c = *string++;
}
if (c && !TIsWhitespace<CHT>(c)) { // The space is not.
D_COUT("Invalid am in HH::MM AM");
return nullptr;
}
hour = h;
minute = m;
return string;
}
if (c == 'p') { //< Case HH:MM PM
D_COUT(" HH:MMpm ");
c = *string++;
if (TToLower<CHT>(c) == 'm') { //< The 'm' is optional.
c = *string++;
}
if (c && !TIsWhitespace<CHT>(c)) { //< The space is not.
D_COUT("Invalid am in HH::MM PM");
return nullptr;
}
hour = h + 12;
minute = m;
return string;
}
if (c != ':') return nullptr;
D_COUT("\n Cases HH:MM:SS, HH:MM:SSam, and HH:MM:SSpm");
if (!TScanSigned<ISC, IUC, CHT>(string, s)) return nullptr;
if (s < 0) {
D_COUT("\nSeconds:" << s << " can't be negative!");
return nullptr;
}
if (s > 60) {
D_COUT("\nSeconds:" << s << " can't be >= 60!");
return nullptr;
}
D_COUT(':' << s);
string = TSTRSkipNumbers<CHT>(string);
c = TToLower<CHT>(*string);
if (!c || TIsWhitespace<CHT>(c)) {
D_COUT(" HH:MM:SS ");
hour = h;
minute = m;
second = s;
return string;
}
if (c == 'a') {
D_COUT(" HH:MM:SSam ");
c = *string++;
if (TToLower<CHT>(c) == 'm') { //< The 'm' is optional.
c = *string++;
}
if (!c || !TIsWhitespace<CHT>(c)) { //< The space is not.
D_COUT("\nInvalid am in HH::MM:SS AM");
return nullptr;
}
hour = h;
minute = m;
second = s;
return string;
}
if (c != 'p') {
D_COUT("\nExpecting a PM but found:" << c);
return nullptr; // Format error!
}
D_COUT(" HH:MM:SSpm ");
c = TToLower<CHT>(*string++);
if (c == 'm') { //< The 'm' is optional.
c = *string++;
}
if (!c || !TIsWhitespace<CHT>(c)) { //< The space is not.
D_COUT("\nInvalid am in HH::MM:SS PM");
return nullptr;
}
hour = h + 12;
minute = m;
second = s;
return string;
}
/* Scans the given string for a timestamp. */
template<typename CHT = CHR>
const CHT* TSScan(const CHT* string, AClock& clock) {
D_ASSERT(string);
D_COUT("\n Scanning AClock:\"" << string << "\n Scanning: ");
string = TSTRSkimodulear<CHT>(string, '0');
CHT c = *string, //< The current CHT.
delimiter; //< The delimiter.
const CHT* stop; //< Might not need
ISC hour = 0,
minute = 0,
second = 0;
if (c == '@') {
if (!(string = TScanTime<CHT>(string, hour, minute, second))) {
D_COUT("\nCase @ invalid time");
return nullptr;
}
clock.hour = hour;
clock.minute = minute;
clock.second = second;
return string + 1;
}
if (c == '#') {
if (!(string = TScanTime<CHT>(string, hour, minute, second))) {
D_COUT("\nCase @ invalid time");
}
clock.hour += hour;
clock.minute += minute;
clock.second += second;
return string + 1;
}
ISC value1, //< The first date field scanned.
value2, //< The second date field scanned.
value3, //< The third date field scanned.
is_last_year = 0; //< Flag for if the date was last year or not.
// SScan value1
if (!TScanSigned<ISC, IUC, CHT>(string, value1)) {
D_COUT("SScan error at value1");
return nullptr;
}
if (value1 < 0) {
D_COUT("Dates can't be negative.");
return nullptr;
}
string = TSTRDecimalEnd<CHT>(string);
if (!string) return nullptr;
delimiter = *string++;
D_COUT(value1);
if (delimiter == '@') {
D_COUT(" HH@ ");
if (!(string = TScanTime<CHT>(string, hour, minute, second))) {
D_COUT("\nInvalid time DD@");
return nullptr;
}
clock.day = value1;
return string + 1;
}
// SScan value2.
string = TSTRSkimodulear<CHT>(string, '0');
if (!TScanSigned<ISC, IUC, CHT>(string, value2)) {
D_COUT("\n Failed scanning value2 of date.");
return nullptr;
}
if (value2 < 0) {
D_COUT("Day can't be negative.");
return nullptr; //< Invalid month and day.
}
D_COUT(value2);
string = TSTRDecimalEnd<CHT>(string);
c = *string;
if (c != delimiter) {
D_COUT("\n Cases MM/DD and MM/YYyy");
if (c == '@') {
if (!(string = TScanTime<CHT>(string, hour, minute, second))) {
D_COUT(" invalid time ");
return nullptr;
}
}
if (!c || TIsWhitespace<CHT>(c)) {
D_COUT("\n Format is MM/DD and year is " << clock.year + ClockEpoch());
// is_last_year = ((value1 >= std_tm.mon) &&
// (value2 >= std_tm.mday)) ? 0:1;
clock.year += is_last_year;
clock.month = value1 - 1;
clock.day = value2;
clock.hour = 0;
clock.hour = hour;
clock.minute = minute;
clock.second = second;
return string + 1;
}
c = TToLower<CHT>(c);
if ((value1 < 12) && (value2 > 0) &&
(value2 <= ClockMonthDayCount(value1))) {
D_COUT(" MM/DD ");
if (value1 > 11) {
D_COUT("\nInvalid MM/DD@ month");
return nullptr;
}
// We need to find out what year it is.
AClock clock;
ClockInit(clock);
if (value2 > ClockMonthDayCount(clock.year - ClockEpoch(), value1)) {
D_COUT("\nInvalid MM/DD@ day");
return nullptr;
}
clock.month = value1 - 1;
clock.day = value2;
clock.hour = hour;
clock.minute = minute;
clock.second = second;
if (!(string = TScanTime(string, hour, minute, second))) {
D_COUT("\nInvalid MM/DD@");
return nullptr;
}
return string + 1;
}
if ((value1 < 12) && (value2 > ClockMonthDayCount(value1))) {
D_COUT(" MM/YYyy");
clock.month = value1 - 1;
clock.year = value2;
if (!(string = TScanTime<CHT>(string, hour, minute, second))) {
D_COUT("\nInvalid MM / YYYY@ time");
return nullptr;
}
return string + 1;
}
D_COUT("\nInvalid MM/DD or MM/YYyy format");
return nullptr;
}
// Formats MM/DD/YYyy and YYyy/MM/DD
string = TSTRSkimodulear<CHT>(++string, '0');
c = *string;
// Then there are 3 values and 2 delimiters.
if (!TIsDigit<CHT>(c) || !TScanSigned<ISC, IUC, CHT>(string, value3)) {
D_COUT("\n SlotRead error reading value3 of date." << CHT(c) << ": ");
return nullptr; //< Invalid format!
}
string = TSTRDecimalEnd<CHT>(string);
D_COUT(c << value3);
// Now we need to check what format it is in.
c = *string;
if (c == '@') {
if (!(stop = TScanTime<CHT>(string, hour, minute, second))) {
D_COUT("Invalid YYyy/MM/DD@ time.");
return nullptr;
}
}
clock.hour = hour;
clock.minute = minute;
clock.second = second;
if (TIsWhitespace<CHT>(*(++string))) {
D_COUT("No date found.");
return nullptr;
}
if (value1 > 11) { //<
D_COUT("\n Case YYyy/MM/DD");
if (value2 == 0 || value2 > 12) {
D_COUT("Invalid number of months");
return nullptr;
}
if (value2 > ClockMonthDayCount(value2, value1)) {
D_COUT("Invalid number of days");
return nullptr;
} // 17/05/06
if (value1 < 100) {
D_COUT("\n Case YY/MM/DD");
value1 += 2000 - ClockEpoch();
} else {
D_COUT("\n Case YYYY/MM/DD");
value1 -= ClockEpoch();
}
clock.year = value1;
clock.month = value2 - 1;
clock.day = value3;
return string + 1;
}
D_COUT("\n Cases MM/DD/YY and MM/DD/YYYY");
if (value1 > 11) {
D_COUT("\nInvalid month.");
return nullptr;
}
if (value2 > ClockMonthDayCount(value1, value3)) {
D_COUT("\nInvalid day.");
return nullptr;
}
clock.month = value1 - 1;
clock.day = value2;
if (value3 < 100) {
D_COUT("\n Case MM/DD/YY");
clock.year = value3 + (2000 - ClockEpoch());
} else {
D_COUT("\n Case MM/DD/YYYY");
clock.year = value3 - ClockEpoch();
}
return string + 1;
}
template<typename CHT, typename IS>
const CHT* TScanTime(const CHT* origin, ISC& result) {
AClock clock;
const CHT* stop = TSScan<CHT>(origin, clock);
result = ISC(ClockSeconds(clock));
return stop;
}
template<typename CHT, typename IS>
const CHT* TScanTime(const CHT* origin, ISD& result) {
AClock clock;
const CHT* stop = TSScan<CHT>(origin, clock);
result = ISD(ClockSeconds(clock));
return stop;
}
template<typename CHT>
const CHT* TSScan(const CHT* origin, TMD& result) {
origin = TScanTime<CHT, ISC>(origin, result.seconds);
if (!origin) return nullptr;
if (*origin++ != ':') {
result.ticks = 0;
return origin - 1;
}
return TScanUnsigned<IUC, CHT>(origin, result.ticks);
}
#endif // #if USING_STR
template<typename IS>
IS TClockSet(AClock* clock, IS t) {
// Algorithm:
// 1. Using manual modulo convert in the following order:
// a. Year based on seconds per year.
// b. Day of year based on seconds per day.
// c. Month based on day of year and leap year.
// d. Hour.
// e. Minute.
// f. Second.
IS value = t / SecondsPerYear;
t -= value * SecondsPerYear;
clock->year = (ISC)(value + ClockEpoch());
value = t / cSecondsPerDay;
t -= value * cSecondsPerDay;
clock->day = (ISC)value;
value = t / SecondsPerHour;
t -= value * SecondsPerHour;
clock->hour = (ISC)value;
value = t / SecondsPerMinute;
clock->minute = (ISC)value;
clock->second = (ISC)(t - value * SecondsPerMinute);
return t;
}
/* A time in seconds stored as either a 32-bit or 64-bit IS.
The difference between a TClock and AClock is that that TClock stores the AClock
and the TMD or ISC. */
template<typename IS>
struct LIB_MEMBER TClock {
AClock clock; //< A human-readable clock.
/* Constructs a clock from the given seconds timestamp. */
TClock(IS t) { ClockInit(clock, t); }
/* Prints the given */
template<typename Printer, typename IS>
Printer& Print(Printer& o) {
return o << clock.Clock();
}
};
} //< namespace _
#if USING_CONSOLE == YES_0
inline _::COut& operator<<(_::COut& o, const _::AClock& item) {
return _::TSPrint(o, item);
}
template<typename IS>
_::COut& operator<<(_::COut& o, _::TClock<IS> item) {
return o << item.clock;
}
#endif
#endif
#endif