parseDate

Parses a date in a format specified in ISO 8601:2004.

Returns the number of characters used to compose a valid date: 0 if no date can be composed.

Fields in dt will either be correct (e.g. months will be >= 1 and <= 12) or the default, which is 1 for year, month, and day, and 0 for all other fields. Unless one is absolutely sure that 0001-01-01 can never be encountered, one should check the return value to be sure that the parsing succeeded as expected.

A third parameter is available for the ExtendedDate version: this allows for parsing expanded year representations. The parameter is the number of extra year digits beyond four, and defaults to zero. It must be within the range [0,5]: this allows for a maximum year of 999 999 999, which should be enough for now.

When using expanded year representations, be careful to use ExtendedDate.year instead of the Time's year value.

  1. size_t parseDate(T[] src, DT dt)
    size_t
    parseDate
    (
    T
    )
    (
    T[] src
    ,
    ref DT dt
    )
  2. size_t parseDate(T[] src, FullDate fd, ubyte expanded)

Examples

Time t;
ExtendedDate ed;

parseDate("19",             t);    // January 1st, 1900
parseDate("1970",           t);    // January 1st, 1970
parseDate("1970-02",        t);    // February 1st, 1970
parseDate("19700203",       t);    // February 3rd, 1970
parseDate("+19700203",     ed, 2); // March 1st, 197002
parseDate("-197002-04-01", ed, 2); // April 1st, -197003 (197003 BCE)
parseDate("00000101",       t);    // January 1st, -1 (1 BCE)
parseDate("1700-W14-2",     t);    // April 6th, 1700
parseDate("2008W01",        t);    // December 31st, 2007
parseDate("1987-221",       t);    // August 9th, 1987
parseDate("1234abcd",       t);    // January 1st, 1234; return value is 4
parseDate("12abcdef",       t);    // January 1st, 1200; return value is 2
parseDate("abcdefgh",       t);    // January 1st, 0001; return value is 0

Meta