datefmt 1.0.3

Datetime parsing and formatting


To use this package, run the following command in your project's root directory:

datefmt

Date formatting and parsing based on strftime.

Basic Usage

You can parse something by just calling parse:

import datefmt;
auto st = "Thu, 17 Apr 2014 14:47:35 GMT".parse("%a, %d %b %Y %H:%M:%S GMT");
assert(st == SysTime(DateTime(2014, 4, 17, 14, 47, 35), UTC()));

And you can convert it to string the same way:

auto st = SysTime(DateTime(2014, 4, 17, 14, 47, 35), UTC());
auto formatted = st.format("%a, %d %b %Y %H:%M:%S GMT");
assert(formatted == "Thu, 17 Apr 2014 14:47:35 GMT");

datefmt also supports the tryParse pattern popularized by C#:

SysTime st;
if (tryParse(userInput, "%a, %d %b %Y %H:%M:%S GMT", st))
{
    // use the value
}

Format sets

Some standards, like ISO8601, specify a collection of related formats. If you have a document that must provide values in ISO8601 format, a compliant document might include a date like 20100418, 2018-12-15T13:00, or 1904-10-18 04:00:00.000000Z.

To handle this, datefmt has a notion of format collections, simply called Format. A Format has a primary format, which is used for converting to string, and an array of alternative forms that are used for parsing.

It's simple to make your own:

Format customFormat = {
    primaryFormat: "%a, %d %b %Y %H:%M:%S %Z",
    formatOptions: [
        "%d %b %Y %H:%M:%S %Z",
        "%a %d %b %Y %H:%M:%S %Z",
        "%a, %d %b %Y %H:%M:%S",
    ]
};
SysTime st = parse(someRfcishString, customFormat);

Dub

Add a dependency on "datefmt": "~>1.0.0".

Authors:
  • dhasenan
Dependencies:
none
Versions:
1.0.3 2019-May-14
1.0.2 2017-Dec-20
1.0.1 2017-Dec-14
1.0.0 2017-Dec-12
0.4.0 2017-Sep-01
Show all 9 versions
Download Stats:
  • 0 downloads today

  • 2 downloads this week

  • 5 downloads this month

  • 449 downloads total

Score:
0.8
Short URL:
datefmt.dub.pm