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 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 alias StdTime = typeof(SysTime.init.stdTime); // long
38 
39 /// Convert from StdTime to Duration
40 alias stdDur = hnsecs;
41 
42 /// Like SysTime.stdTime.
43 @property StdTime stdTime(Duration d)
44 {
45 	return d.total!"hnsecs"();
46 }
47 
48 @property bool empty(Duration d)
49 {
50 	return !d.stdTime;
51 }
52 
53 /// Workaround SysTime.fracSecs only being available in 2.067,
54 /// and SysTime.fracSec becoming deprecated in the same version.
55 static if (!is(typeof(SysTime.init.fracSecs)))
56 @property Duration fracSecs(SysTime s)
57 {
58 	enum hnsecsPerSecond = convert!("seconds", "hnsecs")(1);
59 	return hnsecs(s.stdTime % hnsecsPerSecond);
60 }
61 
62 /// As above, for Duration.split and Duration.get
63 static if (!is(typeof(Duration.init.split!())))
64 @property auto split(units...)(Duration d)
65 {
66 	static struct Result
67 	{
68 		mixin("long " ~ [units].join(", ") ~ ";");
69 	}
70 
71 	Result result;
72 	foreach (unit; units)
73 	{
74 		static if (is(typeof(d.get!unit))) // unit == "msecs" || unit == "usecs" || unit == "hnsecs" || unit == "nsecs")
75 			long value = d.get!unit();
76 		else
77 			long value = mixin("d.fracSec." ~ unit);
78 		mixin("result." ~ unit ~ " = value;");
79 	}
80 	return result;
81 }
82 
83 // ***************************************************************************
84 
85 // fpdur conflict test
86 static assert(1.5.seconds == 1500.msecs);