Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102885 > unrolled thread
| Started by | Tom P <werotizy@freent.dd> |
|---|---|
| First post | 2016-02-13 18:58 +0100 |
| Last post | 2016-02-14 13:27 +0100 |
| Articles | 7 — 3 participants |
Back to article view | Back to comp.lang.python
problem with dateutil Tom P <werotizy@freent.dd> - 2016-02-13 18:58 +0100
Re: problem with dateutil Gary Herron <gherron@digipen.edu> - 2016-02-13 10:13 -0800
Re: problem with dateutil Tom P <werotizy@freent.dd> - 2016-02-13 21:27 +0100
Re: problem with dateutil Gary Herron <gherron@digipen.edu> - 2016-02-13 12:45 -0800
Re: problem with dateutil Tom P <werotizy@freent.dd> - 2016-02-14 13:26 +0100
Re: problem with dateutil Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-02-13 21:01 +0000
Re: problem with dateutil Tom P <werotizy@freent.dd> - 2016-02-14 13:27 +0100
| From | Tom P <werotizy@freent.dd> |
|---|---|
| Date | 2016-02-13 18:58 +0100 |
| Subject | problem with dateutil |
| Message-ID | <di995iFr285U1@mid.individual.net> |
I am writing a program that has to deal with various date/time formats and convert these into timestamps. It looks as if dateutil.parser.parse should be able to handle about any format, but what I get is: datetimestr = '2012-10-22 11:22:33' print(dateutil.parser.parse(datetimestr)) result: datetime.datetime(2012, 10, 22, 11, 22, 33) However: datetimestr = '2012:10:22 11:22:33' print(dateutil.parser.parse(datetimestr)) result: datetime.datetime(2016, 2, 13, 11, 22, 33) In other words, it's getting the date wrong when colons are used to separate YYYY:MM:DD. Is there a way to include this as a valid format?
[toc] | [next] | [standalone]
| From | Gary Herron <gherron@digipen.edu> |
|---|---|
| Date | 2016-02-13 10:13 -0800 |
| Message-ID | <mailman.94.1455387608.22075.python-list@python.org> |
| In reply to | #102885 |
On 02/13/2016 09:58 AM, Tom P wrote:
> I am writing a program that has to deal with various date/time formats
> and convert these into timestamps. It looks as if
> dateutil.parser.parse should be able to handle about any format, but
> what I get is:
>
> datetimestr = '2012-10-22 11:22:33'
> print(dateutil.parser.parse(datetimestr))
> result: datetime.datetime(2012, 10, 22, 11, 22, 33)
>
> However:
> datetimestr = '2012:10:22 11:22:33'
> print(dateutil.parser.parse(datetimestr))
> result: datetime.datetime(2016, 2, 13, 11, 22, 33)
>
> In other words, it's getting the date wrong when colons are used to
> separate YYYY:MM:DD. Is there a way to include this as a valid format?
>
Yes, there is a way to specify your own format. Search the datetime
documentation for
datetime.strptime(date_string, format)
Gary Herron
--
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418
[toc] | [prev] | [next] | [standalone]
| From | Tom P <werotizy@freent.dd> |
|---|---|
| Date | 2016-02-13 21:27 +0100 |
| Message-ID | <di9ht8FtcmgU1@mid.individual.net> |
| In reply to | #102886 |
On 02/13/2016 07:13 PM, Gary Herron wrote: > On 02/13/2016 09:58 AM, Tom P wrote: >> I am writing a program that has to deal with various date/time formats >> and convert these into timestamps. It looks as if >> dateutil.parser.parse should be able to handle about any format, but >> what I get is: >> >> datetimestr = '2012-10-22 11:22:33' >> print(dateutil.parser.parse(datetimestr)) >> result: datetime.datetime(2012, 10, 22, 11, 22, 33) >> >> However: >> datetimestr = '2012:10:22 11:22:33' >> print(dateutil.parser.parse(datetimestr)) >> result: datetime.datetime(2016, 2, 13, 11, 22, 33) >> >> In other words, it's getting the date wrong when colons are used to >> separate YYYY:MM:DD. Is there a way to include this as a valid format? >> > > Yes, there is a way to specify your own format. Search the datetime > documentation for > datetime.strptime(date_string, format) > > Gary Herron > Thanks. I started out with datetime.strptime but AFAICS that means I have to go through try/except for every conceivable format. Are you saying that I can't use dateutil.parser?
[toc] | [prev] | [next] | [standalone]
| From | Gary Herron <gherron@digipen.edu> |
|---|---|
| Date | 2016-02-13 12:45 -0800 |
| Message-ID | <mailman.96.1455396346.22075.python-list@python.org> |
| In reply to | #102890 |
On 02/13/2016 12:27 PM, Tom P wrote: > On 02/13/2016 07:13 PM, Gary Herron wrote: >> On 02/13/2016 09:58 AM, Tom P wrote: >>> I am writing a program that has to deal with various date/time formats >>> and convert these into timestamps. It looks as if >>> dateutil.parser.parse should be able to handle about any format, but >>> what I get is: >>> >>> datetimestr = '2012-10-22 11:22:33' >>> print(dateutil.parser.parse(datetimestr)) >>> result: datetime.datetime(2012, 10, 22, 11, 22, 33) >>> >>> However: >>> datetimestr = '2012:10:22 11:22:33' >>> print(dateutil.parser.parse(datetimestr)) >>> result: datetime.datetime(2016, 2, 13, 11, 22, 33) >>> >>> In other words, it's getting the date wrong when colons are used to >>> separate YYYY:MM:DD. Is there a way to include this as a valid format? >>> >> >> Yes, there is a way to specify your own format. Search the datetime >> documentation for >> datetime.strptime(date_string, format) >> >> Gary Herron >> > > Thanks. I started out with datetime.strptime but AFAICS that means I > have to go through try/except for every conceivable format. Are you > saying that I can't use dateutil.parser? Well now... If by "every conceivable format" you are including formats that the author of dateutil.parser did not conceive of, then of course you cannot use dateutil.parser. But you have the code for dateutil.parser -- perhaps you could modify it to accept whatever odd formats you care about. Gary Herron -- Dr. Gary Herron Department of Computer Science DigiPen Institute of Technology (425) 895-4418
[toc] | [prev] | [next] | [standalone]
| From | Tom P <werotizy@freent.dd> |
|---|---|
| Date | 2016-02-14 13:26 +0100 |
| Message-ID | <diba2nFc2t1U1@mid.individual.net> |
| In reply to | #102891 |
On 02/13/2016 09:45 PM, Gary Herron wrote:
> On 02/13/2016 12:27 PM, Tom P wrote:
>> On 02/13/2016 07:13 PM, Gary Herron wrote:
>>> On 02/13/2016 09:58 AM, Tom P wrote:
>>>> I am writing a program that has to deal with various date/time formats
>>>> and convert these into timestamps. It looks as if
>>>> dateutil.parser.parse should be able to handle about any format, but
>>>> what I get is:
>>>>
>>>> datetimestr = '2012-10-22 11:22:33'
>>>> print(dateutil.parser.parse(datetimestr))
>>>> result: datetime.datetime(2012, 10, 22, 11, 22, 33)
>>>>
>>>> However:
>>>> datetimestr = '2012:10:22 11:22:33'
>>>> print(dateutil.parser.parse(datetimestr))
>>>> result: datetime.datetime(2016, 2, 13, 11, 22, 33)
>>>>
>>>> In other words, it's getting the date wrong when colons are used to
>>>> separate YYYY:MM:DD. Is there a way to include this as a valid format?
>>>>
>>>
>>> Yes, there is a way to specify your own format. Search the datetime
>>> documentation for
>>> datetime.strptime(date_string, format)
>>>
>>> Gary Herron
>>>
>>
>> Thanks. I started out with datetime.strptime but AFAICS that means I
>> have to go through try/except for every conceivable format. Are you
>> saying that I can't use dateutil.parser?
>
> Well now... If by "every conceivable format" you are including formats
> that the author of dateutil.parser did not conceive of, then of course
> you cannot use dateutil.parser. But you have the code for
> dateutil.parser -- perhaps you could modify it to accept whatever odd
> formats you care about.
>
> Gary Herron
>
>
I had a look at the code for dateutil.parser. Have you looked at it?
Meanwhile I'm living with
try:
dt = datetime.datetime.strptime(datetimestr, '%Y:%m:%d %H:%M:%S')
except ValueError:
dt = dateutil.parser.parse(datetimestr)
unixtime = time.mktime(dt.timetuple())
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2016-02-13 21:01 +0000 |
| Message-ID | <mailman.98.1455397291.22075.python-list@python.org> |
| In reply to | #102885 |
On 13/02/2016 17:58, Tom P wrote:
> I am writing a program that has to deal with various date/time formats
> and convert these into timestamps. It looks as if dateutil.parser.parse
> should be able to handle about any format, but what I get is:
>
> datetimestr = '2012-10-22 11:22:33'
> print(dateutil.parser.parse(datetimestr))
> result: datetime.datetime(2012, 10, 22, 11, 22, 33)
>
> However:
> datetimestr = '2012:10:22 11:22:33'
> print(dateutil.parser.parse(datetimestr))
> result: datetime.datetime(2016, 2, 13, 11, 22, 33)
>
> In other words, it's getting the date wrong when colons are used to
> separate YYYY:MM:DD. Is there a way to include this as a valid format?
>
From
http://labix.org/python-dateutil#head-a23e8ae0a661d77b89dfb3476f85b26f0b30349c
<quote>
parserinfo
This parameter allows one to change how the string is parsed, by
using a different parserinfo class instance. Using it you may, for
example, intenationalize the parser strings, or make it ignore
additional words.
</quote>
HTH.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Tom P <werotizy@freent.dd> |
|---|---|
| Date | 2016-02-14 13:27 +0100 |
| Message-ID | <diba66Fc2t1U2@mid.individual.net> |
| In reply to | #102892 |
On 02/13/2016 10:01 PM, Mark Lawrence wrote: > On 13/02/2016 17:58, Tom P wrote: >> I am writing a program that has to deal with various date/time formats >> and convert these into timestamps. It looks as if dateutil.parser.parse >> should be able to handle about any format, but what I get is: >> >> datetimestr = '2012-10-22 11:22:33' >> print(dateutil.parser.parse(datetimestr)) >> result: datetime.datetime(2012, 10, 22, 11, 22, 33) >> >> However: >> datetimestr = '2012:10:22 11:22:33' >> print(dateutil.parser.parse(datetimestr)) >> result: datetime.datetime(2016, 2, 13, 11, 22, 33) >> >> In other words, it's getting the date wrong when colons are used to >> separate YYYY:MM:DD. Is there a way to include this as a valid format? >> > > From > http://labix.org/python-dateutil#head-a23e8ae0a661d77b89dfb3476f85b26f0b30349c > > > <quote> > parserinfo > This parameter allows one to change how the string is parsed, by > using a different parserinfo class instance. Using it you may, for > example, intenationalize the parser strings, or make it ignore > additional words. > </quote> > > HTH. > Thanks, let me look at that.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web