Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

mir.date

Fast BetterC Date type with Boost ABI and mangling compatability.
Category Functions
Main date types Date YearMonth YearQuarter
Other date types Month Quarter DayOfWeek
Date checking valid yearIsLeapYear
Date conversion daysToDayOfWeek quarter
Other AllowDayOverflow DateTimeException AssumePeriod
License:
Authors:
Jonathan M Davis, Ilia Ki (boost-like and BetterC rework)
pure nothrow @nogc @safe bool valid(string units)(int value)
if (units == "months" || units == "hours" || units == "minutes" || units == "seconds");
Returns whether the given value is valid for the given unit type when in a time point. Naturally, a duration is not held to a particular range, but the values in a time point are (e.g. a month must be in the range of 1 - 12 inclusive).
Parameters:
units The units of time to validate.
int value The number to validate.
Examples:
assert(valid!"hours"(12));
assert(!valid!"hours"(32));
assert(valid!"months"(12));
assert(!valid!"months"(13));
pure nothrow @nogc @safe bool valid(string units)(int year, int month, int day)
if (units == "days");
Returns whether the given day is valid for the given year and month.
Parameters:
units The units of time to validate.
int year The year of the day to validate.
int month The month of the day to validate (January is 1).
int day The day to validate.
Examples:
assert(valid!"days"(2016, 2, 29));
assert(!valid!"days"(2016, 2, 30));
assert(valid!"days"(2017, 2, 20));
assert(!valid!"days"(2017, 2, 29));
enum AllowDayOverflow: bool;
no
yes
pure nothrow @nogc @safe bool yearIsLeapYear(int year);
Whether the given Gregorian Year is a leap year.
Parameters:
int year The year to to be tested.
Examples:
foreach (year; [1, 2, 100, 2001, 2002, 2003, 2005, 2006, 2007, 2009, 2010])
{
    assert(!yearIsLeapYear(year));
    assert(!yearIsLeapYear(-year));
}

foreach (year; [0, 4, 8, 400, 800, 1600, 1996, 2000, 2004, 2008, 2012])
{
    assert(yearIsLeapYear(year));
    assert(yearIsLeapYear(-year));
}
enum Month: short;
jan
feb
mar
apr
may
jun
jul
aug
sep
oct
nov
dec
class DateTimeException: object.Exception;
pure nothrow @nogc @safe this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null);

pure nothrow @nogc @safe this(string msg, Throwable nextInChain, string file = __FILE__, size_t line = __LINE__);
enum DayOfWeek: int;
Represents the 7 days of the Gregorian week (Monday is 0).
mon
tue
wed
thu
fri
sat
sun
struct YearMonthDay;
pure nothrow @nogc @property @safe Quarter quarter();
const pure nothrow @nogc @property @safe Timestamp timestamp();
template opCast(T : Timestamp)
pure nothrow @nogc @property @safe YearQuarter yearQuarter();
template opCast(T : YearQuarter)
Examples:
import mir.timestamp;
auto timestamp = cast(Timestamp) YearMonthDay(2020, Month.may, 12);
pure nothrow @nogc @safe this(short year, Month month, ubyte day);
pure nothrow @nogc @safe this(Date date);
Examples:
auto d = YearMonthDay(2020, Month.may, 31);
auto ym = d.YearMonth;
assert(ym.year == 2020);
assert(ym.month == Month.may);
pure nothrow @nogc @safe this(YearMonth yearMonth, AssumePeriod assumePeriod = AssumePeriod.begin);
pure nothrow @nogc @safe this(YearQuarter yearQuarter, AssumePeriod assumePeriodMonth = AssumePeriod.begin, AssumePeriod assumePeriodDay = AssumePeriod.begin);
pure @nogc @safe this(Timestamp timestamp);
pure nothrow @nogc ref @safe YearMonthDay add(string units : "months")(long months, AllowDayOverflow allowOverflow = AllowDayOverflow.yes);
pure nothrow @nogc ref @safe YearMonthDay add(string units : "quarters")(long quarters);
const pure nothrow @nogc @property @safe int dayOfYear();
Day of the year this Date is on.
Examples:
assert(YearMonthDay(1999, cast(Month) 1, 1).dayOfYear == 1);
assert(YearMonthDay(1999, cast(Month) 12, 31).dayOfYear == 365);
assert(YearMonthDay(2000, cast(Month) 12, 31).dayOfYear == 366);
const pure nothrow @nogc @property @safe bool isLeapYear();
Whether this Date is in a leap year.
const pure nothrow @nogc @property @safe ubyte daysInMonth();
The last day in the month that this Date is in.
const pure nothrow @nogc @property @safe bool isAD();
Whether the current year is a date in A.D.
enum AssumePeriod: int;
Controls the assumed start period of days for YearMonth or days and quarters for YearQuarter
begin
end
struct YearMonth;
Represents a date as a pair of years and months.
const void toISOExtString(W)(ref scope W w) scope
if (isOutputRange!(W, char));
Examples:
auto ym = YearMonth(1999, Month.jan);
assert(ym.toISOExtString == "1999-01");
pure nothrow @nogc @property @safe Quarter quarter();
Examples:
assert(YearMonth(1999, Month.jan).quarter == 1);
const pure nothrow @nogc @property @safe Timestamp timestamp();
template opCast(T : Timestamp)
Examples:
import mir.timestamp;
auto ym0 = YearMonth(2020, Month.may);
auto timestamp1 = cast(Timestamp) ym0;
auto ym1 = YearMonth(timestamp1);
pure nothrow @nogc @safe this(short year, Month month);
Examples:
auto ym = YearMonth(2000, Month.dec);
pure @nogc @safe this(short year, ushort month);
Examples:
auto ym = YearMonth(2000, 12);
pure nothrow @nogc @safe this(Date date);
Examples:
auto ym = YearMonth(Date(2000, Month.dec, 31));
Examples:
auto d = Date(2020, Month.may, 31);
auto ym = d.YearMonth;
assert(ym.year == 2020);
assert(ym.month == Month.may);
pure nothrow @nogc @safe this(YearMonthDay yearMonthDay);
Examples:
auto ym = YearMonth(YearMonthDay(2000, Month.dec, 31));
pure nothrow @nogc @safe this(YearQuarter yearQuarter, AssumePeriod assumePeriod = AssumePeriod.begin);
Examples:
auto ym1 = YearMonth(YearQuarter(2000, Quarter.q1));
auto ym2 = YearMonth(YearQuarter(2000, Quarter.q1), AssumePeriod.end);
pure @nogc @safe this(Timestamp timestamp);
const pure nothrow @nogc @safe Date trustedWithDayOfMonth(int days);
Examples:
auto ym = YearMonth(2000, Month.nov);
assert(ym.trustedWithDayOfMonth(6) == Date(2000, 11, 6));
const pure nothrow @nogc @safe int opCmp(YearMonth rhs);
Examples:
auto ym = YearMonth(2000, Month.nov);
assert(ym.opCmp(YearMonth(2000, Month.nov)) == 0);
assert(ym.opCmp(YearMonth(2000, Month.oct)) == 1);
assert(ym.opCmp(YearMonth(2000, Month.dec)) == -1);
assert(ym.opCmp(YearMonth(2001, Month.nov)) == -1);
const pure nothrow @nogc @safe size_t toHash();
Examples:
assert(YearMonth(2000, Month.dec).toHash == 32012);
const pure nothrow @nogc @property @safe Date endOfMonth();
Examples:
assert(YearMonth(2000, Month.dec).endOfMonth == Date(2000, Month.dec, 31));
const pure nothrow @nogc @property @safe ushort lengthOfMonth();
Examples:
assert(YearMonth(2000, Month.dec).lengthOfMonth == 31);
pure @nogc @safe this(scope const(char)[] str);
Examples:
auto ym = YearMonth("1999-01");
assert(ym.year == 1999);
assert(ym.month == 1);
pure nothrow @nogc ref @safe YearMonth add(string units : "months")(long months);
Examples:
auto ym0 = YearMonth(2020, Month.jan);

ym0.add!"months"(1);
assert(ym0.year == 2020);
assert(ym0.month == Month.feb);

auto ym1 = ym0.add!"months"(1);
assert(ym1.year == 2020);
assert(ym1.month == Month.mar);

// also changes ym0
assert(ym0.year == 2020);
assert(ym0.month == Month.mar);

ym1.add!"months"(10);
assert(ym1.year == 2021);
assert(ym1.month == Month.jan);

ym1.add!"months"(-13);
assert(ym1.year == 2019);
assert(ym1.month == Month.dec);
pure nothrow @nogc ref @safe YearMonth add(string units : "quarters")(long quarters);
Examples:
auto yq0 = YearMonth(2020, Month.jan);

yq0.add!"quarters"(1);
assert(yq0.year == 2020);
assert(yq0.month == Month.apr);

auto yq1 = yq0.add!"quarters"(1);
assert(yq1.year == 2020);
assert(yq1.month == Month.jul);

// also changes yq0
assert(yq0.year == 2020);
assert(yq0.month == Month.jul);

yq1.add!"quarters"(2);
assert(yq1.year == 2021);
assert(yq1.month == Month.jan);

yq1.add!"quarters"(-5);
assert(yq1.year == 2019);
assert(yq1.month == Month.oct);
pure nothrow @nogc ref @safe YearMonth add(string units : "years")(long years);
Examples:
auto ym0 = YearMonth(2020, Month.jan);

ym0.add!"years"(1);
assert(ym0.year == 2021);
assert(ym0.month == Month.jan);

auto ym1 = ym0.add!"years"(1);
assert(ym1.year == 2022);
assert(ym1.month == Month.jan);

// also changes ym0
assert(ym0.year == 2022);
assert(ym0.month == Month.jan);
pure nothrow @nogc @safe YearMonth addMonths(long months);
Examples:
auto ym0 = YearMonth(2020, Month.jan);

auto ym1 = ym0.addMonths(15);
assert(ym1.year == 2021);
assert(ym1.month == Month.apr);

auto ym2 = ym1.addMonths(-6);
assert(ym2.year == 2020);
assert(ym2.month == Month.oct);

auto ym3 = YearMonth(2020, Month.dec).addMonths(3);
assert(ym3.year == 2021);
assert(ym3.month == Month.mar);

// ym0 is left unchagned
assert(ym0.year == 2020);
assert(ym0.month == Month.jan);
pure nothrow @nogc @safe YearMonth addQuarters(long quarters);
Examples:
auto ym0 = YearMonth(2020, Month.jan);

auto ym1 = ym0.addQuarters(5);
assert(ym1.year == 2021);
assert(ym1.month == Month.apr);

auto ym2 = ym1.addQuarters(-2);
assert(ym2.year == 2020);
assert(ym2.month == Month.oct);

auto ym3 = YearMonth(2020, Month.dec).addQuarters(1);
assert(ym3.year == 2021);
assert(ym3.month == Month.mar);

// ym0 is left unchagned
assert(ym0.year == 2020);
assert(ym0.month == Month.jan);
pure nothrow @nogc @safe YearMonth addYears(long years);
Examples:
auto ym0 = YearMonth(2020, Month.jan);

auto ym1 = ym0.addYears(1);
assert(ym1.year == 2021);
assert(ym1.month == Month.jan);

// leaves ym0 unchanged
assert(ym0.year == 2020);
assert(ym0.month == Month.jan);
int opBinary(string op : "-")(YearMonth rhs);
YearMonth opBinary(string op)(int rhs)
if (op == "+" || op == "-");
template opBinaryRight(string op : "+")
pure nothrow @nogc ref @safe YearMonth opOpAssign(string op)(int rhs) return
if (op == "+" || op == "-");
Examples:
auto x = YearMonth(2020, Month.mar);
auto x1 = x + 1;
assert(x1 == YearMonth(2020, Month.apr));
auto x2 = x + 2;
assert(x2 == YearMonth(2020, Month.may));
auto x3 = x + 3;
assert(x3 == YearMonth(2020, Month.jun));
Examples:
auto ym = YearMonth(2020, Month.mar);
ym += 2;
assert(ym == YearMonth(2020, Month.may));
ym -= 1;
assert(ym == YearMonth(2020, Month.apr));
Examples:
Get a slice of YearMonths
import mir.ndslice.topology: iota;

static immutable result1 = [YearMonth(2020, Month.mar), YearMonth(2020, Month.apr), YearMonth(2020, Month.may), YearMonth(2020, Month.jun)];
static immutable result2 = [YearMonth(2020, Month.mar), YearMonth(2020, Month.may), YearMonth(2020, Month.jul), YearMonth(2020, Month.sep)];

auto ym = YearMonth(2020, Month.mar);

auto x = ym + 4.iota!uint;
assert(x == result1);

// every other month
auto y = ym + iota!uint([4], 0, 2);
assert(y == result2);
const pure nothrow @nogc @property @safe int dayOfYear(AssumePeriod assumePeriod = AssumePeriod.begin);
Day of the year this Date is on.
Examples:
assert(YearMonth(1999, cast(Month) 1).dayOfYear == 1);
assert(YearMonth(1999, cast(Month) 12).dayOfYear(AssumePeriod.begin) == 335);
assert(YearMonth(1999, cast(Month) 12).dayOfYear(AssumePeriod.end) == 365);
assert(YearMonth(2000, cast(Month) 12).dayOfYear(AssumePeriod.begin) == 336);
assert(YearMonth(2000, cast(Month) 12).dayOfYear(AssumePeriod.end) == 366);
const pure nothrow @nogc @property @safe bool isLeapYear();
Whether this Date is in a leap year.
Examples:
assert(YearMonth(1999, cast(Month) 12).isLeapYear == false);
assert(YearMonth(2000, cast(Month) 12).isLeapYear == true);
const pure nothrow @nogc @property @safe ubyte daysInMonth();
The last day in the month that this Date is in.
Examples:
assert(YearMonth(2020, Month.dec).daysInMonth == 31);
const pure nothrow @nogc @property @safe bool isAD();
Whether the current year is a date in A.D.
Examples:
assert(YearMonth(2020, Month.jan).isAD == true);
enum Quarter: short;
q1
q2
q3
q4
pure nothrow @nogc @safe Quarter quarter(Month month);
Returns the quarter for a given month.
Parameters:
Month month month
Examples:
assert(Month.jan.quarter == Quarter.q1);
assert(Month.feb.quarter == Quarter.q1);
assert(Month.mar.quarter == Quarter.q1);
assert(Month.apr.quarter == Quarter.q2);
assert(Month.may.quarter == Quarter.q2);
assert(Month.jun.quarter == Quarter.q2);
assert(Month.jul.quarter == Quarter.q3);
assert(Month.aug.quarter == Quarter.q3);
assert(Month.sep.quarter == Quarter.q3);
assert(Month.oct.quarter == Quarter.q4);
assert(Month.nov.quarter == Quarter.q4);
assert(Month.dec.quarter == Quarter.q4);
struct YearQuarter;
Represents a date as a pair of years and quarters.
const pure nothrow @nogc @property @safe Month month(AssumePeriod assumePeriod = AssumePeriod.begin);
Examples:
auto yq = YearQuarter(2000, Quarter.q4);
assert(yq.month == 10);
assert(yq.month(AssumePeriod.end) == 12);
const pure nothrow @nogc @property @safe ubyte day(AssumePeriod assumePeriod = AssumePeriod.begin);
Examples:
auto yq = YearQuarter(2000, Quarter.q4);
assert(yq.day == 1);
assert(yq.day(AssumePeriod.end) == 31);
const pure nothrow @nogc @property @safe Timestamp timestamp();
Examples:
import mir.timestamp;
auto yq = YearQuarter(2020, Quarter.q2);
auto ts = yq.timestamp;
template opCast(T : Timestamp)
Examples:
import mir.timestamp;
auto yq = YearQuarter(2020, Quarter.q2);
auto timestamp = cast(Timestamp) yq;
pure nothrow @nogc @safe this(short year, Quarter quarter);
Examples:
auto yq = YearQuarter(2000, Quarter.q4);
pure nothrow @nogc @safe this(short year, Month month);
Examples:
auto yq = YearQuarter(2000, Month.dec);
pure nothrow @nogc @safe this(Date date);
Examples:
auto yq = YearQuarter(Date(2000, Month.dec, 31));
pure nothrow @nogc @safe this(YearMonthDay yearMonthDay);
Examples:
auto ym = YearQuarter(YearMonthDay(2000, Month.dec, 31));
pure nothrow @nogc @safe this(YearMonth yearMonth);
Examples:
auto yq = YearQuarter(YearMonth(2000, Month.dec));
pure @nogc @safe this(Timestamp timestamp);
Examples:
import mir.timestamp;
auto ts = Timestamp(2020, 4);
auto yq = YearQuarter(ts);
pure nothrow @nogc ref @safe YearQuarter add(string units : "quarters")(long quarters);
Examples:
auto yq0 = YearQuarter(2020, Quarter.q1);

yq0.add!"quarters"(1);
assert(yq0.year == 2020);
assert(yq0.quarter == Quarter.q2);

auto yq1 = yq0.add!"quarters"(1);
assert(yq1.year == 2020);
assert(yq1.quarter == Quarter.q3);

// also changes yq0
assert(yq0.year == 2020);
assert(yq0.quarter == Quarter.q3);

yq1.add!"quarters"(2);
assert(yq1.year == 2021);
assert(yq1.quarter == Quarter.q1);

yq1.add!"quarters"(-5);
assert(yq1.year == 2019);
assert(yq1.quarter == Quarter.q4);
pure nothrow @nogc ref @safe YearQuarter add(string units : "years")(long years);
Examples:
auto yq0 = YearQuarter(2020, Quarter.q1);

yq0.add!"years"(1);
assert(yq0.year == 2021);
assert(yq0.quarter == Quarter.q1);

auto yq1 = yq0.add!"years"(1);
assert(yq1.year == 2022);
assert(yq1.quarter == Quarter.q1);

// also changes yq0
assert(yq0.year == 2022);
assert(yq0.quarter == Quarter.q1);
pure nothrow @nogc @safe YearQuarter addQuarters(long quarters);
Examples:
auto yq0 = YearQuarter(2020, Quarter.q1);

auto yq1 = yq0.addQuarters(5);
assert(yq1.year == 2021);
assert(yq1.quarter == Quarter.q2);

auto yq2 = yq1.addQuarters(-2);
assert(yq2.year == 2020);
assert(yq2.quarter == Quarter.q4);

auto yq3 = YearQuarter(2020, Quarter.q4).addQuarters(1);
assert(yq3.year == 2021);
assert(yq3.quarter == Quarter.q1);

// yq0 is left unchagned
assert(yq0.year == 2020);
assert(yq0.quarter == Quarter.q1);
pure nothrow @nogc @safe YearQuarter addYears(long years);
Examples:
auto yq0 = YearQuarter(2020, Quarter.q1);

auto yq1 = yq0.addYears(1);
assert(yq1.year == 2021);
assert(yq1.quarter == Quarter.q1);

// leaves yq0 unchanged
assert(yq0.year == 2020);
assert(yq0.quarter == Quarter.q1);
int opBinary(string op : "-")(YearQuarter rhs);
YearQuarter opBinary(string op)(int rhs)
if (op == "+" || op == "-");
template opBinaryRight(string op : "+")
pure nothrow @nogc ref @safe YearQuarter opOpAssign(string op)(int rhs) return
if (op == "+" || op == "-");
Examples:
auto x = YearQuarter(2020, Quarter.q1);
auto x1 = x + 1;
assert(x1 == YearQuarter(2020, Quarter.q2));
auto x2 = x + 2;
assert(x2 == YearQuarter(2020, Quarter.q3));
auto x3 = x + 3;
assert(x3 == YearQuarter(2020, Quarter.q4));
Examples:
auto yq = YearQuarter(2020, Quarter.q1);
yq += 2;
assert(yq == YearQuarter(2020, Quarter.q3));
yq -= 1;
assert(yq == YearQuarter(2020, Quarter.q2));
Examples:
Get a slice of YearQuarters
import mir.ndslice.topology: iota;

static immutable result1 = [YearQuarter(2020, Quarter.q1), YearQuarter(2020, Quarter.q2), YearQuarter(2020, Quarter.q3), YearQuarter(2020, Quarter.q4)];
static immutable result2 = [YearQuarter(2020, Quarter.q1), YearQuarter(2020, Quarter.q3), YearQuarter(2021, Quarter.q1), YearQuarter(2021, Quarter.q3)];

auto yq = YearQuarter(2020, Quarter.q1);

auto x = yq + 4.iota!uint;
assert(x == result1);

// every other quarter
auto y = yq + iota!uint([4], 0, 2);
assert(y == result2);
const pure nothrow @nogc @property @safe int dayOfQuarter(AssumePeriod assumePeriodMonth, AssumePeriod assumePeriodDay);

const pure nothrow @nogc @property @safe int dayOfQuarter(AssumePeriod assumePeriod = AssumePeriod.begin);
Day of the quarter this Date is on.
Examples:
assert(YearQuarter(1999, cast(Quarter) 1).dayOfQuarter == 1);
assert(YearQuarter(1999, cast(Quarter) 1).dayOfQuarter(AssumePeriod.begin, AssumePeriod.end) == 31);
assert(YearQuarter(1999, cast(Quarter) 1).dayOfQuarter(AssumePeriod.end) == 90);

assert(YearQuarter(2000, cast(Quarter) 1).dayOfQuarter(AssumePeriod.begin, AssumePeriod.end) == 31);
assert(YearQuarter(2000, cast(Quarter) 1).dayOfQuarter(AssumePeriod.end) == 91);

assert(YearQuarter(2000, cast(Quarter) 4).dayOfQuarter == 1);
assert(YearQuarter(2000, cast(Quarter) 4).dayOfQuarter(AssumePeriod.begin, AssumePeriod.end) == 31);
assert(YearQuarter(2000, cast(Quarter) 4).dayOfQuarter(AssumePeriod.end) == 92);
const pure nothrow @nogc @property @safe int dayOfYear(AssumePeriod assumePeriodMonth, AssumePeriod assumePeriodDay);

const pure nothrow @nogc @property @safe int dayOfYear(AssumePeriod assumePeriod = AssumePeriod.begin);
Day of the year this Date is on.
Examples:
assert(YearQuarter(1999, cast(Quarter) 1).dayOfYear == 1);
assert(YearQuarter(1999, cast(Quarter) 4).dayOfYear == 274);
assert(YearQuarter(1999, cast(Quarter) 4).dayOfYear(AssumePeriod.begin, AssumePeriod.end) == 304);
assert(YearQuarter(1999, cast(Quarter) 4).dayOfYear(AssumePeriod.end) == 365);
assert(YearQuarter(2000, cast(Quarter) 4).dayOfYear == 275);
assert(YearQuarter(2000, cast(Quarter) 4).dayOfYear(AssumePeriod.begin, AssumePeriod.end) == 305);
assert(YearQuarter(2000, cast(Quarter) 4).dayOfYear(AssumePeriod.end) == 366);
const pure nothrow @nogc @property @safe bool isLeapYear();
Whether this Date is in a leap year.
Examples:
assert(YearQuarter(1999, cast(Quarter) 4).isLeapYear == false);
assert(YearQuarter(2000, cast(Quarter) 4).isLeapYear == true);
const pure nothrow @nogc @property @safe ubyte daysInMonth(AssumePeriod assumePeriod = AssumePeriod.begin);
The last day in the month that this Date is in.
Examples:
auto yq = YearQuarter(2020, Quarter.q3);
assert(yq.daysInMonth == 31);
assert(yq.daysInMonth(AssumePeriod.end) == 30);
const pure nothrow @nogc @property @safe bool isAD();
Whether the current year is a date in A.D.
Examples:
assert(YearQuarter(2020, Quarter.q1).isAD == true);
struct Date;

deprecated alias date = Date;
Represents a date in the Proleptic Gregorian Calendar ranging from 32,768 B.C. to 32,767 A.D. Positive years are A.D. Non-positive years are B.C.
Year, month, and day are kept separately internally so that Date is optimized for calendar-based operations.
Date uses the Proleptic Gregorian Calendar, so it assumes the Gregorian leap year calculations for its entire length. As per ISO 8601, it treats 1 B.C. as year 0, i.e. 1 B.C. is 0, 2 B.C. is -1, etc. Use yearBC to use B.C. as a positive integer with 1 B.C. being the year prior to 1 A.D.
Year 0 is a leap year.
const pure nothrow @nogc @safe uint toHash();
static pure nothrow @nogc @safe Date trustedCreate(int _year, int _month, int _day);
Throws:
DateTimeException if the resulting Date would not be valid.
Parameters:
int _year Year of the Gregorian Calendar. Positive values are A.D. Non-positive values are B.C. with year 0 being the year prior to 1 A.D.
int _month Month of the year (January is 1).
int _day Day of the month.
const pure nothrow @nogc @property @safe Timestamp timestamp();
Examples:
import mir.timestamp;
auto d1 = Date(2020, Month.may, 15);
auto ts2 = d1.timestamp;
pure @nogc @safe this(Timestamp timestamp);
Examples:
import mir.timestamp;
auto ts = Date(2020, Month.may, 15).timestamp;
auto d2 = Date(ts);
pure @nogc @safe this(scope const(char)[] str);
Examples:
auto d = Date("2020-12-31");
pure @nogc @safe this(YearMonthDay ymd);
Examples:
auto d = Date(YearMonthDay(2020, Month.may, 31));
pure @nogc @safe this(YearQuarter yq, AssumePeriod assumePeriodMonth, AssumePeriod assumePeriodDay);
pure @nogc @safe this(YearQuarter yq, AssumePeriod assumePeriod = AssumePeriod.begin);
Examples:
auto d1 = Date(YearQuarter(2020, Quarter.q2));
auto d2 = Date(YearQuarter(2020, Quarter.q2), AssumePeriod.end);
pure nothrow @nogc @safe this(YearMonth ym, AssumePeriod assumePeriod = AssumePeriod.begin);
Examples:
auto d1 = Date(YearMonth(2020, Month.may));
auto d2 = Date(YearMonth(2020, Month.may), AssumePeriod.end);
pure @nogc @safe this(int _year, int _month, int _day);
static pure nothrow @nogc @safe bool fromYMD(int _year, int _month, int _day, out Date value);
deprecated pure nothrow @nogc @safe this(int day);
Parameters:
int day Julian day.
const pure nothrow @nogc @safe int opCmp(Date rhs);
Compares this Date with the given Date.
Returns:
this < rhs < 0
this == rhs 0
this > rhs > 0
const pure nothrow @nogc @property @safe DayOfWeek dayOfWeek();
Day of the week this Date is on.
static pure nothrow @nogc @safe Date fromDayNumber(int dayNumber);
Parameters:
int dayNumber Day Of Gregorian Calendar Minus One
const pure nothrow @nogc @property @safe int dayNumber();
Returns; Day Of Gregorian Calendar Minus One
const pure nothrow @nogc @property @safe int dayOfGregorianCal();
The Xth day of the Gregorian Calendar that this Date is on.
Examples:
assert(Date(1, 1, 1).dayOfGregorianCal == 1);
assert(Date(1, 12, 31).dayOfGregorianCal == 365);
assert(Date(2, 1, 1).dayOfGregorianCal == 366);

assert(Date(0, 12, 31).dayOfGregorianCal == 0);
assert(Date(0, 1, 1).dayOfGregorianCal == -365);
assert(Date(-1, 12, 31).dayOfGregorianCal == -366);

assert(Date(2000, 1, 1).dayOfGregorianCal == 730_120);
assert(Date(2010, 12, 31).dayOfGregorianCal == 734_137);
pure nothrow @nogc @property @safe void dayOfGregorianCal(int day);
The Xth day of the Gregorian Calendar that this Date is on.
Parameters:
int day The day of the Gregorian Calendar to set this Date to.

Note Zero value corresponds to

Examples:
import mir.test;
auto date = Date.init;
assert(date == Date(1, 1, 1));

date.dayOfGregorianCal = 365;
assert(date == Date(1, 12, 31));

date.dayOfGregorianCal = 366;
assert(date == Date(2, 1, 1));

date.dayOfGregorianCal = 0;
assert(date == Date(0, 12, 31));

date.dayOfGregorianCal = -365;
assert(date == Date(-0, 1, 1));

date.dayOfGregorianCal = -366;
assert(date == Date(-1, 12, 31));

date.dayOfGregorianCal = 730_120;
assert(date == Date(2000, 1, 1));

date.dayOfGregorianCal = 734_137;
assert(date == Date(2010, 12, 31));
const pure nothrow @nogc @property @safe YearMonthDay yearMonthDay();
const pure nothrow @nogc @property @safe YearQuarter yearQuarter();
Examples:
auto d = Date(2020, Month.may, 31);
auto yq = d.yearQuarter;
assert(yq.year == 2020);
assert(yq.quarter == Quarter.q2);
const pure nothrow @nogc @property @safe short year();
const pure nothrow @nogc @property @safe Quarter quarter();
const pure nothrow @nogc @property @safe Month month();
const pure nothrow @nogc @property @safe ubyte day();
Examples:
auto d = Date(2020, Month.may, 31);
assert(d.year == 2020);
assert(d.quarter == Quarter.q2);
assert(d.month == Month.may);
assert(d.day == 31);
const pure nothrow @nogc @property @safe Date endOfQuarter();
Date for the last day in the quarter that this Date is in.
Examples:
assert(Date(1999, 1, 6).endOfQuarter == Date(1999, 3, 31));
assert(Date(1999, 2, 7).endOfQuarter == Date(1999, 3, 31));
assert(Date(2000, 2, 7).endOfQuarter == Date(2000, 3, 31));
assert(Date(2000, 6, 4).endOfQuarter == Date(2000, 6, 30));
const pure nothrow @nogc @property @safe Date endOfMonth();
Date for the last day in the month that this Date is in.
Examples:
assert(Date(1999, 1, 6).endOfMonth == Date(1999, 1, 31));
assert(Date(1999, 2, 7).endOfMonth == Date(1999, 2, 28));
assert(Date(2000, 2, 7).endOfMonth == Date(2000, 2, 29));
assert(Date(2000, 6, 4).endOfMonth == Date(2000, 6, 30));
const int opBinary(string op : "-")(Date rhs);
const Date opBinary(string op : "+")(int rhs);
const Date opBinaryRight(string op : "+")(int rhs);
const Date opBinary(string op : "-")(int rhs);
pure nothrow @nogc ref @safe Date opOpAssign(string op)(int rhs) return
if (op == "+" || op == "-");
Examples:
auto d = Date(2020, 1, 1);
d += 2;
assert(d == Date(2020, 1, 3));
d -= 1;
assert(d == Date(2020, 1, 2));
Examples:
Get a slice of Dates
import mir.ndslice.topology: iota, map;

static immutable result1 = [Date(2020, Month.mar, 1), Date(2020, Month.mar, 2), Date(2020, Month.mar, 3), Date(2020, Month.mar, 4)];
static immutable result2 = [Date(2020, Month.mar, 1), Date(2020, Month.mar, 3), Date(2020, Month.mar, 5), Date(2020, Month.mar, 7)];
static immutable result3 = [Date(2020, Month.mar, 1), Date(2020, Month.apr, 1), Date(2020, Month.may, 1), Date(2020, Month.jun, 1)];
static immutable result4 = [Date(2020, Month.mar, 1), Date(2020, Month.jun, 1), Date(2020, Month.sep, 1), Date(2020, Month.dec, 1)];
static immutable result5 = [Date(2020, Month.mar, 1), Date(2021, Month.mar, 1), Date(2022, Month.mar, 1), Date(2023, Month.mar, 1)];

auto d = Date(2020, Month.mar, 1);

auto x = d + 4.iota!uint;
assert(x == result1);

// every other date
auto y = d + iota!uint([4], 0, 2);
assert(y == result2);

// every month
auto z = (d.YearMonth + 4.iota!uint).map!Date;
assert(z == result3);

// every quarter
auto a = (d.YearQuarter + 4.iota!uint).map!(a => a.Date(AssumePeriod.end, AssumePeriod.begin));
assert(a == result4);

// every year
auto b = (d.year + 4.iota!uint).map!(a => YearMonthDay(cast(short) a, Month.mar, 1).Date);
assert(b == result5);
const pure nothrow @nogc @property @safe int julianDay();
The Julian day for this Date at noon (since the Julian day changes at noon).
const pure nothrow @nogc @property @safe long modJulianDay();
The modified Julian day for any time on this date (since, the modified Julian day changes at midnight).
const pure nothrow @safe string toISOString();

const void toISOString(W)(ref scope W w) scope
if (isOutputRange!(W, char));
Converts this Date to a string with the format YYYYMMDD. If writer is set, the resulting string will be written directly to it.
Returns:
A string when not using an output range; void otherwise.
Examples:
assert(Date.init.toISOString == "null");
assert(Date(2010, 7, 4).toISOString == "20100704");
assert(Date(1998, 12, 25).toISOString == "19981225");
assert(Date(0, 1, 5).toISOString == "00000105");
assert(Date(-4, 1, 5).toISOString == "-00040105", Date(-4, 1, 5).toISOString());
const pure nothrow @safe string toISOExtString();

alias toString = toISOExtString;

const void toISOExtString(W)(ref scope W w) scope
if (isOutputRange!(W, char));
Converts this Date to a string with the format YYYY-MM-DD. If writer is set, the resulting string will be written directly to it.
Returns:
A string when not using an output range; void otherwise.
Examples:
assert(Date.init.toISOExtString == "null");
assert(Date(2010, 7, 4).toISOExtString == "2010-07-04");
assert(Date(1998, 12, 25).toISOExtString == "1998-12-25");
assert(Date(0, 1, 5).toISOExtString == "0000-01-05");
assert(Date(-4, 1, 5).toISOExtString == "-0004-01-05");
const pure nothrow @safe string toSimpleString();

const void toSimpleString(W)(ref scope W w) scope
if (isOutputRange!(W, char));
Converts this Date to a string with the format YYYY-Mon-DD. If writer is set, the resulting string will be written directly to it.
Returns:
A string when not using an output range; void otherwise.
Examples:
assert(Date.init.toSimpleString == "null");
assert(Date(2010, 7, 4).toSimpleString == "2010-Jul-04");
assert(Date(1998, 12, 25).toSimpleString == "1998-Dec-25");
assert(Date(0, 1, 5).toSimpleString == "0000-Jan-05");
assert(Date(-4, 1, 5).toSimpleString == "-0004-Jan-05");
pure nothrow @nogc @safe bool fromISOString(C)(scope const(C)[] str, out Date value)
if (isSomeChar!C);

pure @safe Date fromISOString(C)(scope const(C)[] str)
if (isSomeChar!C);
Creates a Date from a string with the format YYYYMMDD.
Parameters:
const(C)[] str A string formatted in the way that .date.toISOString formats dates.
Date value (optional) result value.
Throws:
DateTimeException if the given string is not in the correct format or if the resulting Date would not be valid. Two arguments overload is nothrow.
Returns:
bool on success for two arguments overload, and the resulting date for single argument overdload.
pure nothrow @nogc @safe bool fromISOExtString(C)(scope const(C)[] str, out Date value)
if (isSomeChar!C);

pure @safe Date fromISOExtString(C)(scope const(C)[] str)
if (isSomeChar!C);
Creates a Date from a string with the format YYYY-MM-DD.
Parameters:
const(C)[] str A string formatted in the way that .date.toISOExtString formats dates.
Date value (optional) result value.
Throws:
DateTimeException if the given string is not in the correct format or if the resulting Date would not be valid. Two arguments overload is nothrow.
Returns:
bool on success for two arguments overload, and the resulting date for single argument overdload.
pure nothrow @nogc @safe bool fromSimpleString(C)(scope const(C)[] str, out Date value)
if (isSomeChar!C);

pure @safe Date fromSimpleString(C)(scope const(C)[] str)
if (isSomeChar!C);
Creates a Date from a string with the format YYYY-Mon-DD.
Parameters:
const(C)[] str A string formatted in the way that .date.toSimpleString formats dates. The function is case sensetive.
Date value (optional) result value.
Throws:
DateTimeException if the given string is not in the correct format or if the resulting Date would not be valid. Two arguments overload is nothrow.
Returns:
bool on success for two arguments overload, and the resulting date for single argument overdload.
pure nothrow @nogc @safe bool fromString(C)(scope const(C)[] str, out Date value);

pure @safe Date fromString(C)(scope const(C)[] str)
if (isSomeChar!C);
Creates a Date from a string with the format YYYY-MM-DD, YYYYMMDD, or YYYY-Mon-DD.
Parameters:
const(C)[] str A string formatted in the way that .date.toISOExtString, .date.toISOString, and .date.toSimpleString format dates. The function is case sensetive.
Date value (optional) result value.
Throws:
DateTimeException if the given string is not in the correct format or if the resulting Date would not be valid. Two arguments overload is nothrow.
Returns:
bool on success for two arguments overload, and the resulting date for single argument overdload.
static pure nothrow @nogc @property @safe Date min();
Returns the Date farthest in the past which is representable by Date.
static pure nothrow @nogc @property @safe Date max();
Returns the Date farthest in the future which is representable by Date.
pure nothrow @nogc @safe int daysToDayOfWeek(DayOfWeek currDoW, DayOfWeek dow);
Returns the number of days from the current day of the week to the given day of the week. If they are the same, then the result is 0.
Parameters:
DayOfWeek currDoW The current day of the week.
DayOfWeek dow The day of the week to get the number of days to.
Examples:
assert(daysToDayOfWeek(DayOfWeek.mon, DayOfWeek.mon) == 0);
assert(daysToDayOfWeek(DayOfWeek.mon, DayOfWeek.sun) == 6);
assert(daysToDayOfWeek(DayOfWeek.mon, DayOfWeek.wed) == 2);
pure nothrow @nogc @safe ubyte maxDay(int year, int month);
The maximum valid Day in the given month in the given year.
Parameters:
int year The year to get the day for.
int month The month of the Gregorian Calendar to get the day for.