std.datetime.date
License
Types 7
Exception type used by std.datetime. It's an alias to
TimeException. Either can be caught without concern about
which module it came from.
Represents the 12 months of the Gregorian year (January is 1).
Represents the 7 days of the Gregorian week (Sunday is 0).
In some date calculations, adding months or years can cause the date to fall on a day of the month which is not valid (e.g. February 29th 2001 or June 31st 2000). If overflow is allowed (as is the default), then the month will be incremented accordingly (so, February 29th 2001 would become March 1st 2001, and June 31st 2000 would become July 1st 2000). If overflow is not allowed, then the day will be adjusted to the last valid day in that month (so, February 29th 2001 would become February 28th 2001 and June 31st 2000 would become June 30th 2000).
AllowDayOverflow only applies to calculations involving months or years.
If set to AllowDayOverflow.no, then day overflow is not allowed.
Otherwise, if set to AllowDayOverflow.yes, then day overflow is allowed.
Combines the Date and
TimeOfDay structs to give an object which holds
both the date and the time. It is optimized for calendar-based operations and has no concept of time zone. For an object which is optimized for time operations based on the system time, use
SysTime. SysTime has
a concept of time zone and has much higher precision (hnsecs). DateTime is intended primarily for calendar-based uses rather than precise time operations.
int opCmp(DateTime rhs) const @safe pure nothrow @nogcCompares this DateTime with the given `DateTime.`.short year() @property const @safe pure nothrow @nogcYear of the Gregorian Calendar. Positive numbers are A.D. Non-positive are B.C.void year(int year) @property @safe pureYear of the Gregorian Calendar. Positive numbers are A.D. Non-positive are B.C.short yearBC() @property const @safe pureYear B.C. of the Gregorian Calendar counting year 0 as 1 B.C.void yearBC(int year) @property @safe pureYear B.C. of the Gregorian Calendar counting year 0 as 1 B.C.DateTime add(string units)(long value, AllowDayOverflow allowOverflow = AllowDayOverflow.yes) if (units == "years" || units == "months") ref @safe pure nothrow @nogcAdds the given number of years or months to this DateTime, mutating it. A negative number will subtract.DateTime roll(string units)(long value, AllowDayOverflow allowOverflow = AllowDayOverflow.yes) if (units == "years" || units == "months") ref @safe pure nothrow @nogcAdds the given number of years or months to this DateTime, mutating it. A negative number will subtract.DateTime roll(string units)(long value) if (units == "days") ref @safe pure nothrow @nogcAdds the given number of units to this DateTime, mutating it. A negative number will subtract.DateTime roll(string units)(long value) if (units == "hours" ||
units == "minutes" ||
units == "seconds") ref @safe pure nothrow @nogcdittoDateTime opBinary(string op)(Duration duration) if (op == "+" || op == "-") const @safe pure nothrow @nogcGives the result of adding or subtracting a Duration from this DateTime.DateTime opBinaryRight(string op)(Duration duration) if (op == "+") const @safe pure nothrow @nogcdittoDateTime opOpAssign(string op)(Duration duration) if (op == "+" || op == "-") ref @safe pure nothrow @nogcGives the result of adding or subtracting a duration from this DateTime, as well as assigning the result to this DateTime.Duration opBinary(string op)(DateTime rhs) if (op == "-") const @safe pure nothrow @nogcGives the difference between two DateTimes.int diffMonths(DateTime rhs) const @safe pure nothrow @nogcReturns the difference between the two DateTimes in months.int dayOfGregorianCal() @property const @safe pure nothrow @nogcThe Xth day of the Gregorian Calendar that this DateTime is on.void dayOfGregorianCal(int days) @property @safe pure nothrow @nogcThe Xth day of the Gregorian Calendar that this DateTime is on. Setting this property does not affect the time portion of DateTime.ubyte isoWeek() @property const @safe pure nothrowThe ISO 8601 week of the year that this DateTime is in.short isoWeekYear() @property const @safe pure nothrowThe year of the ISO 8601 week calendar that this DateTime is in.DateTime endOfMonth() @property const @safe pure nothrowDateTime for the last day in the month that this DateTime is in. The time portion of endOfMonth is always 23:59:59.ubyte daysInMonth() @property const @safe pure nothrow @nogcThe last day in the month that this DateTime is in.long julianDay() @property const @safe pure nothrow @nogcThe en.wikipedia.org/wiki/Julianday for this DateTime at the given time. For example, prior to noon, 1996-03-31 would be the Julian day number 2450173, so this function returns 2450173, while from ...long modJulianDay() @property const @safe pure nothrow @nogcThe modified en.wikipedia.org/wiki/Julian_day for any time on this date (since, the modified Julian day changes at midnight).string toISOString() const @safe pure nothrowConverts this DateTime to a string with the format `YYYYMMDDTHHMMSS`. If `writer` is set, the resulting string will be written directly to it.string toISOExtString() const @safe pure nothrowConverts this DateTime to a string with the format `YYYY-MM-DDTHH:MM:SS`. If `writer` is set, the resulting string will be written directly to it.string toSimpleString() const @safe pure nothrowConverts this DateTime to a string with the format `YYYY-Mon-DD HH:MM:SS`. If `writer` is set, the resulting string will be written directly to it.DateTime fromISOString(S)(scope const S isoString) if (isSomeString!S) @safe pureCreates a DateTime from a string with the format YYYYMMDDTHHMMSS. Whitespace is stripped from the given string.DateTime fromISOExtString(S)(scope const S isoExtString) if (isSomeString!(S)) @safe pureCreates a DateTime from a string with the format YYYY-MM-DDTHH:MM:SS. Whitespace is stripped from the given string.DateTime fromSimpleString(S)(scope const S simpleString) if (isSomeString!(S)) @safe pureCreates a DateTime from a string with the format YYYY-Mon-DD HH:MM:SS. Whitespace is stripped from the given string.DateTime min() @property @safe pure nothrow @nogcReturns the DateTime farthest in the past which is representable by DateTime.this(Date date, TimeOfDay tod = TimeOfDay.init)Params: date = The date portion of DateTime. tod = The time portion of DateTime.this(int year, int month, int day, int hour = 0, int minute = 0, int second = 0)Params: year = The year portion of the date. month = The month portion of the date (January is 1). day = The day portion of the date. hour = The hour portion of the time; minute = The minut...Represents a date in the
ProlepticGregorian 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
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.
short year() @property const @safe pure nothrow @nogcYear of the Gregorian Calendar. Positive numbers are A.D. Non-positive are B.C.void year(int year) @property @safe pureYear of the Gregorian Calendar. Positive numbers are A.D. Non-positive are B.C.ushort yearBC() @property const @safe pureYear B.C. of the Gregorian Calendar counting year 0 as 1 B.C.void yearBC(int year) @property @safe pureYear B.C. of the Gregorian Calendar counting year 0 as 1 B.C.Date add(string units)(long value, AllowDayOverflow allowOverflow = AllowDayOverflow.yes) if (units == "years") @safe pure nothrow @nogc refAdds the given number of years or months to this Date, mutating it. A negative number will subtract.Date add(string units)(long months, AllowDayOverflow allowOverflow = AllowDayOverflow.yes) if (units == "months") @safe pure nothrow @nogc refDate roll(string units)(long value, AllowDayOverflow allowOverflow = AllowDayOverflow.yes) if (units == "years") @safe pure nothrow @nogc refAdds the given number of years or months to this Date, mutating it. A negative number will subtract.Date roll(string units)(long months, AllowDayOverflow allowOverflow = AllowDayOverflow.yes) if (units == "months") @safe pure nothrow @nogc refDate roll(string units)(long days) if (units == "days") ref @safe pure nothrow @nogcAdds the given number of units to this Date, mutating it. A negative number will subtract.Date opBinary(string op)(Duration duration) if (op == "+" || op == "-") const @safe pure nothrow @nogcGives the result of adding or subtracting a Duration from this Date.Date opOpAssign(string op)(Duration duration) if (op == "+" || op == "-") ref @safe pure nothrow @nogcGives the result of adding or subtracting a Duration from this Date, as well as assigning the result to this Date.Duration opBinary(string op)(Date rhs) if (op == "-") const @safe pure nothrow @nogcGives the difference between two Dates.int diffMonths(Date rhs) const @safe pure nothrow @nogcReturns the difference between the two Dates in months.void setDayOfYear(bool useExceptions = false)(int day)int dayOfGregorianCal() @property const @safe pure nothrow @nogcThe Xth day of the Gregorian Calendar that this Date is on.void dayOfGregorianCal(int day) @property @safe pure nothrow @nogcThe Xth day of the Gregorian Calendar that this Date is on.@property auto isoWeekAndYear() const @safe pure nothrowThe ISO 8601 week and year of the year that this Date is in.ubyte isoWeek() @property const @safe pure nothrowThe ISO 8601 week of the year that this Date is in.short isoWeekYear() @property const @safe pure nothrowThe year inside the ISO 8601 week calendar that this Date is in.Date endOfMonth() @property const @safe pure nothrowDate for the last day in the month that this Date is in.ubyte daysInMonth() @property const @safe pure nothrow @nogcThe last day in the month that this Date is in.long julianDay() @property const @safe pure nothrow @nogcThe en.wikipedia.org/wiki/Julian_day for this Date at noon (since the Julian day changes at noon).long modJulianDay() @property const @safe pure nothrow @nogcThe modified en.wikipedia.org/wiki/Julian_day for any time on this date (since, the modified Julian day changes at midnight).string toISOString() const @safe pure nothrowConverts this Date to a string with the format `YYYYMMDD`. If `writer` is set, the resulting string will be written directly to it.string toISOExtString() const @safe pure nothrowConverts this Date to a string with the format `YYYY-MM-DD`. If `writer` is set, the resulting string will be written directly to it.string toSimpleString() const @safe pure nothrowConverts this Date to a string with the format `YYYY-Mon-DD`. If `writer` is set, the resulting string will be written directly to it.Date fromISOString(S)(scope const S isoString) if (isSomeString!S) @safe pureCreates a Date from a string with the format YYYYMMDD. Whitespace is stripped from the given string.Date fromISOExtString(S)(scope const S isoExtString) if (isSomeString!(S)) @safe pureCreates a Date from a string with the format YYYY-MM-DD. Whitespace is stripped from the given string.Date fromSimpleString(S)(scope const S simpleString) if (isSomeString!(S)) @safe pureCreates a Date from a string with the format YYYY-Mon-DD. Whitespace is stripped from the given string.Date min() @property @safe pure nothrow @nogcReturns the Date farthest in the past which is representable by Date.Represents a time of day with hours, minutes, and seconds. It uses 24 hour time.
ubyte _hourubyte _minuteubyte _secondubyte maxHourubyte maxMinuteubyte maxSecondint opCmp(TimeOfDay rhs) const @safe pure nothrow @nogcCompares this TimeOfDay with the given TimeOfDay.TimeOfDay roll(string units)(long value) if (units == "hours") ref @safe pure nothrow @nogcAdds the given number of units to this TimeOfDay, mutating it. A negative number will subtract.TimeOfDay roll(string units)(long value) if (units == "minutes" || units == "seconds") ref @safe pure nothrow @nogcdittoTimeOfDay opBinary(string op)(Duration duration) if (op == "+" || op == "-") const @safe pure nothrow @nogcGives the result of adding or subtracting a Duration from this TimeOfDay.TimeOfDay opBinaryRight(string op)(Duration duration) if (op == "+") const @safe pure nothrow @nogcdittoTimeOfDay opOpAssign(string op)(Duration duration) if (op == "+" || op == "-") ref @safe pure nothrow @nogcGives the result of adding or subtracting a Duration from this TimeOfDay, as well as assigning the result to this TimeOfDay.Duration opBinary(string op)(TimeOfDay rhs) if (op == "-") const @safe pure nothrow @nogcGives the difference between two TimeOfDays.string toISOString() const @safe pure nothrowConverts this TimeOfDay to a string with the format `HHMMSS`. If `writer` is set, the resulting string will be written directly to it.string toISOExtString() const @safe pure nothrowConverts this TimeOfDay to a string with the format `HH:MM:SS`. If `writer` is set, the resulting string will be written directly to it.TimeOfDay fromISOString(S)(scope const S isoString) if (isSomeString!S) @safe pureCreates a TimeOfDay from a string with the format HHMMSS. Whitespace is stripped from the given string.TimeOfDay fromISOExtString(S)(scope const S isoExtString) if (isSomeString!S) @safe pureCreates a TimeOfDay from a string with the format HH:MM:SS. Whitespace is stripped from the given string.this(int hour, int minute, int second = 0)Params: hour = Hour of the day [0 - 24. minute = Minute of the hour [0 - 60. second = Second of the minute [0 - 60.Functions 16
bool valid(string units)(int value) if (units == "months" ||
units == "hours" ||
units == "minutes" ||
units == "seconds") @safe pure nothrow @nogcReturns 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...bool valid(string units)(int year, int month, int day) if (units == "days") @safe pure nothrow @nogcReturns whether the given day is valid for the given year and month.void enforceValid(string units)(int value, string file = __FILE__, size_t line = __LINE__) if (units == "months" ||
units == "hours" ||
units == "minutes" ||
units == "seconds") @safe pureParams: units = The units of time to validate. value = The number to validate. file = The file that the DateTimeException will list if thrown. line = The line number that the DateTimeException wi...void enforceValid(string units)(int year, Month month, int day, string file = __FILE__, size_t line = __LINE__) if (units == "days") @safe pureBecause the validity of the day number depends on both on the year and month of which the day is occurring, take all three variables to validate the day.int daysToDayOfWeek(DayOfWeek currDoW, DayOfWeek dow) @safe pure nothrow @nogcReturns 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.int monthsToMonth(int currMonth, int month) @safe pureReturns the number of months from the current months of the year to the given month of the year. If they are the same, then the result is 0.bool yearIsLeapYear(int year) @safe pure nothrow @nogcWhether the given Gregorian Year is a leap year.bool validTimeUnits(string[] units...) @safe pure nothrow @nogcWhether all of the given strings are valid units of time.int cmpTimeUnits(string lhs, string rhs) @safe pureCompares two time unit strings. `"years"` are the largest units and `"hnsecs"` are the smallest.long splitUnitsFromHNSecs(string units)(ref long hnsecs) if (validTimeUnits(units) && CmpTimeUnits!(units, "months") < 0) @safe pure nothrow @nogcVariables 9
string[] timeStringsArray of the strings representing time units, starting with the smallest unit and going to the largest. It does not include "nsecs".
Includes "hnsecs" (hecto-nanoseconds (100 ns)), "usecs" (microseconds), "msecs" (milliseconds), "seconds", "minutes", "hours", "days", "weeks", "months", and "years"
string[12] _monthNamesdaysInYear = 365daysInLeapYear = 366daysIn4Years = daysInYear * 3 + daysInLeapYeardaysIn100Years = daysIn4Years * 25 - 1daysIn400Years = daysIn100Years * 4 + 1int[13] lastDayNonLeapint[13] lastDayLeapTemplates 2
Whether the given type defines all of the necessary functions for it to function as a time point.
Tmust define a static property namedminwhich is the smallest
value of T as Unqual!T.
Tmust define a static property namedmaxwhich is the largest
value of T as Unqual!T.
Tmust define anopBinaryfor addition and subtraction that
accepts Duration and returns Unqual!T.
Tmust define anopOpAssignfor addition and subtraction that
accepts Duration and returns ref Unqual!T.
Tmust define aopBinaryfor subtraction which acceptsT
and returns Duration.
if (validTimeUnits(lhs, rhs))Compares two time unit strings at compile time. "years" are the largest units and "hnsecs" are the smallest.
This template is used instead of cmpTimeUnits because exceptions can't be thrown at compile time and cmpTimeUnits must enforce that the strings it's given are valid time unit strings. This template uses a template constraint instead.