Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > strptime - dates formatted differently on different computers

Reply
Thread Tools

strptime - dates formatted differently on different computers

 
 
noydb
Guest
Posts: n/a
 
      12-11-2012
On Tuesday, December 11, 2012 3:35:29 AM UTC-5, Greg Donald wrote:
> On Mon, Dec 10, 2012 at 10:34:31PM -0700, Michael Torrie wrote:
>
> > I use a module I got from pypi called dateutil. It has a nice submodule

>
> > called parser that can handle a variety of date formats with good

>
> > accuracy. Not sure how it works, but it handles all the common American

>
> > date formats I've thrown at it.

>
>
>
> from dateutil.parser import parse
>
> dt = parse( whatever )
>
>
>
> I've throw all kind of date and timestamps at it.. have yet to see anything it won't parse.
>
>
>
>
>
> --
>
> Greg Donald



Thanks - I tried this (dateutil.parser import parsed...), and it works. I'm skeptical of it working for any crazy date string thrown at it, but for my purposes it should suffice -- and my purposes for now was purely just curiousity on how to handle if it became necessary.

I tried figuring out Steve D'Aprano's solution above on my system (windows 7, python 2.7) - no luck. Sorry, I am a newbie, so I'm a bit lost on this --- my locale module doesnt offer a nl_langinfo function -- why would this be?
 
Reply With Quote
 
 
 
 
noydb
Guest
Posts: n/a
 
      12-11-2012
On Tuesday, December 11, 2012 3:35:29 AM UTC-5, Greg Donald wrote:
> On Mon, Dec 10, 2012 at 10:34:31PM -0700, Michael Torrie wrote:
>
> > I use a module I got from pypi called dateutil. It has a nice submodule

>
> > called parser that can handle a variety of date formats with good

>
> > accuracy. Not sure how it works, but it handles all the common American

>
> > date formats I've thrown at it.

>
>
>
> from dateutil.parser import parse
>
> dt = parse( whatever )
>
>
>
> I've throw all kind of date and timestamps at it.. have yet to see anything it won't parse.
>
>
>
>
>
> --
>
> Greg Donald



Thanks - I tried this (dateutil.parser import parsed...), and it works. I'm skeptical of it working for any crazy date string thrown at it, but for my purposes it should suffice -- and my purposes for now was purely just curiousity on how to handle if it became necessary.

I tried figuring out Steve D'Aprano's solution above on my system (windows 7, python 2.7) - no luck. Sorry, I am a newbie, so I'm a bit lost on this --- my locale module doesnt offer a nl_langinfo function -- why would this be?
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-12-2012
On Tue, 11 Dec 2012 02:35:29 -0600, Greg Donald wrote:

> On Mon, Dec 10, 2012 at 10:34:31PM -0700, Michael Torrie wrote:
>> I use a module I got from pypi called dateutil. It has a nice
>> submodule called parser that can handle a variety of date formats with
>> good accuracy. Not sure how it works, but it handles all the common
>> American date formats I've thrown at it.

>
> from dateutil.parser import parse
> dt = parse( whatever )
>
> I've throw all kind of date and timestamps at it.. have yet to see
> anything it won't parse.


The question is not "will it parse", but will it parse CORRECTLY?

What will it parse 11/12/10 as, and how do you know that is the intended
date?


--
Steven
 
Reply With Quote
 
Greg Donald
Guest
Posts: n/a
 
      12-12-2012
On Tue, Dec 11, 2012 at 2:18 PM, Marc Christiansen
<> wrote:
>>>> parse('1. Januar 2013')

> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/usr/lib64/python3.3/site-packages/dateutil/parser.py", line 720, in parse
> return DEFAULTPARSER.parse(timestr, **kwargs)
> File "/usr/lib64/python3.3/site-packages/dateutil/parser.py", line 310, in parse
> raise ValueError("unknown string format")
> ValueError: unknown string format


A parserinfo class can be passed to parse() for unknown locale strings.

>>>> parse('1.2.2013') # ambiguous, I know

> datetime.datetime(2013, 1, 2, 0, 0) # should be datetime.datetime(2013, 2, 1, 0, 0)


In [2]: parse('1.2.2013', dayfirst=True)
Out[2]: datetime.datetime(2013, 2, 1, 0, 0)



--
Greg Donald
 
Reply With Quote
 
Greg Donald
Guest
Posts: n/a
 
      12-12-2012
On Tue, Dec 11, 2012 at 11:05 PM, Steven D'Aprano
<steve+> wrote:
> The question is not "will it parse", but will it parse CORRECTLY?
>
> What will it parse 11/12/10 as, and how do you know that is the intended
> date?


If it were me I'd look at more of the source dates I was tasked with
parsing and dial it in using the appropriate dayfirst, yearfirst, etc.
options.



--
Greg Donald
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Page displays differently on different computers... what's going on? Tony Calguire Firefox 9 08-17-2007 03:59 AM
problem using the strptime function on HPUXThe strptime function works just kongkolvyu C Programming 4 01-04-2006 11:03 PM
Dates dates dates dates... SQL and ASP.NET David Lozzi ASP .Net 1 09-30-2005 02:18 PM
Dates! Dates! Dates! PW ASP General 4 08-09-2004 04:42 PM
Special Characters display differently on different computers Ryan Javascript 2 10-28-2003 01:44 PM



Advertisments