Skip to content

Commit 45c06c7

Browse files
committed
wip
1 parent 65d9a02 commit 45c06c7

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

source/mir/timestamp.d

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Timestamp
44
module mir.timestamp;
55

66
private alias isDigit = (dchar c) => uint(c - '0') < 10;
7-
import mir.serde: serdeIgnore, serdeRegister;
7+
import mir.serde: serdeIgnore;
88

99
version(D_Exceptions)
1010
///
@@ -414,6 +414,11 @@ struct Timestamp
414414
if (precision == precision.month)
415415
return T(opCast!AT);
416416

417+
foreach (AT; T.AllowedTypes)
418+
static if (AT.stringof == "Duration")
419+
if (isDuration)
420+
return T(opCast!AT);
421+
417422
foreach (AT; T.AllowedTypes)
418423
static if (AT.stringof == "YearMonthDay" || AT.stringof == "Date" || AT.stringof == "date")
419424
if (precision == precision.day)
@@ -494,6 +499,7 @@ struct Timestamp
494499
|| T.stringof == "Date"
495500
|| T.stringof == "date"
496501
|| T.stringof == "TimeOfDay"
502+
|| T.stringof == "Duration"
497503
|| T.stringof == "DateTime"
498504
|| T.stringof == "SysTime")
499505
{
@@ -524,9 +530,24 @@ struct Timestamp
524530
import std.datetime.systime: SysTime;
525531
import std.datetime.timezone: UTC, SimpleTimeZone;
526532
auto ret = SysTime(DateTime(year, month, day, hour, minute, second), UTC());
533+
ret.fracSecs = getPhobosFraction.hnsecs;
534+
if (offset)
535+
{
536+
ret = ret.toOtherTZ(new immutable SimpleTimeZone(offset.minutes));
537+
}
538+
return ret;
539+
}
540+
else
541+
static if (T.stringof == "Duration")
542+
{
543+
if (!isDuration)
544+
throw ExpectedDuration;
545+
long coeff;
546+
547+
import core.time : hnsecs, minutes;
527548
if (fractionCoefficient)
528549
{
529-
long coeff = fractionCoefficient;
550+
coeff = fractionCoefficient;
530551
int exp = fractionExponent;
531552
while (exp > -7)
532553
{
@@ -538,14 +559,36 @@ struct Timestamp
538559
exp++;
539560
coeff /= 10;
540561
}
541-
ret.fracSecs = coeff.hnsecs;
542562
}
543-
if (offset)
563+
coeff += ((((year * 7 + month) * 24 + hour) * 60 + minute) * 60 + second) * 10_000_000 + getPhobosFraction;
564+
if (isNegativeDuration)
565+
coeff = -coeff;
566+
567+
import mir.conv: to;
568+
import core.time: hnsecs;
569+
return hnsecs(coeff).to!T;
570+
}
571+
}
572+
573+
private long getPhobosFraction() @property const @safe pure nothrow @nogc
574+
{
575+
long coeff;
576+
if (fractionCoefficient)
577+
{
578+
coeff = fractionCoefficient;
579+
int exp = fractionExponent;
580+
while (exp > -7)
544581
{
545-
ret = ret.toOtherTZ(new immutable SimpleTimeZone(offset.minutes));
582+
exp--;
583+
coeff *= 10;
584+
}
585+
while (exp < -7)
586+
{
587+
exp++;
588+
coeff /= 10;
546589
}
547-
return ret;
548590
}
591+
return coeff;
549592
}
550593

551594
/++

0 commit comments

Comments
 (0)