This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
/
time.d
4488 lines (3790 loc) · 151 KB
/
time.d
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
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Written in the D programming language
/++
Module containing core time functionality, such as $(LREF Duration) (which
represents a duration of time) or $(LREF MonoTime) (which represents a
timestamp of the system's monotonic clock).
Various functions take a string (or strings) to represent a unit of time
(e.g. $(D convert!("days", "hours")(numDays))). The valid strings to use
with such functions are "years", "months", "weeks", "days", "hours",
"minutes", "seconds", "msecs" (milliseconds), "usecs" (microseconds),
"hnsecs" (hecto-nanoseconds - i.e. 100 ns) or some subset thereof. There
are a few functions that also allow "nsecs", but very little actually
has precision greater than hnsecs.
$(BOOKTABLE Cheat Sheet,
$(TR $(TH Symbol) $(TH Description))
$(LEADINGROW Types)
$(TR $(TDNW $(LREF Duration)) $(TD Represents a duration of time of weeks
or less (kept internally as hnsecs). (e.g. 22 days or 700 seconds).))
$(TR $(TDNW $(LREF TickDuration)) $(TD Represents a duration of time in
system clock ticks, using the highest precision that the system provides.))
$(TR $(TDNW $(LREF MonoTime)) $(TD Represents a monotonic timestamp in
system clock ticks, using the highest precision that the system provides.))
$(TR $(TDNW $(LREF FracSec)) $(TD Represents fractional seconds
(portions of time smaller than a second).))
$(LEADINGROW Functions)
$(TR $(TDNW $(LREF convert)) $(TD Generic way of converting between two time
units.))
$(TR $(TDNW $(LREF dur)) $(TD Allows constructing a $(LREF Duration) from
the given time units with the given length.))
$(TR $(TDNW $(LREF weeks)$(NBSP)$(LREF days)$(NBSP)$(LREF hours)$(BR)
$(LREF minutes)$(NBSP)$(LREF seconds)$(NBSP)$(LREF msecs)$(BR)
$(LREF usecs)$(NBSP)$(LREF hnsecs)$(NBSP)$(LREF nsecs))
$(TD Convenience aliases for $(LREF dur).))
$(TR $(TDNW $(LREF abs)) $(TD Returns the absolute value of a duration.))
)
$(BOOKTABLE Conversions,
$(TR $(TH )
$(TH From $(LREF Duration))
$(TH From $(LREF TickDuration))
$(TH From $(LREF FracSec))
$(TH From units)
)
$(TR $(TD $(B To $(LREF Duration)))
$(TD -)
$(TD $(D tickDuration.)$(SXREF conv, to)$(D !Duration()))
$(TD -)
$(TD $(D dur!"msecs"(5)) or $(D 5.msecs()))
)
$(TR $(TD $(B To $(LREF TickDuration)))
$(TD $(D duration.)$(SXREF conv, to)$(D !TickDuration()))
$(TD -)
$(TD -)
$(TD $(D TickDuration.from!"msecs"(msecs)))
)
$(TR $(TD $(B To $(LREF FracSec)))
$(TD $(D duration.fracSec))
$(TD -)
$(TD -)
$(TD $(D FracSec.from!"msecs"(msecs)))
)
$(TR $(TD $(B To units))
$(TD $(D duration.total!"days"))
$(TD $(D tickDuration.msecs))
$(TD $(D fracSec.msecs))
$(TD $(D convert!("days", "msecs")(msecs)))
))
Copyright: Copyright 2010 - 2012
License: $(WEB www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
Authors: Jonathan M Davis and Kato Shoichi
Source: $(DRUNTIMESRC core/_time.d)
Macros:
NBSP=
SXREF=<a href="std_$1.html#$2">$(D $2)</a>
+/
module core.time;
import core.exception;
import core.stdc.time;
import core.stdc.stdio;
import core.internal.traits : _Unqual = Unqual;
version(Windows)
{
import core.sys.windows.windows;
}
else version(Posix)
{
import core.sys.posix.time;
import core.sys.posix.sys.time;
}
//This probably should be moved somewhere else in druntime which
//is OSX-specific.
version(OSX)
{
public import core.sys.osx.mach.kern_return;
extern(C) nothrow @nogc
{
struct mach_timebase_info_data_t
{
uint numer;
uint denom;
}
alias mach_timebase_info_data_t* mach_timebase_info_t;
kern_return_t mach_timebase_info(mach_timebase_info_t);
ulong mach_absolute_time();
}
}
//To verify that an lvalue isn't required.
version(unittest) T copy(T)(T t)
{
return t;
}
/++
Represents a duration of time of weeks or less (kept internally as hnsecs).
(e.g. 22 days or 700 seconds).
It is used when representing a duration of time - such as how long to
sleep with $(CXREF Thread, sleep).
In std.datetime, it is also used as the result of various arithmetic
operations on time points.
Use the $(LREF dur) function or one of its non-generic aliases to create
$(D Duration)s.
It's not possible to create a Duration of months or years, because the
variable number of days in a month or year makes it impossible to convert
between months or years and smaller units without a specific date. So,
nothing uses $(D Duration)s when dealing with months or years. Rather,
functions specific to months and years are defined. For instance,
$(XREF datetime, Date) has $(D add!"years") and $(D add!"months") for adding
years and months rather than creating a Duration of years or months and
adding that to a $(XREF datetime, Date). But Duration is used when dealing
with weeks or smaller.
Examples:
--------------------
assert(dur!"days"(12) == dur!"hnsecs"(10_368_000_000_000L));
assert(dur!"hnsecs"(27) == dur!"hnsecs"(27));
assert(std.datetime.Date(2010, 9, 7) + dur!"days"(5) ==
std.datetime.Date(2010, 9, 12));
assert(days(-12) == dur!"hnsecs"(-10_368_000_000_000L));
assert(hnsecs(-27) == dur!"hnsecs"(-27));
assert(std.datetime.Date(2010, 9, 7) - std.datetime.Date(2010, 10, 3) ==
days(-26));
--------------------
+/
struct Duration
{
@safe pure:
public:
/++
A $(D Duration) of $(D 0). It's shorter than doing something like
$(D dur!"seconds"(0)) and more explicit than $(D Duration.init).
+/
static @property nothrow @nogc Duration zero() { return Duration(0); }
/++
Largest $(D Duration) possible.
+/
static @property nothrow @nogc Duration max() { return Duration(long.max); }
/++
Most negative $(D Duration) possible.
+/
static @property nothrow @nogc Duration min() { return Duration(long.min); }
unittest
{
assert(zero == dur!"seconds"(0));
assert(Duration.max == Duration(long.max));
assert(Duration.min == Duration(long.min));
assert(Duration.min < Duration.zero);
assert(Duration.zero < Duration.max);
assert(Duration.min < Duration.max);
assert(Duration.min - dur!"hnsecs"(1) == Duration.max);
assert(Duration.max + dur!"hnsecs"(1) == Duration.min);
}
/++
Compares this $(D Duration) with the given $(D Duration).
Returns:
$(TABLE
$(TR $(TD this < rhs) $(TD < 0))
$(TR $(TD this == rhs) $(TD 0))
$(TR $(TD this > rhs) $(TD > 0))
)
+/
int opCmp(Duration rhs) const nothrow @nogc
{
if(_hnsecs < rhs._hnsecs)
return -1;
if(_hnsecs > rhs._hnsecs)
return 1;
return 0;
}
unittest
{
foreach(T; _TypeTuple!(Duration, const Duration, immutable Duration))
{
foreach(U; _TypeTuple!(Duration, const Duration, immutable Duration))
{
T t = 42;
U u = t;
assert(t == u);
assert(copy(t) == u);
assert(t == copy(u));
}
}
foreach(D; _TypeTuple!(Duration, const Duration, immutable Duration))
{
foreach(E; _TypeTuple!(Duration, const Duration, immutable Duration))
{
assert((cast(D)Duration(12)).opCmp(cast(E)Duration(12)) == 0);
assert((cast(D)Duration(-12)).opCmp(cast(E)Duration(-12)) == 0);
assert((cast(D)Duration(10)).opCmp(cast(E)Duration(12)) < 0);
assert((cast(D)Duration(-12)).opCmp(cast(E)Duration(12)) < 0);
assert((cast(D)Duration(12)).opCmp(cast(E)Duration(10)) > 0);
assert((cast(D)Duration(12)).opCmp(cast(E)Duration(-12)) > 0);
assert(copy(cast(D)Duration(12)).opCmp(cast(E)Duration(12)) == 0);
assert(copy(cast(D)Duration(-12)).opCmp(cast(E)Duration(-12)) == 0);
assert(copy(cast(D)Duration(10)).opCmp(cast(E)Duration(12)) < 0);
assert(copy(cast(D)Duration(-12)).opCmp(cast(E)Duration(12)) < 0);
assert(copy(cast(D)Duration(12)).opCmp(cast(E)Duration(10)) > 0);
assert(copy(cast(D)Duration(12)).opCmp(cast(E)Duration(-12)) > 0);
assert((cast(D)Duration(12)).opCmp(copy(cast(E)Duration(12))) == 0);
assert((cast(D)Duration(-12)).opCmp(copy(cast(E)Duration(-12))) == 0);
assert((cast(D)Duration(10)).opCmp(copy(cast(E)Duration(12))) < 0);
assert((cast(D)Duration(-12)).opCmp(copy(cast(E)Duration(12))) < 0);
assert((cast(D)Duration(12)).opCmp(copy(cast(E)Duration(10))) > 0);
assert((cast(D)Duration(12)).opCmp(copy(cast(E)Duration(-12))) > 0);
}
}
}
/++
Adds or subtracts two durations.
The legal types of arithmetic for $(D Duration) using this operator are
$(TABLE
$(TR $(TD Duration) $(TD +) $(TD Duration) $(TD -->) $(TD Duration))
$(TR $(TD Duration) $(TD -) $(TD Duration) $(TD -->) $(TD Duration))
$(TR $(TD Duration) $(TD +) $(TD TickDuration) $(TD -->) $(TD Duration))
$(TR $(TD Duration) $(TD -) $(TD TickDuration) $(TD -->) $(TD Duration))
)
Params:
rhs = The duration to add to or subtract from this $(D Duration).
+/
Duration opBinary(string op, D)(D rhs) const nothrow @nogc
if((op == "+" || op == "-") &&
(is(_Unqual!D == Duration) ||
is(_Unqual!D == TickDuration)))
{
static if(is(_Unqual!D == Duration))
return Duration(mixin("_hnsecs " ~ op ~ " rhs._hnsecs"));
else if(is(_Unqual!D == TickDuration))
return Duration(mixin("_hnsecs " ~ op ~ " rhs.hnsecs"));
}
unittest
{
foreach(D; _TypeTuple!(Duration, const Duration, immutable Duration))
{
foreach(E; _TypeTuple!(Duration, const Duration, immutable Duration))
{
assert((cast(D)Duration(5)) + (cast(E)Duration(7)) == Duration(12));
assert((cast(D)Duration(5)) - (cast(E)Duration(7)) == Duration(-2));
assert((cast(D)Duration(7)) + (cast(E)Duration(5)) == Duration(12));
assert((cast(D)Duration(7)) - (cast(E)Duration(5)) == Duration(2));
assert((cast(D)Duration(5)) + (cast(E)Duration(-7)) == Duration(-2));
assert((cast(D)Duration(5)) - (cast(E)Duration(-7)) == Duration(12));
assert((cast(D)Duration(7)) + (cast(E)Duration(-5)) == Duration(2));
assert((cast(D)Duration(7)) - (cast(E)Duration(-5)) == Duration(12));
assert((cast(D)Duration(-5)) + (cast(E)Duration(7)) == Duration(2));
assert((cast(D)Duration(-5)) - (cast(E)Duration(7)) == Duration(-12));
assert((cast(D)Duration(-7)) + (cast(E)Duration(5)) == Duration(-2));
assert((cast(D)Duration(-7)) - (cast(E)Duration(5)) == Duration(-12));
assert((cast(D)Duration(-5)) + (cast(E)Duration(-7)) == Duration(-12));
assert((cast(D)Duration(-5)) - (cast(E)Duration(-7)) == Duration(2));
assert((cast(D)Duration(-7)) + (cast(E)Duration(-5)) == Duration(-12));
assert((cast(D)Duration(-7)) - (cast(E)Duration(-5)) == Duration(-2));
}
foreach(T; _TypeTuple!(TickDuration, const TickDuration, immutable TickDuration))
{
assertApprox((cast(D)Duration(5)) + cast(T)TickDuration.from!"usecs"(7), Duration(70), Duration(80));
assertApprox((cast(D)Duration(5)) - cast(T)TickDuration.from!"usecs"(7), Duration(-70), Duration(-60));
assertApprox((cast(D)Duration(7)) + cast(T)TickDuration.from!"usecs"(5), Duration(52), Duration(62));
assertApprox((cast(D)Duration(7)) - cast(T)TickDuration.from!"usecs"(5), Duration(-48), Duration(-38));
assertApprox((cast(D)Duration(5)) + cast(T)TickDuration.from!"usecs"(-7), Duration(-70), Duration(-60));
assertApprox((cast(D)Duration(5)) - cast(T)TickDuration.from!"usecs"(-7), Duration(70), Duration(80));
assertApprox((cast(D)Duration(7)) + cast(T)TickDuration.from!"usecs"(-5), Duration(-48), Duration(-38));
assertApprox((cast(D)Duration(7)) - cast(T)TickDuration.from!"usecs"(-5), Duration(52), Duration(62));
assertApprox((cast(D)Duration(-5)) + cast(T)TickDuration.from!"usecs"(7), Duration(60), Duration(70));
assertApprox((cast(D)Duration(-5)) - cast(T)TickDuration.from!"usecs"(7), Duration(-80), Duration(-70));
assertApprox((cast(D)Duration(-7)) + cast(T)TickDuration.from!"usecs"(5), Duration(38), Duration(48));
assertApprox((cast(D)Duration(-7)) - cast(T)TickDuration.from!"usecs"(5), Duration(-62), Duration(-52));
assertApprox((cast(D)Duration(-5)) + cast(T)TickDuration.from!"usecs"(-7), Duration(-80), Duration(-70));
assertApprox((cast(D)Duration(-5)) - cast(T)TickDuration.from!"usecs"(-7), Duration(60), Duration(70));
assertApprox((cast(D)Duration(-7)) + cast(T)TickDuration.from!"usecs"(-5), Duration(-62), Duration(-52));
assertApprox((cast(D)Duration(-7)) - cast(T)TickDuration.from!"usecs"(-5), Duration(38), Duration(48));
}
}
}
/++
Adds or subtracts two durations.
The legal types of arithmetic for $(D Duration) using this operator are
$(TABLE
$(TR $(TD TickDuration) $(TD +) $(TD Duration) $(TD -->) $(TD Duration))
$(TR $(TD TickDuration) $(TD -) $(TD Duration) $(TD -->) $(TD Duration))
)
Params:
lhs = The $(D TickDuration) to add to this $(D Duration) or to
subtract this $(D Duration) from.
+/
Duration opBinaryRight(string op, D)(D lhs) const nothrow @nogc
if((op == "+" || op == "-") &&
is(_Unqual!D == TickDuration))
{
return Duration(mixin("lhs.hnsecs " ~ op ~ " _hnsecs"));
}
unittest
{
foreach(D; _TypeTuple!(Duration, const Duration, immutable Duration))
{
foreach(T; _TypeTuple!(TickDuration, const TickDuration, immutable TickDuration))
{
assertApprox((cast(T)TickDuration.from!"usecs"(7)) + cast(D)Duration(5), Duration(70), Duration(80));
assertApprox((cast(T)TickDuration.from!"usecs"(7)) - cast(D)Duration(5), Duration(60), Duration(70));
assertApprox((cast(T)TickDuration.from!"usecs"(5)) + cast(D)Duration(7), Duration(52), Duration(62));
assertApprox((cast(T)TickDuration.from!"usecs"(5)) - cast(D)Duration(7), Duration(38), Duration(48));
assertApprox((cast(T)TickDuration.from!"usecs"(-7)) + cast(D)Duration(5), Duration(-70), Duration(-60));
assertApprox((cast(T)TickDuration.from!"usecs"(-7)) - cast(D)Duration(5), Duration(-80), Duration(-70));
assertApprox((cast(T)TickDuration.from!"usecs"(-5)) + cast(D)Duration(7), Duration(-48), Duration(-38));
assertApprox((cast(T)TickDuration.from!"usecs"(-5)) - cast(D)Duration(7), Duration(-62), Duration(-52));
assertApprox((cast(T)TickDuration.from!"usecs"(7)) + (cast(D)Duration(-5)), Duration(60), Duration(70));
assertApprox((cast(T)TickDuration.from!"usecs"(7)) - (cast(D)Duration(-5)), Duration(70), Duration(80));
assertApprox((cast(T)TickDuration.from!"usecs"(5)) + (cast(D)Duration(-7)), Duration(38), Duration(48));
assertApprox((cast(T)TickDuration.from!"usecs"(5)) - (cast(D)Duration(-7)), Duration(52), Duration(62));
assertApprox((cast(T)TickDuration.from!"usecs"(-7)) + cast(D)Duration(-5), Duration(-80), Duration(-70));
assertApprox((cast(T)TickDuration.from!"usecs"(-7)) - cast(D)Duration(-5), Duration(-70), Duration(-60));
assertApprox((cast(T)TickDuration.from!"usecs"(-5)) + cast(D)Duration(-7), Duration(-62), Duration(-52));
assertApprox((cast(T)TickDuration.from!"usecs"(-5)) - cast(D)Duration(-7), Duration(-48), Duration(-38));
}
}
}
/++
Adds or subtracts two durations as well as assigning the result to this
$(D Duration).
The legal types of arithmetic for $(D Duration) using this operator are
$(TABLE
$(TR $(TD Duration) $(TD +) $(TD Duration) $(TD -->) $(TD Duration))
$(TR $(TD Duration) $(TD -) $(TD Duration) $(TD -->) $(TD Duration))
$(TR $(TD Duration) $(TD +) $(TD TickDuration) $(TD -->) $(TD Duration))
$(TR $(TD Duration) $(TD -) $(TD TickDuration) $(TD -->) $(TD Duration))
)
Params:
rhs = The duration to add to or subtract from this $(D Duration).
+/
ref Duration opOpAssign(string op, D)(in D rhs) nothrow @nogc
if((op == "+" || op == "-") &&
(is(_Unqual!D == Duration) ||
is(_Unqual!D == TickDuration)))
{
static if(is(_Unqual!D == Duration))
mixin("_hnsecs " ~ op ~ "= rhs._hnsecs;");
else if(is(_Unqual!D == TickDuration))
mixin("_hnsecs " ~ op ~ "= rhs.hnsecs;");
return this;
}
unittest
{
static void test1(string op, E)(Duration actual, in E rhs, Duration expected, size_t line = __LINE__)
{
if(mixin("actual " ~ op ~ " rhs") != expected)
throw new AssertError("op failed", __FILE__, line);
if(actual != expected)
throw new AssertError("op assign failed", __FILE__, line);
}
static void test2(string op, E)
(Duration actual, in E rhs, Duration lower, Duration upper, size_t line = __LINE__)
{
assertApprox(mixin("actual " ~ op ~ " rhs"), lower, upper, "op failed", line);
assertApprox(actual, lower, upper, "op assign failed", line);
}
foreach(E; _TypeTuple!(Duration, const Duration, immutable Duration))
{
test1!"+="(Duration(5), (cast(E)Duration(7)), Duration(12));
test1!"-="(Duration(5), (cast(E)Duration(7)), Duration(-2));
test1!"+="(Duration(7), (cast(E)Duration(5)), Duration(12));
test1!"-="(Duration(7), (cast(E)Duration(5)), Duration(2));
test1!"+="(Duration(5), (cast(E)Duration(-7)), Duration(-2));
test1!"-="(Duration(5), (cast(E)Duration(-7)), Duration(12));
test1!"+="(Duration(7), (cast(E)Duration(-5)), Duration(2));
test1!"-="(Duration(7), (cast(E)Duration(-5)), Duration(12));
test1!"+="(Duration(-5), (cast(E)Duration(7)), Duration(2));
test1!"-="(Duration(-5), (cast(E)Duration(7)), Duration(-12));
test1!"+="(Duration(-7), (cast(E)Duration(5)), Duration(-2));
test1!"-="(Duration(-7), (cast(E)Duration(5)), Duration(-12));
test1!"+="(Duration(-5), (cast(E)Duration(-7)), Duration(-12));
test1!"-="(Duration(-5), (cast(E)Duration(-7)), Duration(2));
test1!"+="(Duration(-7), (cast(E)Duration(-5)), Duration(-12));
test1!"-="(Duration(-7), (cast(E)Duration(-5)), Duration(-2));
}
foreach(T; _TypeTuple!(TickDuration, const TickDuration, immutable TickDuration))
{
test2!"+="(Duration(5), cast(T)TickDuration.from!"usecs"(7), Duration(70), Duration(80));
test2!"-="(Duration(5), cast(T)TickDuration.from!"usecs"(7), Duration(-70), Duration(-60));
test2!"+="(Duration(7), cast(T)TickDuration.from!"usecs"(5), Duration(52), Duration(62));
test2!"-="(Duration(7), cast(T)TickDuration.from!"usecs"(5), Duration(-48), Duration(-38));
test2!"+="(Duration(5), cast(T)TickDuration.from!"usecs"(-7), Duration(-70), Duration(-60));
test2!"-="(Duration(5), cast(T)TickDuration.from!"usecs"(-7), Duration(70), Duration(80));
test2!"+="(Duration(7), cast(T)TickDuration.from!"usecs"(-5), Duration(-48), Duration(-38));
test2!"-="(Duration(7), cast(T)TickDuration.from!"usecs"(-5), Duration(52), Duration(62));
test2!"+="(Duration(-5), cast(T)TickDuration.from!"usecs"(7), Duration(60), Duration(70));
test2!"-="(Duration(-5), cast(T)TickDuration.from!"usecs"(7), Duration(-80), Duration(-70));
test2!"+="(Duration(-7), cast(T)TickDuration.from!"usecs"(5), Duration(38), Duration(48));
test2!"-="(Duration(-7), cast(T)TickDuration.from!"usecs"(5), Duration(-62), Duration(-52));
test2!"+="(Duration(-5), cast(T)TickDuration.from!"usecs"(-7), Duration(-80), Duration(-70));
test2!"-="(Duration(-5), cast(T)TickDuration.from!"usecs"(-7), Duration(60), Duration(70));
test2!"+="(Duration(-7), cast(T)TickDuration.from!"usecs"(-5), Duration(-62), Duration(-52));
test2!"-="(Duration(-7), cast(T)TickDuration.from!"usecs"(-5), Duration(38), Duration(48));
}
foreach(D; _TypeTuple!(const Duration, immutable Duration))
{
foreach(E; _TypeTuple!(Duration, const Duration, immutable Duration,
TickDuration, const TickDuration, immutable TickDuration))
{
D lhs = D(120);
E rhs = E(120);
static assert(!__traits(compiles, lhs += rhs), D.stringof ~ " " ~ E.stringof);
}
}
}
/++
The legal types of arithmetic for $(D Duration) using this operator
overload are
$(TABLE
$(TR $(TD Duration) $(TD *) $(TD long) $(TD -->) $(TD Duration))
)
Params:
value = The value to multiply this $(D Duration) by.
+/
Duration opBinary(string op)(long value) const nothrow @nogc
if(op == "*")
{
return Duration(_hnsecs * value);
}
unittest
{
foreach(D; _TypeTuple!(Duration, const Duration, immutable Duration))
{
assert((cast(D)Duration(5)) * 7 == Duration(35));
assert((cast(D)Duration(7)) * 5 == Duration(35));
assert((cast(D)Duration(5)) * -7 == Duration(-35));
assert((cast(D)Duration(7)) * -5 == Duration(-35));
assert((cast(D)Duration(-5)) * 7 == Duration(-35));
assert((cast(D)Duration(-7)) * 5 == Duration(-35));
assert((cast(D)Duration(-5)) * -7 == Duration(35));
assert((cast(D)Duration(-7)) * -5 == Duration(35));
assert((cast(D)Duration(5)) * 0 == Duration(0));
assert((cast(D)Duration(-5)) * 0 == Duration(0));
}
}
/++
The legal types of arithmetic for $(D Duration) using this operator
overload are
$(TABLE
$(TR $(TD Duration) $(TD *) $(TD long) $(TD -->) $(TD Duration))
)
Params:
value = The value to multiply this $(D Duration) by.
+/
ref Duration opOpAssign(string op)(long value) nothrow @nogc
if(op == "*")
{
_hnsecs *= value;
return this;
}
unittest
{
static void test(D)(D actual, long value, Duration expected, size_t line = __LINE__)
{
if((actual *= value) != expected)
throw new AssertError("op failed", __FILE__, line);
if(actual != expected)
throw new AssertError("op assign failed", __FILE__, line);
}
test(Duration(5), 7, Duration(35));
test(Duration(7), 5, Duration(35));
test(Duration(5), -7, Duration(-35));
test(Duration(7), -5, Duration(-35));
test(Duration(-5), 7, Duration(-35));
test(Duration(-7), 5, Duration(-35));
test(Duration(-5), -7, Duration(35));
test(Duration(-7), -5, Duration(35));
test(Duration(5), 0, Duration(0));
test(Duration(-5), 0, Duration(0));
const cdur = Duration(12);
immutable idur = Duration(12);
static assert(!__traits(compiles, cdur *= 12));
static assert(!__traits(compiles, idur *= 12));
}
/++
The legal types of arithmetic for $(D Duration) using this operator
overload are
$(TABLE
$(TR $(TD Duration) $(TD /) $(TD long) $(TD -->) $(TD Duration))
)
Params:
value = The value to divide from this duration.
Throws:
$(D TimeException) if an attempt to divide by $(D 0) is made.
+/
Duration opBinary(string op)(long value) const
if(op == "/")
{
if(value == 0)
throw new TimeException("Attempted division by 0.");
return Duration(_hnsecs / value);
}
unittest
{
//Unfortunately, putting these inside of the foreach loop results in
//linker errors regarding multiple definitions and the lambdas.
_assertThrown!TimeException((){Duration(5) / 0;}());
_assertThrown!TimeException((){Duration(-5) / 0;}());
_assertThrown!TimeException((){(cast(const Duration)Duration(5)) / 0;}());
_assertThrown!TimeException((){(cast(const Duration)Duration(-5)) / 0;}());
_assertThrown!TimeException((){(cast(immutable Duration)Duration(5)) / 0;}());
_assertThrown!TimeException((){(cast(immutable Duration)Duration(-5)) / 0;}());
foreach(D; _TypeTuple!(Duration, const Duration, immutable Duration))
{
assert((cast(D)Duration(5)) / 7 == Duration(0));
assert((cast(D)Duration(7)) / 5 == Duration(1));
assert((cast(D)Duration(5)) / -7 == Duration(0));
assert((cast(D)Duration(7)) / -5 == Duration(-1));
assert((cast(D)Duration(-5)) / 7 == Duration(0));
assert((cast(D)Duration(-7)) / 5 == Duration(-1));
assert((cast(D)Duration(-5)) / -7 == Duration(0));
assert((cast(D)Duration(-7)) / -5 == Duration(1));
}
}
/++
The legal types of arithmetic for $(D Duration) using this operator
overload are
$(TABLE
$(TR $(TD Duration) $(TD /) $(TD long) $(TD -->) $(TD Duration))
)
Params:
value = The value to divide from this $(D Duration).
Throws:
$(D TimeException) if an attempt to divide by $(D 0) is made.
+/
ref Duration opOpAssign(string op)(long value)
if(op == "/")
{
if(value == 0)
throw new TimeException("Attempted division by 0.");
_hnsecs /= value;
return this;
}
unittest
{
_assertThrown!TimeException((){Duration(5) /= 0;}());
_assertThrown!TimeException((){Duration(-5) /= 0;}());
static void test(Duration actual, long value, Duration expected, size_t line = __LINE__)
{
if((actual /= value) != expected)
throw new AssertError("op failed", __FILE__, line);
if(actual != expected)
throw new AssertError("op assign failed", __FILE__, line);
}
test(Duration(5), 7, Duration(0));
test(Duration(7), 5, Duration(1));
test(Duration(5), -7, Duration(0));
test(Duration(7), -5, Duration(-1));
test(Duration(-5), 7, Duration(0));
test(Duration(-7), 5, Duration(-1));
test(Duration(-5), -7, Duration(0));
test(Duration(-7), -5, Duration(1));
const cdur = Duration(12);
immutable idur = Duration(12);
static assert(!__traits(compiles, cdur /= 12));
static assert(!__traits(compiles, idur /= 12));
}
/++
Multiplies an integral value and a $(D Duration).
The legal types of arithmetic for $(D Duration) using this operator
overload are
$(TABLE
$(TR $(TD long) $(TD *) $(TD Duration) $(TD -->) $(TD Duration))
)
Params:
value = The number of units to multiply this $(D Duration) by.
+/
Duration opBinaryRight(string op)(long value) const nothrow @nogc
if(op == "*")
{
return opBinary!op(value);
}
unittest
{
foreach(D; _TypeTuple!(Duration, const Duration, immutable Duration))
{
assert(5 * cast(D)Duration(7) == Duration(35));
assert(7 * cast(D)Duration(5) == Duration(35));
assert(5 * cast(D)Duration(-7) == Duration(-35));
assert(7 * cast(D)Duration(-5) == Duration(-35));
assert(-5 * cast(D)Duration(7) == Duration(-35));
assert(-7 * cast(D)Duration(5) == Duration(-35));
assert(-5 * cast(D)Duration(-7) == Duration(35));
assert(-7 * cast(D)Duration(-5) == Duration(35));
assert(0 * cast(D)Duration(-5) == Duration(0));
assert(0 * cast(D)Duration(5) == Duration(0));
}
}
/++
Returns the negation of this $(D Duration).
+/
Duration opUnary(string op)() const nothrow @nogc
if(op == "-")
{
return Duration(-_hnsecs);
}
unittest
{
foreach(D; _TypeTuple!(Duration, const Duration, immutable Duration))
{
assert(-(cast(D)Duration(7)) == Duration(-7));
assert(-(cast(D)Duration(5)) == Duration(-5));
assert(-(cast(D)Duration(-7)) == Duration(7));
assert(-(cast(D)Duration(-5)) == Duration(5));
assert(-(cast(D)Duration(0)) == Duration(0));
}
}
/++
Returns a $(LREF TickDuration) with the same number of hnsecs as this
$(D Duration).
Note that the conventional way to convert between $(D Duration) and
$(D TickDuration) is using $(XREF conv, to), e.g.:
$(D duration.to!TickDuration())
+/
TickDuration opCast(T)() const nothrow @nogc
if(is(_Unqual!T == TickDuration))
{
return TickDuration.from!"hnsecs"(_hnsecs);
}
unittest
{
foreach(D; _TypeTuple!(Duration, const Duration, immutable Duration))
{
foreach(units; _TypeTuple!("seconds", "msecs", "usecs", "hnsecs"))
{
enum unitsPerSec = convert!("seconds", units)(1);
if(TickDuration.ticksPerSec >= unitsPerSec)
{
foreach(T; _TypeTuple!(TickDuration, const TickDuration, immutable TickDuration))
{
auto t = TickDuration.from!units(1);
assertApprox(cast(T)cast(D)dur!units(1), t - TickDuration(1), t + TickDuration(1), units);
t = TickDuration.from!units(2);
assertApprox(cast(T)cast(D)dur!units(2), t - TickDuration(1), t + TickDuration(1), units);
}
}
else
{
auto t = TickDuration.from!units(1);
assert(t.to!(units, long)() == 0, units);
t = TickDuration.from!units(1_000_000);
assert(t.to!(units, long)() >= 900_000, units);
assert(t.to!(units, long)() <= 1_100_000, units);
}
}
}
}
//Temporary hack until bug http://d.puremagic.com/issues/show_bug.cgi?id=5747 is fixed.
Duration opCast(T)() const nothrow @nogc
if(is(_Unqual!T == Duration))
{
return this;
}
/++
Splits out the Duration into the given units.
split takes the list of time units to split out as template arguments.
The time unit strings must be given in decreasing order. How it returns
the values for those units depends on the overload used.
The overload which accepts function arguments takes integral types in
the order that the time unit strings were given, and those integers are
passed by $(D ref). split assigns the values for the units to each
corresponding integer. Any integral type may be used, but no attempt is
made to prevent integer overflow, so don't use small integral types in
circumstances where the values for those units aren't likely to fit in
an integral type that small.
The overload with no arguments returns the values for the units in a
struct with members whose names are the same as the given time unit
strings. The members are all $(D long)s. This overload will also work
with no time strings being given, in which case $(I all) of the time
units from weeks through hnsecs will be provided (but no nsecs, since it
would always be $(D 0)).
For both overloads, the entire value of the Duration is split among the
units (rather than splitting the Duration across all units and then only
providing the values for the requested units), so if only one unit is
given, the result is equivalent to $(LREF total).
$(D "nsecs") is accepted by split, but $(D "years") and $(D "months")
are not.
For negative durations, all of the split values will be negative.
+/
template split(units...)
if(allAreAcceptedUnits!("weeks", "days", "hours", "minutes", "seconds",
"msecs", "usecs", "hnsecs", "nsecs")(units) &&
unitsAreInDescendingOrder(units))
{
/++ Ditto +/
void split(Args...)(out Args args) const nothrow @nogc
if(units.length != 0 && args.length == units.length && allAreMutableIntegralTypes!Args)
{
long hnsecs = _hnsecs;
foreach(i, unit; units)
{
static if(unit == "nsecs")
args[i] = cast(typeof(args[i]))convert!("hnsecs", "nsecs")(hnsecs);
else
args[i] = cast(typeof(args[i]))splitUnitsFromHNSecs!unit(hnsecs);
}
}
/++ Ditto +/
auto split() const nothrow @nogc
{
static if(units.length == 0)
return split!("weeks", "days", "hours", "minutes", "seconds", "msecs", "usecs", "hnsecs")();
else
{
static string genMemberDecls()
{
string retval;
foreach(unit; units)
{
retval ~= "long ";
retval ~= unit;
retval ~= "; ";
}
return retval;
}
static struct SplitUnits
{
mixin(genMemberDecls());
}
static string genSplitCall()
{
auto retval = "split(";
foreach(i, unit; units)
{
retval ~= "su.";
retval ~= unit;
if(i < units.length - 1)
retval ~= ", ";
else
retval ~= ");";
}
return retval;
}
SplitUnits su = void;
mixin(genSplitCall());
return su;
}
}
/+
Whether all of the given arguments are integral types.
+/
private template allAreMutableIntegralTypes(Args...)
{
static if(Args.length == 0)
enum allAreMutableIntegralTypes = true;
else static if(!is(Args[0] == long) &&
!is(Args[0] == int) &&
!is(Args[0] == short) &&
!is(Args[0] == byte) &&
!is(Args[0] == ulong) &&
!is(Args[0] == uint) &&
!is(Args[0] == ushort) &&
!is(Args[0] == ubyte))
{
enum allAreMutableIntegralTypes = false;
}
else
enum allAreMutableIntegralTypes = allAreMutableIntegralTypes!(Args[1 .. $]);
}
unittest
{
foreach(T; _TypeTuple!(long, int, short, byte, ulong, uint, ushort, ubyte))
static assert(allAreMutableIntegralTypes!T);
foreach(T; _TypeTuple!(long, int, short, byte, ulong, uint, ushort, ubyte))
static assert(!allAreMutableIntegralTypes!(const T));
foreach(T; _TypeTuple!(char, wchar, dchar, float, double, real, string))
static assert(!allAreMutableIntegralTypes!T);
static assert(allAreMutableIntegralTypes!(long, int, short, byte));
static assert(!allAreMutableIntegralTypes!(long, int, short, char, byte));
static assert(!allAreMutableIntegralTypes!(long, int*, short));
}
}
///
unittest
{
{
auto d = dur!"days"(12) + dur!"minutes"(7) + dur!"usecs"(501223);
long days;
int seconds;
short msecs;
d.split!("days", "seconds", "msecs")(days, seconds, msecs);
assert(days == 12);
assert(seconds == 7 * 60);
assert(msecs == 501);
auto splitStruct = d.split!("days", "seconds", "msecs")();
assert(splitStruct.days == 12);
assert(splitStruct.seconds == 7 * 60);
assert(splitStruct.msecs == 501);
auto fullSplitStruct = d.split();
assert(fullSplitStruct.weeks == 1);
assert(fullSplitStruct.days == 5);
assert(fullSplitStruct.hours == 0);
assert(fullSplitStruct.minutes == 7);
assert(fullSplitStruct.seconds == 0);
assert(fullSplitStruct.msecs == 501);
assert(fullSplitStruct.usecs == 223);
assert(fullSplitStruct.hnsecs == 0);
assert(d.split!"minutes"().minutes == d.total!"minutes");
}
{
auto d = dur!"days"(12);
assert(d.split!"weeks"().weeks == 1);
assert(d.split!"days"().days == 12);
assert(d.split().weeks == 1);
assert(d.split().days == 5);
}
{
auto d = dur!"days"(7) + dur!"hnsecs"(42);
assert(d.split!("seconds", "nsecs")().nsecs == 4200);
}
{
auto d = dur!"days"(-7) + dur!"hours"(-9);
auto result = d.split!("days", "hours")();
assert(result.days == -7);