pytz

Localizing Epoch Time with pytz in Python

眉间皱痕 提交于 2019-11-30 02:01:19
Im working on converting epoch timestamps to dates in different timezones with pytz. What I am trying to do is create a DateTime object that accepts an Olson database timezone and an epoch time and returns a localized datetime object. Eventually I need to answer questions like "What hour was it in New York at epoch time 1350663248?" Something is not working correctly here: import datetime, pytz, time class DateTime: def __init__(self, timezone, epoch): self.timezone = timezone self.epoch = epoch timezoneobject = pytz.timezone(timezone) datetimeobject = datetime.datetime.fromtimestamp( self

Python: How to convert a timezone aware timestamp to UTC without knowing if DST is in effect

本小妞迷上赌 提交于 2019-11-30 01:17:51
问题 I am trying to convert a naive timestamp that is always in Pacific time to UTC time. In the code below, I'm able to specify that this timestamp I have is in Pacific time, but it doesn't seem to know that it should be an offset of -7 hours from UTC because it's only 10/21 and DST has not yet ended. The script: import pytz import datetime naive_date = datetime.datetime.strptime("2013-10-21 08:44:08", "%Y-%m-%d %H:%M:%S") localtz = pytz.timezone('America/Los_Angeles') date_aware_la = naive_date

How can I remove a pytz timezone from a datetime object?

坚强是说给别人听的谎言 提交于 2019-11-29 21:09:46
Is there a simple way to remove the timezone from a pytz datetime object? e.g. reconstructing dt from dt_tz in this example: >>> import datetime >>> import pytz >>> dt = datetime.datetime.now() >>> dt datetime.datetime(2012, 6, 8, 9, 27, 32, 601000) >>> dt_tz = pytz.utc.localize(dt) >>> dt_tz datetime.datetime(2012, 6, 8, 9, 27, 32, 601000, tzinfo=<UTC>) user1094786 To remove a timezone (tzinfo) from a datetime object: # dt_tz is a datetime.datetime object dt = dt_tz.replace(tzinfo=None) If you are using a library like arrow , then you can remove timezone by simply converting an arrow object

cx_freeze: How do I add package files into library.zip?

戏子无情 提交于 2019-11-29 18:19:24
问题 I've noticed that pytz misses zoneinfo folder when I try to roll a zip for Windows. Right now I have a workaround that I use after python setup.py build , namely 7z a -xr!*.py* build\exe.win32-2.7\library.zip C:\Python27\Lib\site-packages\pytz Is there a proper way to achieve that from setup.py or something? 回答1: You could fix this, adding the following method: def include_files(): path_base = "C:\\Python27\\Lib\\site-packages\\pytz\\zoneinfo\\" skip_count = len(path_base) zip_includes = [

How to get the first datetime of a day?

烂漫一生 提交于 2019-11-29 11:37:33
Using pytz and Python 3.4, how to get the first datetime of a given day (lets say, 2014-10-19), in a given timezone (lets say 'America/Sao_Paulo' )? Use localize() method to attach the timezone: from datetime import datetime import pytz # $ pip install pytz tz = pytz.timezone('America/Sao_Paulo') naive = datetime(2014, 10, 19) aware = tz.localize(naive, is_dst=None) If you run the code; it generates NonExistentTimeError . How to deal with this error depends on the application e.g., to get some valid local time near the midnight: aware = tz.normalize(tz.localize(naive, is_dst=False)) Or you

how to get tz_info object corresponding to current timezone?

耗尽温柔 提交于 2019-11-29 02:57:27
Is there a cross-platform function in python (or pytz ) that returns a tzinfo object corresponding to the timezone currently set on the computer? environment variables cannot be counted on as they are not cross-platform >>> 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' >>> tzlocal module that returns pytz timezones works on *nix and win32: from datetime import datetime from tzlocal import get

Module pytz was already imported

笑着哭i 提交于 2019-11-29 02:36:25
I keep getting the following error while running Python code: C:\Python26\lib\site-packages\pytz\__init__.py:32: UserWarning: Module pytz was already imported from C:\Python26\lib\site-packages\pytz\__init__.pyc, but c:\python26\lib\site-packages\pytz-2011h-py2.6.egg is being added to sys.path from pkg_resources import resource_stream What does it mean and how can I solve it? You've got the package installed in pytz and also as a .egg . Remove the .egg and you won't get the warning. However, note that it's referred to as a "spurious warning" -- this isn't actually a problem, though it could

Localizing Epoch Time with pytz in Python

大城市里の小女人 提交于 2019-11-28 23:36:38
问题 Im working on converting epoch timestamps to dates in different timezones with pytz. What I am trying to do is create a DateTime object that accepts an Olson database timezone and an epoch time and returns a localized datetime object. Eventually I need to answer questions like "What hour was it in New York at epoch time 1350663248?" Something is not working correctly here: import datetime, pytz, time class DateTime: def __init__(self, timezone, epoch): self.timezone = timezone self.epoch =

pytz: Why is normalize needed when converting between timezones?

不羁岁月 提交于 2019-11-28 23:03:18
I'm reading the not so complete pytz documentation and I'm stuck on understand one part of it. Converting between timezones also needs special attention. This also needs to use the normalize method to ensure the conversion is correct. >>> utc_dt = utc.localize(datetime.utcfromtimestamp(1143408899)) >>> utc_dt.strftime(fmt) '2006-03-26 21:34:59 UTC+0000' >>> au_tz = timezone('Australia/Sydney') >>> au_dt = au_tz.normalize(utc_dt.astimezone(au_tz)) >>> au_dt.strftime(fmt) '2006-03-27 08:34:59 EST+1100' >>> utc_dt2 = utc.normalize(au_dt.astimezone(utc)) >>> utc_dt2.strftime(fmt) '2006-03-26 21:34

datetime and timezone conversion with pytz - mind blowing behaviour

情到浓时终转凉″ 提交于 2019-11-28 22:31:37
问题 I'm trying to convert timezone aware datetime object to UTC and then back to it's original timezone. I have a following snippet t = datetime( 2013, 11, 22, hour=11, minute=0, tzinfo=pytz.timezone('Europe/Warsaw') ) now in ipython: In [18]: t Out[18]: datetime.datetime( 2013, 11, 22, 11, 0, tzinfo=<DstTzInfo 'Europe/Warsaw' WMT+1:24:00 STD> ) and now let's try to do conversion to UTC and back. I would expect to have the same representation as: In [19]: t.astimezone(pytz.utc).astimezone(pytz