forked from swissmicros/SDKdemo
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdatetime.cc
953 lines (826 loc) · 28.1 KB
/
datetime.cc
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
// ****************************************************************************
// datetime.cc DB48X project
// ****************************************************************************
//
// File Description:
//
// Date and time related functions and commands
//
//
//
//
//
//
//
//
// ****************************************************************************
// (C) 2024 Christophe de Dinechin <[email protected]>
// This software is licensed under the terms outlined in LICENSE.txt
// ****************************************************************************
// This file is part of DB48X.
//
// DB48X is free software: you can redistribute it and/or modify
// it under the terms outlined in the LICENSE.txt file
//
// DB48X is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ****************************************************************************
#include "datetime.h"
#include "arithmetic.h"
#include "dmcp.h"
#include "renderer.h"
#include "tag.h"
#include "unit.h"
// ============================================================================
//
// Date and time utilities
//
// ============================================================================
bool to_time(object_p tobj, tm_t &tm, bool error)
// ----------------------------------------------------------------------------
// Convert a HH.MMSS time value to a time
// ----------------------------------------------------------------------------
{
if (!tobj)
return false;
tobj = object::strip(tobj);
algebraic_g time = nullptr;
uint scale = 100;
if (unit_p u = unit::get(tobj))
if (object_p uexpr = u->uexpr())
if (symbol_p sym = uexpr->as_quoted<symbol>())
if (sym->matches("hms"))
time = u->value();
if (time)
scale = 60;
else
time = tobj->as_real();
if (!time)
{
if (error)
rt.type_error();
return false;
}
algebraic_g factor = integer::make(scale);
uint hour = time->as_uint32(0, false);
time = (time * factor) % factor;
uint min = time->as_uint32(0, false);
time = (time * factor) % factor;
uint sec = time->as_uint32(0, false);
factor = integer::make(100);
time = (time * factor) % factor;
uint csec = time->as_uint32(0, false);
if (hour >= 24 || min >= 60 || sec >= 60)
{
if (error)
rt.invalid_time_error();
return false;
}
tm.hour = hour;
tm.min = min;
tm.sec = sec;
tm.csec = csec;
return true;
}
uint to_date(object_p dtobj, dt_t &dt, tm_t &tm, bool error)
// ----------------------------------------------------------------------------
// Convert a YYYYMMDD.HHMMSS value to a date and optional time
// ----------------------------------------------------------------------------
{
if (!dtobj)
return 0;
dtobj = object::strip(dtobj);
algebraic_g date = nullptr;
if (unit_p u = unit::get(dtobj))
if (object_p uexpr = u->uexpr())
if (symbol_p sym = uexpr->as_quoted<symbol>())
if (sym->matches("date"))
date = u->value();
if (!date)
date = dtobj->as_real();
if (!date)
{
if (error)
rt.type_error();
return 0;
}
algebraic_g factor = integer::make(100);
algebraic_g time = integer::make(1);
time = date % time;
uint d = date->as_uint32(0, false) % 100;
date = date / factor;
uint m = date->as_uint32(0, false) % 100;
date = date / factor;
uint y = date->as_uint32(0,false);
const uint days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
bool bisext = m == 2 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
if (m < 1 || m > 12 || d < 1 || d > days[m-1] + bisext)
{
if (error)
rt.invalid_date_error();
return 0;
}
dt.year = y;
dt.month = m;
dt.day = d;
if (time && !time->is_zero())
{
time = time * factor;
uint hour = time->as_uint32(0, false);
time = (time * factor) % factor;
uint min = time->as_uint32(0, false);
time = (time * factor) % factor;
uint sec = time->as_uint32(0, false);
time = (time * factor) % factor;
uint csec = time->as_uint32(0, false);
if (hour >= 24 || min >= 60 || sec >= 60)
{
if (error)
rt.invalid_time_error();
return 0;
}
tm.hour = hour;
tm.min = min;
tm.sec = sec;
tm.csec = csec;
return 2;
}
return 1;
}
algebraic_p to_days(object_p dobj, bool error)
// ----------------------------------------------------------------------------
// Return the value interpreted as a number of days
// ----------------------------------------------------------------------------
{
if (!dobj)
return nullptr;
dobj = object::strip(dobj);
algebraic_p dval = nullptr;
if (unit_g u = unit::get(dobj))
{
unit_g day = unit::make(integer::make(1), +symbol::make("d"));
if (day->convert(u))
dval = u->value();
else if (!error)
rt.clear_error();
}
if (!dval)
{
dval = dobj->as_real();
if (!dval && error)
rt.type_error();
}
return dval;
}
algebraic_p julian_day_number(const dt_t &dt, const tm_t &tm)
// ----------------------------------------------------------------------------
// Compute julian day number for a dt_t structure
// ----------------------------------------------------------------------------
{
ularge csecs = (tm.hour * 3600 + tm.min * 60 + tm.sec) * 100 + tm.csec;
ularge jval = julian_day_number(dt.day, dt.month, dt.year);
algebraic_g jdn = integer::make(jval);
if (csecs)
{
algebraic_g frac = +fraction::make(integer::make(csecs),
+integer::make(8640000));
jdn = jdn + frac;
}
return jdn;
}
algebraic_p julian_day_number(algebraic_p dtobj, bool error)
// ----------------------------------------------------------------------------
// Compute the Julian day number associate with a date value
// ----------------------------------------------------------------------------
{
dt_t dt;
tm_t tm{};
if (to_date(dtobj, dt, tm, error))
return julian_day_number(dt, tm);
return 0;
}
ularge julian_day_number(int d, int m, int y)
// ----------------------------------------------------------------------------
// Compute the Julian day number given day, month and year
// ----------------------------------------------------------------------------
{
uint rm = (m-14)/12;
ularge jdn = ((1461 * (y + 4800 + rm)) / 4
+ (367 * (m - 2 - 12 * rm)) / 12
- (3 * ((y + 4900 + rm) / 100)) / 4
+ d
- 32075);
return jdn;
}
algebraic_p date_from_julian_day(object_p jdn, bool error)
// ----------------------------------------------------------------------------
// Create a day from a Julian day object
// ----------------------------------------------------------------------------
{
if (!jdn)
return nullptr;
if (algebraic_g jval = jdn->as_real())
{
large jdn = jval->as_int64(0, error);
enum
{
y = 4716,
j = 1401,
m = 2,
n = 12,
r = 4,
p = 1461,
v = 3,
u = 5,
s = 153,
w = 2,
B = 274277,
C = -38
};
large f = jdn + j + (((4 * jdn + B) / 146097) * 3) / 4 + C;
large e = r * f + v;
large g = (e % p) / r;
large h = u * g + w;
uint day = (h % s) / u + 1;
uint month = (h / s + m ) % n + 1;
uint year = e / p - y + (n + m - month) / n;
ularge dval = year * 10000 + month * 100 + day;
algebraic_g date = integer::make(dval);
algebraic_g fp = integer::make(1);
fp = jval % fp;
if (!fp->is_zero())
{
algebraic_g factor = integer::make(86400);
fp = fp * factor;
ularge hval = fp->as_uint64(0, false);
uint hour = hval / 3600;
uint min = (hval / 60) % 60;
uint sec = hval % 60;
hval = hour * 10000 + min * 100 + sec;
fp = +fraction::make(integer::make(hval),
integer::make(1000000));
date = date + fp;
}
date = unit::make(date, +symbol::make("date"));
return date;
}
if (error)
rt.type_error();
return nullptr;
}
algebraic_p days_between_dates(object_p date1, object_p date2, bool error)
// ----------------------------------------------------------------------------
// Compute the number of days between two dates
// ----------------------------------------------------------------------------
{
dt_t dt1, dt2;
tm_t tm1{}, tm2{};
if (to_date(date1, dt1, tm1, error) && to_date(date2, dt2, tm2, error))
{
algebraic_g day1 = julian_day_number(dt1, tm1);
algebraic_g day2 = julian_day_number(dt2, tm2);
if (algebraic_g diff = day1 - day2)
if (unit_p days= unit::make(+diff, +symbol::make("d")))
return days;
}
return nullptr;
}
static algebraic_p days_after(object_p date, object_p days, bool error, int s)
// ----------------------------------------------------------------------------
// Compute the number of days after a given date
// ----------------------------------------------------------------------------
{
if (algebraic_g num = to_days(days, error))
{
dt_t dt;
tm_t tm{};
if (to_date(date, dt, tm, error))
{
algebraic_g jdn = julian_day_number(dt, tm);
jdn = s > 0 ? jdn + num : jdn - num;
num = date_from_julian_day(jdn);
return num;
}
}
return nullptr;
}
algebraic_p days_after(object_p date, object_p days, bool error)
// ----------------------------------------------------------------------------
// Compute the number of days after a given date
// ----------------------------------------------------------------------------
{
return days_after(date, days, error, 1);
}
algebraic_p days_before(object_p date, object_p days, bool error)
// ----------------------------------------------------------------------------
// Compute the number of days before a given date
// ----------------------------------------------------------------------------
{
return days_after(date, days, error, -1);
}
// ============================================================================
//
// Date and time related RPL commands
//
// ============================================================================
COMMAND_BODY(DateTime)
// ----------------------------------------------------------------------------
// Return current date and time
// ----------------------------------------------------------------------------
{
dt_t dt;
tm_t tm;
rtc_read(&tm, &dt);
ularge tval = tm.hour * 10000 + tm.min * 100 + tm.sec;
ularge dval = dt.year * 10000 + dt.month * 100 + dt.day;
dval = dval * 1000000ULL + tval;
if (decimal_g date = decimal::make(dval, -6))
if (unit_g result = unit::make(+date, +symbol::make("date")))
if (rt.push(+result))
return OK;
return ERROR;
}
COMMAND_BODY(Date)
// ----------------------------------------------------------------------------
// Return current date
// ----------------------------------------------------------------------------
{
dt_t dt;
tm_t tm;
rtc_read(&tm, &dt);
ularge dval = dt.year * 10000 + dt.month * 100 + dt.day;
if (integer_g date = integer::make(dval))
if (unit_g result = unit::make(+date, +symbol::make("date")))
if (rt.push(+result))
return OK;
return ERROR;
}
static bool setDate(object_p dobj)
// ----------------------------------------------------------------------------
// Set the system date based on input
// ----------------------------------------------------------------------------
{
dt_t dt;
tm_t tm;
rtc_read(&tm, &dt);
if (!to_date(dobj, dt, tm))
return false;
rtc_write(&tm, &dt);
return true;
}
COMMAND_BODY(SetDate)
// ----------------------------------------------------------------------------
// Set the current date
// ----------------------------------------------------------------------------
{
if (object_p d = rt.top())
if (setDate(d))
if (rt.drop())
return OK;
return ERROR;
}
COMMAND_BODY(Time)
// ----------------------------------------------------------------------------
// Return the current time
// ----------------------------------------------------------------------------
{
dt_t dt;
tm_t tm;
rtc_read(&tm, &dt);
ularge tval = tm.hour * 10000 + tm.min * 100 + tm.sec;
if (integer_g itime = integer::make(tval))
if (integer_g ratio = integer::make(10000))
if (fraction_g time = fraction::make(itime, ratio))
if (algebraic_g sexag = from_hms_dms(+time, ""))
if (unit_g result = unit::make(+sexag, +symbol::make("hms")))
if (rt.push(+result))
return OK;
return ERROR;
}
COMMAND_BODY(ChronoTime)
// ----------------------------------------------------------------------------
// Return the current time with a precision of 1/100th of a second
// ----------------------------------------------------------------------------
{
dt_t dt;
tm_t tm;
rtc_read(&tm, &dt);
ularge tval = tm.hour * 1000000 + tm.min * 10000 + tm.sec * 100 + tm.csec;
if (integer_g itime = integer::make(tval))
if (integer_g ratio = integer::make(1000000))
if (fraction_g time = fraction::make(itime, ratio))
if (algebraic_g sexag = from_hms_dms(+time, ""))
if (unit_g result = unit::make(+sexag, +symbol::make("hms")))
if (rt.push(+result))
return OK;
return ERROR;
}
static bool setTime(object_p tobj)
// ----------------------------------------------------------------------------
// Set the system date based on input
// ----------------------------------------------------------------------------
{
dt_t dt;
tm_t tm;
rtc_read(&tm, &dt);
if (!to_time(tobj, tm))
return false;
rtc_write(&tm, &dt);
return true;
}
COMMAND_BODY(SetTime)
// ----------------------------------------------------------------------------
// Set the current time
// ----------------------------------------------------------------------------
{
if (object_p t = rt.top())
if (setTime(t))
if (rt.drop())
return OK;
return ERROR;
}
void render_time(renderer &r, algebraic_g &value,
cstring hrs, cstring min, cstring sec,
uint base, bool ampm)
// ----------------------------------------------------------------------------
// Render a time (or an angle) as hours/minutes/seconds
// ----------------------------------------------------------------------------
{
if (!value || !value->is_real())
return;
bool as_time = *hrs == ':';
uint h = value->as_uint32(0, false);
r.flush();
r.printf("%u", h);
r.put(hrs);
algebraic_g one = integer::make(1);
algebraic_g factor = integer::make(base);
value = (value * factor) % factor;
uint m = value ? value->as_uint32(0, false) : 0;
r.printf("%02u", m);
r.put(min);
value = (value * factor) % factor;
uint s = value ? value->as_uint32(0, false) : 0;
r.printf("%02u", s);
r.put(sec);
value = value % one;
if (value && !value->is_zero(false))
{
if (as_time && algebraic::to_decimal(value))
{
settings::SaveLeadingZero slz(false);
auto dm = Settings.DisplayMode();
if (dm == object::ID_Sci || dm == object::ID_Eng)
dm = object::ID_Fix;
settings::SaveDisplayMode sdm(dm);
value->render(r);
}
else if (algebraic::to_fraction(value))
{
value->render(r);
}
}
if (ampm)
r.put(h < 12 ? 'A' : 'P');
}
size_t render_dms(renderer &r, algebraic_g value,
cstring deg, cstring min, cstring sec)
// ----------------------------------------------------------------------------
// Render a number as "degrees / minutes / seconds"
// ----------------------------------------------------------------------------
{
if (!value || !value->is_real())
return 0;
bool neg = value->is_negative(false);
if (neg)
{
r.put('-');
value = -value;
}
render_time(r, value, deg, min, sec, 60, false);
return r.size();
}
size_t render_date(renderer &r, algebraic_g date)
// ----------------------------------------------------------------------------
// Render a number as "degrees / minutes / seconds"
// ----------------------------------------------------------------------------
{
if (!date || !date->is_real())
return 0;
bool neg = date->is_negative();
if (neg)
{
r.put('-');
date = -date;
}
algebraic_g factor = integer::make(100);
algebraic_g time = integer::make(1);
time = date % time;
uint day = date->as_uint32(0, false) % 100;
date = date / factor;
uint month = date->as_uint32(0, false) % 100;
date = date / factor;
uint year = date->as_uint32(0, false);
char mname[4];
if (Settings.ShowMonthName() && month >=1 && month <= 12)
snprintf(mname, 4, "%s", get_month_shortcut(month));
else
snprintf(mname, 4, "%u", month);
char ytext[6];
if (Settings.TwoDigitYear())
snprintf(ytext, 6, "%02u", year % 100);
else
snprintf(ytext, 6, "%u", year);
if (Settings.ShowDayOfWeek())
{
ularge jdn = julian_day_number(day, month, year);
uint dow = jdn % 7;
r.printf("%s ", get_wday_shortcut(dow));
}
char sep = Settings.DateSeparator();
uint index = 2 * Settings.YearFirst() + Settings.MonthBeforeDay();
switch(index)
{
case 0: r.printf("%u%c%s%c%s", day, sep, mname, sep, ytext); break;
case 1: r.printf("%s%c%u%c%s", mname, sep, day, sep, ytext); break;
case 2: r.printf("%s%c%u%c%s", ytext, sep, day, sep, mname); break;
case 3: r.printf("%s%c%s%c%u", ytext, sep, mname, sep, day); break;
}
if (time && !time->is_zero())
{
r.put(", ");
time = time * factor;
render_time(r, time, ":", ":", "", 100, Settings.Time12H());
}
return r.size();
}
// ============================================================================
//
// HMS and DMS commands
//
// ============================================================================
algebraic_p to_hms_dms(algebraic_r x, cstring name)
// ----------------------------------------------------------------------------
// Convert an algebraic value to HMS or DMS value (i.e. no unit)
// ----------------------------------------------------------------------------
{
if (unit_g u = unit::get(x))
{
algebraic_g uexpr = u->uexpr();
if (symbol_p sym = uexpr->as_quoted<symbol>())
{
if (sym->matches("dms") || sym->matches("hms") || sym->matches("°"))
return u->value();
algebraic::angle_unit amode = object::ID_object;
if (sym->matches("pir") || sym->matches("πr"))
amode = object::ID_PiRadians;
else if (sym->matches("grad"))
amode = object::ID_Grad;
else if (sym->matches("r"))
amode = object::ID_Rad;
if (amode)
{
algebraic_g angle = u->value();
return algebraic::convert_angle(angle, amode, object::ID_Deg);
}
}
if (integer_g one = integer::make(1))
if (symbol_g uname = symbol::make(name))
if (unit_g to = unit::make(+one, +uname))
if (to->convert(u))
return u->value();
rt.inconsistent_units_error();
return nullptr;
}
if (!x->is_real())
{
rt.type_error();
return nullptr;
}
return x;
}
object::result to_hms_dms(cstring name)
// ----------------------------------------------------------------------------
// Convert the top of stack to HMS or DMS unit
// ----------------------------------------------------------------------------
{
algebraic_g x = rt.top()->as_algebraic();
algebraic_g xc = to_hms_dms(x, name);
if (!x || !xc)
return object::ERROR;
if (!arithmetic::to_fraction(xc))
{
if (!rt.error())
rt.value_error();
return object::ERROR;
}
algebraic_g sym = +symbol::make(name);
unit_g unit = unit::make(xc, sym);
if (!rt.top(unit))
return object::ERROR;
return object::OK;
}
algebraic_p from_hms_dms(algebraic_g x, cstring name)
// ----------------------------------------------------------------------------
// Convert a value from HMS input
// ----------------------------------------------------------------------------
{
if (x->is_real())
{
// Compatibility mode (including behaviour for 1.60->2.00)
if (!algebraic::to_fraction(x))
return nullptr;
algebraic_g hours = IntPart::run(x);
algebraic_g fp = FracPart::run(x);
algebraic_g hundred = integer::make(100);
algebraic_g min = hundred * fp;
algebraic_g sec = hundred * FracPart::run(min);
min = IntPart::run(min);
algebraic_g ratio = +fraction::make(integer::make(100),
integer::make(6000));
sec = sec * ratio;
min = (min + sec) * ratio;
hours = hours + min;
return hours;
}
else if (unit_g u = unit::get(x))
{
algebraic_g uexpr = u->uexpr();
if (symbol_p sym = uexpr->as_quoted<symbol>())
{
if (sym->matches(name))
{
uexpr = u->value();
return uexpr;
}
}
rt.inconsistent_units_error();
}
else
{
rt.type_error();
}
return nullptr;
}
object::result from_hms_dms(cstring name)
// ----------------------------------------------------------------------------
// Convert the top of stack from HMS or DMS unit
// ----------------------------------------------------------------------------
{
algebraic_g x = rt.top()->as_algebraic();
x = from_hms_dms(x, name);
if (x && rt.top(x))
return object::OK;
return object::ERROR;
}
COMMAND_BODY(ToHMS)
// ----------------------------------------------------------------------------
// Convert value to HMS format
// ----------------------------------------------------------------------------
{
return to_hms_dms("hms");
}
COMMAND_BODY(ToDMS)
// ----------------------------------------------------------------------------
// Convert value to DMS format
// ----------------------------------------------------------------------------
{
return to_hms_dms("dms");
}
COMMAND_BODY(FromHMS)
// ----------------------------------------------------------------------------
// Convert value from HMS format
// ----------------------------------------------------------------------------
{
return from_hms_dms("hms");
}
COMMAND_BODY(FromDMS)
// ----------------------------------------------------------------------------
// Convert value from DMS format
// ----------------------------------------------------------------------------
{
return from_hms_dms("dms");
}
static object::result hms_dms_add_sub(cstring name, bool sub)
// ----------------------------------------------------------------------------
// Addition or subtraction of DMS/HMS values
// ----------------------------------------------------------------------------
{
algebraic_g x = algebraic_p(rt.stack(0));
algebraic_g y = algebraic_p(rt.stack(1));
// Convert both arguments to DMS
x = from_hms_dms(x, name);
y = from_hms_dms(y, name);
if (!x || !y)
return object::ERROR;
// Add or subtract
x = sub ? y - x : y + x;
// Build result
algebraic_g sym = +symbol::make(name);
unit_g unit = unit::make(x, sym);
if (!rt.drop() || !rt.top(unit))
return object::ERROR;
return object::OK;
}
COMMAND_BODY(DMSAdd)
// ----------------------------------------------------------------------------
// Addition of DMS values
// ----------------------------------------------------------------------------
{
return hms_dms_add_sub("dms", false);
}
COMMAND_BODY(DMSSub)
// ----------------------------------------------------------------------------
// Subtraction of DMS values
// ----------------------------------------------------------------------------
{
return hms_dms_add_sub("dms", true);
}
COMMAND_BODY(HMSAdd)
// ----------------------------------------------------------------------------
// Addition of HMS values
// ----------------------------------------------------------------------------
{
return hms_dms_add_sub("hms", false);
}
COMMAND_BODY(HMSSub)
// ----------------------------------------------------------------------------
// Subtraction of HMS values
// ----------------------------------------------------------------------------
{
return hms_dms_add_sub("hms", true);
}
COMMAND_BODY(DateAdd)
// ----------------------------------------------------------------------------
// Add a date to a number of days
// ----------------------------------------------------------------------------
{
if (object_p d1 = rt.stack(1))
{
if (object_p d2 = rt.stack(0))
{
if (algebraic_p daf = days_after(d1, d2))
if (rt.drop() && rt.top(daf))
return OK;
if (algebraic_p daf = days_after(d2, d1))
if (rt.drop() && rt.top(daf))
return OK;
}
}
return ERROR;
}
COMMAND_BODY(DateSub)
// ----------------------------------------------------------------------------
// Compute the number of days between two dates
// ----------------------------------------------------------------------------
{
if (object_p d1 = rt.stack(1))
if (object_p d2 = rt.stack(0))
if (algebraic_p dd = days_between_dates(d1, d2))
if (rt.drop() && rt.top(dd))
return OK;
return ERROR;
}
COMMAND_BODY(JulianDayNumber)
// ----------------------------------------------------------------------------
// Return the Julian day number for current date and time
// ----------------------------------------------------------------------------
{
dt_t dt;
tm_t tm{};
if (object_p d = rt.top())
if (to_date(d, dt, tm))
if (algebraic_g jdn = julian_day_number(dt, tm))
if (rt.top(jdn))
return OK;
return ERROR;
}
COMMAND_BODY(DateFromJulianDayNumber)
// ----------------------------------------------------------------------------
// Return the date for a given Julian day number
// ----------------------------------------------------------------------------
{
if (object_p jdn = rt.top())
if (algebraic_p date = date_from_julian_day(jdn))
if (rt.top(date))
return OK;
return ERROR;
}
COMMAND_BODY(TimedEval)
// ----------------------------------------------------------------------------
// Evaluate and return the time it took
// ----------------------------------------------------------------------------
{
uint start = sys_current_ms();
if (result err = Eval::do_evaluate())
return err;
uint end = sys_current_ms();
if (integer_g duration = integer::make(end - start))
if (symbol_g ms = symbol::make("ms"))
if (unit_g result = unit::make(+duration, +ms))
if (tag_g tval = tag::make("duration", +result))
if (rt.push(+tval))
return OK;
return ERROR;
}