Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
datetime module: iso date *input* needed
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Expand all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
george young  
View profile  
 More options Jan 30 2003, 5:15 pm
Newsgroups: comp.lang.python
From: george young <g...@ll.mit.edu>
Date: Thu, 30 Jan 2003 18:17:27 -0500
Local: Thurs, Jan 30 2003 6:17 pm
Subject: datetime module: iso date *input* needed
[python 2.3a1, linux]
I'm using the 2.3a datetime module.  The datetime.isoformat() works nicely:
  >>> import datetime
  >>> d=datetime.datetime.now()
  >>> d
  datetime.datetime(2003, 1, 30, 17, 48, 7, 848769)
  >> d.isoformat()
  '2003-01-30T17:48:07.848769'

but now I need a way to make a datetime object from a date in iso format.
I can't find a function in datetime to do this, e.g. something like:
  d=datetime.datetime.parse_iso('2003-01-30T17:48:07.848769')

I thought about using the time module strptime function, but it doesn't do
fractions of a second.  I used to move date/times between python and postgres as
epoch integers, but I hope to change to a human-readable format for better
debugging.  

I'm sure I could do this somehow with mx.DateTime, but if the datetime module
is going to be the new standard, I would like to use it in my new code...

-- George
--
 I cannot think why the whole bed of the ocean is
 not one solid mass of oysters, so prolific they seem. Ah,
 I am wandering! Strange how the brain controls the brain!
        -- Sherlock Holmes in "The Dying Detective"


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dale Strickland-Clark  
View profile  
 More options Jan 30 2003, 6:52 pm
Newsgroups: comp.lang.python
From: Dale Strickland-Clark <d...@riverhall.NOTHANKS.co.uk>
Date: Thu, 30 Jan 2003 23:51:40 +0000
Local: Thurs, Jan 30 2003 6:51 pm
Subject: Re: datetime module: iso date *input* needed

A bit of a sledgehammer but what about:

>>> import re
>>> t = '2003-01-30T17:48:07.848769'
>>> p = re.compile('[.:T-]')
>>> isobits = [int(s) for s in p.split(t)]
>>> isobits

[2003, 1, 30, 17, 48, 7, 848769]

>>> apply(datetime.datetime, isobits)

I assume this will work but I don't have 2.3a installed so no datetime
module to play with.

--
Dale Strickland-Clark
Riverhall Systems Ltd


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Peters  
View profile  
 More options Jan 31 2003, 12:01 am
Newsgroups: comp.lang.python
From: Tim Peters <tim....@comcast.net>
Date: Thu, 30 Jan 2003 23:30:29 -0500
Local: Thurs, Jan 30 2003 11:30 pm
Subject: RE: datetime module: iso date *input* needed
[george young]

> I'm using the 2.3a datetime module.  The datetime.isoformat()
> works nicely:
>   >>> import datetime
>   >>> d=datetime.datetime.now()
>   >>> d
>   datetime.datetime(2003, 1, 30, 17, 48, 7, 848769)
>   >> d.isoformat()
>   '2003-01-30T17:48:07.848769'

> but now I need a way to make a datetime object from a date in iso format.
> I can't find a function in datetime to do this, e.g. something like:
>   d=datetime.datetime.parse_iso('2003-01-30T17:48:07.848769')

You can stop looking:  datetime doesn't support any kind of conversion from
string.  The number of bottomless pits in any datetime module is unbounded,
and Guido declared this particular pit out-of-bounds at the start so that
there was a fighting chance to get *anything* done for 2.3.

> I thought about using the time module strptime function, but it doesn't
> do fractions of a second.

As above.  You'll have to parse it yourself for now, but part of the point
of the ISO format is that it's dead easy to parse.  The only irregularities
are that the fractional-seconds and UTC offset portions are optional.  A
simple regexp can handle all variations easily -- and I never put "simple"
next to "regexp" to be cruel <wink>.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Boddie  
View profile  
 More options Jan 31 2003, 6:26 am
Newsgroups: comp.lang.python
From: p...@boddie.net (Paul Boddie)
Date: 31 Jan 2003 03:26:19 -0800
Local: Fri, Jan 31 2003 6:26 am
Subject: Re: datetime module: iso date *input* needed

george young <g...@ll.mit.edu> wrote in message <news:20030130181727.5a006f5d.gry@ll.mit.edu>...

> but now I need a way to make a datetime object from a date in iso format.
> I can't find a function in datetime to do this, e.g. something like:
>   d=datetime.datetime.parse_iso('2003-01-30T17:48:07.848769')

http://www.mnot.net/python/isodate.py

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Henshaw  
View profile  
 More options Jan 31 2003, 10:41 pm
Newsgroups: comp.lang.python
From: Andrew Henshaw <andrew.hens...@gtri.gatech.edu>
Date: Fri, 31 Jan 2003 22:41:05 -0500
Local: Fri, Jan 31 2003 10:41 pm
Subject: Re: datetime module: iso date *input* needed

Wouldn't you have ambiguities with the fractional portion (e.g..0848 and
.848)? Perhaps it would be best to leave the seconds in floating-point
format.

Andy


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google