The OpenD Programming Language

1 // Written in the D programming language
2 
3 /++
4     $(SCRIPT inhibitQuickIndex = 1;)
5 
6     Phobos provides the following functionality for time:
7 
8     $(DIVC quickindex,
9     $(BOOKTABLE ,
10     $(TR $(TH Functionality) $(TH Symbols)
11     )
12     $(TR
13         $(TD Points in Time)
14         $(TD
15             $(REF_ALTTEXT Clock, Clock, std, datetime, systime)$(NBSP)
16             $(REF_ALTTEXT Date, Date, std, datetime, date)$(NBSP)
17             $(REF_ALTTEXT TimeOfDay, TimeOfDay, std, datetime, date)$(NBSP)
18             $(REF_ALTTEXT DateTime, DateTime, std, datetime, date)$(NBSP)
19             $(REF_ALTTEXT SysTime, SysTime, std, datetime, systime)$(NBSP)
20         )
21     )
22     $(TR
23         $(TD $(MREF_ALTTEXT Timezones, std, datetime, timezone))
24         $(TD
25             $(REF_ALTTEXT TimeZone, TimeZone, std, datetime, timezone)$(NBSP)
26             $(REF_ALTTEXT UTC, UTC, std, datetime, timezone)$(NBSP)
27             $(REF_ALTTEXT LocalTime, LocalTime, std, datetime, timezone)$(NBSP)
28             $(REF_ALTTEXT PosixTimeZone, PosixTimeZone, std, datetime, timezone)$(NBSP)
29             $(REF_ALTTEXT WindowsTimeZone, WindowsTimeZone, std, datetime, timezone)$(NBSP)
30             $(REF_ALTTEXT SimpleTimeZone, SimpleTimeZone, std, datetime, timezone)$(NBSP)
31         )
32     )
33     $(TR
34         $(TD Intervals and Ranges of Time)
35         $(TD
36             $(REF_ALTTEXT Interval, Interval, std, datetime, interval)$(NBSP)
37             $(REF_ALTTEXT PosInfInterval, PosInfInterval, std, datetime, interval)$(NBSP)
38             $(REF_ALTTEXT NegInfInterval, NegInfInterval, std, datetime, interval)$(NBSP)
39         )
40     )
41     $(TR
42         $(TD $(MREF_ALTTEXT Durations of Time, core, time))
43         $(TD
44             $(REF_ALTTEXT Duration, Duration, core, time)$(NBSP)
45             $(REF_ALTTEXT weeks, weeks, core, time)$(NBSP)
46             $(REF_ALTTEXT days, days, core, time)$(NBSP)
47             $(REF_ALTTEXT hours, hours, core, time)$(NBSP)
48             $(REF_ALTTEXT minutes, minutes, core, time)$(NBSP)
49             $(REF_ALTTEXT seconds, seconds, core, time)$(NBSP)
50             $(REF_ALTTEXT msecs, msecs, core, time)$(NBSP)
51             $(REF_ALTTEXT usecs, usecs, core, time)$(NBSP)
52             $(REF_ALTTEXT hnsecs, hnsecs, core, time)$(NBSP)
53             $(REF_ALTTEXT nsecs, nsecs, core, time)$(NBSP)
54         )
55     )
56     $(TR
57         $(TD Time Measurement and Benchmarking)
58         $(TD
59             $(REF_ALTTEXT MonoTime, MonoTime, core, time)$(NBSP)
60             $(REF_ALTTEXT StopWatch, StopWatch, std, datetime, stopwatch)$(NBSP)
61             $(REF_ALTTEXT benchmark, benchmark, std, datetime, stopwatch)$(NBSP)
62         )
63     )
64     ))
65 
66     This functionality is separated into the following modules:
67 
68     $(UL
69         $(LI $(MREF std, datetime, date) for points in time without timezones.)
70         $(LI $(MREF std, datetime, timezone) for classes which represent timezones.)
71         $(LI $(MREF std, datetime, systime) for a point in time with a timezone.)
72         $(LI $(MREF std, datetime, interval) for types which represent series of points in time.)
73         $(LI $(MREF std, datetime, stopwatch) for measuring time.)
74     )
75 
76     See_Also:
77         $(MREF core, time)$(BR)
78         $(DDLINK intro-to-datetime, Introduction to std.datetime,
79                  Introduction to std&#46;datetime)<br>
80         $(HTTP en.wikipedia.org/wiki/ISO_8601, ISO 8601)<br>
81         $(HTTP en.wikipedia.org/wiki/Tz_database,
82               Wikipedia entry on TZ Database)<br>
83         $(HTTP en.wikipedia.org/wiki/List_of_tz_database_time_zones,
84               List of Time Zones)<br>
85 
86     License:   $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
87     Authors:   $(HTTP jmdavisprog.com, Jonathan M Davis) and Kato Shoichi
88     Source:    $(PHOBOSSRC std/datetime/package.d)
89 +/
90 module std.datetime;
91 
92 /// Get the current time from the system clock.
93 @safe unittest
94 {
95     import std.datetime.systime : SysTime, Clock;
96 
97     SysTime currentTime = Clock.currTime();
98 }
99 
100 /**
101 Construct a specific point in time without timezone information
102 and get its ISO string.
103  */
104 @safe unittest
105 {
106     import std.datetime.date : DateTime;
107 
108     auto dt = DateTime(2018, 1, 1, 12, 30, 10);
109     assert(dt.toISOString() == "20180101T123010");
110     assert(dt.toISOExtString() == "2018-01-01T12:30:10");
111 }
112 
113 /**
114 Construct a specific point in time in the UTC timezone and
115 add two days.
116  */
117 @safe unittest
118 {
119     import std.datetime.systime : SysTime;
120     import std.datetime.timezone : UTC;
121     import core.time : days;
122 
123     auto st = SysTime(DateTime(2018, 1, 1, 12, 30, 10), UTC());
124     assert(st.toISOExtString() == "2018-01-01T12:30:10Z");
125     st += 2.days;
126     assert(st.toISOExtString() == "2018-01-03T12:30:10Z");
127 }
128 
129 public import core.time;
130 public import std.datetime.date;
131 public import std.datetime.interval;
132 public import std.datetime.systime;
133 public import std.datetime.timezone;
134 
135 import core.exception : AssertError;
136 import std.functional : unaryFun;
137 import std.traits;
138 import std.typecons : Flag, Yes, No;
139 
140 
141 // Verify module example.
142 @safe unittest
143 {
144     auto currentTime = Clock.currTime();
145     auto timeString = currentTime.toISOExtString();
146     auto restoredTime = SysTime.fromISOExtString(timeString);
147 }
148 
149 // Verify Examples for core.time.Duration which couldn't be in core.time.
150 @safe unittest
151 {
152     assert(std.datetime.Date(2010, 9, 7) + dur!"days"(5) ==
153            std.datetime.Date(2010, 9, 12));
154 
155     assert(std.datetime.Date(2010, 9, 7) - std.datetime.Date(2010, 10, 3) ==
156            dur!"days"(-26));
157 }
158 
159 @safe unittest
160 {
161     import std.traits : hasUnsharedAliasing;
162     /* https://issues.dlang.org/show_bug.cgi?id=6642 */
163     static assert(!hasUnsharedAliasing!Date);
164     static assert(!hasUnsharedAliasing!TimeOfDay);
165     static assert(!hasUnsharedAliasing!DateTime);
166     static assert(!hasUnsharedAliasing!SysTime);
167 }