Parsing a date string into a std.datetime.datetime

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 23 14:17:21 PDT 2014


On Thursday, 23 October 2014 at 11:13:26 UTC, Colin wrote:
> Hi,
>
> I'm looking for an easy way to parse a dates into a datetime 
> object.
>
> Most of my dates will be of the form:
> mmm dd, yyyy HH:MM AM|PM
>
> So like: "May 30, 2014 12:12 PM"
>
> I can easily write a regex or whatever to pull these out of 
> that one format, but it's not guaranteed they'll all be in the 
> one format and I may have to deal with others.
>
> Is there a helper function that I'm missing that can parse 
> these dates? Maybe something similar to pythons dateutil.parser 
> [1] ?
>
> If not maybe adding this function to std.datetime would be a 
> good project to undertake for myself...
>
> [1] - https://labix.org/python-dateutil

std.datetime supports the ISO formats but, it does not currently 
support generating or parsing custom strings for dates or times. 
It's on my todo list (probably after splitting std.datetime into 
a package), but I don't know exactly when I'm going to get to it. 
The first step will be figuring out what the format strings will 
look like, since what languages like C do is a complete mess. I 
had a proposal on it that was discussed a while ago, but it was 
too complicated. It'll probably end up being something closer to 
this http://pr.stewartsplace.org.uk/d/sutil/datetime_format.html 
though I'm afraid that that approach as it's presented might not 
be flexible enough. I'll probably need to do something like add a 
templated function that returns a custom struct with the values 
that you want so that you can get them effeiently to build the 
string yourself in the cases where you need to do something wacky 
enough that the normal custom string formatting functions aren't 
flexible enough. Then leaving the normal custom string format 
generating and parsing functions simpler works better.

In any case, I intend to get to it, but I've been dreadfully slow 
about it. It's the number one thing missing from std.datetime. 
I'd prefer to do it myself, but there's certainly no reason why 
someone else can't do it if they really want to.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list