Discussion:
Parsing a date string into a std.datetime.datetime
Colin via Digitalmars-d-learn
2014-10-23 11:13:25 UTC
Permalink
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
Jonathan M Davis via Digitalmars-d-learn
2014-10-23 21:17:21 UTC
Permalink
Post by Colin via Digitalmars-d-learn
Hi,
I'm looking for an easy way to parse a dates into a datetime
object.
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
Colin via Digitalmars-d-learn
2014-10-24 09:17:54 UTC
Permalink
On Thursday, 23 October 2014 at 21:17:23 UTC, Jonathan M Davis
Post by Jonathan M Davis via Digitalmars-d-learn
Post by Colin via Digitalmars-d-learn
Hi,
I'm looking for an easy way to parse a dates into a datetime
object.
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
Ok, thanks for the informative reply Jonathan.

For now I'll go with parsing the few types of dates I may need,
and maybe port over in the future when you get to it.

Cheers!

Loading...