Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #75407 > unrolled thread
| Started by | Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> |
|---|---|
| First post | 2014-07-31 11:11 +0200 |
| Last post | 2014-07-31 15:29 +0200 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.python
converting ISO8601 date and time string representations to datetime Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-07-31 11:11 +0200
Re: converting ISO8601 date and time string representations to datetime Roy Smith <roy@panix.com> - 2014-07-31 07:52 -0400
Re: converting ISO8601 date and time string representations to datetime Skip Montanaro <skip@pobox.com> - 2014-07-31 07:59 -0500
Re: converting ISO8601 date and time string representations to datetime Roy Smith <roy@panix.com> - 2014-07-31 09:13 -0400
Re: converting ISO8601 date and time string representations to datetime Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-07-31 15:29 +0200
| From | Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> |
|---|---|
| Date | 2014-07-31 11:11 +0200 |
| Subject | converting ISO8601 date and time string representations to datetime |
| Message-ID | <mailman.12461.1406797909.18130.python-list@python.org> |
Hi, I'm trying to convert ISO8601-compliant strings representing dates or dates and times into datetime.datetime objects. I tried using the strptime method, but the problem here is that I can only specify one format argument, which can be used to parse either a full date/time string or just a date string, but not both. While I could just try parsing with one format, then catch the ValueError in case it fails and try another format, it feels like there should be a more elegant way to do this. I do know about the dateutil package, but I'd prefer to stick to the stdlib for this. Any suggestions ? Wolfgang
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2014-07-31 07:52 -0400 |
| Message-ID | <roy-FFB727.07524431072014@news.panix.com> |
| In reply to | #75407 |
In article <mailman.12461.1406797909.18130.python-list@python.org>, Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> wrote: > Hi, > I'm trying to convert ISO8601-compliant strings representing dates or > dates and times into datetime.datetime objects. https://pypi.python.org/pypi/iso8601 or https://pypi.python.org/pypi/isodate There's also.... > I do know about the dateutil package, but I'd prefer to stick to the > stdlib for this. Sadly, the stdlib datetime really doesn't make life easy for dealing with ISO-8601. Dateutil is the classic answer, but it's slow. The isodate module cited above is what we use.
[toc] | [prev] | [next] | [standalone]
| From | Skip Montanaro <skip@pobox.com> |
|---|---|
| Date | 2014-07-31 07:59 -0500 |
| Message-ID | <mailman.12463.1406811576.18130.python-list@python.org> |
| In reply to | #75410 |
On Thu, Jul 31, 2014 at 6:52 AM, Roy Smith <roy@panix.com> wrote: > Sadly, the stdlib datetime really doesn't make life easy for dealing > with ISO-8601. Dateutil is the classic answer, but it's slow. A useful feature for dateutil would be a "sniff" function which, given a date string that dateutil.parser.parse can handle, instead returns a format string suitable for use with time.strptime. Skip
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2014-07-31 09:13 -0400 |
| Message-ID | <mailman.12464.1406812399.18130.python-list@python.org> |
| In reply to | #75410 |
On Jul 31, 2014, at 8:59 AM, Skip Montanaro wrote: > On Thu, Jul 31, 2014 at 6:52 AM, Roy Smith <roy@panix.com> wrote: >> Sadly, the stdlib datetime really doesn't make life easy for dealing >> with ISO-8601. Dateutil is the classic answer, but it's slow. > > A useful feature for dateutil would be a "sniff" function which, given > a date string that dateutil.parser.parse can handle, instead returns a > format string suitable for use with time.strptime. +0.8. But, even more than that, dateutil should come with a library of strings for common formats, and a built-in function to parse 8601. It is, after all, the international standard. -- Roy Smith roy@panix.com
[toc] | [prev] | [next] | [standalone]
| From | Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> |
|---|---|
| Date | 2014-07-31 15:29 +0200 |
| Message-ID | <mailman.12467.1406813397.18130.python-list@python.org> |
| In reply to | #75410 |
On 31.07.2014 15:13, Roy Smith wrote: > On Jul 31, 2014, at 8:59 AM, Skip Montanaro wrote: > >> On Thu, Jul 31, 2014 at 6:52 AM, Roy Smith <roy@panix.com> wrote: >>> Sadly, the stdlib datetime really doesn't make life easy for dealing >>> with ISO-8601. Dateutil is the classic answer, but it's slow. >> >> A useful feature for dateutil would be a "sniff" function which, given >> a date string that dateutil.parser.parse can handle, instead returns a >> format string suitable for use with time.strptime. > > +0.8. But, even more than that, dateutil should come with a library of strings for common formats, and a built-in function to parse 8601. It is, after all, the international standard. > Thanks for your feedback. I'll have a look at isodate as you suggested, Roy. What this makes me wonder is whether there shouldn't be a builtin method for parsing ISO-8601 strings in the stdlib datetime module, just as there is .isoformat() for the reverse action already. Alternatively, the existing format argument to datetime.strptime() could be made optional and when absent, strptime() could try to parse ISO-8601 ? Wolfgang
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web