Skip to content

Commit 4939ed3

Browse files
committed
Convert Date to gregorian calendar
1 parent 5a28f57 commit 4939ed3

File tree

2 files changed

+65
-51
lines changed

2 files changed

+65
-51
lines changed

source/mir/date.d

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,10 +1914,11 @@ struct YearQuarter
19141914
19151915
Year 0 is a leap year.
19161916
+/
1917-
extern(C++, "boost", "gregorian")
1918-
extern(C++, class)
1917+
// extern(C++, "boost", "gregorian")
1918+
// extern(C++, class)
1919+
extern(C++, "mir")
19191920
@serdeProxy!YearMonthDay
1920-
struct date
1921+
struct Date
19211922
{
19221923
extern(D):
19231924
public:
@@ -1927,7 +1928,7 @@ public:
19271928
///
19281929
uint toHash() @safe pure nothrow @nogc const scope
19291930
{
1930-
return _julianDay;
1931+
return _dayNumber;
19311932
}
19321933

19331934
/++
@@ -1957,7 +1958,7 @@ public:
19571958
{
19581959
if (_year == 1)
19591960
{
1960-
ret._julianDay = dayOfYear;
1961+
ret._dayNumber = dayOfYear;
19611962
goto R;
19621963
}
19631964

@@ -1975,11 +1976,11 @@ public:
19751976

19761977
days += dayOfYear;
19771978

1978-
ret._julianDay = days;
1979+
ret._dayNumber = days;
19791980
}
19801981
else if (_year == 0)
19811982
{
1982-
ret._julianDay = dayOfYear - daysInLeapYear;
1983+
ret._dayNumber = dayOfYear - daysInLeapYear;
19831984
}
19841985
else
19851986
{
@@ -2005,10 +2006,10 @@ public:
20052006
else
20062007
days -= daysInLeapYear - dayOfYear;
20072008

2008-
ret._julianDay = days;
2009+
ret._dayNumber = days;
20092010
}
20102011
R:
2011-
ret._julianDay += _julianShift;
2012+
ret._dayNumber -= 1;
20122013
return ret;
20132014
}
20142015

@@ -2073,7 +2074,7 @@ public:
20732074
version(mir_test)
20742075
@safe unittest
20752076
{
2076-
auto d = date(YearMonthDay(2020, Month.may, 31));
2077+
auto d = Date(YearMonthDay(2020, Month.may, 31));
20772078
}
20782079

20792080
version(D_Exceptions)
@@ -2094,8 +2095,8 @@ public:
20942095
version(mir_test)
20952096
@safe unittest
20962097
{
2097-
auto d1 = date(YearQuarter(2020, Quarter.q2));
2098-
auto d2 = date(YearQuarter(2020, Quarter.q2), AssumePeriod.end);
2098+
auto d1 = Date(YearQuarter(2020, Quarter.q2));
2099+
auto d2 = Date(YearQuarter(2020, Quarter.q2), AssumePeriod.end);
20992100
}
21002101

21012102
version(D_Exceptions)
@@ -2109,8 +2110,8 @@ public:
21092110
version(mir_test)
21102111
@safe unittest
21112112
{
2112-
auto d1 = date(YearMonth(2020, Month.may));
2113-
auto d2 = date(YearMonth(2020, Month.may), AssumePeriod.end);
2113+
auto d1 = Date(YearMonth(2020, Month.may));
2114+
auto d2 = Date(YearMonth(2020, Month.may), AssumePeriod.end);
21142115
}
21152116

21162117
version(D_Exceptions)
@@ -2190,9 +2191,10 @@ public:
21902191
Params:
21912192
day = Julian day.
21922193
+/
2194+
deprecated("Use `fromDayNumber` adjusted by -1_721_426")
21932195
this(int day) @safe pure nothrow @nogc
21942196
{
2195-
_julianDay = day;
2197+
_dayNumber = day - (1 + _julianShift);
21962198
}
21972199

21982200
version (mir_test)
@@ -2218,7 +2220,7 @@ public:
22182220
+/
22192221
int opCmp(Date rhs) const @safe pure nothrow @nogc
22202222
{
2221-
return this._julianDay - rhs._julianDay;
2223+
return this._dayNumber - rhs._dayNumber;
22222224
}
22232225

22242226
version (mir_test)
@@ -2310,7 +2312,7 @@ public:
23102312
+/
23112313
@property DayOfWeek dayOfWeek() const @safe pure nothrow @nogc
23122314
{
2313-
return getDayOfWeek(_julianDay);
2315+
return getDayOfWeek(_dayNumber);
23142316
}
23152317

23162318
version (mir_test)
@@ -2324,12 +2326,23 @@ public:
23242326
static assert(!__traits(compiles, idate.dayOfWeek = DayOfWeek.sun));
23252327
}
23262328

2329+
/++
2330+
Params:
2331+
dayNumber = Day Of Gregorian Calendar Minus One
2332+
+/
2333+
static Date fromDayNumber(int dayNumber) @safe pure nothrow @nogc
2334+
{
2335+
Date date;
2336+
date._dayNumber = dayNumber;
2337+
return date;
2338+
}
2339+
23272340
/++
23282341
The Xth day of the Gregorian Calendar that this $(LREF Date) is on.
23292342
+/
23302343
@property int dayOfGregorianCal() const @safe pure nothrow @nogc
23312344
{
2332-
return _julianDay - _julianShift;
2345+
return _dayNumber + 1;
23332346
}
23342347

23352348
///
@@ -2369,18 +2382,21 @@ public:
23692382
23702383
Params:
23712384
day = The day of the Gregorian Calendar to set this $(LREF Date) to.
2385+
2386+
Note:
2387+
Zero value corresponds to
23722388
+/
23732389
@property void dayOfGregorianCal(int day) @safe pure nothrow @nogc
23742390
{
2375-
this = Date(day + _julianShift);
2391+
_dayNumber = day - 1;
23762392
}
23772393

23782394
///
23792395
version (mir_test)
23802396
@safe unittest
23812397
{
2398+
import mir.test;
23822399
auto date = Date.init;
2383-
date.dayOfGregorianCal = 1;
23842400
assert(date == Date(1, 1, 1));
23852401

23862402
date.dayOfGregorianCal = 365;
@@ -2417,20 +2433,20 @@ public:
24172433
static assert(!__traits(compiles, idate.dayOfGregorianCal = 187));
24182434
}
24192435

2420-
private enum uint _startDict = Date(1900, 1, 1)._julianDay; // [
2421-
private enum uint _endDict = Date(2040, 1, 1)._julianDay; // )
2436+
private enum uint _startDict = Date(1900, 1, 1)._dayNumber; // [
2437+
private enum uint _endDict = Date(2040, 1, 1)._dayNumber; // )
24222438
static immutable _dictYMD = ()
24232439
{
24242440
YearMonthDay[Date._endDict - Date._startDict] dict;
24252441
foreach (uint i; 0 .. dict.length)
2426-
dict[i] = Date(i + Date._startDict).yearMonthDayImpl;
2442+
dict[i] = Date.fromDayNumber(i + Date._startDict).yearMonthDayImpl;
24272443
return dict;
24282444
}();
24292445

24302446
///
24312447
YearMonthDay yearMonthDay() const @safe pure nothrow @nogc @property
24322448
{
2433-
uint day = _julianDay;
2449+
uint day = _dayNumber;
24342450
if (day < _endDict)
24352451
{
24362452
import mir.checkedint: subu;
@@ -2445,7 +2461,7 @@ public:
24452461
///
24462462
YearQuarter yearQuarter() const @safe pure nothrow @nogc @property
24472463
{
2448-
uint day = _julianDay;
2464+
uint day = _dayNumber;
24492465
if (day < _endDict)
24502466
{
24512467
return yearMonthDay().YearQuarter;
@@ -2748,7 +2764,7 @@ public:
27482764
version(mir_test)
27492765
@safe unittest
27502766
{
2751-
auto d = date(2020, Month.may, 31);
2767+
auto d = Date(2020, Month.may, 31);
27522768
auto yq = d.yearQuarterImpl;
27532769
}
27542770

@@ -2759,7 +2775,7 @@ public:
27592775
{
27602776
with(yearMonthDay)
27612777
{
2762-
int d = _julianDay - day;
2778+
int d = _dayNumber - day;
27632779
final switch (month) with(Month)
27642780
{
27652781
case jan: d += maxDay(year, jan); goto case;
@@ -2778,7 +2794,7 @@ public:
27782794
case nov: d += maxDay(year, nov); goto case;
27792795
case dec: d += maxDay(year, dec); break;
27802796
}
2781-
return Date(d);
2797+
return Date.fromDayNumber(d);
27822798
}
27832799
}
27842800

@@ -2798,7 +2814,7 @@ public:
27982814
@property Date endOfMonth() const @safe pure nothrow @nogc
27992815
{
28002816
with(yearMonthDay)
2801-
return Date(_julianDay + maxDay(year, month) - day);
2817+
return Date.fromDayNumber(_dayNumber + maxDay(year, month) - day);
28022818
}
28032819

28042820
///
@@ -2853,25 +2869,25 @@ public:
28532869
///
28542870
int opBinary(string op : "-")(Date rhs) const
28552871
{
2856-
return _julianDay - rhs._julianDay;
2872+
return _dayNumber - rhs._dayNumber;
28572873
}
28582874

28592875
///
28602876
Date opBinary(string op : "+")(int rhs) const
28612877
{
2862-
return Date(_julianDay + rhs);
2878+
return Date.fromDayNumber(_dayNumber + rhs);
28632879
}
28642880

28652881
///
28662882
Date opBinaryRight(string op : "+")(int rhs) const
28672883
{
2868-
return Date(_julianDay + rhs);
2884+
return Date.fromDayNumber(_dayNumber + rhs);
28692885
}
28702886

28712887
///
28722888
Date opBinary(string op : "-")(int rhs) const
28732889
{
2874-
return Date(_julianDay - rhs);
2890+
return Date.fromDayNumber(_dayNumber - rhs);
28752891
}
28762892

28772893
///
@@ -2943,7 +2959,7 @@ public:
29432959
+/
29442960
@property int julianDay() const @safe pure nothrow @nogc
29452961
{
2946-
return _julianDay;
2962+
return _dayNumber + (1 + _julianShift);
29472963
}
29482964

29492965
version (mir_test)
@@ -3716,7 +3732,7 @@ public:
37163732
+/
37173733
@property static Date min() @safe pure nothrow @nogc
37183734
{
3719-
return Date(-(int.max / 2));
3735+
return Date.fromDayNumber(int.max);
37203736
}
37213737

37223738
/++
@@ -3725,7 +3741,7 @@ public:
37253741
+/
37263742
@property static Date max() @safe pure nothrow @nogc
37273743
{
3728-
return Date(int.max / 2);
3744+
return Date.fromDayNumber(int.min);
37293745
}
37303746

37313747
private:
@@ -3766,7 +3782,7 @@ package:
37663782
+/
37673783
ref Date _addDays(long days) return @safe pure nothrow @nogc
37683784
{
3769-
_julianDay = cast(int)(_julianDay + days);
3785+
_dayNumber = cast(int)(_dayNumber + days);
37703786
return this;
37713787
}
37723788

@@ -3929,11 +3945,12 @@ package:
39293945
static assert(!__traits(compiles, idate._addDays(12)));
39303946
}
39313947

3932-
int _julianDay;
3948+
int _dayNumber;
39333949
}
39343950

39353951
/// ditto
3936-
alias Date = date;
3952+
deprecated("use `Date` instead")
3953+
alias date = Date;
39373954

39383955
/++
39393956
Returns the number of days from the current day of the week to the given

source/mir/series.d

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ See_also: $(LREF unionSeries), $(LREF troykaSeries), $(LREF troykaGalop).
3030
+/
3131
@safe version(mir_test) unittest
3232
{
33+
import mir.test;
3334
import mir.ndslice;
3435
import mir.series;
3536

@@ -86,21 +87,17 @@ See_also: $(LREF unionSeries), $(LREF troykaSeries), $(LREF troykaGalop).
8687
assert(series0.getVerbose(refDate) == 3);
8788
assert(series0.getExtraVerbose(refDate, "My exception msg") == 3);
8889

89-
assert(collectExceptionMsg!Exception(
90-
series0.get(missingDate)
91-
) == "Series double[date]: Missing required key");
90+
collectExceptionMsg!Exception(series0.get(missingDate)).should
91+
== "Series double[Date]: Missing required key";
9292

93-
assert(collectExceptionMsg!Exception(
94-
series0.get(missingDate, new Exception("My exception msg"))
95-
) == "My exception msg");
93+
collectExceptionMsg!Exception(series0.get(missingDate, new Exception("My exception msg"))).should
94+
== "My exception msg";
9695

97-
assert(collectExceptionMsg!Exception(
98-
series0.getVerbose(missingDate)
99-
) == "Series double[date]: Missing 2016-03-01 key");
96+
collectExceptionMsg!Exception(series0.getVerbose(missingDate)).should
97+
== "Series double[Date]: Missing 2016-03-01 key";
10098

101-
assert(collectExceptionMsg!Exception(
102-
series0.getExtraVerbose(missingDate, "My exception msg")
103-
) == "My exception msg. Series double[date]: Missing 2016-03-01 key");
99+
collectExceptionMsg!Exception(series0.getExtraVerbose(missingDate, "My exception msg")).should
100+
== "My exception msg. Series double[Date]: Missing 2016-03-01 key";
104101

105102
// assign with get*
106103
series0.get(refDate) = 100;

0 commit comments

Comments
 (0)