@@ -4,6 +4,7 @@ Timestamp
44module mir.timestamp ;
55
66private alias isDigit = (dchar c) => uint (c - ' 0' ) < 10 ;
7+ import mir.serde: serdeIgnore;
78
89version (D_Exceptions)
910// /
@@ -106,6 +107,8 @@ struct Timestamp
106107 }
107108 }
108109
110+ @serdeIgnore:
111+
109112 /+ +
110113 Year
111114 +/
@@ -258,6 +261,133 @@ struct Timestamp
258261 this .precision = Precision.fraction;
259262 }
260263
264+ // /
265+ this (Date )(const Date datetime)
266+ if (Date .stringof == " Date" || Date .stringof == " date" )
267+ {
268+ static if (__traits(hasMember, Date , " yearMonthDay" ))
269+ with (datetime.yearMonthDay) this (year, cast (ubyte )month, day);
270+ else
271+ with (datetime) this (year, month, day);
272+ }
273+
274+ // /
275+ version (mir_test)
276+ @safe unittest {
277+ import mir.date : Date ;
278+ auto dt = Date (1982 , 4 , 1 );
279+ Timestamp ts = dt;
280+ assert (dt.toISOExtString == ts.toString);
281+ assert (dt == cast (Date ) ts);
282+ }
283+
284+ // /
285+ version (mir_test)
286+ @safe unittest {
287+ import std.datetime.date : Date ;
288+ auto dt = Date (1982 , 4 , 1 );
289+ Timestamp ts = dt;
290+ assert (dt.toISOExtString == ts.toString);
291+ assert (dt == cast (Date ) ts);
292+ }
293+
294+ // /
295+ this (DateTime )(const DateTime datetime)
296+ if (DateTime .stringof == " DateTime" )
297+ {
298+ with (datetime) this (year, cast (ubyte )month, day, hour, minute, second);
299+ }
300+
301+ // /
302+ version (mir_test)
303+ @safe unittest {
304+ import std.datetime.date : DateTime ;
305+ auto dt = DateTime (1982 , 4 , 1 , 20 , 59 , 22 );
306+ Timestamp ts = dt;
307+ assert (dt.toISOExtString ~ " Z" == ts.toString);
308+ assert (dt == cast (DateTime ) ts);
309+ }
310+
311+ // /
312+ this (SysTime)(const SysTime systime)
313+ if (SysTime.stringof == " SysTime" )
314+ {
315+ with (systime.toUTC) this (year, month, day, hour, minute, second, - 7 , fracSecs.total! " hnsecs" );
316+ offset = cast (short ) systime.utcOffset.total! " minutes" ;
317+ }
318+
319+ // /
320+ version (mir_test)
321+ @safe unittest {
322+ import core.time : hnsecs, minutes;
323+ import std.datetime.date : DateTime ;
324+ import std.datetime.timezone : SimpleTimeZone;
325+ import std.datetime.systime : SysTime;
326+
327+ auto dt = DateTime (1982 , 4 , 1 , 20 , 59 , 22 );
328+ auto tz = new immutable SimpleTimeZone(- 330. minutes);
329+ auto st = SysTime(dt, 1234567. hnsecs, tz);
330+ Timestamp ts = st;
331+
332+ assert (st.toISOExtString == ts.toString);
333+ assert (st == cast (SysTime) ts);
334+ }
335+
336+ // /
337+ T opCast (T)() const
338+ if (T.stringof == " YearMonth"
339+ || T.stringof == " YearMonthDay"
340+ || T.stringof == " Date"
341+ || T.stringof == " date"
342+ || T.stringof == " DateTime"
343+ || T.stringof == " SysTime" )
344+ {
345+ static if (T.stringof == " YearMonth" )
346+ {
347+ return T (year, month, day);
348+ }
349+ else
350+ static if (T.stringof == " Date" || T.stringof == " date" || T.stringof == " YearMonthDay" )
351+ {
352+ return T (year, month, day);
353+ }
354+ else
355+ static if (T.stringof == " DateTime" )
356+ {
357+ return T (year, month, day, hour, minute, second);
358+ }
359+ else
360+ static if (T.stringof == " SysTime" )
361+ {
362+ import core.time : hnsecs, minutes;
363+ import std.datetime.date : DateTime ;
364+ import std.datetime.systime : SysTime;
365+ import std.datetime.timezone : UTC , SimpleTimeZone;
366+ auto ret = SysTime(DateTime (year, month, day, hour, minute, second), UTC ());
367+ if (fractionCoefficient)
368+ {
369+ long coeff = fractionCoefficient;
370+ int exp = fractionExponent;
371+ while (exp > - 7 )
372+ {
373+ exp-- ;
374+ coeff *= 10 ;
375+ }
376+ while (exp < - 7 )
377+ {
378+ exp++ ;
379+ coeff /= 10 ;
380+ }
381+ ret.fracSecs = coeff.hnsecs;
382+ }
383+ if (offset)
384+ {
385+ ret = ret.toOtherTZ(new immutable SimpleTimeZone(offset.minutes));
386+ }
387+ return ret;
388+ }
389+ }
390+
261391 /+ +
262392 Attaches local offset, doesn't adjust other fields.
263393 Local-time offsets may be represented as either `hour*60+minute` offsets from UTC,
0 commit comments