Wolfgang Thallergcc16 merge934c3885 months agoHistory
10938 lines · 423.0 KB · D
RawDownload
// Written in the D programming language/++ $(SCRIPT inhibitQuickIndex = 1;)$(DIVC quickindex,$(BOOKTABLE,$(TR $(TH Category) $(TH Functions))$(TR $(TD Main date types) $(TD    $(LREF Date)    $(LREF DateTime)))$(TR $(TD Other date types) $(TD    $(LREF Month)    $(LREF DayOfWeek)    $(LREF TimeOfDay)))$(TR $(TD Date checking) $(TD    $(LREF valid)    $(LREF validTimeUnits)    $(LREF yearIsLeapYear)    $(LREF isTimePoint)    $(LREF enforceValid)))$(TR $(TD Date conversion) $(TD    $(LREF daysToDayOfWeek)    $(LREF monthsToMonth)))$(TR $(TD Time units) $(TD    $(LREF cmpTimeUnits)    $(LREF timeStrings)))$(TR $(TD Other) $(TD    $(LREF AllowDayOverflow)    $(LREF DateTimeException)))))     License:   $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).    Authors:   $(HTTP jmdavisprog.com, Jonathan M Davis)    Source:    $(PHOBOSSRC std/datetime/date.d)+/module std.datetime.date; import core.time : TimeException;import std.traits : isSomeString, Unqual;import std.typecons : Flag;import std.range.primitives : isOutputRange; version (StdUnittest) import std.exception : assertThrown; @safe unittest{    initializeTests();}  /++    Exception type used by std.datetime. It's an alias to    $(REF TimeException,core,time). Either can be caught without concern about    which module it came from.  +/alias DateTimeException = TimeException;  /++    Represents the 12 months of the Gregorian year (January is 1).  +/enum Month : ubyte{    jan = 1, ///    feb,     ///    mar,     ///    apr,     ///    may,     ///    jun,     ///    jul,     ///    aug,     ///    sep,     ///    oct,     ///    nov,     ///    dec      ///} ///@safe pure unittest{    assert(Date(2018, 10, 1).month == Month.oct);    assert(DateTime(1, 1, 1).month == Month.jan);}  /++    Represents the 7 days of the Gregorian week (Sunday is 0).  +/enum DayOfWeek : ubyte{    sun = 0, ///    mon,     ///    tue,     ///    wed,     ///    thu,     ///    fri,     ///    sat      ///} ///@safe pure unittest{    assert(Date(2018, 10, 1).dayOfWeek == DayOfWeek.mon);    assert(DateTime(5, 5, 5).dayOfWeek == DayOfWeek.thu);} /++    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.  +/alias AllowDayOverflow = Flag!"allowDayOverflow";  /++    Array 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"`  +/immutable string[] timeStrings = ["hnsecs", "usecs", "msecs", "seconds", "minutes",                                  "hours", "days", "weeks", "months", "years"];  /++    Combines the $(REF Date,std,datetime,date) and    $(REF TimeOfDay,std,datetime,date) 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    $(REF SysTime,std,datetime,systime). $(REF SysTime,std,datetime,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.  +/struct DateTime{public:     /++        Params:            date = The date portion of $(LREF DateTime).            tod  = The time portion of $(LREF DateTime).      +/    this(Date date, TimeOfDay tod = TimeOfDay.init) @safe pure nothrow @nogc    {        _date = date;        _tod = tod;    }     @safe unittest    {        {            auto dt = DateTime.init;            assert(dt._date == Date.init);            assert(dt._tod == TimeOfDay.init);        }         {            auto dt = DateTime(Date(1999, 7 ,6));            assert(dt._date == Date(1999, 7, 6));            assert(dt._tod == TimeOfDay.init);        }         {            auto dt = DateTime(Date(1999, 7 ,6), TimeOfDay(12, 30, 33));            assert(dt._date == Date(1999, 7, 6));            assert(dt._tod == TimeOfDay(12, 30, 33));        }    }      /++        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 minute portion of the time;            second = The second portion of the time;      +/    this(int year, int month, int day, int hour = 0, int minute = 0, int second = 0) @safe pure    {        _date = Date(year, month, day);        _tod = TimeOfDay(hour, minute, second);    }     @safe unittest    {        {            auto dt = DateTime(1999, 7 ,6);            assert(dt._date == Date(1999, 7, 6));            assert(dt._tod == TimeOfDay.init);        }         {            auto dt = DateTime(1999, 7 ,6, 12, 30, 33);            assert(dt._date == Date(1999, 7, 6));            assert(dt._tod == TimeOfDay(12, 30, 33));        }    }      /++        Compares this $(LREF DateTime) with the given `DateTime.`.         Returns:            $(BOOKTABLE,            $(TR $(TD this &lt; rhs) $(TD &lt; 0))            $(TR $(TD this == rhs) $(TD 0))            $(TR $(TD this &gt; rhs) $(TD &gt; 0))            )     +/    int opCmp(DateTime rhs) const @safe pure nothrow @nogc    {        immutable dateResult = _date.opCmp(rhs._date);         if (dateResult != 0)            return dateResult;         return _tod.opCmp(rhs._tod);    }     @safe unittest    {        // Test A.D.        assert(DateTime(Date.init, TimeOfDay.init).opCmp(DateTime.init) == 0);         assert(DateTime(Date(1999, 1, 1)).opCmp(DateTime(Date(1999, 1, 1))) == 0);        assert(DateTime(Date(1, 7, 1)).opCmp(DateTime(Date(1, 7, 1))) == 0);        assert(DateTime(Date(1, 1, 6)).opCmp(DateTime(Date(1, 1, 6))) == 0);         assert(DateTime(Date(1999, 7, 1)).opCmp(DateTime(Date(1999, 7, 1))) == 0);        assert(DateTime(Date(1999, 7, 6)).opCmp(DateTime(Date(1999, 7, 6))) == 0);         assert(DateTime(Date(1, 7, 6)).opCmp(DateTime(Date(1, 7, 6))) == 0);         assert(DateTime(Date(1999, 7, 6)).opCmp(DateTime(Date(2000, 7, 6))) < 0);        assert(DateTime(Date(2000, 7, 6)).opCmp(DateTime(Date(1999, 7, 6))) > 0);        assert(DateTime(Date(1999, 7, 6)).opCmp(DateTime(Date(1999, 8, 6))) < 0);        assert(DateTime(Date(1999, 8, 6)).opCmp(DateTime(Date(1999, 7, 6))) > 0);        assert(DateTime(Date(1999, 7, 6)).opCmp(DateTime(Date(1999, 7, 7))) < 0);        assert(DateTime(Date(1999, 7, 7)).opCmp(DateTime(Date(1999, 7, 6))) > 0);         assert(DateTime(Date(1999, 8, 7)).opCmp(DateTime(Date(2000, 7, 6))) < 0);        assert(DateTime(Date(2000, 8, 6)).opCmp(DateTime(Date(1999, 7, 7))) > 0);        assert(DateTime(Date(1999, 7, 7)).opCmp(DateTime(Date(2000, 7, 6))) < 0);        assert(DateTime(Date(2000, 7, 6)).opCmp(DateTime(Date(1999, 7, 7))) > 0);        assert(DateTime(Date(1999, 7, 7)).opCmp(DateTime(Date(1999, 8, 6))) < 0);        assert(DateTime(Date(1999, 8, 6)).opCmp(DateTime(Date(1999, 7, 7))) > 0);          assert(DateTime(Date(1999, 7, 6), TimeOfDay(0, 0, 0)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(0, 0, 0))) == 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 0, 0)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 0, 0))) == 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(0, 30, 0)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(0, 30, 0))) == 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(0, 0, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(0, 0, 33))) == 0);         assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 0)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 0))) == 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33))) == 0);         assert(DateTime(Date(1999, 7, 6), TimeOfDay(0, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(0, 30, 33))) == 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(0, 0, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(0, 0, 33))) == 0);         assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(13, 30, 33))) < 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(13, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33))) > 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 31, 33))) < 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 31, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33))) > 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 34))) < 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 34)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33))) > 0);         assert(DateTime(Date(1999, 7, 6), TimeOfDay(13, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 34))) > 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 34)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(13, 30, 33))) < 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(13, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 31, 33))) > 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 31, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(13, 30, 33))) < 0);         assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 31, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 34))) > 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 34)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 31, 33))) < 0);         assert(DateTime(Date(1999, 7, 6), TimeOfDay(13, 30, 33)).opCmp(                   DateTime(Date(2000, 7, 6), TimeOfDay(12, 30, 33))) < 0);        assert(DateTime(Date(2000, 7, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(13, 30, 33))) > 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 31, 33)).opCmp(                   DateTime(Date(2000, 7, 6), TimeOfDay(12, 30, 33))) < 0);        assert(DateTime(Date(2000, 7, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 31, 33))) > 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 34)).opCmp(                   DateTime(Date(2000, 7, 6), TimeOfDay(12, 30, 33))) < 0);        assert(DateTime(Date(2000, 7, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 34))) > 0);         assert(DateTime(Date(1999, 7, 6), TimeOfDay(13, 30, 33)).opCmp(                   DateTime(Date(1999, 8, 6), TimeOfDay(12, 30, 33))) < 0);        assert(DateTime(Date(1999, 8, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(13, 30, 33))) > 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 31, 33)).opCmp(                   DateTime(Date(1999, 8, 6), TimeOfDay(12, 30, 33))) < 0);        assert(DateTime(Date(1999, 8, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 31, 33))) > 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 34)).opCmp(                   DateTime(Date(1999, 8, 6), TimeOfDay(12, 30, 33))) < 0);        assert(DateTime(Date(1999, 8, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 34))) > 0);         assert(DateTime(Date(1999, 7, 6), TimeOfDay(13, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 7), TimeOfDay(12, 30, 33))) < 0);        assert(DateTime(Date(1999, 7, 7), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(13, 30, 33))) > 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 31, 33)).opCmp(                   DateTime(Date(1999, 7, 7), TimeOfDay(12, 31, 33))) < 0);        assert(DateTime(Date(1999, 7, 7), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33))) > 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 34)).opCmp(                   DateTime(Date(1999, 7, 7), TimeOfDay(12, 30, 33))) < 0);        assert(DateTime(Date(1999, 7, 7), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 34))) > 0);         // Test B.C.        assert(DateTime(Date(-1, 1, 1), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1, 1, 1), TimeOfDay(12, 30, 33))) == 0);        assert(DateTime(Date(-1, 7, 1), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1, 7, 1), TimeOfDay(12, 30, 33))) == 0);        assert(DateTime(Date(-1, 1, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1, 1, 6), TimeOfDay(12, 30, 33))) == 0);         assert(DateTime(Date(-1999, 7, 1), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1999, 7, 1), TimeOfDay(12, 30, 33))) == 0);        assert(DateTime(Date(-1999, 7, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1999, 7, 6), TimeOfDay(12, 30, 33))) == 0);         assert(DateTime(Date(-1, 7, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1, 7, 6), TimeOfDay(12, 30, 33))) == 0);         assert(DateTime(Date(-2000, 7, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1999, 7, 6), TimeOfDay(12, 30, 33))) < 0);        assert(DateTime(Date(-1999, 7, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-2000, 7, 6), TimeOfDay(12, 30, 33))) > 0);        assert(DateTime(Date(-1999, 7, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1999, 8, 6), TimeOfDay(12, 30, 33))) < 0);        assert(DateTime(Date(-1999, 8, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1999, 7, 6), TimeOfDay(12, 30, 33))) > 0);        assert(DateTime(Date(-1999, 7, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1999, 7, 7), TimeOfDay(12, 30, 33))) < 0);        assert(DateTime(Date(-1999, 7, 7), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1999, 7, 6), TimeOfDay(12, 30, 33))) > 0);         assert(DateTime(Date(-2000, 8, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1999, 7, 7), TimeOfDay(12, 30, 33))) < 0);        assert(DateTime(Date(-1999, 8, 7), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-2000, 7, 6), TimeOfDay(12, 30, 33))) > 0);        assert(DateTime(Date(-2000, 7, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1999, 7, 7), TimeOfDay(12, 30, 33))) < 0);        assert(DateTime(Date(-1999, 7, 7), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-2000, 7, 6), TimeOfDay(12, 30, 33))) > 0);        assert(DateTime(Date(-1999, 7, 7), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1999, 8, 6), TimeOfDay(12, 30, 33))) < 0);        assert(DateTime(Date(-1999, 8, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1999, 7, 7), TimeOfDay(12, 30, 33))) > 0);         // Test Both        assert(DateTime(Date(-1999, 7, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33))) < 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1999, 7, 6), TimeOfDay(12, 30, 33))) > 0);         assert(DateTime(Date(-1999, 8, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33))) < 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1999, 8, 6), TimeOfDay(12, 30, 33))) > 0);         assert(DateTime(Date(-1999, 7, 7), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33))) < 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1999, 7, 7), TimeOfDay(12, 30, 33))) > 0);         assert(DateTime(Date(-1999, 8, 7), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33))) < 0);        assert(DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1999, 8, 7), TimeOfDay(12, 30, 33))) > 0);         assert(DateTime(Date(-1999, 8, 6), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(1999, 6, 6), TimeOfDay(12, 30, 33))) < 0);        assert(DateTime(Date(1999, 6, 8), TimeOfDay(12, 30, 33)).opCmp(                   DateTime(Date(-1999, 7, 6), TimeOfDay(12, 30, 33))) > 0);         auto dt = DateTime(Date(1999, 7, 6), TimeOfDay(12, 33, 30));        const cdt = DateTime(Date(1999, 7, 6), TimeOfDay(12, 33, 30));        immutable idt = DateTime(Date(1999, 7, 6), TimeOfDay(12, 33, 30));        assert(dt.opCmp(dt) == 0);        assert(dt.opCmp(cdt) == 0);        assert(dt.opCmp(idt) == 0);        assert(cdt.opCmp(dt) == 0);        assert(cdt.opCmp(cdt) == 0);        assert(cdt.opCmp(idt) == 0);        assert(idt.opCmp(dt) == 0);        assert(idt.opCmp(cdt) == 0);        assert(idt.opCmp(idt) == 0);    }      /++        The date portion of $(LREF DateTime).      +/    @property Date date() const @safe pure nothrow @nogc    {        return _date;    }     @safe unittest    {        {            auto dt = DateTime.init;            assert(dt.date == Date.init);        }         {            auto dt = DateTime(Date(1999, 7, 6));            assert(dt.date == Date(1999, 7, 6));        }         const cdt = DateTime(1999, 7, 6);        immutable idt = DateTime(1999, 7, 6);        assert(cdt.date == Date(1999, 7, 6));        assert(idt.date == Date(1999, 7, 6));    }      /++        The date portion of $(LREF DateTime).         Params:            date = The Date to set this $(LREF DateTime)'s date portion to.      +/    @property void date(Date date) @safe pure nothrow @nogc    {        _date = date;    }     @safe unittest    {        auto dt = DateTime.init;        dt.date = Date(1999, 7, 6);        assert(dt._date == Date(1999, 7, 6));        assert(dt._tod == TimeOfDay.init);         const cdt = DateTime(1999, 7, 6);        immutable idt = DateTime(1999, 7, 6);        static assert(!__traits(compiles, cdt.date = Date(2010, 1, 1)));        static assert(!__traits(compiles, idt.date = Date(2010, 1, 1)));    }      /++        The time portion of $(LREF DateTime).      +/    @property TimeOfDay timeOfDay() const @safe pure nothrow @nogc    {        return _tod;    }

Lines 1–500 of 10938.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t