Live data from Hacker News

Introducing Times (for Python)

nvie.com

11–20 of 20 posts

Re: Introducing Times (for Python)

#11
post #8
post #6

This solves one of my problems with datetime and timezones in Python. The other being that it is totally awkward and bafflingly hard to add and substract from datetime objects.

Why do you find it difficult? >>> datetime.datetime.now() - datetime.timedelta(days=1) datetime.datetime(2012, 2, 1, 23, 33, 30, 591993)

Yup, works fine if you're doing things only locally. However, if you're using timezone aware datetime objects you're out of luck, because then datetime.timedelta doesn't immediately apply.

Just creating timezone aware datetime objects is really too difficult with the standard library anyway. You need to install `pytz` or create your own `datetime.tzinfo` classes just to be able to represent timezones.

To top it off, if you have datetime objects with timezones and datetime objects without timezone information (*even created using utcfromtimezone()!), you can't compare them.

Re: Introducing Times (for Python)

#12

IMO dateutil provides this in a more transparent way by providing tzinfo subclasses: py> from dateutil.tz import gettz py> from datetime import datetime py> new_york = gettz('America/New York') py> los_angeles = gettz('America/Los Angeles') py> minsk = gettz('Europe/Minsk') py> fmt = "%Y-%m-%d %H:%M%z" py> datetime.now(new_york).strftime(fmt) '2012-02-03 01:51-0500' py> datetime.now(los_angeles).strftime(fmt) '2012-0…

dateutil is really great; j2labs is quick to point out `parse()` which I also think is worth mentioning on its own.

I think this project still fills a nice niche. datetime.now(los_angeles).astimezone(minsk).strftime(fmt) is long enough that you're going to have to find the StackOverflow thread in question to implement this, and maybe go to the stdlib. This library makes that a lot less painful.

If you go to the source, you'll see how tiny it is! It's nowhere near the complexity of dateutil. I'd love to see some of dateutil's darker corners wrapped into this library.

Re: Introducing Times (for Python)

#13
This is good, but for my app, we just decided to do all of the presentation on the client side using javascript. This prevents us from having to know or store the users tz at all. That way, whatever tz they are in is the correct one. =)

We thought this might be an issue for things like emailing purchase receipts to people since that is something that can't just be 'rendered' on the client, but we ended up just asking ourselves "What would amazon do?" and looked at an emailed Amazon receipt.

You will notice that there isn't a single time in the receipt other than when the email was sent. Looked at a receipt from Apple. Same thing. Problem solved by just removing the timestamp from the receipt.

Thought that was an interesting tidbit that I hadn't thought of before.

This is the JS library I used and am pretty happy with: http://momentjs.com/

Re: Introducing Times (for Python)

#14

IMO dateutil provides this in a more transparent way by providing tzinfo subclasses: py> from dateutil.tz import gettz py> from datetime import datetime py> new_york = gettz('America/New York') py> los_angeles = gettz('America/Los Angeles') py> minsk = gettz('Europe/Minsk') py> fmt = "%Y-%m-%d %H:%M%z" py> datetime.now(new_york).strftime(fmt) '2012-02-03 01:51-0500' py> datetime.now(los_angeles).strftime(fmt) '2012-0…

> For me, the most annoying thing about Python is that out of the box, AFAICT, you cannot get a TZ-aware datetime.

That's because the stdlib would have to get updated every 3-6 months, which it is not. Hence delegating to e.g. pytz for timezones provision.

> I don't know of any included solution for making TZ-aware datetime, short of writing your own subclasses for each timezone.

There isn't indeed, because there can't be an stdlib-provided way to do this which is actually correct.

Re: Introducing Times (for Python)

#15

This is good, but for my app, we just decided to do all of the presentation on the client side using javascript. This prevents us from having to know or store the users tz at all. That way, whatever tz they are in is the correct one. =) We thought this might be an issue for things like emailing purchase receipts to people since that is something that can't just be 'rendered' on the client, but we ended up just asking…

Interesting.

I recently was evaluating JS and Python for handling time in my app. The goal was not to localize to a specific timezone for the client, but rather to inform the client of where in the world a current time is.

I ended up picking the Python backend solution because it was easier to reason about with the more abstract `datetime` primitives.

Re: Introducing Times (for Python)

#16

IMO dateutil provides this in a more transparent way by providing tzinfo subclasses: py> from dateutil.tz import gettz py> from datetime import datetime py> new_york = gettz('America/New York') py> los_angeles = gettz('America/Los Angeles') py> minsk = gettz('Europe/Minsk') py> fmt = "%Y-%m-%d %H:%M%z" py> datetime.now(new_york).strftime(fmt) '2012-02-03 01:51-0500' py> datetime.now(los_angeles).strftime(fmt) '2012-0…

> For me, the most annoying thing about Python is that out of the box, AFAICT, you cannot get a TZ-aware datetime. That's because the stdlib would have to get updated every 3-6 months, which it is not. Hence delegating to e.g. pytz for timezones provision. > I don't know of any included solution for making TZ-aware datetime, short of writing your own subclasses for each timezone. There isn't indeed, because there can…

dateutil includes tzinfo implementations 'tzutc' (whose offset is always zero), 'tzoffset' (whose offset is passed as a parameter) and 'tzlocal' (whose offset is the value of time.timezone). All these could easily and safely be implemented in the stdlib without having to push out updates every time the Olson timezone database has a new release.

It also contains tzinfo implementations that read timezone definitions from a system-wide copy of the Olson database (for most POSIX OSs) or the registry (for Windows-based OSs). These could also be added to the standard library without requiring regular Python updates, because the OS's usual timezone update system will take care of things.

Perhaps the stdlib can't possibly solve timezone issues in all possible cases, but it could be a lot more useful than it currently is.

Re: Introducing Times (for Python)

#17
post #10
post #9

Earlier quoted context omitted.

Doesn't do months or years. Who knows how it handles dst transitions. I can't remember for sure but I don't think you can subtract two dates and get a delta?

Months and years are tricky because they can consist of a variable number of days. To me, it's a better way of handling things without being ambiguous. For example, is one month from now exactly 30 days from today, or is it the next month on the same month day? It's pretty simple to do either with the datetime library. When taking into account DST, you should first convert from local time to UTC (pytz does this) befo…

> Months and years are tricky because they can consist of a variable number of days.

Then again, days can consist of a variable number of seconds due to e.g. the leap second.

Re: Introducing Times (for Python)

#19
Great initiative. Dealing with timezone is a pain and I totally agree with you that storing universal time is the way to go. It's a little bit like the 'validating input problem': Strongly validating and converting input/output so that the entire application can be safe. I.e. it's not to the sqrt function to validate its parameters. (Although it'd throw an exception if it fails.)

Re: Introducing Times (for Python)

#20
post #10
post #9

Earlier quoted context omitted.

Doesn't do months or years. Who knows how it handles dst transitions. I can't remember for sure but I don't think you can subtract two dates and get a delta?

Months and years are tricky because they can consist of a variable number of days. To me, it's a better way of handling things without being ambiguous. For example, is one month from now exactly 30 days from today, or is it the next month on the same month day? It's pretty simple to do either with the datetime library. When taking into account DST, you should first convert from local time to UTC (pytz does this) befo…

yes, it's very damn tricky. That's why I want the lib to figure it out once and correctly.
Post reply on HN