[Python-bugs-list] [ python-Bugs-665194 ] datetime-RFC2822 roundtripping

SourceForge.net noreply@sourceforge.net
Mon, 13 Jan 2003 05:05:21 -0800


Bugs item #665194, was opened at 2003-01-09 19:24
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=665194&group_id=5470

Category: None
Group: Feature Request
Status: Open
Resolution: None
Priority: 5
Submitted By: Walter Dörwald (doerwalter)
Assigned to: Nobody/Anonymous (nobody)
Summary: datetime-RFC2822 roundtripping

Initial Comment:
It would be good to have a simply way to convert
between datetime objects and RFC2822 style strings.
>From string to datetime is easy with
datetime.datetime(*email.Utils.parsedate(m)[:7]) (but
this drops the timezone), but the other direction seems
impossible. email.Utils.formatdate takes a timestamp
argument, but AFAICT there's no way to get a timestamp
out of a datetime object.

Of course the best solution (ignoring backwards
compatibility) would be for parsedate und formatdate to
return/accept datetime objects or for datetime to have
the appropriate methods.

----------------------------------------------------------------------

>Comment By: Walter Dörwald (doerwalter)
Date: 2003-01-13 14:05

Message:
Logged In: YES 
user_id=89016

totimestamp() should be the inverse of fromtimestamp(), i.e.
foo.totimestamp() should be the same as
time.mktime(foo.timetuple()).
datetime.datetime.totimestamp() should raise OverflowError
iff time.mktime() raises OverflowError.

But as this may lose precision, I'd say it would be better,
if datetime supported RFC1123 conversion directly, i.e. two
methods frommime() and tomime(), that parse and format
strings like "Sun, 06 Nov 1994 08:49:37 GMT" (what
rfc822.parsedate() and rfc822.formatdate() do)

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2003-01-11 17:32

Message:
Logged In: YES 
user_id=31435

Define what totimestamp() should do.  The range of 
timestamps supported by the *platform* C library (and so 
indirectly by Python's time module) isn't defined by any 
standard, and isn't easily discoverable either.  It may or 
may not work before 1970, may or may not after 2038.  
datetime covers days far outside that range.  Note that 
even a double doesn't have enough bits of precision to 
cover the full range of datetime values, either.

In contrast, ordinals are wholly under Python's control, so 
we can promise surprise-free conversion in both directions.  
All we can promise about timestamps is that if the platform 
supports a timestamp for a time in datetime's range, 
datetime can make sense of it.

----------------------------------------------------------------------

Comment By: Walter Dörwald (doerwalter)
Date: 2003-01-11 13:33

Message:
Logged In: YES 
user_id=89016

OK, I'll mark this a feature request.

datetime has fromordinal() and toordinal(), it has 
fromtimestamp(), so I'd say a totimestamp() method would be 
a logical addition.

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2003-01-11 06:10

Message:
Logged In: YES 
user_id=31435

You can get a timestamp like so:

>>> time.mktime(datetime.date(2002, 1, 1).timetuple())
1009861200.0
>>>

The dates for which this works depends on the platform 
mktime implementation, though.

BTW, this sounds a lot more like a new feature request 
than a bug!

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=665194&group_id=5470