-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
datetime-RFC2822 roundtripping #37748
Comments
It would be good to have a simply way to convert Of course the best solution (ignoring backwards |
Logged In: YES 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 BTW, this sounds a lot more like a new feature request |
Logged In: YES OK, I'll mark this a feature request. datetime has fromordinal() and toordinal(), it has |
Logged In: YES Define what totimestamp() should do. The range of In contrast, ordinals are wholly under Python's control, so |
Logged In: YES totimestamp() should be the inverse of fromtimestamp(), i.e. But as this may lose precision, I'd say it would be better, |
Logged In: YES time.strptime doesn't solve your problem? |
Logged In: YES time.strptime() is locale aware, but RFC1123 & RFC822 |
Moving to feature requests. |
Here is a patch that implements to formatting part. It adds a method mimeformat() to the datetime class. The datetime object must have a tzinfo for this to work. |
Patch includes tests, should be updated. |
-1 on adding more formatting/parsing methods to datetime. +1 on adding functions to email.utils that work with datetime objects. Adding bpo-5094 as a dependency because RFC 2822 requires timezone information for proper formatting. |
Here is a patch that adds datetime support to email.utils.formatdate. Ultimately the email package will give programs access to datetime+timezone representations of the dates in various headers, so this provides the output end of the round trip. Alexander, if you could review this that would be great. There may be bugs in the logic of formatdate, but my hope is that I haven't added any new ones. |
Rather than shoehorning datetime class support into existing functions, I think a separate set of functions should be written to convert between RFC 2822 timestamps and datetime instances. Currently, email.utils has three functions dealing with date-time specification in email messages: formatdate(timeval=None, localtime=False, usegmt=False), To work with datetime instances, we can just provide two functions. For lack of better names, I'll call them format_datetime and parse_datetime. Rather than using a localtime flag in the format function, I suggest to always interpret naive datetime instances (those with tzinfo = None) as time given in UTC with no information about the local time zone. Per RFC 2822, this should be formatted with "-0000" in the timezone field. The format_datetime() may take usegmt flag, but it may be better to handle usegmt=True case by a separate format_datetime_gmt() function. The parse_datetime() function should use a similar convention and produce aware datetime instances unless TZ field contains "-0000". In this case a naive datetime containing unchanged time data should be produced. The problem of guessing the local timezone offset or converting naive datetime instance presumed to be in local time to an aware instance does not belong to the email package. This functionality should be added to the datetime module. See bpo-9527. There is a remaining question to which I don't have an immediate answer: How should parse_datetime() handle valid RFC 2882 date-time specifications that cannot be represented as a valid datetime. For example, a spec with seconds=60 or timezone > 2400. |
Do you think we can get 9527 in? My patch was based on the non-existence of a LocalTimezone facility in the stdlib. Good point about the special interpretation of -0000. Given 9527 (and only given 9527) it would indeed make sense to restrict email to aware datetimes plus naive datetimes for -0000 timestamps. I'll have to keep a flag for the 60th second outside of the datetime instance (or pretend it doesn't exist :) |
On Thu, May 5, 2011 at 12:44 PM, R. David Murray <[email protected]> wrote:
I hope we can. Pure Python implementation can be improved by deducing ..
If you can find an e-mail message archived somewhere with 60 seconds |
Yes, since the package will save the original text anyway, I think just clamping to 59 is best. Hmm. Maybe instead I could put in an assert that says "please report this incident to bugs.python.org so we can argue that datetime should get support for leap seconds) :) |
OK, here is a new patch proposal. As suggested by Alexander, it adds two functions to email.utils: format_datetime and parsedate_to_datetime. Adding these does not require having localtime. In fact, with this patch you can get an aware datetime representing email's current best guess at localtime by doing: dt = parsedate_to_datetime(formatdate(localtime=True)) Reviews welcome, but this is simple enough I'll probably just commit it if no one objects. |
New changeset 5f7b03dcd523 by R David Murray in branch 'default': |
New changeset df12ce0c96eb by R David Murray in branch 'default': |
Most of the localtime() logic is now implemented (correctly) in datetime.astimezone(). Attached patch fixes email.utils.localtime() implementation. |
David, bpo-665194.diff patch is a bug fix for localtime(). If you decide to keep localtime(), there is not much of a rush because bug fixes can go in after beta. |
Well, I guess I'd like to keep it since it does satisfy a need in the email package: to manipulate dates in the local timezone when composing messages. It isn't a critical need, though; the critical need is just to be able to have a datetime that has the correct timezone as its tzinfo for 'now', and your astimezone change supplies that. On the third hand, 'util.localtime()' is a lot more intuitive then 'datetime.now().astimezone()', so I'd probably still have a util.localtime() helper even if I restricted it to only generating 'now'. So, on balance, since the method is in and tested with the extra functionality, and that functionality will probably prove useful to programs manipulating emails, I'm coming down in favor of keeping it. |
Alexander, this slipped off my radar. Some of the tests Brian added fail with the patch applied. I fixed most of them by adding back the support for converting an aware datetime to an aware localtime. The remaining two I believe are what you are fixing with this patch (in addition to converting to using astimezone for the current-localtime case). So there I fixed the tests. Hopefully I got this right, and hopefully you can find time to review it. I'll probably check it in before the RC regardless, since it looks like an improvement to me. |
I'll take a look tomorrow morning. (EDT:-) |
I noticed this part: + # We have an aware datetime. Use aware datetime arithmetic to find the Why don't you just return dt.astimezone() here? A round trip through a floating point timestamp always makes me nervous. I doubt loss of precision is a problem for the e-mail package, but who knows. |
Heh, I was just copying the previous code, and didn't think about being able to update it. Will make that change. |
And my tests and code were wrong, and I was wrong about what you were trying to fix. So since the other tests were passing before, presumably there is some test that could be added to exercise the bug you were fixing. Do you remember what that was? |
Yes, the issue was the one that was mentioned in an XXX comment: in many places UTC offset was different at different historical times but standard C library and Python's time module provides a single constant for it. There are plenty of examples that can be found in Olson's database (Europe/Kiev comes to mind), but it is not easy to devise a test that will work cross-platform. Maybe we should just restrict the test to Linux/BSD family of OSes? |
I think restricting the test is fine. If we find a platform-specific bug on another platform we can add a test specific to that platform when we fix the bug. Can you provide the test? I'm going to commit what I've got at this point to make sure I don't miss the RC. |
Please commit. I'll add the test. On Wed, Aug 22, 2012 at 9:11 PM, R. David Murray <[email protected]> wrote:
|
New changeset 71b9cca81598 by R David Murray in branch 'default': |
Leaving open until the test is committed. |
New changeset a2d83fba8fd8 by R David Murray in branch 'default': |
New changeset 604222c1f8a0 by Alexander Belopolsky in branch 'default': |
New changeset 4c134e6ba0df by Alexander Belopolsky in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: