1 /**
2  * Time string formatting and such.
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <ae@cy.md>
12  */
13 
14 module ae.utils.time;
15 
16 public import ae.utils.time.common;
17 public import ae.utils.time.format;
18 public import ae.utils.time.fpdur;
19 public import ae.utils.time.parse;
20 public import ae.utils.time.parsedur;
21 
22 unittest
23 {
24 	import core.stdc.time : time_t;
25 
26 	enum f = `U\.9`;
27 	static if (time_t.sizeof == 4)
28 		assert("1234567890.123456789".parseTime!f.formatTime!f == "1234567890.123456700");
29 	else
30 		assert("123456789012.123456789".parseTime!f.formatTime!f == "123456789012.123456700");
31 }
32 
33 // ***************************************************************************
34 
35 import std.datetime;
36 
37 /// `typeof(SysTime.stdTime)`, the numeric type used to store absolute time in D.
38 alias StdTime = typeof(SysTime.init.stdTime); // long
39 
40 /// Convert from `StdTime` to `Duration`.
41 alias stdDur = hnsecs;
42 
43 /// Like `SysTime.stdTime`.
44 @property StdTime stdTime(Duration d)
45 {
46 	return d.total!"hnsecs"();
47 }
48 
49 /// `true` when the duration `d` is zero.
50 @property bool empty(Duration d)
51 {
52 	return !d.stdTime;
53 }
54 
55 /// Workaround SysTime.fracSecs only being available in 2.067,
56 /// and SysTime.fracSec becoming deprecated in the same version.
57 static if (!is(typeof(SysTime.init.fracSecs)))
58 @property Duration fracSecs(SysTime s)
59 {
60 	enum hnsecsPerSecond = convert!("seconds", "hnsecs")(1);
61 	return hnsecs(s.stdTime % hnsecsPerSecond);
62 }
63 
64 /// As above, for Duration.split and Duration.get
65 static if (!is(typeof(Duration.init.split!())))
66 @property auto split(units...)(Duration d)
67 {
68 	static struct Result
69 	{
70 		mixin("long " ~ [units].join(", ") ~ ";");
71 	}
72 
73 	Result result;
74 	foreach (unit; units)
75 	{
76 		static if (is(typeof(d.get!unit))) // unit == "msecs" || unit == "usecs" || unit == "hnsecs" || unit == "nsecs")
77 			long value = d.get!unit();
78 		else
79 			long value = mixin("d.fracSec." ~ unit);
80 		mixin("result." ~ unit ~ " = value;");
81 	}
82 	return result;
83 }
84 
85 // ***************************************************************************
86 
87 // fpdur conflict test
88 static assert(1.5.seconds == 1500.msecs);