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