Skip to content

Commit 95f50e5

Browse files
committed
update timestamp
1 parent 4e877fe commit 95f50e5

1 file changed

Lines changed: 33 additions & 22 deletions

File tree

source/mir/timestamp.d

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,11 @@ struct Timestamp
342342
///
343343
version (mir_test)
344344
@safe unittest {
345+
import mir.test: should;
345346
import std.datetime.date : TimeOfDay;
346347
auto dt = TimeOfDay(7, 14, 30);
347348
Timestamp ts = dt;
348-
assert(dt.toISOExtString == ts.toString);
349+
(dt.toISOExtString ~ "-00:00").should == ts.toString;
349350
assert(dt == cast(TimeOfDay) ts);
350351
}
351352

@@ -362,7 +363,7 @@ struct Timestamp
362363
import std.datetime.date : DateTime;
363364
auto dt = DateTime(1982, 4, 1, 20, 59, 22);
364365
Timestamp ts = dt;
365-
assert(dt.toISOExtString == ts.toString);
366+
assert(dt.toISOExtString ~ "-00:00" == ts.toString);
366367
assert(dt == cast(DateTime) ts);
367368
}
368369

@@ -434,15 +435,15 @@ struct Timestamp
434435
auto duration = 5.weeks + 2.days + 7.hours + 40.minutes + 4.seconds + 9876543.hnsecs;
435436
Timestamp ts = duration;
436437

437-
assert(ts.toISOExtString == `0005-02-88T07:40:04.9876543`);
438+
assert(ts.toISOExtString == `0005-02-88T07:40:04.9876543-00:00`);
438439
assert(duration == cast(Duration) ts);
439440

440441
duration = -duration;
441442
ts = Timestamp(duration);
442-
assert(ts.toISOExtString == `0005-02-99T07:40:04.9876543`);
443+
assert(ts.toISOExtString == `0005-02-99T07:40:04.9876543-00:00`);
443444
assert(duration == cast(Duration) ts);
444445

445-
assert(Timestamp(Duration.zero).toISOExtString == `0000-00-88T00:00:00.0000000`);
446+
assert(Timestamp(Duration.zero).toISOExtString == `0000-00-88T00:00:00.0000000-00:00`);
446447
}
447448

448449
/++
@@ -732,8 +733,8 @@ struct Timestamp
732733
Timestamp(2021, 01, 29, 12, 42, 44).withOffset(7 * 60).toString.should == "2021-01-29T19:42:44+07";
733734
Timestamp(2021, 01, 29, 12, 42, 44).withOffset(7 * 60 + 30).toString.should == "2021-01-29T20:12:44+07:30";
734735

735-
Timestamp.onlyTime(7, 40).toString.should == "07:40";
736-
Timestamp.onlyTime(7, 40, 30).toString.should == "07:40:30";
736+
Timestamp.onlyTime(7, 40).toString.should == "07:40-00:00";
737+
Timestamp.onlyTime(7, 40, 30).toString.should == "07:40:30-00:00";
737738
Timestamp.onlyTime(7, 40, 30, -3, 56).withOffset(0).toString.should == "07:40:30.056Z";
738739
}
739740

@@ -756,9 +757,9 @@ struct Timestamp
756757
assert(Timestamp(-9999, 7, 4).toISOExtString == "-9999-07-04");
757758
assert(Timestamp(-10000, 10, 20).toISOExtString == "-10000-10-20");
758759

759-
assert(Timestamp.onlyTime(7, 40).toISOExtString == "07:40");
760-
assert(Timestamp.onlyTime(7, 40, 30).toISOExtString == "07:40:30");
761-
assert(Timestamp.onlyTime(7, 40, 30, -3, 56).toISOExtString == "07:40:30.056");
760+
assert(Timestamp.onlyTime(7, 40).toISOExtString == "07:40-00:00");
761+
assert(Timestamp.onlyTime(7, 40, 30).toISOExtString == "07:40:30-00:00");
762+
assert(Timestamp.onlyTime(7, 40, 30, -3, 56).toISOExtString == "07:40:30.056-00:00");
762763

763764
const cdate = Timestamp(1999, 7, 6);
764765
immutable idate = Timestamp(1999, 7, 6);
@@ -784,19 +785,20 @@ struct Timestamp
784785
version (mir_test)
785786
@safe pure nothrow unittest
786787
{
787-
assert(Timestamp.init.toISOString == "0000T");
788-
assert(Timestamp(2010, 7, 4).toISOString == "20100704");
789-
assert(Timestamp(1998, 12, 25).toISOString == "19981225");
790-
assert(Timestamp(0, 1, 5).toISOString == "00000105");
791-
assert(Timestamp(-4, 1, 5).toISOString == "-00040105");
788+
import mir.test;
789+
Timestamp.init.toISOString.should == "0000T";
790+
Timestamp(2010, 7, 4).toISOString.should == "20100704";
791+
Timestamp(1998, 12, 25).toISOString.should == "19981225";
792+
Timestamp(0, 1, 5).toISOString.should == "00000105";
793+
Timestamp(-4, 1, 5).toISOString.should == "-00040105";
792794

793795
// YYYYMMDDThhmmss±hhmm
794-
assert(Timestamp(2021).toISOString == "2021T");
795-
assert(Timestamp(2021, 01).toISOString == "2021-01T"); // always extended
796-
assert(Timestamp(2021, 01, 29).toISOString == "20210129");
797-
assert(Timestamp(2021, 01, 29, 19, 42).toISOString == "20210129T1942");
798-
assert(Timestamp(2021, 01, 29, 12, 42, 44).withOffset(7 * 60).toISOString == "20210129T194244+07");
799-
assert(Timestamp(2021, 01, 29, 12, 42, 44).withOffset(7 * 60 + 30).toISOString == "20210129T201244+0730");
796+
Timestamp(2021).toISOString.should == "2021T";
797+
Timestamp(2021, 01).toISOString.should == "2021-01T"; // always extended
798+
Timestamp(2021, 01, 29).toISOString.should == "20210129";
799+
Timestamp(2021, 01, 29, 19, 42).toISOString.should == "20210129T1942";
800+
Timestamp(2021, 01, 29, 12, 42, 44).withOffset(7 * 60).toISOString.should == "20210129T194244+07";
801+
Timestamp(2021, 01, 29, 12, 42, 44).withOffset(7 * 60 + 30).toISOString.should == "20210129T201244+0730";
800802
static assert(Timestamp(2021, 01, 29, 12, 42, 44).withOffset(7 * 60 + 30).toISOString == "20210129T201244+0730");
801803

802804
assert(Timestamp.onlyTime(7, 40).toISOString == "T0740");
@@ -966,7 +968,11 @@ struct Timestamp
966968
}
967969

968970
if (t.isLocalTime)
971+
{
972+
static if (ext)
973+
w.put("-00:00");
969974
return;
975+
}
970976

971977
if (t.offset == 0)
972978
{
@@ -1485,6 +1491,8 @@ struct Timestamp
14851491
return true;
14861492
}
14871493

1494+
bool neg = str[0] == '-';
1495+
14881496
int hour;
14891497
int minute;
14901498
if (str.length < 3 || str[0].isDigit || !fromString(str[0 .. 3], hour))
@@ -1515,7 +1523,10 @@ struct Timestamp
15151523
return false;
15161524
}
15171525

1518-
value.offset = cast(short)(hour * 60 + (hour < 0 ? -minute : minute));
1526+
if (neg && hour == 0 && minute == 0)
1527+
value.setLocalTimezone;
1528+
else
1529+
value.offset = cast(short)(hour * 60 + (hour < 0 ? -minute : minute));
15191530
value.addMinutes(cast(short)-int(value.offset));
15201531
return true;
15211532
}

0 commit comments

Comments
 (0)