views:

202

answers:

3

I need to know the current time at CDT when my Python script is run. However this script will be run in multiple different timezones so a simple offset won't work. I only need a solution for Linux, but a cross platform solution would be ideal.

+2  A: 

You can use time.gmtime() to get time GMT (UTC) from any machine no matter the timezone, then you can apply your offset.

Max Shawabkeh
The problem is knowing what the offset is, so that doesn't help.
Lennart Regebro
Lennart, no, he's asking how to get time at a previously known timezone from any other, for which `gmtime()` is the perfect solution..
Max Shawabkeh
I know. The problem still is knowing what the offsets between the two timezones, CDT and the other one, are.
Lennart Regebro
If you know the time at GMT (offset 0), you don't care what zone you are in or what its offset is - the offset between CDT and GMT is constant.
Max Shawabkeh
Most likely he want to care about Daylight saving. Knowing the offset to CDT when CDT is not in effect would be pretty daft.
Lennart Regebro
yeah you're right. Getting the current UTC time and then applying the offset to my desired timezone is all I need so my current timezone is not important. Thanks!
Plumo
@Richard: So you want conversion to CDT, which is daylight saving time, even when it is *not* in effect!? Why?
Lennart Regebro
+6  A: 

pytz or dateutil.tz is the trick here. Basically it's something like this:

>>> from pytz import timezone
>>> mytz = timezone('Europe/Paris')
>>> yourtz = timezone('US/Eastern')

>>> from datetime import datetime
>>> now = datetime.now(mytz)
>>> alsonow = now.astimezone(yourtz)

The difficulty actually lies in figuring out which timezone you are in. dateutil.tz is better at that.

>>> from dateutil.tz import tzlocal, gettz
>>> mytz = tzlocal()
>>> yourtz = gettz('US/Eastern')

If you want all the nitty gritty details of why timezones are evil, they are here:

http://regebro.wordpress.com/2007/12/18/python-and-time-zones-fighting-the-beast/

http://regebro.wordpress.com/2008/05/10/python-and-time-zones-part-2-the-beast-returns/

http://regebro.wordpress.com/2008/05/13/thanks-for-the-testing-help-conclusions/

Lennart Regebro
+1 for `pytz` suggestion.
jathanism
+1  A: 

A simple offset will work, you just need to offset from UTC.

Using datetime you can get the current utc (gmt) time and use datetime objects:

datetime.datetime.utcnow() - Provides time at UTC

datetime.datetime.now() - Provides time at local machine

To get the CT time from any system you need to know the CT time offset from UTC. Then to account for daylight savings time code a function to get the current offset.

>>> import datetime
>>> utc = datetime.datetime.utcnow()
>>> current_ct_offset = get_current_ct_offset()
>>> ct_datetime = utc + datetime.timedelta(hours=current_ct_offset)

I could be overlooking something here, but if your only concerned about one timezone and your not doing tz name handling, it's pretty straight forward.

monkut
Well, he is concerned about two timezones. The local, and CDT. And mostly likely, with CDT he doesn't actually mean Central Daylight saving time without caring about shifts in daylight saving, because that would be weird.
Lennart Regebro
Perhaps the question needs more clarification. My understanding is that he wants "to know the current time at <a single specific timezone>". In this case you only need to know UTC time and the <specific timezone> utc-offset. In this case you do not need to be concerned with the local time.
monkut
Yes. But the only way to reliably know the offset, if he cares about daylight saving (and it would be stupid not to) is by using a timezone database. Hence, my answer is still the correct one.
Lennart Regebro