Converting to a non-daylight savings timezone - python

In python, I am trying to convert a bunch of UTC datetimes into a localized timezone but STANDARD time year round. For example, for pacific time, I need PST year round, not PDT and PST switching from summer to winter. Or to rephrase my question, is there a way to specify 'Standard' time for a timezone?

You can specify the UTC offset for a specific time zone that does not change with daylight savings. Following example converts UTC to Pacific Standard Time (PST).
from datetime import datetime, timedelta, timezone
utc = datetime(2019, 8, 7, 10, 30, tzinfo=timezone.utc)
pst = utc.astimezone(timezone(-timedelta(hours=8)))
print(utc)
# 2019-08-07 10:30:00+00:00
print(pst)
# 2019-08-07 02:30:00-08:00
To obtain the number of hours of offset for a named time zone:
import pytz
def get_standard_time_offset(tz_name):
t = pytz.timezone(tz_name)
# DATE HERE IS AN ARBITRARY WINTER DATE THAT WOULDN'T WORK IN THE SOUTHERN HEMISPHERE
offset = t.utcoffset(datetime(2010,1,1))
hrsoffset = offset.total_seconds()/3600
return hrsoffset

from datetime import datetime, timezone
utc_dt = "enter here UTC time "
utc_dt.replace(tzinfo=timezone.utc).astimezone(tz=None)

Related

Convert Local time to UTC before 1970

I'm trying to get UTC times for dates before 1970 but tz and tzinfo in Python only contain timezone databases for dates post-1970.
If I'm trying to find the UTC time for a date before 1970, say, Bill Clinton's birthday:
August 16, 1946 8:51AM
Hope, AK
datetime and tzinfo will return 16 Aug 1946 13:51 UTC
But it turns out that that is incorrect because it uses Chicago's timezone (which had its own timezone)
How can I find the proper time conversion and trust it for dates before 1970 in Python?
Time zone rules as we know them today came into play around 1900, see e.g. P. Eggert's tz repo. So Bill Clinton's birthday should not suffer from missing tz rules.
Ex, using pytz (deprecated):
from datetime import datetime
import pytz
bday = "August 16, 1946 8:51AM Hope, AK"
# Hope, AK is not a time zone, so strip it
bday = bday.rstrip(" Hope, AK")
# Hope in Arkansas uses US Central time, so we can use
tz = pytz.timezone("America/Chicago")
bday_dt = tz.localize(datetime.strptime(bday, "%B %d, %Y %I:%M%p"))
print(bday_dt)
# 1946-08-16 08:51:00-05:00
print(bday_dt.astimezone(pytz.UTC))
# 1946-08-16 13:51:00+00:00
...or using Python 3.9's zoneinfo
from zoneinfo import ZoneInfo
bday_dt = datetime.strptime(bday, "%B %d, %Y %I:%M%p").replace(tzinfo=ZoneInfo("America/Chicago"))
print(bday_dt)
# 1946-08-16 08:51:00-05:00
print(bday_dt.astimezone(ZoneInfo("UTC")))
# 1946-08-16 13:51:00+00:00

django, python why timestamp changes after localize

CODE:
import pytz
from django.utils import timezone
KST = pytz.timezone('Asia/Seoul')
UTC = pytz.timezone('UTC')
default_time = timezone.datetime(2021, 11, 29, 16, 44)
current_manual_kst = KST.localize(default_time)
current_manual_utc = default_time
print(current_manual_kst.timestamp())
print(current_manual_utc.timestamp())
RESULT:
>>> 1638171840.0
>>> 1638204240.0
So, I can see that results are different.
I thought timestamps should be the same but results are not.
Why this happened?
And How to get the same timestamps (by default: UTC) from KST.localized datetime?
A timestamp is expressed in UNIX time, which is the number of seconds since midnight January 1st 1970 UTC. In order to convert a datetime to such a UNIX timestamp, that datetime needs to be interpreted as some timezone. Because you can't express it relative to 1970 UTC without defining what timezone it's in. So if you have a naïve datetime object (without timezone) and take its timestamp(), it is interpreted as being in your local timezone and is converted to UTC from there.
And 16:44 in Seoul is apparently a different time than 16:44 in your computer's "local" timezone.

How to convert UTC to EST with Python and take care of daylight saving automatically?

If I have a bunch of data with date & time in UTC format, how can I convert them to EST.
It can determine when they will be -4(in summer) and -5(in winter) automatically every year?
Thanks
You'll need to use the pytz module (available from PyPI):
import pytz
from datetime import datetime
est = pytz.timezone('US/Eastern')
utc = pytz.utc
fmt = '%Y-%m-%d %H:%M:%S %Z%z'
winter = datetime(2016, 1, 24, 18, 0, 0, tzinfo=utc)
summer = datetime(2016, 7, 24, 18, 0, 0, tzinfo=utc)
print(winter.strftime(fmt))
print(summer.strftime(fmt))
print(winter.astimezone(est).strftime(fmt))
print(summer.astimezone(est).strftime(fmt))
which will print:
2016-01-24 18:00:00 UTC+0000
2016-07-24 18:00:00 UTC+0000
2016-01-24 13:00:00 EST-0500
2016-07-24 14:00:00 EDT-0400
The reason why you'll need to use 'US/Eastern' and not 'EST' is exemplified in the last two lines of output.
If you have a pandas series with object datatype, you can first convert it into a DateTime series using pd.to_datetime()
df[col] = pd.to_datetime(your_series, format = '%Y-%m-%d %H:%M:%S', errors ='coerce')
Check if it is timezone aware or not by using series.dt.tz
df[col].dt.tz
If it's not timezone aware, we should make it timezone aware by using series.dt.tz_localize(). Also, do read about the ambiguous and nonexistent parameters of this function
df[col] = your_series[col].dt.tz_localize('UTC')
Now convert this series into the required timezone by series.dt.tz_convert()
df[col] = your_series[col].dt.tz_convert('US/Eastern')
The above method will take care of daylight savings time. If you want to check more timezones you can pip install pytz and
import pytz
pytz.common_timezones
In case you just want the normalized hour offset for your existing timedelta shifting:
from datetime import datetime
import pytz
def curr_est_offset():
tz_est = pytz.timezone('US/Eastern')
offset = tz_est.utcoffset(datetime.utcnow())
offset_seconds = (offset.days * 86400) + offset.seconds
offset_hours = offset_seconds // 3600
return offset_hours # -4 or -5
As mentioned above, you can use pandas.DataFrame.tz_convert() like this:
import pandas as pd
from datetime import datetime
df = pd.read_csv("your_data_file_path.csv", index_col=False, engine='python')
df['Date'] = pd.to_datetime(df['Date'])
df['Date'] = df['Date'].dt.tz_localize('US/Eastern').dt.tz_convert('UTC')
df['Date'] = df['Date'].apply(lambda x: datetime.replace(x, tzinfo=None))
What the last row does is removing the timezone info from the datetime object, so you can operate with the date and time only (don't worry, that doesn't change the timezone again, it just strips it from the timestamp string).
This is thebjorn's answer converted from Python 2 to Python 3 with a few additional comments. Thanks to thebjorn.
For convention, I use these terms:
EST: Eastern Standard Time (winter)
EDT: Eastern Daylight Time
(summer)
EPT: Eastern Prevailing Time (standard or daylight saving
time as appropriate)
Code:
# Convert EPT / UTC
import pytz
import datetime from datetime as dtdt
# Timezones
ept = pytz.timezone('US/Eastern')
utc = pytz.utc
# str format
fmt = '%Y-%m-%d %H:%M:%S %Z%z'
print("\nEPT/UTC examples:")
print("\nWinter (EST) example:")
# Create a UTC time in the winter
winter_utc = dtdt(2016, 1, 24, 18, 0, 0, tzinfo=utc)
print(" UTC: ", winter_utc.strftime(fmt))
# Convert from UTC to eastern prevailing time. Since, the timestamp is in the
# winter, prevailing time is standard time.
winter_ept = winter_utc.astimezone(ept)
print(" EPT: ", winter_ept.strftime(fmt))
# Let's convert back to UTC to show we get back to the original value.
winter_utc2 = winter_ept.astimezone(utc)
print(" UTC: ", winter_utc2.strftime(fmt))
# Let's do that again for a summer datetime.
print("\nSummer (EDT) example:")
summer_utc = dtdt(2016, 7, 24, 18, 0, 0, tzinfo=utc)
print(" UTC: ", summer_utc.strftime(fmt))
# Convert from UTC to eastern prevailing time. Since, the timestamp is in the
# winter, prevailing time is daylight saving time.
summer_ept = summer_utc.astimezone(ept)
print(" EPT: ", summer_ept.strftime(fmt))
# Let's convert back to UTC to show we get back to the original value.
summer_utc2 = summer_ept.astimezone(utc)
print(" UTC: ", summer_utc2.strftime(fmt))
Console:
EPT/UTC examples:
Winter (EST) example:
UTC: 2016-01-24 18:00:00 UTC+0000
EPT: 2016-01-24 13:00:00 EST-0500
UTC: 2016-01-24 18:00:00 UTC+0000
Summer (EDT) example:
UTC: 2016-07-24 18:00:00 UTC+0000
EPT: 2016-07-24 14:00:00 EDT-0400
UTC: 2016-07-24 18:00:00 UTC+0000

Python: converting date string to UTC

I am parsing a 3rd party website HTML with dates and times which are always in UK time format, however they don't have any timezone info in the source. Converting the string to an object is easy enough using datetime.strptime(), but how do I add timezone info?
Ultimately, I need to convert these strings to a datetime object in UTC format. The code will always run on a PC which is timezone aware, i.e. datetime.now() will return UK time.
temp = '07/12/2017 13:30'
dt = datetime.strptime(temp, '%d/%m/%Y %H:%M')
Is there a nicer way to do this?
offset = datetime.now() - datetime.utcnow()
dt -= offset
Use pytz
import datetime
import pytz
temp = '07/12/2017 13:30'
dt = datetime.strptime(temp, '%d/%m/%Y %H:%M')
timezone = pytz.timezone("Etc/Greenwich")
d_aware = timezone.localize(dt)
d_aware.tzinfo
> <DstTzInfo 'Etc/Greenwich' PST-1 day, 16:00:00 STD>
d_aware
datetime.datetime(2017, 12, 7, 13, 30, tzinfo=<StaticTzInfo 'Etc/Greenwich'>)
There are some good libraries that make working with dates so much easier. I like dateparser, parsedatetime, and arrow;
import dateparser as dp
dt = dp.parse('07-12-2017 13:30 PST')
print (dt)
dt = dp.parse("Yesterday at 3:00am EST")
print(dt)
2017-07-12 13:30:00-08:00
2017-12-06 17:07:07.557109-05:00

Python datetime and utc offset conversion ignoring timezone/daylight savings

I have two operations I want to perform, one the inverse of the other.
I have a UNIX timestamp at UTC, say for instance, 1425508527. From this I want to get the year, month, day etc. given a UTC offset. EG. what is the year/month/day/time in (UTC -6 hours)? The answer is March 4, 2015 at 16:35:27. Without providing an offset (or offset zero) the answer should be March 4, 2015 at 22:35:27.
Now I have the date at some location, along with the UTC offset. For instance March 4, 2015 at 16:35:27 and the offset (UTC -6 hours). The UNIX UTC timestamp I should get should be 1425508527.
I am able to almost do 2. (using python datetime library) like this:
import datetime.datetime as datetime
import time
import dateutil.tz as tz
utc_offset = 6
time.mktime(datetime(2015,3,4,16,35,27,
tzinfo=tz.tzoffset(None, utc_offset*60*60)).utctimetuple())
# => 1425486927
The problem with the above is that utc_offset has to be given the wrong sign. According to this map, utc_offset should be set to -6. Number 1. I've had no luck with. I don't need/want to deal with timezone information like daylight savings time. How do I implement this in Python?
If your system uses Unix time, which
does not count leap seconds, then the conversion can be done as follows:
Part 1: timestamp and offset to local date
import datetime as DT
import calendar
timestamp = 1425508527
offset = -6
date = DT.datetime(1970,1,1) + DT.timedelta(seconds=timestamp)
print(date)
# 2015-03-04 22:35:27
localdate = date + DT.timedelta(hours=offset)
print(localdate)
# 2015-03-04 16:35:27
Part 2: local date and offset to timestamp
utcdate = localdate - DT.timedelta(hours=offset)
assert date == utcdate
timetuple = utcdate.utctimetuple()
timestamp2 = calendar.timegm(timetuple)
print(timestamp2)
# 1425508527
assert timestamp == timestamp2

Categories

Resources