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 <vladimir@thecybershadow.net>
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 // ***************************************************************************
23 
24 import std.datetime;
25 
26 alias StdTime = typeof(SysTime.init.stdTime); // long
27 
28 @property bool empty(Duration d)
29 {
30 	return !d.total!"hnsecs"();
31 }
32 
33 /// Workaround SysTime.fracSecs only being available in 2.067,
34 /// and SysTime.fracSec becoming deprecated in the same version.
35 static if (!is(typeof(SysTime.init.fracSecs)))
36 @property Duration fracSecs(SysTime s)
37 {
38 	enum hnsecsPerSecond = convert!("seconds", "hnsecs")(1);
39 	return hnsecs(s.stdTime % hnsecsPerSecond);
40 }
41 
42 /// As above, for Duration.split and Duration.get
43 static if (!is(typeof(Duration.init.split!())))
44 @property auto split(units...)(Duration d)
45 {
46 	static struct Result
47 	{
48 		mixin("long " ~ [units].join(", ") ~ ";");
49 	}
50 
51 	Result result;
52 	foreach (unit; units)
53 	{
54 		static if (is(typeof(d.get!unit))) // unit == "msecs" || unit == "usecs" || unit == "hnsecs" || unit == "nsecs")
55 			long value = d.get!unit();
56 		else
57 			long value = mixin("d.fracSec." ~ unit);
58 		mixin("result." ~ unit ~ " = value;");
59 	}
60 	return result;
61 }
62 
63 // ***************************************************************************
64 
65 // fpdur conflict test
66 static assert(1.5.seconds == 1500.msecs);