-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtamguchrono.cxx
executable file
·943 lines (793 loc) · 30.6 KB
/
tamguchrono.cxx
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
/*
* Tamgu (탐구)
*
* Copyright 2019-present NAVER Corp.
* under BSD 3-clause
*/
/* --- CONTENTS ---
Project : Tamgu (탐구)
Version : See tamgu.cxx for the version number
filename : tamguchrono.cxx
Date : 2017/09/01
Purpose :
Programmer : Claude ROUX (claude.roux@naverlabs.com)
Reviewer :
*/
#include "tamgu.h"
#include "instructions.h"
#include "constobjects.h"
#include "tamguchrono.h"
#include "tamgufvector.h"
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
//We need to declare once again our local definitions.
Exporting basebin_hash<chronoMethod> Tamguchrono::methods;
static ThreadLock classlock;
Exporting short Tamguchrono::idtype = 0;
static ThreadLock clock_locker;
static void locking_clock(Tamgu* c) {
if (globalTamgu->threadMODE && c->hasLock())
clock_locker.lock->lock();
}
static void unlocking_clock(Tamgu* c) {
if (globalTamgu->threadMODE && c->hasLock())
clock_locker.lock->unlock();
}
//MethodInitialization will add the right references to "name", which is always a new method associated to the object we are creating
void Tamguchrono::AddMethod(TamguGlobal* global, string name, chronoMethod func, unsigned long arity, string infos) {
short idname = global->Getid(name);
methods[idname] = func;
if (global->infomethods.find(idtype) != global->infomethods.end() &&
global->infomethods[idtype].find(name) != global->infomethods[idtype].end())
return;
global->infomethods[idtype][name] = infos;
global->RecordArity(idtype, idname, arity);
}
void Tamguchrono::Setidtype(TamguGlobal* global) {
Locking lock(classlock);
if (Tamguchrono::methods.isEmpty())
Tamguchrono::InitialisationModule(global,"");
}
bool Tamguchrono::InitialisationModule(TamguGlobal* global, string version) {
methods.clear();
Tamguchrono::idtype = global->Getid("chrono");
Tamguchrono::AddMethod(global, "_initial", &Tamguchrono::MethodUnit, P_ONE | P_NONE, "unit(int i): 1 is second. 2 is milliseconds. 3 is microsecond. 4 is nanosecond.");
Tamguchrono::AddMethod(global, "reset", &Tamguchrono::MethodReset, P_NONE, "reset(): reset the chrono");
Tamguchrono::AddMethod(global, "start", &Tamguchrono::MethodReset, P_NONE, "start(): start the chrono");
Tamguchrono::AddMethod(global, "stop", &Tamguchrono::MethodStop, P_NONE, "stop(): stop the chrono and returns the intermediate time");
Tamguchrono::AddMethod(global, "unit", &Tamguchrono::MethodUnit, P_ONE, "unit(int i): 1 is second. 2 is milliseconds. 3 is microsecond. 4 is nanosecond.");
if (version != "") {
global->newInstance[Tamguchrono::idtype] = new Tamguchrono(global);
global->RecordCompatibilities(Tamguchrono::idtype);
global->CreateSystemVariable(new TamguConstInt(1), "c_second", a_float);
global->CreateSystemVariable(new TamguConstInt(2), "c_millisecond", a_float);
global->CreateSystemVariable(new TamguConstInt(3), "c_microsecond", a_float);
global->CreateSystemVariable(new TamguConstInt(4), "c_nanosecond", a_float);
}
return true;
}
Tamgu* Tamguchrono::Put(Tamgu* index, Tamgu* val, short idthread) {
if (val->Type() == Tamguchrono::idtype) {
value = ((Tamguchrono*)val)->value;
return aTRUE;
}
return aFALSE;
}
Tamgu* Tamguchrono::Eval(Tamgu* context, Tamgu* index, short idthread) {
return this;
}
string Tamguchrono::String() {
char buffer[100];
double chronoval = value.time_since_epoch().count();
sprintf_s(buffer, 100, "%fs", chronoval);
return buffer;
}
Tamgu* Tamguchrono::MethodUnit(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
if (callfunc->Size() == 0)
return aFALSE;
unit = callfunc->Evaluate(0, contextualpattern, idthread)->Integer();
return aTRUE;
}
Tamgu* Tamguchrono::MethodReset(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
//First parameter is a chrono
value = std::chrono::high_resolution_clock::now();
return this;
}
Tamgu* Tamguchrono::MethodStop(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
//First parameter is a chrono
std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now();
switch (unit) {
case 1:
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::seconds>( end - value ).count());
case 2:
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::milliseconds>( end - value ).count());
case 3:
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::microseconds>( end - value ).count());
case 4:
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::nanoseconds>( end - value ).count());
}
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::milliseconds>( end - value ).count());
}
Tamgu* Tamguchrono::minus(Tamgu* bb, bool autoself) {
if (bb->Type() != idtype)
return aNULL;
Tamguchrono* b = (Tamguchrono*)bb;
switch (unit) {
case 1:
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::seconds>( value - b->value ).count());
case 2:
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::milliseconds>( value - b->value ).count());
case 3:
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::microseconds>( value - b->value ).count());
case 4:
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::nanoseconds>( value - b->value ).count());
}
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::seconds>( value - b->value ).count());
}
//We need to declare once again our local definitions.
Exporting basebin_hash<clockMethod> Tamguclock::methods;
Exporting short Tamguclock::idtype = 0;
//MethodInitialization will add the right references to "name", which is always a new method associated to the object we are creating
void Tamguclock::AddMethod(TamguGlobal* global, string name, clockMethod func, unsigned long arity, string infos) {
short idname = global->Getid(name);
methods[idname] = func;
if (global->infomethods.find(idtype) != global->infomethods.end() &&
global->infomethods[idtype].find(name) != global->infomethods[idtype].end())
return;
global->infomethods[idtype][name] = infos;
global->RecordArity(idtype, idname, arity);
}
void Tamguclock::Setidtype(TamguGlobal* global) {
Locking lock(classlock);
if (Tamguclock::methods.isEmpty())
Tamguclock::InitialisationModule(global,"");
}
bool Tamguclock::InitialisationModule(TamguGlobal* global, string version) {
methods.clear();
Tamguclock::idtype = global->Getid("clock");
Tamguclock::AddMethod(global, "_initial", &Tamguclock::MethodUTC, P_NONE | P_ONE, "utc(string utc_str): Initializes a clock with a string.");
Tamguclock::AddMethod(global, "reset", &Tamguclock::MethodReset, P_NONE, "reset(): reset the chrono");
Tamguclock::AddMethod(global, "start", &Tamguclock::MethodReset, P_NONE, "start(): start the chrono");
Tamguclock::AddMethod(global, "stop", &Tamguclock::MethodStop, P_NONE, "stop(): stop the chrono and returns the intermediate time");
Tamguclock::AddMethod(global, "unit", &Tamguclock::MethodUnit, P_ONE, "unit(int i): 1 is second. 2 is milliseconds. 3 is microsecond. 4 is nanosecond.");
Tamguclock::AddMethod(global, "timezone", &Tamguclock::MethodTimezone, P_NONE | P_ONE, "timezone(int time_zone): Defines the time difference with GM time.");
Tamguclock::AddMethod(global, "format", &Tamguclock::MethodFormat, P_ONE, "format(string frm): frm describes the format, which is used to return a string.");
Tamguclock::AddMethod(global, "microseconds", &Tamguclock::MethodMicroseconds, P_NONE, "microseconds(): returns the microseconds.");
Tamguclock::AddMethod(global, "utc", &Tamguclock::MethodUTC, P_NONE|P_ONE, "utc(string utc_str|bool time_zone): utc() returns the UTC time. utc('...') uses the date to instantiate itself.");
Tamguclock::AddMethod(global, "iso", &Tamguclock::MethodISO8601, P_NONE|P_ONE, "iso(string utc_str|bool time_zone): iso() returns the ISO8601 time. iso('...') uses the date to instantiate itself.");
if (version != "") {
global->newInstance[Tamguclock::idtype] = new Tamguclock(global);
global->RecordCompatibilities(Tamguclock::idtype);
}
return true;
}
Tamgu* Tamguclock::Put(Tamgu* index, Tamgu* val, short idthread) {
if (val->Type() == Tamguclock::idtype) {
value = ((Tamguclock*)val)->value;
return aTRUE;
}
return aFALSE;
}
Tamgu* Tamguclock::Eval(Tamgu* context, Tamgu* index, short idthread) {
return this;
}
string Tamguclock::String() {
return ISO8601(false);
}
Tamgu* Tamguclock::MethodFormat(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
locking_clock(this);
std::ostringstream os;
string frm = callfunc->Evaluate(0, contextualpattern, idthread)->String();
std::time_t currentTime = std::chrono::system_clock::to_time_t(value);
long pos;
os << std::put_time(std::gmtime(¤tTime), STR(frm));
string s = os.str();
pos = frm.find("%fm");
if (pos != -1) {
auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(value);
auto epoch = now_ms.time_since_epoch();
auto val = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
long vali = val.count();
os.str("");
if (isdigit(s[pos+2])) {
int nb = s[pos+2] - 48;
long p10 = 1;
while (nb) {
p10 *= 10;
nb--;
}
nb = s[pos+2] - 48;
os << std::setw(nb) << std::setfill('0') << vali % p10;
s.replace(pos, 3, os.str());
}
else {
os << std::setw(3) << std::setfill('0') << vali % 1000;
s.replace(pos, 2, os.str());
}
}
pos =frm.find("%fc");
if (pos != -1) {
auto now_ms = std::chrono::time_point_cast<std::chrono::microseconds>(value);
auto epoch = now_ms.time_since_epoch();
auto val = std::chrono::duration_cast<std::chrono::microseconds>(epoch);
os.str("");
if (isdigit(s[pos+2])) {
int nb = s[pos+2] - 48;
long p10 = 1;
while (nb) {
p10 *= 10;
nb--;
}
nb = s[pos+2] - 48;
os << std::setw(nb) << std::setfill('0') << val.count() % p10;
s.replace(pos, 3, os.str());
}
else {
os << std::setw(6) << std::setfill('0') << val.count() % 1000000;
s.replace(pos, 2, os.str());
}
}
unlocking_clock(this);
return globalTamgu->Providestring(s);
}
Tamgu* Tamguclock::MethodUnit(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
if (callfunc->Size() == 0)
return aFALSE;
unit = callfunc->Evaluate(0, contextualpattern, idthread)->Integer();
return aTRUE;
}
string Tamguclock::compute_time_zone() {
locking_clock(this);
std::time_t currentTime = std::chrono::system_clock::to_time_t(value);
std::tm* local_tm = std::localtime(¤tTime);
char buffer[101];
strftime(buffer, 100, "%z", local_tm);
unlocking_clock(this);
string res;
res = buffer[0];
res += buffer[1];
res += buffer[2];
res += ":";
res += buffer[3];
res += buffer[4];
return res;
}
Tamgu* Tamguclock::MethodTimezone(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
if (callfunc->Size() == 0) {
timezone_offset = compute_time_zone();
return aFALSE;
}
string timezone = callfunc->Evaluate(0, contextualpattern, idthread)->String();
long sz = timezone.size();
if (!sz)
return globalTamgu->Returnerror("Error: missing value for time zone", idthread);
vector<string> sv;
char sgn = timezone[0];
string dt;
long i = 0;
if (sgn != '+' && sgn != '-')
sgn = '+';
else
i = 1;
for (; i < sz; i++) {
if (isdigit(timezone[i]))
dt += timezone[i];
else {
if (dt != "") {
sv.push_back(dt);
dt = "";
}
}
}
if (dt.size())
sv.push_back(dt);
if (sv.size() >= 1) {
timezone = sgn;
if (sv[0].size() == 1)
timezone += "0" + sv[0];
else {
if (sv[0].size() == 2)
timezone += sv[0];
else
globalTamgu->Returnerror("Error: wrong value for time zone", idthread);
}
if (sv.size() == 2) {
timezone += ":";
if (sv[1].size() == 1)
timezone += sv[1] + "0";
else {
if (sv[1].size() == 2)
timezone += sv[1];
else
globalTamgu->Returnerror("Error: wrong value for time zone", idthread);
}
}
else {
if (sv.size() == 1)
timezone += ":00";
else
globalTamgu->Returnerror("Error: wrong value for time zone", idthread);
}
}
else
globalTamgu->Returnerror("Error: wrong value for time zone", idthread);
timezone_offset = timezone;
return aTRUE;
}
Tamgu* Tamguclock::MethodReset(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
//First parameter is a chrono
value = std::chrono::system_clock::now();
return this;
}
Tamgu* Tamguclock::MethodStop(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
//First parameter is a chrono
std::chrono::system_clock::time_point end = std::chrono::system_clock::now();
switch (unit) {
case 1:
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::seconds>( end - value ).count());
case 2:
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::milliseconds>( end - value ).count());
case 3:
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::microseconds>( end - value ).count());
case 4:
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::nanoseconds>( end - value ).count());
}
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::milliseconds>( end - value ).count());
}
Tamgu* Tamguclock::plus(Tamgu* bb, bool autoself) {
long val = bb->Integer();
Tamguclock* chrono;
if (autoself)
chrono = this;
else
chrono = new Tamguclock(value);
switch (unit) {
case 1: {
auto add = std::chrono::seconds(val);
chrono->value += add;
break;
}
case 2: {
auto add = std::chrono::milliseconds(val);
chrono->value += add;
break;
}
case 3: {
auto add = std::chrono::microseconds(val);
chrono->value += add;
break;
}
case 4: {
val /= 1000;
auto add = std::chrono::microseconds(val);
chrono->value += add;
break;
}
}
return chrono;
}
Tamgu* Tamguclock::minus(Tamgu* bb, bool autoself) {
Tamguclock* chrono;
if (autoself)
chrono = this;
else
chrono = new Tamguclock(value);
if (bb->Type() == idtype) {
//In this case this is a time difference
Tamguclock* b = (Tamguclock*)bb;
switch (unit) {
case 1:
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::seconds>( value - b->value ).count());
case 2:
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::milliseconds>( value - b->value ).count());
case 3:
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::microseconds>( value - b->value ).count());
case 4:
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::nanoseconds>( value - b->value ).count());
}
return globalTamgu->ProvideConstfloat(std::chrono::duration_cast<std::chrono::seconds>( value - b->value ).count());
}
long val = bb->Integer();
switch (unit) {
case 1: {
auto add = std::chrono::seconds(val);
chrono->value -= add;
break;
}
case 2: {
auto add = std::chrono::milliseconds(val);
chrono->value -= add;
break;
}
case 3: {
auto add = std::chrono::microseconds(val);
chrono->value -= add;
break;
}
case 4: {
val /= 1000;
auto add = std::chrono::microseconds(val);
chrono->value -= add;
break;
}
}
return chrono;
}
Tamgu* Tamguclock::multiply(Tamgu* bb, bool autoself) {
double v = bb->Float();
auto epoch = value.time_since_epoch();
switch (unit) {
case 1: {
auto val = std::chrono::duration_cast<std::chrono::seconds>(epoch);
val *= v;
return globalTamgu->Providefloat(val.count());
}
case 2: {
auto val = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
val *= v;
return globalTamgu->Providefloat(val.count());
}
case 3: {
auto val = std::chrono::duration_cast<std::chrono::microseconds>(epoch);
val *= v;
return globalTamgu->Providefloat(val.count());
}
}
auto val = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
val *= v;
return globalTamgu->Providefloat(val.count());
}
Tamgu* Tamguclock::divide(Tamgu* bb, bool autoself) {
double v = bb->Float();
if (!v)
return globalTamgu->Returnerror("MAT(203): Cannot divide by 0");
auto epoch = value.time_since_epoch();
switch (unit) {
case 1: {
auto val = std::chrono::duration_cast<std::chrono::seconds>(epoch);
val /= v;
return globalTamgu->Providefloat(val.count());
}
case 2: {
auto val = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
val /= v;
return globalTamgu->Providefloat(val.count());
}
case 3: {
auto val = std::chrono::duration_cast<std::chrono::microseconds>(epoch);
val /= v;
return globalTamgu->Providefloat(val.count());
}
}
auto val = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
val /= v;
return globalTamgu->Providefloat(val.count());
}
Tamgu* Tamguclock::divideinteger(Tamgu* bb, bool autoself) {
double v = bb->Float();
if (!v)
return globalTamgu->Returnerror("MAT(203): Cannot divide by 0");
auto epoch = value.time_since_epoch();
switch (unit) {
case 1: {
auto val = std::chrono::duration_cast<std::chrono::seconds>(epoch);
val /= v;
return globalTamgu->Providefloat(val.count());
}
case 2: {
auto val = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
val /= v;
return globalTamgu->Providefloat(val.count());
}
case 3: {
auto val = std::chrono::duration_cast<std::chrono::microseconds>(epoch);
val /= v;
return globalTamgu->Providefloat(val.count());
}
}
auto val = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
val /= v;
return globalTamgu->Provideint(val.count());
}
Tamgu* Tamguclock::mod(Tamgu* bb, bool autoself) {
long v = bb->Integer();
if (!v)
return globalTamgu->Returnerror("MAT(203): Cannot divide by 0");
auto epoch = value.time_since_epoch();
switch (unit) {
case 1: {
auto val = std::chrono::duration_cast<std::chrono::seconds>(epoch);
val %= v;
return globalTamgu->Providefloat(val.count());
}
case 2: {
auto val = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
val %= v;
return globalTamgu->Providefloat(val.count());
}
case 3: {
auto val = std::chrono::duration_cast<std::chrono::microseconds>(epoch);
val %= v;
return globalTamgu->Providefloat(val.count());
}
}
auto val = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
val %= v;
return globalTamgu->Providefloat(val.count());
}
#ifdef WIN32
Tamgu* Tamguclock::set_the_utc_date(Tamgufvector* iv) {
locking_clock(this);
time_t x = 0;
struct tm temps;
localtime_s(&temps, &x);
int fulldate = 0;
long sz = iv->values.size();
if (!sz)
return aFALSE;
temps.tm_year = iv->values[0] - 1900;
temps.tm_mon = iv->values[1] - 1;
temps.tm_mday = iv->values[2];
temps.tm_hour = iv->values[3];
temps.tm_min = iv->values[4];
temps.tm_sec = iv->values[5];
x = mktime(&temps);
unlocking_clock(this);
if (x <= 0)
return aFALSE;
value = std::chrono::system_clock::from_time_t(x);
int v = (int)iv->values[7];
double tm = (iv->values[7] - (double)v)*60;
char buffer[100];
if (v >= 0)
sprintf_s(buffer, 100, "+%02d:%02d", v, (int)tm);
else
sprintf_s(buffer, 100, "%02d:%02d", v - 1, (int)tm);
timezone_offset = buffer;
auto now_ms = std::chrono::time_point_cast<std::chrono::microseconds>(value);
auto epoch = now_ms.time_since_epoch();
auto val = std::chrono::duration_cast<std::chrono::microseconds>(epoch);
int diff = iv->values[6] - (val.count()%1000000);
value += std::chrono::microseconds(diff);
return aTRUE;
}
Tamgu* Tamguclock::set_the_iso8601_date(Tamgufvector* iv) {
locking_clock(this);
time_t x = 0;
struct tm temps;
localtime_s(&temps, &x);
int fulldate = 0;
long sz = iv->values.size();
if (!sz)
return aFALSE;
temps.tm_year = iv->values[0] - 1900;
temps.tm_mon = iv->values[1] - 1;
temps.tm_mday = iv->values[2];
temps.tm_hour = iv->values[3];
temps.tm_min = iv->values[4];
temps.tm_sec = iv->values[5];
x = mktime(&temps);
unlocking_clock(this);
if (x <= 0)
return aFALSE;
value = std::chrono::system_clock::from_time_t(x);
int v = (int)iv->values[7];
double tm = (iv->values[7] - (double)v)*60;
char buffer[100];
if (v >= 0)
sprintf_s(buffer, 100, "+%02d:%02d", v, (int)tm);
else
sprintf_s(buffer, 100, "%02d:%02d", v - 1, (int)tm);
timezone_offset = buffer;
auto now_ms = std::chrono::time_point_cast<std::chrono::microseconds>(value);
auto epoch = now_ms.time_since_epoch();
auto val = std::chrono::duration_cast<std::chrono::microseconds>(epoch);
int diff = iv->values[6] - (val.count()%1000000);
value += std::chrono::microseconds(diff);
return aTRUE;
}
#else
Tamgu* Tamguclock::set_the_utc_date(Tamgufvector* iv) {
locking_clock(this);
time_t x = 0;
struct tm* temps = localtime(&x);
temps->tm_year = iv->values[0] - 1900;
//Month
temps->tm_mon = iv->values[1] - 1;
temps->tm_mday = iv->values[2];
temps->tm_hour = iv->values[3];
temps->tm_min = iv->values[4];
temps->tm_sec = iv->values[5];
x = mktime(temps);
unlocking_clock(this);
if (x <= 0)
return aFALSE;
value = std::chrono::system_clock::from_time_t(x);
int v = (int)iv->values[7];
double tm = (iv->values[7] - (double)v)*60;
char buffer[100];
if (v >= 0)
sprintf_s(buffer, 100, "+%02d:%02d", v, (int)tm);
else
sprintf_s(buffer, 100, "%02d:%02d", v - 1, (int)tm);
timezone_offset = buffer;
auto now_ms = std::chrono::time_point_cast<std::chrono::microseconds>(value);
auto epoch = now_ms.time_since_epoch();
auto val = std::chrono::duration_cast<std::chrono::microseconds>(epoch);
int diff = iv->values[6] - (val.count()%1000000);
value += std::chrono::microseconds(diff);
return aTRUE;
}
Tamgu* Tamguclock::set_the_iso8601_date(Tamgufvector* iv) {
locking_clock(this);
time_t x = 0;
struct tm* temps = localtime(&x);
temps->tm_year = iv->values[0] - 1900;
//Month
temps->tm_mon = iv->values[1] - 1;
temps->tm_mday = iv->values[2];
temps->tm_hour = iv->values[3];
temps->tm_min = iv->values[4];
temps->tm_sec = iv->values[5];
x = mktime(temps);
unlocking_clock(this);
if (x <= 0)
return aFALSE;
value = std::chrono::system_clock::from_time_t(x);
int v = (int)iv->values[7];
double tm = (iv->values[7] - (double)v)*60;
char buffer[100];
if (v >= 0)
sprintf_s(buffer, 100, "+%02d:%02d", v, (int)tm);
else
sprintf_s(buffer, 100, "%02d:%02d", v - 1, (int)tm);
timezone_offset = buffer;
auto now_ms = std::chrono::time_point_cast<std::chrono::microseconds>(value);
auto epoch = now_ms.time_since_epoch();
auto val = std::chrono::duration_cast<std::chrono::microseconds>(epoch);
int diff = iv->values[6] - (val.count()%1000000);
value += std::chrono::microseconds(diff);
return aTRUE;
}
#endif
Tamgu* Tamguclock::getstringdate(string& v) {
vector<string> values;
string dt;
string mn;
Tamgufvector* fv = globalTamgu->Providefvector();
double tm = -1;
char sep = '-';
bool timezone = false;
for (long i = 0; i < v.size(); i++) {
if (timezone == true && v[i] == ':') {
tm = conversionfloathexa(STR(dt));
dt = "";
}
else {
if (v[i] == sep) {
fv->values.push_back(conversionfloathexa(STR(dt)));
if (timezone)
dt = sep;
else
dt = "";
}
else {
if (v[i] == 'T') {
fv->values.push_back(conversionfloathexa(STR(dt)));
dt = "";
sep = ':';
}
else {
if (v[i] == '.') {
fv->values.push_back(conversionfloathexa(STR(dt)));
if (v.find("+", i) != -1)
sep = '+';
else
sep = '-';
dt = "";
timezone = true;
}
else
dt += v[i];
}
}
}
}
if (dt != "") {
double v = conversionfloathexa(STR(dt));
if (timezone) {
tm += v / 60;
v = tm;
}
fv->values.push_back(v);
}
return fv;
}
Tamgu* Tamguclock::MethodMicroseconds(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
auto now_ms = std::chrono::time_point_cast<std::chrono::microseconds>(value);
auto epoch = now_ms.time_since_epoch();
auto val = std::chrono::duration_cast<std::chrono::microseconds>(epoch);
std::ostringstream os;
os << std::setw(6) << std::setfill('0') << val.count() % 1000000;
return globalTamgu->Providestring(os.str());
}
string Tamguclock::UTC(bool compute) {
std::ostringstream os;
auto now_ms = std::chrono::time_point_cast<std::chrono::microseconds>(value);
auto epoch = now_ms.time_since_epoch();
auto val = std::chrono::duration_cast<std::chrono::microseconds>(epoch);
std::time_t currentTime = std::chrono::system_clock::to_time_t(value);
std::tm* utc_tm = std::gmtime(¤tTime);
string tmoff = timezone_offset;
if (compute)
tmoff = compute_time_zone();
os << std::put_time(utc_tm, "%FT%T");
os << '.' << std::setw(6) << std::setfill('0') << val.count() % 1000000;
os << tmoff;
return os.str();
}
string Tamguclock::ISO8601(bool compute) {
std::ostringstream os;
auto now_ms = std::chrono::time_point_cast<std::chrono::microseconds>(value);
auto epoch = now_ms.time_since_epoch();
auto val = std::chrono::duration_cast<std::chrono::microseconds>(epoch);
std::time_t currentTime = std::chrono::system_clock::to_time_t(value);
std::tm* utc_tm = std::localtime(¤tTime);
string tmoff = timezone_offset;
if (compute)
tmoff = compute_time_zone();
os << std::put_time(utc_tm, "%FT%T");
os << '.' << std::setw(6) << std::setfill('0') << val.count() % 1000000;
os << tmoff;
return os.str();
}
Tamgu* Tamguclock::MethodUTC(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Tamgu* argument = NULL;
if (callfunc->Size() == 1)
argument = callfunc->Evaluate(0, contextualpattern, idthread);
if (callfunc->Size() == 0 || argument->Type() == a_boolean) {
string utc_string;
if (argument != NULL && argument->Boolean())
utc_string = UTC(true);
else
utc_string = UTC(false);
return globalTamgu->Providestring(utc_string);
}
if (argument->Type() == Tamguclock::idtype) {
value = ((Tamguclock*)argument)->value;
return aTRUE;
}
string dte = argument->String();
Tamgufvector* fv = (Tamgufvector*)getstringdate(dte);
if (fv->Size() != 8)
return globalTamgu->Returnerror("Error: Wrong format", idthread);
Tamgu* res = set_the_utc_date(fv);
fv->Release();
return res;
}
Tamgu* Tamguclock::MethodISO8601(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Tamgu* argument = NULL;
if (callfunc->Size() == 1)
argument = callfunc->Evaluate(0, contextualpattern, idthread);
if (callfunc->Size() == 0 || argument->Type() == a_boolean) {
string utc_string;
if (argument != NULL && argument->Boolean())
utc_string = ISO8601(true);
else
utc_string = ISO8601(false);
return globalTamgu->Providestring(utc_string);
}
if (argument->Type() == Tamguclock::idtype) {
value = ((Tamguclock*)argument)->value;
return aTRUE;
}
string dte = argument->String();
Tamgufvector* fv = (Tamgufvector*)getstringdate(dte);
if (fv->Size() != 8)
return globalTamgu->Returnerror("Error: Wrong format", idthread);
Tamgu* res = set_the_iso8601_date(fv);
fv->Release();
return res;
}