Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #671: TimeSpan.d

File TimeSpan.d, 8.3 kB (added by schveiguy, 4 years ago)

TimeSpan file

Line 
1 /*******************************************************************************
2
3         copyright:      Copyright (c) 2007 Kris Bell. All rights reserved
4
5         license:        BSD style: $(LICENSE)
6         
7         version:        Oct 2007: Initial release
8         
9         author:         Steve Schveighoffer
10
11         Common types used by Tango
12
13 *******************************************************************************/
14
15 module tango.core.TimeSpan;
16
17 struct TimeSpan
18 {
19         long ticks;
20
21         //
22         // enum to allow specifying standard measurements
23         //
24         enum Measure
25         {
26                 Day,
27                 Hour,
28                 Minute,
29                 Second,
30                 Millisecond,
31                 Microsecond,
32                 Nanosecond,
33         }
34
35         //
36         // useful constants
37         //
38         static const long NanosecondsPerTick = 100;
39         static const long TicksPerMicrosecond = 1000 / NanosecondsPerTick;
40         static const long TicksPerMillisecond = TicksPerMicrosecond * 1000;
41         static const long TicksPerSecond = TicksPerMillisecond * 1000;
42         static const long TicksPerMinute = TicksPerSecond * 60;
43         static const long TicksPerHour = TicksPerMinute * 60;
44         static const long TicksPerDay = TicksPerHour * 24;
45
46         static const long MillisPerSecond = 1000;
47         static const long MillisPerMinute = MillisPerSecond * 60;
48         static const long MillisPerHour = MillisPerMinute * 60;
49         static const long MillisPerDay = MillisPerHour * 24;
50
51         static const long DaysPerYear = 365;
52         static const long DaysPer4Years = DaysPerYear * 4 + 1;
53         static const long DaysPer100Years = DaysPer4Years * 25 - 1;
54         static const long DaysPer400Years = DaysPer100Years * 4 + 1;
55
56         static const long DaysTo1601 = DaysPer400Years * 4;
57         static const long DaysTo10000 = DaysPer400Years * 25 - 366;
58
59         static const TimeSpan    min = {long.min},
60                                  max = {long.max};
61
62         //
63         // this causes problems with old compilers (DMD 1.018)
64         //
65         /*static TimeSpan opCall(long ticks)
66         {
67                 TimeSpan t;
68                 t.ticks = ticks;
69                 return t;
70         }*/
71
72         //
73         // common operators
74         //
75         bool opEquals(TimeSpan t)
76         {
77                 return ticks is t.ticks;
78         }
79
80         long opCmp(TimeSpan t)
81         {
82                 return ticks - t.ticks;
83         }
84
85         TimeSpan opAdd(TimeSpan t)
86         {
87                 return TimeSpan(ticks + t.ticks);
88         }
89
90         TimeSpan opAddAssign(TimeSpan t)
91         {
92                 ticks += t.ticks;
93                 return *this;
94         }
95
96         TimeSpan opSub(TimeSpan t)
97         {
98                 return TimeSpan(ticks - t.ticks);
99         }
100
101         TimeSpan opSubAssign(TimeSpan t)
102         {
103                 ticks -= t.ticks;
104                 return *this;
105         }
106
107         TimeSpan opMul(long v)
108         {
109                 return TimeSpan(ticks * v);
110         }
111
112         TimeSpan opMulAssign(long v)
113         {
114                 ticks *= v;
115                 return *this;
116         }
117
118         //
119         // should not be used for scaling to other units.  Use the toXXX
120         // methods for that.  This is more for using to sleep for half a
121         // duration instead of the full duration.
122         //
123         TimeSpan opDiv(long v)
124         {
125                 return TimeSpan(ticks / v);
126         }
127
128         TimeSpan opDivAssign(long v)
129         {
130                 ticks /= v;
131                 return *this;
132         }
133
134         //
135         // conversion routines.  Converts to the specific measure.  This does
136         // not return only the part of the timespan that is in that
137         // measurement.  For example, if a timespan represents 1.5 seconds,
138         // toMilliseconds returns 1500, not 500.
139         //
140         long convertTo(Measure m)
141         {
142                 switch(m)
143                 {
144                 case Measure.Day:
145                         return ticks / TicksPerDay;
146                 case Measure.Hour:
147                         return ticks / TicksPerHour;
148                 case Measure.Minute:
149                         return ticks / TicksPerMinute;
150                 case Measure.Second:
151                         return ticks / TicksPerSecond;
152                 case Measure.Millisecond:
153                         return ticks / TicksPerMillisecond;
154                 case Measure.Microsecond:
155                         return ticks / TicksPerMicrosecond;
156                 case Measure.Nanosecond:
157                         return ticks * NanosecondsPerTick;
158                 }
159                 return long.max;
160         }
161
162         long toDays()
163         {
164                 return convertTo(Measure.Day);
165         }
166
167         long toHours()
168         {
169                 return convertTo(Measure.Hour);
170         }
171
172         long toMinutes()
173         {
174                 return convertTo(Measure.Minute);
175         }
176
177         long toSeconds()
178         {
179                 return convertTo(Measure.Second);
180         }
181
182         long toMilliseconds()
183         {
184                 return convertTo(Measure.Millisecond);
185         }
186
187         long toMicroseconds()
188         {
189                 return convertTo(Measure.Microsecond);
190         }
191
192         long toNanoseconds()
193         {
194                 return convertTo(Measure.Nanosecond);
195         }
196
197         //
198         //  Construction routines.  Build a timespan from common measurements
199         //
200
201         //  TODO: need a better name for this
202         //  this is useful for converting from other structures which have
203         //  representations of time in multiple measurement types.  For
204         //  example, a timeval which has a seconds member and a microseconds
205         //  member.
206         static TimeSpan fromStandard(long days, long hours = 0, long minutes = 0, long seconds = 0, long milliseconds = 0, long microseconds = 0, long nanoseconds = 0)
207         {
208                 return TimeSpan(days * TicksPerDay +
209                                 hours * TicksPerHour +
210                                 minutes * TicksPerMinute +
211                                 seconds * TicksPerSecond +
212                                 milliseconds * TicksPerMillisecond +
213                                 microseconds * TicksPerMicrosecond +
214                                 nanoseconds / NanosecondsPerTick);
215         }
216
217         static TimeSpan convertFrom(long value, Measure m)
218         {
219                 long ticks = long.max;
220                 switch(m)
221                 {
222                 case Measure.Day:
223                         ticks = value * TicksPerDay;
224                         break;
225                 case Measure.Hour:
226                         ticks = value * TicksPerHour;
227                         break;
228                 case Measure.Minute:
229                         ticks = value * TicksPerMinute;
230                         break;
231                 case Measure.Second:
232                         ticks = value * TicksPerSecond;
233                         break;
234                 case Measure.Millisecond:
235                         ticks = value * TicksPerMillisecond;
236                         break;
237                 case Measure.Microsecond:
238                         ticks = value * TicksPerMicrosecond;
239                         break;
240                 case Measure.Nanosecond:
241                         ticks = value / NanosecondsPerTick;
242                         break;
243                 }
244                 return TimeSpan(ticks);
245         }
246
247         static TimeSpan fromDays(long days)
248         {
249                 return convertFrom(days, Measure.Day);
250         }
251
252         static TimeSpan fromHours(long hours)
253         {
254                 return convertFrom(hours, Measure.Hour);
255         }
256
257         static TimeSpan fromMinutes(long minutes)
258         {
259                 return convertFrom(minutes, Measure.Minute);
260         }
261
262         static TimeSpan fromSeconds(long seconds)
263         {
264                 return convertFrom(seconds, Measure.Second);
265         }
266
267         static TimeSpan fromMilliseconds(long milliseconds)
268         {
269                 return convertFrom(milliseconds, Measure.Millisecond);
270         }
271
272         static TimeSpan fromMicroseconds(long microseconds)
273         {
274                 return convertFrom(microseconds, Measure.Microsecond);
275         }
276
277         static TimeSpan fromNanoseconds(long nanoseconds)
278         {
279                 return convertFrom(nanoseconds, Measure.Nanosecond);
280         }
281
282         // TODO: need a toUtf8 routine here?
283 }