How to Get System Timezone Setting and Pass It to Pytz.Timezone

How to get system timezone setting and pass it to pytz.timezone?

A very simple method to solve this question:

import time

def localTzname():
offsetHour = time.timezone / 3600
return 'Etc/GMT%+d' % offsetHour

Update: @MartijnPieters said 'This won't work with DST / summertime.' So how about this version?

import time

def localTzname():
if time.daylight:
offsetHour = time.altzone / 3600
else:
offsetHour = time.timezone / 3600
return 'Etc/GMT%+d' % offsetHour

Get system local timezone in python

I don't think this is possible using pytz or pandas, but you can always install python-dateutil or tzlocal:

from dateutil.tz import tzlocal
datetime.now(tzlocal())

or

from tzlocal import get_localzone
local_tz = get_localzone()

how to get tz_info object corresponding to current timezone?

>>> import datetime
>>> today = datetime.datetime.now()
>>> insummer = datetime.datetime(2009,8,15,10,0,0)
>>> from pytz import reference
>>> localtime = reference.LocalTimezone()
>>> localtime.tzname(today)
'PST'
>>> localtime.tzname(insummer)
'PDT'
>>>

Python: Figure out local timezone

Try dateutil, which has a tzlocal type that does what you need.

How to get the common name for a pytz timezone eg. EST/EDT for America/New_York

Given a pytz timezone for a particular user(calculated from his offset), i want to display the common name for that timezone. I'm assuming people are more accustomed to seeing EST or PST instead of spelled out like America/NewYork.

If you need this derived from a datetime object localized with pytz...

>>> import pytz as tz
>>> from datetime import datetime as dt
>>> CT = tz.timezone('America/Chicago')
>>>
>>> summer_day = dt(2010, 7, 4, 0, 1, 1)
>>> bar = CT.localize(summer_day, is_dst=None)
>>> bar.tzname()
'CDT'
>>>
>>> christmas = dt(2010, 12, 25, 0, 1, 1)
>>> foo = CT.localize(christmas, is_dst=None)
>>> foo.tzname()
'CST'
>>>

How do I get a value of datetime.today() in Python that is timezone aware?

In the standard library, there is no cross-platform way to create aware timezones without creating your own timezone class. (Edit: Python 3.9 introduces zoneinfo in the standard library which does provide this functionality.)

On Windows, there's win32timezone.utcnow(), but that's part of pywin32. I would rather suggest to use the pytz library, which has a constantly updated database of most timezones.

Working with local timezones can be very tricky (see "Further reading" links below), so you may rather want to use UTC throughout your application, especially for arithmetic operations like calculating the difference between two time points.

You can get the current date/time like so:

import pytz
from datetime import datetime
datetime.utcnow().replace(tzinfo=pytz.utc)

Mind that datetime.today() and datetime.now() return the local time, not the UTC time, so applying .replace(tzinfo=pytz.utc) to them would not be correct.

Another nice way to do it is:

datetime.now(pytz.utc)

which is a bit shorter and does the same.


Further reading/watching why to prefer UTC in many cases:

  • pytz documentation
  • What Every Developer Should Know About Time – development hints for many real-life use cases
  • The Problem with Time & Timezones - Computerphile – funny, eye-opening explanation about the complexity of working with timezones (video)

How to make a timezone aware datetime object

In general, to make a naive datetime timezone-aware, use the localize method:

import datetime
import pytz

unaware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0)
aware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0, pytz.UTC)

now_aware = pytz.utc.localize(unaware)
assert aware == now_aware

For the UTC timezone, it is not really necessary to use localize since there is no daylight savings time calculation to handle:

now_aware = unaware.replace(tzinfo=pytz.UTC)

works. (.replace returns a new datetime; it does not modify unaware.)

Python pytz timezone function returns a timezone that is off by 9 minutes

Unless your local timezone has a fixed UTC offset then it is pointless to talk about its specific value without providing a specific date/time.

If you provide the time e.g., the current time then you'll see that pytz produces the expected UTC offset:

>>> from datetime import datetime
>>> import pytz
>>> datetime.now(pytz.timezone('America/Chicago')).strftime('%Z%z')
'CST-0600'

See

  • Datetime Timezone conversion using pytz
  • pytz localize vs datetime replace

If you don't provide a specific date/time then pytz may return an arbitrary utc offset from the set of available utc offsets for the given timezone. The recent pytz versions return utc offsets that correspond to the earliest time (LMT as a rule) but you should not rely on it. You and your friend may use different pytz versions that may explain the difference in results.

Setting timezone in Python

>>> import os, time
>>> time.strftime('%X %x %Z')
'12:45:20 08/19/09 CDT'
>>> os.environ['TZ'] = 'Europe/London'
>>> time.tzset()
>>> time.strftime('%X %x %Z')
'18:45:39 08/19/09 BST'

To get the specific values you've listed:

>>> year = time.strftime('%Y')
>>> month = time.strftime('%m')
>>> day = time.strftime('%d')
>>> hour = time.strftime('%H')
>>> minute = time.strftime('%M')

See here for a complete list of directives. Keep in mind that the strftime() function will always return a string, not an integer or other type.



Related Topics



Leave a reply



Submit