1 /*********************************************************************************************** 2 * Copyright: © 2017-2021 UI Manufaktur UG 3 * License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. 4 * Authors: UI Manufaktur Team 5 * Documentation [DE]: https://ui-manufaktur.com/docu/uim-core/dataytypes/datetime 6 ************************************************************************************************/ 7 module uim.core.datatypes.datetime; 8 9 @safe: 10 import uim.core; 11 12 enum startUNIX = DateTime(1970, 1, 1, 0, 0, 0); 13 14 @safe long toTimestamp(SysTime untilTime) { 15 return (untilTime - cast(SysTime)startUNIX).total!"hnsecs"(); 16 } 17 @safe SysTime fromTimestamp(long aTimestamp) { 18 return (cast(SysTime)startUNIX + aTimestamp.hnsecs); 19 } 20 @safe long toJSTimestamp(long jsTimestamp) { 21 return (fromJSTimestamp(jsTimestamp) - cast(SysTime)startUNIX).total!"msecs"(); 22 } 23 @safe SysTime fromJSTimestamp(long jsTimestamp) { 24 return (cast(SysTime)startUNIX + jsTimestamp.msecs); 25 } 26 27 // Current SysTime based on System Clock 28 @safe auto now() { 29 return Clock.currTime(); } 30 unittest { 31 auto now1 = now; auto now2 = now; 32 assert(now2 >= now1); 33 } 34 35 // Current DateTime based on System Clock 36 @safe DateTime nowDateTime() { 37 return cast(DateTime)now; } 38 unittest { 39 auto dt1 = nowDateTime; auto dt2 = nowDateTime; 40 assert(dt2 >= dt1); 41 } 42 43 /// convert time to region format using SysTime 44 @safe string timeToDateString(size_t time, string regionFormat = "DE") { 45 auto sysTime = SysTime(time); 46 auto day = to!string(sysTime.day); 47 auto mon = to!string(cast(int)sysTime.month); 48 auto year = to!string(sysTime.year); 49 auto hour = to!string(sysTime.hour); 50 auto min = to!string(sysTime.minute); 51 auto sec = to!string(sysTime.second); 52 53 switch(regionFormat) { 54 case "UK": 55 return "%s/%s/%s - %s:%s:%s".format(day, mon, year, hour, min, sec); 56 case "US": 57 return "%s/%s/%s - %s:%s:%s".format(mon, day, year, hour, min, sec); 58 default: 59 return "%s. %s. %s - %s:%s:%s".format(day, mon, year, hour, min, sec); 60 } 61 } 62 unittest{ 63 64 } 65 66 /// Convert timestamp to DateTime 67 @safe string timestampToDateTimeDE(string timeStamp) { return timestampToDateTimeDE(to!size_t(timeStamp)); } 68 @safe string timestampToDateTimeDE(size_t timeStamp) { return SysTime(timeStamp).toISOExtString.split(".")[0].replace("T", " "); } 69 unittest{ 70 /// TODO 71 } 72 73 /// Convert now to Javascript 74 @safe long nowForJs() { 75 auto jsTime = DateTime(1970, 1, 1, 0, 0, 0); 76 auto dTime = cast(DateTime)now(); 77 return (dTime - jsTime).total!"msecs"; 78 } 79 unittest{ 80 /// TODO 81 } 82 83 /// Convert DateTime to Javascript 84 @safe long datetimeForJs(string dt) { 85 auto jsTime = DateTime(1970, 1, 1, 0, 0, 0); 86 auto dTime = cast(DateTime)SysTime.fromISOExtString(dt); 87 return (dTime-jsTime).total!"msecs"; 88 } 89 unittest{ 90 /// TODO 91 } 92 93 /// Convert Javascript to dateTime 94 @safe DateTime jsToDatetime(long jsTime) { 95 auto result = DateTime(1970, 1, 1, 0, 0, 0)+msecs(jsTime); 96 return cast(DateTime)result; 97 } 98 unittest{ 99 /// TODO 100 } 101 102 /// Convert dateTime to german Date string 103 @safe string germanDate(long dt) { 104 return germanDate(cast(DateTime)fromTimestamp(dt)); 105 } 106 @safe string germanDate(DateTime dt) { 107 auto strDay = to!string(dt.day); 108 if (strDay.length < 2) strDay = "0"~strDay; 109 110 auto strMonth = to!string(cast(int)dt.month); 111 if (strMonth.length < 2) strMonth = "0"~strMonth; 112 113 auto strYear = to!string(dt.year); 114 return "%s.%s.%s".format(strDay, strMonth, strYear); 115 } 116 unittest{ 117 /// TODO 118 } 119 120 // Convert dateTime to ISO string 121 @safe string isoDate(DateTime dt) { 122 auto m = (cast(int)dt.month < 10 ? "0"~to!string(cast(int)dt.month) : to!string(cast(int)dt.month)); 123 auto d = (dt.day < 10 ? "0"~to!string(dt.day) : to!string(dt.day)); 124 return "%s-%s-%s".format(dt.year, m, d); 125 } 126 unittest{ 127 /// TODO 128 } 129 130 /// Convert dateTiem to german Date string 131 @safe string toYYYYMMDD(SysTime datetime, string separator = "") { 132 return toYYYYMMDD(cast(DateTime)datetime, separator); 133 } 134 @safe string toYYYYMMDD(DateTime datetime, string separator = "") { 135 string[] results; 136 results ~= to!string(datetime.year); 137 results ~= (datetime.month < 10 ? "0" : "")~to!string(to!int(datetime.month)); 138 results ~= (datetime.day < 10 ? "0" : "")~to!string(datetime.day); 139 return results.join(separator); 140 } 141 unittest{ 142 assert(DateTime(Date(1999, 7, 6)).toYYYYMMDD("-") == "1999-07-06"); 143 }