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
Other date types Month DayOfWeek
Date checking valid yearIsLeapYear
Date conversion daysToDayOfWeek
Other AllowDayOverflow DateTimeException
Authors:
Jonathan M Davis, Ilya Yaroshenko (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;
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.
struct date;

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.
pure @nogc @safe this(scope const(char)[] str);
pure @nogc @safe this(YearMonthDay ymd);
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);
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.
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.
Examples:
auto date = Date.init;
date.dayOfGregorianCal = 1;
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 short year();
const pure nothrow @nogc @property @safe Month month();
const pure nothrow @nogc @property @safe ubyte day();
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 lhs);
const Date opBinary(string op : "+")(int lhs);
const Date opBinaryRight(string op : "+")(int lhs);
const Date opBinary(string op : "-")(int lhs);
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 scope void toISOString(W)(ref scope W w)
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 scope void toISOExtString(W)(ref scope W w)
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 scope void toSimpleString(W)(ref scope W w)
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 void serialize(S)(ref S serializer);
ASDF(JSON) serialization support
Date deserialize(D)(auto ref D deserializer);
ASDF(JSON) deserialization support
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.