This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 424
/
time.d
4010 lines (3443 loc) · 141 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.))
$(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 units)
)
$(TR $(TD $(B To $(LREF Duration)))
$(TD -)
$(TD $(D tickDuration.)$(REF_SHORT to, std,conv)$(D !Duration()))
$(TD $(D dur!"msecs"(5)) or $(D 5.msecs()))
)
$(TR $(TD $(B To $(LREF TickDuration)))
$(TD $(D duration.)$(REF_SHORT to, std,conv)$(D !TickDuration()))
$(TD -)
$(TD $(D TickDuration.from!"msecs"(msecs)))
)
$(TR $(TD $(B To units))
$(TD $(D duration.total!"days"))
$(TD $(D tickDuration.msecs))
$(TD $(D convert!("days", "msecs")(msecs)))
))
Copyright: Copyright 2010 - 2012
License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
Authors: $(HTTP jmdavisprog.com, Jonathan M Davis) and Kato Shoichi
Source: $(DRUNTIMESRC core/_time.d)
Macros:
NBSP=
+/
module core.time;
import core.exception;
import core.stdc.time;
import core.stdc.stdio;
import core.internal.string;
version (Windows)
{
import core.sys.windows.winbase /+: QueryPerformanceCounter, QueryPerformanceFrequency+/;
}
else version (Posix)
{
import core.sys.posix.time;
import core.sys.posix.sys.time;
}
version (OSX)
version = Darwin;
else version (iOS)
version = Darwin;
else version (TVOS)
version = Darwin;
else version (WatchOS)
version = Darwin;
//This probably should be moved somewhere else in druntime which
//is Darwin-specific.
version (Darwin)
{
public import core.sys.darwin.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();
}
}
/++
What type of clock to use with $(LREF MonoTime) / $(LREF MonoTimeImpl) or
$(D std.datetime.Clock.currTime). They default to $(D ClockType.normal),
and most programs do not need to ever deal with the others.
The other $(D ClockType)s are provided so that other clocks provided by the
underlying C, system calls can be used with $(LREF MonoTimeImpl) or
$(D std.datetime.Clock.currTime) without having to use the C API directly.
In the case of the monotonic time, $(LREF MonoTimeImpl) is templatized on
$(D ClockType), whereas with $(D std.datetime.Clock.currTime), its a runtime
argument, since in the case of the monotonic time, the type of the clock
affects the resolution of a $(LREF MonoTimeImpl) object, whereas with
$(REF SysTime, std,datetime), its resolution is always hecto-nanoseconds
regardless of the source of the time.
$(D ClockType.normal), $(D ClockType.coarse), and $(D ClockType.precise)
work with both $(D Clock.currTime) and $(LREF MonoTimeImpl).
$(D ClockType.second) only works with $(D Clock.currTime). The others only
work with $(LREF MonoTimeImpl).
+/
version (CoreDdoc) enum ClockType
{
/++
Use the normal clock.
+/
normal = 0,
/++
$(BLUE Linux,OpenBSD-Only)
Uses $(D CLOCK_BOOTTIME).
+/
bootTime = 1,
/++
Use the coarse clock, not the normal one (e.g. on Linux, that would be
$(D CLOCK_REALTIME_COARSE) instead of $(D CLOCK_REALTIME) for
$(D clock_gettime) if a function is using the realtime clock). It's
generally faster to get the time with the coarse clock than the normal
clock, but it's less precise (e.g. 1 msec instead of 1 usec or 1 nsec).
Howeover, it $(I is) guaranteed to still have sub-second precision
(just not as high as with $(D ClockType.normal)).
On systems which do not support a coarser clock,
$(D MonoTimeImpl!(ClockType.coarse)) will internally use the same clock
as $(D Monotime) does, and $(D Clock.currTime!(ClockType.coarse)) will
use the same clock as $(D Clock.currTime). This is because the coarse
clock is doing the same thing as the normal clock (just at lower
precision), whereas some of the other clock types
(e.g. $(D ClockType.processCPUTime)) mean something fundamentally
different. So, treating those as $(D ClockType.normal) on systems where
they weren't natively supported would give misleading results.
Most programs should not use the coarse clock, exactly because it's
less precise, and most programs don't need to get the time often
enough to care, but for those rare programs that need to get the time
extremely frequently (e.g. hundreds of thousands of times a second) but
don't care about high precision, the coarse clock might be appropriate.
Currently, only Linux and FreeBSD/DragonFlyBSD support a coarser clock, and on other
platforms, it's treated as $(D ClockType.normal).
+/
coarse = 2,
/++
Uses a more precise clock than the normal one (which is already very
precise), but it takes longer to get the time. Similarly to
$(D ClockType.coarse), if it's used on a system that does not support a
more precise clock than the normal one, it's treated as equivalent to
$(D ClockType.normal).
Currently, only FreeBSD/DragonFlyBSD supports a more precise clock, where it uses
$(D CLOCK_MONOTONIC_PRECISE) for the monotonic time and
$(D CLOCK_REALTIME_PRECISE) for the wall clock time.
+/
precise = 3,
/++
$(BLUE Linux,OpenBSD,Solaris-Only)
Uses $(D CLOCK_PROCESS_CPUTIME_ID).
+/
processCPUTime = 4,
/++
$(BLUE Linux-Only)
Uses $(D CLOCK_MONOTONIC_RAW).
+/
raw = 5,
/++
Uses a clock that has a precision of one second (contrast to the coarse
clock, which has sub-second precision like the normal clock does).
FreeBSD/DragonFlyBSD are the only systems which specifically have a clock set up for
this (it has $(D CLOCK_SECOND) to use with $(D clock_gettime) which
takes advantage of an in-kernel cached value), but on other systems, the
fastest function available will be used, and the resulting $(D SysTime)
will be rounded down to the second if the clock that was used gave the
time at a more precise resolution. So, it's guaranteed that the time
will be given at a precision of one second and it's likely the case that
will be faster than $(D ClockType.normal), since there tend to be
several options on a system to get the time at low resolutions, and they
tend to be faster than getting the time at high resolutions.
So, the primary difference between $(D ClockType.coarse) and
$(D ClockType.second) is that $(D ClockType.coarse) sacrifices some
precision in order to get speed but is still fairly precise, whereas
$(D ClockType.second) tries to be as fast as possible at the expense of
all sub-second precision.
+/
second = 6,
/++
$(BLUE Linux,OpenBSD,Solaris-Only)
Uses $(D CLOCK_THREAD_CPUTIME_ID).
+/
threadCPUTime = 7,
/++
$(BLUE DragonFlyBSD,FreeBSD,OpenBSD-Only)
Uses $(D CLOCK_UPTIME).
+/
uptime = 8,
/++
$(BLUE FreeBSD-Only)
Uses $(D CLOCK_UPTIME_FAST).
+/
uptimeCoarse = 9,
/++
$(BLUE FreeBSD-Only)
Uses $(D CLOCK_UPTIME_PRECISE).
+/
uptimePrecise = 10,
}
else version (Windows) enum ClockType
{
normal = 0,
coarse = 2,
precise = 3,
second = 6,
}
else version (Darwin) enum ClockType
{
normal = 0,
coarse = 2,
precise = 3,
second = 6,
}
else version (linux) enum ClockType
{
normal = 0,
bootTime = 1,
coarse = 2,
precise = 3,
processCPUTime = 4,
raw = 5,
second = 6,
threadCPUTime = 7,
}
else version (FreeBSD) enum ClockType
{
normal = 0,
coarse = 2,
precise = 3,
second = 6,
uptime = 8,
uptimeCoarse = 9,
uptimePrecise = 10,
}
else version (NetBSD) enum ClockType
{
normal = 0,
coarse = 2,
precise = 3,
second = 6,
}
else version (OpenBSD) enum ClockType
{
normal = 0,
bootTime = 1,
coarse = 2,
precise = 3,
processCPUTime = 4,
second = 6,
threadCPUTime = 7,
uptime = 8,
}
else version (DragonFlyBSD) enum ClockType
{
normal = 0,
coarse = 2,
precise = 3,
second = 6,
uptime = 8,
uptimeCoarse = 9,
uptimePrecise = 10,
}
else version (Solaris) enum ClockType
{
normal = 0,
coarse = 2,
precise = 3,
processCPUTime = 4,
second = 6,
threadCPUTime = 7,
}
else
{
// It needs to be decided (and implemented in an appropriate version branch
// here) which clock types new platforms are going to support. At minimum,
// the ones _not_ marked with $(D Blue Foo-Only) should be supported.
static assert(0, "What are the clock types supported by this system?");
}
// private, used to translate clock type to proper argument to clock_xxx
// functions on posix systems
version (CoreDdoc)
private int _posixClock(ClockType clockType) { return 0; }
else
version (Posix)
{
private auto _posixClock(ClockType clockType)
{
version (linux)
{
import core.sys.linux.time;
with(ClockType) final switch (clockType)
{
case bootTime: return CLOCK_BOOTTIME;
case coarse: return CLOCK_MONOTONIC_COARSE;
case normal: return CLOCK_MONOTONIC;
case precise: return CLOCK_MONOTONIC;
case processCPUTime: return CLOCK_PROCESS_CPUTIME_ID;
case raw: return CLOCK_MONOTONIC_RAW;
case threadCPUTime: return CLOCK_THREAD_CPUTIME_ID;
case second: assert(0);
}
}
else version (FreeBSD)
{
import core.sys.freebsd.time;
with(ClockType) final switch (clockType)
{
case coarse: return CLOCK_MONOTONIC_FAST;
case normal: return CLOCK_MONOTONIC;
case precise: return CLOCK_MONOTONIC_PRECISE;
case uptime: return CLOCK_UPTIME;
case uptimeCoarse: return CLOCK_UPTIME_FAST;
case uptimePrecise: return CLOCK_UPTIME_PRECISE;
case second: assert(0);
}
}
else version (NetBSD)
{
import core.sys.netbsd.time;
with(ClockType) final switch (clockType)
{
case coarse: return CLOCK_MONOTONIC;
case normal: return CLOCK_MONOTONIC;
case precise: return CLOCK_MONOTONIC;
case second: assert(0);
}
}
else version (OpenBSD)
{
import core.sys.openbsd.time;
with(ClockType) final switch (clockType)
{
case bootTime: return CLOCK_BOOTTIME;
case coarse: return CLOCK_MONOTONIC;
case normal: return CLOCK_MONOTONIC;
case precise: return CLOCK_MONOTONIC;
case processCPUTime: return CLOCK_PROCESS_CPUTIME_ID;
case threadCPUTime: return CLOCK_THREAD_CPUTIME_ID;
case uptime: return CLOCK_UPTIME;
case second: assert(0);
}
}
else version (DragonFlyBSD)
{
import core.sys.dragonflybsd.time;
with(ClockType) final switch (clockType)
{
case coarse: return CLOCK_MONOTONIC_FAST;
case normal: return CLOCK_MONOTONIC;
case precise: return CLOCK_MONOTONIC_PRECISE;
case uptime: return CLOCK_UPTIME;
case uptimeCoarse: return CLOCK_UPTIME_FAST;
case uptimePrecise: return CLOCK_UPTIME_PRECISE;
case second: assert(0);
}
}
else version (Solaris)
{
import core.sys.solaris.time;
with(ClockType) final switch (clockType)
{
case coarse: return CLOCK_MONOTONIC;
case normal: return CLOCK_MONOTONIC;
case precise: return CLOCK_MONOTONIC;
case processCPUTime: return CLOCK_PROCESS_CPUTIME_ID;
case threadCPUTime: return CLOCK_THREAD_CPUTIME_ID;
case second: assert(0);
}
}
else
// It needs to be decided (and implemented in an appropriate
// version branch here) which clock types new platforms are going
// to support. Also, ClockType's documentation should be updated to
// mention it if a new platform uses anything that's not supported
// on all platforms..
assert(0, "What are the monotonic clock types supported by this system?");
}
}
unittest
{
// Make sure that the values are the same across platforms.
static if (is(typeof(ClockType.normal))) static assert(ClockType.normal == 0);
static if (is(typeof(ClockType.bootTime))) static assert(ClockType.bootTime == 1);
static if (is(typeof(ClockType.coarse))) static assert(ClockType.coarse == 2);
static if (is(typeof(ClockType.precise))) static assert(ClockType.precise == 3);
static if (is(typeof(ClockType.processCPUTime))) static assert(ClockType.processCPUTime == 4);
static if (is(typeof(ClockType.raw))) static assert(ClockType.raw == 5);
static if (is(typeof(ClockType.second))) static assert(ClockType.second == 6);
static if (is(typeof(ClockType.threadCPUTime))) static assert(ClockType.threadCPUTime == 7);
static if (is(typeof(ClockType.uptime))) static assert(ClockType.uptime == 8);
static if (is(typeof(ClockType.uptimeCoarse))) static assert(ClockType.uptimeCoarse == 9);
static if (is(typeof(ClockType.uptimePrecise))) static assert(ClockType.uptimePrecise == 10);
}
/++
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 $(REF Thread.sleep, core,thread).
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,
$(REF Date, std,datetime) 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 $(REF Date, std,datetime). But Duration is used when dealing
with weeks or smaller.
Examples:
--------------------
import std.datetime;
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); }
version (CoreUnittest) 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
{
return (_hnsecs > rhs._hnsecs) - (_hnsecs < rhs._hnsecs);
}
version (CoreUnittest) unittest
{
import core.internal.traits : rvalueOf;
foreach (T; AliasSeq!(Duration, const Duration, immutable Duration))
{
foreach (U; AliasSeq!(Duration, const Duration, immutable Duration))
{
T t = 42;
// workaround https://issues.dlang.org/show_bug.cgi?id=18296
version (D_Coverage)
U u = T(t._hnsecs);
else
U u = t;
assert(t == u);
assert(rvalueOf(t) == u);
assert(t == rvalueOf(u));
}
}
foreach (D; AliasSeq!(Duration, const Duration, immutable Duration))
{
foreach (E; AliasSeq!(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(rvalueOf(cast(D)Duration(12)).opCmp(cast(E)Duration(12)) == 0);
assert(rvalueOf(cast(D)Duration(-12)).opCmp(cast(E)Duration(-12)) == 0);
assert(rvalueOf(cast(D)Duration(10)).opCmp(cast(E)Duration(12)) < 0);
assert(rvalueOf(cast(D)Duration(-12)).opCmp(cast(E)Duration(12)) < 0);
assert(rvalueOf(cast(D)Duration(12)).opCmp(cast(E)Duration(10)) > 0);
assert(rvalueOf(cast(D)Duration(12)).opCmp(cast(E)Duration(-12)) > 0);
assert((cast(D)Duration(12)).opCmp(rvalueOf(cast(E)Duration(12))) == 0);
assert((cast(D)Duration(-12)).opCmp(rvalueOf(cast(E)Duration(-12))) == 0);
assert((cast(D)Duration(10)).opCmp(rvalueOf(cast(E)Duration(12))) < 0);
assert((cast(D)Duration(-12)).opCmp(rvalueOf(cast(E)Duration(12))) < 0);
assert((cast(D)Duration(12)).opCmp(rvalueOf(cast(E)Duration(10))) > 0);
assert((cast(D)Duration(12)).opCmp(rvalueOf(cast(E)Duration(-12))) > 0);
}
}
}
/++
Adds, subtracts or calculates the modulo of 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 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 == "-" || op == "%") && is(immutable D == immutable Duration)) ||
((op == "+" || op == "-") && is(immutable D == immutable TickDuration)))
{
static if (is(immutable D == immutable Duration))
return Duration(mixin("_hnsecs " ~ op ~ " rhs._hnsecs"));
else
return Duration(mixin("_hnsecs " ~ op ~ " rhs.hnsecs"));
}
version (CoreUnittest) unittest
{
foreach (D; AliasSeq!(Duration, const Duration, immutable Duration))
{
foreach (E; AliasSeq!(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(5)) % (cast(E)Duration(7)) == Duration(5));
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(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(5)) % (cast(E)Duration(-7)) == Duration(5));
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(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(-5)) % (cast(E)Duration(7)) == Duration(-5));
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(-7)) % (cast(E)Duration(5)) == Duration(-2));
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(-5)) % (cast(E)Duration(7)) == Duration(-5));
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(-7)) % (cast(E)Duration(5)) == Duration(-2));
}
foreach (T; AliasSeq!(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(immutable D == immutable TickDuration))
{
return Duration(mixin("lhs.hnsecs " ~ op ~ " _hnsecs"));
}
version (CoreUnittest) unittest
{
foreach (D; AliasSeq!(Duration, const Duration, immutable Duration))
{
foreach (T; AliasSeq!(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, subtracts or calculates the modulo of 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 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)(const scope D rhs) nothrow @nogc
if (((op == "+" || op == "-" || op == "%") && is(immutable D == immutable Duration)) ||
((op == "+" || op == "-") && is(immutable D == immutable TickDuration)))
{
static if (is(immutable D == immutable Duration))
mixin("_hnsecs " ~ op ~ "= rhs._hnsecs;");
else
mixin("_hnsecs " ~ op ~ "= rhs.hnsecs;");
return this;
}
version (CoreUnittest) 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; AliasSeq!(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(5), (cast(E)Duration(7)), Duration(5));
test1!"+="(Duration(7), (cast(E)Duration(5)), Duration(12));
test1!"-="(Duration(7), (cast(E)Duration(5)), Duration(2));
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(5), (cast(E)Duration(-7)), Duration(5));
test1!"+="(Duration(7), (cast(E)Duration(-5)), 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(-5), (cast(E)Duration(7)), Duration(-5));
test1!"+="(Duration(-7), (cast(E)Duration(5)), 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(-12));
test1!"-="(Duration(-5), (cast(E)Duration(-7)), Duration(2));
test1!"%="(Duration(-5), (cast(E)Duration(-7)), Duration(-5));
test1!"+="(Duration(-7), (cast(E)Duration(-5)), Duration(-12));
test1!"-="(Duration(-7), (cast(E)Duration(-5)), Duration(-2));
test1!"%="(Duration(-7), (cast(E)Duration(-5)), Duration(-2));
}
foreach (T; AliasSeq!(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; AliasSeq!(const Duration, immutable Duration))
{
foreach (E; AliasSeq!(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);
}
}
}
/++
Multiplies or divides the duration by an integer value.
The legal types of arithmetic for $(D Duration) using this operator
overload are
$(TABLE
$(TR $(TD Duration) $(TD *) $(TD long) $(TD -->) $(TD Duration))
$(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 == "*" || op == "/")
{
mixin("return Duration(_hnsecs " ~ op ~ " value);");
}
version (CoreUnittest) unittest
{
foreach (D; AliasSeq!(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));
}
}
version (CoreUnittest) unittest
{
foreach (D; AliasSeq!(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));
}
}
/++
Multiplies/Divides the duration by an integer value as well as
assigning the result to this $(D Duration).
The legal types of arithmetic for $(D Duration) using this operator
overload are
$(TABLE
$(TR $(TD Duration) $(TD *) $(TD long) $(TD -->) $(TD Duration))
$(TR $(TD Duration) $(TD /) $(TD long) $(TD -->) $(TD Duration))
)
Params:
value = The value to multiply/divide this $(D Duration) by.
+/
ref Duration opOpAssign(string op)(long value) nothrow @nogc
if (op == "*" || op == "/")
{
mixin("_hnsecs " ~ op ~ "= value;");
return this;
}
version (CoreUnittest) 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));
}
version (CoreUnittest) unittest
{
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));
}
/++
Divides two durations.
The legal types of arithmetic for $(D Duration) using this operator are