pytz US/Eastern converting timestamps back and forth - python

From time and date website:
Generalized Time Zone in New York
ET Eastern Time UTC -5:00 / -4:00 Wed, 11:42:33
Time Zone Currently Being Used in New York
UTC -5 EST Eastern Standard Time New York Wed, 11:42:33
Time Zone Not Currently Being Observed in New York
UTC -4 EDT Eastern Daylight Time 10 Mar 2019
So basically New York time is EST or EDT depending on which period of the year we are currently on due to the DayLight timesaving.
When using US/Eastern in pytz, does it refer to ET, EST or EDT?. Does it take into consideration the EST/EDT time change?
I am using this code to get timestamps for the beginning and end of the day of a given date in Eastern Time:
import datetime
import pytz
session = datetime.datetime.strptime('26/02/2019', '%d/%m/%Y')
exchange_tz = pytz.timezone('US/Eastern')
session_localized = exchange_tz.localize(session)
_period1 = int((session_localized - datetime.datetime(1970, 1, 1, tzinfo=exchange_tz)).total_seconds())
_period2 = int((session_localized - datetime.datetime(1970, 1, 1, tzinfo=exchange_tz)).total_seconds()) + 24*60*60
>>> _period1
1551139200
>>> _period2
1551225600
I am not 100% sure that those timestamps have been properly generated. Because when I try to convert them back to localized datetimes I get 1 hour of difference:
_exchange_tz = pytz.timezone('US/Eastern')
_exchange_ts = _exchange_tz.localize( datetime.datetime.fromtimestamp(1551139200) )
>>> str(_exchange_ts)
'2019-02-26 01:00:00-05:00'
Which as far as I understand, it shall be 2019-02-26 00:00:00. What am I doing wrong?
I have also read in pytz documentation that normalize() function shall be used when doing arithmetics covering, but I am not sure if this apply to this case or not.

When using US/Eastern in pytz, does it refer to ET, EST or EDT?. Does it take into consideration the EST/EDT time change?
These are IANA (aka TZDB, Olson, etc.) time zone identifiers. US/Eastern is a link/alias to America/New_York. Both correctly model the history of the US Eastern time zone, including EST or EDT depending on when they are applicable.
Regarding your example, the timestamp 1551139200 equates to 2019-02-26T00:00:00Z (UTC). In US Eastern Time, that's 2019-02-25T19:00:00-05:00. That should be the result you get. You would first need to localize the input value to UTC (or in the case of UTC, you can assign tzinfo=pytz.utc safely). Then use the astimezone function to convert to US/Eastern.

Related

How to convert local time to UTC, considering daylight saving time?

I have datetime values in local time zone, and I need to convert them into UTC. How can I do this conversion for historical records, considering daylight saving time in the past?
Local UTC
2018/07/20 09:00 ???
2018/12/31 11:00 ???
2019/01/17 13:00 ???
2020/08/15 18:00 ???
This is what I have so far:
import pytz
without_timezone = datetime(2018, 7, 20, 9, 0, 0, 0)
timezone = pytz.timezone("Europe/Vienna")
with_timezone = timezone.localize(without_timezone)
with_timezone
So, I assigned Europe/Vienna to all records (I assume that this considers daylight saving time, right?)
Now I need to convert it into UTC...
Assuming Local contains date/time as observed locally, i.e. including DST active/inactive, you would convert to datetime object, set time zone, and convert to UTC.
Ex:
from datetime import datetime, timezone
from zoneinfo import ZoneInfo # Python 3.9
Local = ["2018/07/20 09:00", "2018/12/31 11:00", "2019/01/17 13:00", "2020/08/15 18:00"]
# to datetime object and set time zone
LocalZone = ZoneInfo("Europe/Vienna")
Local = [datetime.strptime(s, "%Y/%m/%d %H:%M").replace(tzinfo=LocalZone) for s in Local]
for dt in Local:
print(dt.isoformat(" "))
# 2018-07-20 09:00:00+02:00
# 2018-12-31 11:00:00+01:00
# 2019-01-17 13:00:00+01:00
# 2020-08-15 18:00:00+02:00
# to UTC
UTC = [dt.astimezone(timezone.utc) for dt in Local]
for dt in UTC:
print(dt.isoformat(" "))
# 2018-07-20 07:00:00+00:00
# 2018-12-31 10:00:00+00:00
# 2019-01-17 12:00:00+00:00
# 2020-08-15 16:00:00+00:00
Note: with Python 3.9, you don't need third party libraries for time zone handling in Python anymore. There is a deprecation shim for pytz.
First, check your conversion value, here, in PDT, Universal is 5 hours behind, so convert accordingly, as for checking if its daylight savings time, write an if statement checking the date, and month and convert accordingly. Does this help?

'Europe/Madrid' timezone doesn't match 'Etc/GMT+1'

I'm trying to convert a UTC timestamp to one in the Spanish timezone.
>>> import datetime as dt
>>> import pytz
>>> today = dt.datetime.utcfromtimestamp(1573516800)
datetime.datetime(2019, 11, 12, 0, 0)
>>> today.replace(tzinfo=pytz.timezone('Europe/Madrid')).timestamp()
1573517700.0
>>> today.replace(tzinfo=pytz.timezone('Etc/GMT+1')).timestamp()
1573520400.0
I'm surprised that I get different results for Europe/Madrid and Etc/GMT+1. Why is this? Should Europe/Madrid be used differently, or it is possibly a bug?
A few things:
Europe/Madrid is UTC+1 during standard time, and UTC+2 during summer time (aka daylight saving time).
Etc/GMT+1 is UTC-1 for the entire year. Note the sign is opposite what you might expect. See the explanation in the tzdata sources, and on Wikipedia.
Since Madrid is on UTC+1 on the date you gave, you would get the same result for that date if you used Etc/GMT-1. However, I don't recommend that, as you would then later get the wrong result for a date during summer time.
The Etc/GMT±X zones are intended to be used primarily for non-localizable scenarios such as tracking time onboard ships at sea - not for populated locations on land.
As Mason's answer showed, you should be using the localize function rather than replace to assign a time zone. This is covered in the pytz documentation.
UTC Timestamp: The number of seconds since January 1st, 1970 at UTC.
Python datetime: A nice way of seeing this time that is user friendly
The UTC timestamp is not effected by timezones, but the datetime is.
This code takes the given timestamp and converts it to a UTC datetime and a Europe/Madrid timezone.
import datetime as dt
import pytz
# define the old and new timezones
old_timezone = pytz.timezone("UTC")
new_timezone = pytz.timezone("Europe/Madrid")
# get an 'offset-aware' datetime
today = dt.datetime.utcfromtimestamp(1573516800)
my_datetime = old_timezone.localize(today)
# returns datetime in the new timezone
my_datetime_in_new_timezone = my_datetime.astimezone(new_timezone)
print("Old:", str(my_datetime), "\nNew:", str(my_datetime_in_new_timezone), "\nDifference:",
str(my_datetime - my_datetime_in_new_timezone))
Output:
Old: 2019-11-12 00:00:00+00:00
New: 2019-11-12 01:00:00+01:00
Difference: 0:00:00
Code adapted from:
Python: How do you convert datetime/timestamp from one timezone to another timezone?

Is there a "local" timestamp?

I can convert from an UTC timestamp e.g. 1417392000 to a local datetime object including daylight saving time, but when I try and convert the local datetime object to a "local timestamp" then I get the same UTC timestamp as before.
Am I thinking in circles and they are the same? I was supposed to save "local timestamp" from incoming UTC timestamp.
This is my code
print("UTC timestamp %d" % hour[0])
day = self.get_day(hour)
month = self.get_month(hour)
year = self.get_year(hour)
tz = pytz.timezone('Europe/Stockholm')
utc_dt = datetime.utcfromtimestamp(int(hour[0])).replace(tzinfo=pytz.utc)
print("UTC datetime %s" % utc_dt)
dt = tz.normalize(utc_dt.astimezone(tz))
print("STO datetime %s" % dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
print("STO ts %d" % int(time.mktime(dt.timetuple())))
print("STO timestamp %d" % utc_dt.astimezone(tz).timestamp())
day = int(dt.strftime('%d'))
month = int(dt.strftime('%m'))
year = int(dt.strftime('%Y'))
Output
UTC timestamp 1417395600
UTC datetime 2014-12-01 01:00:00+00:00
STO datetime 2014-12-01 02:00:00 CET+0100
STO ts 1417395600
STO timestamp 1417395600
All "timestamps" (i.e. integer representations of the time) are the same. Is it possible to make a "local timestamp" ? The data type is supposed to be a timestamp that is a number and in local time.
As per Wikipedia
Unix time (also known as POSIX time or Epoch time) is a system for
describing instants in time, defined as the number of seconds that
have elapsed since 00:00:00 Coordinated Universal Time (UTC),
Thursday, 1 January 1970
So regardless of what timezone you're on, the epoch will always be calculated in the UTC timezone.
For display purposes, you can convert the timestamp to a local time, but as otherwise the internal representation of the epoch will always be in the UTC timezone
Formal timestamps
The Unix timestamp typically refers to the number of seconds since the epoch in UTC. This value is invariant to timezone. It allows global event ordering but loses timezone information.
Preserving timezone
To preserve timezone information, a standardized format is RFC3339, Date and Time on the Internet: Timestamps. This is just a standardized formatting that encodes date+time+timezone. Some examples:
1985-04-12T23:20:50.52Z
1996-12-19T16:39:57-08:00
1990-12-31T23:59:60Z
1990-12-31T15:59:60-08:00
Normalizing for timezone without preservation of timezone
However, it may depend on your requirements. I once wanted to record some events relative to local-time-of-day and did not mind losing timezone information. I normalized the timestamp with respect to 1970-01-01T00:00:00 in the local timezone. I am a little sheepish about this now as I think it may too easily cause confusion.
import time
# Number of seconds since 1970-01-01T00:00+LTZ (current timezone).
# unix timestamp - timezone offset in seconds
timestamp_localized = time.time() - time.mktime(time.gmtime(0))
However this syntax can be simplified, perhaps at the loss of clarity, by noticing that Python has some localtime and UTC specific functions.
import time
import calendar
# Number of seconds since 1970-01-01T00:00+LTZ (current timezone).
# Interpret local date and time as if it were UTC
timestamp_localized = calendar.timegm(time.localtime())
The difference between these two is that calendar conversion truncates to the second while the difference calculation includes a fractional second.

Why doesn't pytz localize() produce a datetime object with tzinfo matching the tz object that localized it?

Is there anyone who can help me understand what's going on here?
import pytz
from datetime import datetime
tz = pytz.timezone('Europe/Berlin')
print repr(tz)
# <DstTzInfo 'Europe/Berlin' LMT+0:53:00 STD>
dt = datetime(2011, 1, 3, 18, 40)
result = tz.localize(dt)
print repr(result.tzinfo)
# <DstTzInfo 'Europe/Berlin' CET+1:00:00 STD>
assert result.tzinfo == tz, "Why aren't these the same timezone?"
My understanding was that the localize() method on a pytz timezone object would take a naive datetime object, and add a tzinfo property that matches the timezone object performing the localization. That does not appear to be happening in this case.
Clearly, there's something I'm misunderstanding about timezones, or about the way that pytz handles timezones. Can anyone explain?
They are the same time zone - "Europe/Berlin".
When you are printing them, the output includes the abbreviation and offset that applies at that particular point in time.
If you examine the tz data sources, you'll see:
# Zone NAME GMTOFF RULES FORMAT [UNTIL]
Zone Europe/Berlin 0:53:28 - LMT 1893 Apr
1:00 C-Eur CE%sT 1945 May 24 2:00
1:00 SovietZone CE%sT 1946
1:00 Germany CE%sT 1980
1:00 EU CE%sT
So it would appear that when the time zone has not localized a datetime, then it just uses the first entry.
It would also appear that pytz doesn't retain the extra 28 seconds from the original local mean time deviation - but that doesn't matter unless you are working with dates in Berlin before April 1893.

Convert a UTC Timestamp to a datetime object, taking daylight savings time into account

Using:
time.mktime(datetime.datetime.now(pytz.timezone("UTC")).timetuple())
The timestamp on April 19. 2012 shortly after 10:00 in New York was 1334840917. Given this timestamp, how can I convert it back to a datetime object that takes daylight savings time into account?
If I do:
>>>eastern = pytz.timezone("Us/Eastern")
>>>dt = datetime.datetime.fromtimestamp(1334840917,eastern)
>>>dt.hour
The output is: 9
dt = datetime.datetime.fromtimestamp(1334840917 + time.daylight*3600)

Categories