@@ -4,7 +4,7 @@ Timestamp
4
4
module mir.timestamp ;
5
5
6
6
private alias isDigit = (dchar c) => uint (c - ' 0' ) < 10 ;
7
- import mir.serde: serdeIgnore, serdeRegister ;
7
+ import mir.serde: serdeIgnore;
8
8
9
9
version (D_Exceptions)
10
10
// /
@@ -414,6 +414,11 @@ struct Timestamp
414
414
if (precision == precision.month)
415
415
return T (opCast ! AT );
416
416
417
+ foreach (AT ; T.AllowedTypes)
418
+ static if (AT .stringof == " Duration" )
419
+ if (isDuration)
420
+ return T (opCast ! AT );
421
+
417
422
foreach (AT ; T.AllowedTypes)
418
423
static if (AT .stringof == " YearMonthDay" || AT .stringof == " Date" || AT .stringof == " date" )
419
424
if (precision == precision.day)
@@ -494,6 +499,7 @@ struct Timestamp
494
499
|| T.stringof == " Date"
495
500
|| T.stringof == " date"
496
501
|| T.stringof == " TimeOfDay"
502
+ || T.stringof == " Duration"
497
503
|| T.stringof == " DateTime"
498
504
|| T.stringof == " SysTime" )
499
505
{
@@ -524,9 +530,24 @@ struct Timestamp
524
530
import std.datetime.systime : SysTime;
525
531
import std.datetime.timezone : UTC , SimpleTimeZone;
526
532
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;
527
548
if (fractionCoefficient)
528
549
{
529
- long coeff = fractionCoefficient;
550
+ coeff = fractionCoefficient;
530
551
int exp = fractionExponent;
531
552
while (exp > - 7 )
532
553
{
@@ -538,14 +559,36 @@ struct Timestamp
538
559
exp++ ;
539
560
coeff /= 10 ;
540
561
}
541
- ret.fracSecs = coeff.hnsecs;
542
562
}
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 )
544
581
{
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 ;
546
589
}
547
- return ret;
548
590
}
591
+ return coeff;
549
592
}
550
593
551
594
/+ +
0 commit comments