Convert timezone naive datetime string to UTC datetime (but not using pytz) - python

We have the need to convert an incoming datetime string into a UTC datetime string. The incoming string is in this format:
'01/28/2022 12:00 AM'
with intended output as:
'2022-01-28T08:00:00Z'
The string has no declared timezone but is supposed to be PST. We'd like to code this in a way that allows for multiple timezones and date string formats. We can get this working using the following code:
from datetime import datetime
import pytz
UTC_TIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
PROVIDER_DATETIME_FORMAT = '%m/%d/%Y %I:%M %p'
PROVIDER_TIMEZONE = 'America/Los_Angeles'
def dt_string_to_utc_converter(datetime_str)
utc_timezone = pytz.utc
pst_timezone = pytz.timezone(PROVIDER_TIMEZONE)
naive_date = datetime.strptime(datetime_str, PROVIDER_DATETIME_FORMAT)
pst_date = pst_timezone.localize(naive_date)
utc_date = pst_date.astimezone(utc_timezone)
utc_date_str = utc_date.strftime(UTC_TIME_FORMAT)
return utc_date_str
However (for reasons I won't get into) we have been asked to not use the pytz library and instead stick to Python's native datetime, and dateutil libraries. Currently we are committed to Python 3.8.
I've tried a number of things things using datetime's strptime and dateutil's parser, but all to dead ends so far. Are there any obvious patterns i am missing?
Thanks

for the record, here's an option using Python 3.9's zoneinfo and Python 3.7's isoformat.
from datetime import datetime
from zoneinfo import ZoneInfo
def to_isoformat(s, tzname, inputformat='%m/%d/%Y %I:%M %p'):
"""
convert an input string "s" representing date/time in format "inputformat" in
time zone "tzname" to UTC and return an ISO format string with Z denoting UTC.
Parameters
----------
s : str
string representing date/time.
tzname : str
IANA time zone name of time zone that s belongs in.
inputformat : str, optional
date/time string format. The default is '%m/%d/%Y %I:%M %p'.
Returns
-------
str
ISO format string, with seconds precision and Z denoting UTC.
"""
# to datetime and set time zone
dt = datetime.strptime(s, inputformat).replace(tzinfo=ZoneInfo(tzname))
# convert to UTC and make ISO format with Z for UTC
return dt.astimezone(ZoneInfo("UTC")).isoformat(timespec='seconds').replace('+00:00', 'Z')
print(to_isoformat('01/28/2022 12:00 AM', 'America/Los_Angeles'))
# 2022-01-28T08:00:00Z
With Python 3.8, you can basically just replace from zoneinfo import ZoneInfo with from dateutil.tz import gettz as ZoneInfo.

Well, after some more digging I managed to find refs here and here that laid out the basics of what I needed. Final code looks like this:
from datetime import datetime
from dateutil import tz, parser, utils
UTC_TIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
PROVIDER_TIMEZONE = 'America/Los_Angeles'
def dt_string_to_utc_converter(datetime_str)
parsed_dt = parser.parse(datetime_str)
localized_dt = utils.default_tzinfo(parsed_dt, tz.gettz(PROVIDER_TIMEZONE))
utc_dt = localized_dt.astimezone(tz.tzutc())
utc_dt_str = utc_dt.strftime(UTC_TIME_FORMAT)

Related

Python convert timestamp to unix

I know these questions have been asked before but I'm struggling to convert a timestamp string to a unix time and figuring out whether the datetime objects are naive or aware
For example, to convert the time "2021-05-19 12:51:47" to unix:
>>> from datetime import datetime as dt
>>> dt_obj = dt.strptime("2021-05-19 12:51:47", "%Y-%m-%d %H:%M:%S")
>>> dt_obj
datetime.datetime(2021, 5, 19, 12, 51, 47)
is dt_obj naive or aware and how would you determine this? The methods on dt_obj such as timetz, tzinfo, and tzname don't seem to indicate anything - does that mean that dt_obj is naive?
Then to get unix:
>>> dt_obj.timestamp()
1621421507.0
However when I check 1621421507.0 on say https://www.unixtimestamp.com then it tells me that gmt for the above is Wed May 19 2021 10:51:47 GMT+0000, ie 2 hours behind the original timestamp?
since Python's datetime treats naive datetime as local time by default, you need to set the time zone (tzinfo attribute):
from datetime import datetime, timezone
# assuming "2021-05-19 12:51:47" represents UTC:
dt_obj = datetime.fromisoformat("2021-05-19 12:51:47").replace(tzinfo=timezone.utc)
Or, as #Wolf suggested, instead of setting the tzinfo attribute explicitly, you can also modify the input string by adding "+00:00" which is parsed to UTC;
dt_obj = datetime.fromisoformat("2021-05-19 12:51:47" + "+00:00")
In any case, the result
dt_obj.timestamp()
# 1621428707.0
now converts as expected on https://www.unixtimestamp.com/:
As long as you don't specify the timezone when calling strptime, you will produce naive datetime objects. You may pass time zone information via %z format specifier and +00:00 added to the textual date-time representation to get a timezone aware datetime object:
from datetime import datetime
dt_str = "2021-05-19 12:51:47"
print(dt_str)
dt_obj = datetime.strptime(dt_str+"+00:00", "%Y-%m-%d %H:%M:%S%z")
print(dt_obj)
print(dt_obj.timestamp())
The of above script is this:
2021-05-19 12:51:47
2021-05-19 12:51:47+00:00
1621428707.0
datetime.timestamp()
Naive datetime instances are assumed to represent local time and this method relies on the platform C mktime() function to perform the conversion.
So using this does automatically apply yours machine current timezone, following recipe is given to calculate timestamp from naive datetime without influence of timezone:
timestamp = (dt - datetime(1970, 1, 1)) / timedelta(seconds=1)

How to properly format timestamp

I have the following info for timestamp from the database:
time_format: str = "%d/%b/%Y %H:%M %Z"
timestamp = '2020-11-03T21:32:19.722012+00:00'
timezone = 'America/New_York'
How can I use datetime to format this information to look as follows:
11/03/2020 17:32EST
I was able to get this far:
datetime.datetime.now().fromisoformat(timestamp_utc).strftime(time_format)
But can't figure out how to replace datetime.now() with any time and then display the desired timezone in place of "UTC"
As python doens't have fantastic timezone support out of the box I'd recommend the pytz library for this use case.
from datetime import datetime
import pytz
# Input
datetime_str = '2020-11-03T21:32:19.722012+00:00'
timezone_str = 'America/New_York'
output_format = '%m/%d/%Y %H:%M %Z'
# Convert input datetime str to python datetime obj
# this datetime is timezone aware, with tz=UTC
utc_datetime = datetime.fromisoformat(datetime_str)
# Convert input timezone str to a pytz timezone object
new_timezone = pytz.timezone(timezone_str)
# Adjust the UTC datetime to use the new timezone
new_timezone_datetime = utc_datetime.astimezone(new_timezone)
# Print in the desired output format
print(new_timezone_datetime.strftime(output_format))
If I run the above code, I get the following...
11/03/2020 16:32 EST
EDIT: The reason it is 16:32 instead of 17:32 is because American/New_York is the same as US/Eastern, in that they use EST/EDT at different points during the year (daylight savings). 2020-11-03 happens to fall in EST.

Convert a datetime in string to ISO format and then to UTC

Looking forward to convert this date "2020-07-23 23:00:00.000"
First to ISO format and then to UTC
This is something i tried to convert this to ISO format, looking to convert this to UTC
def get_date_in_iso_utc(date_str):
date_time_obj = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S.%f')
return date_time_obj.isoformat()
Use the pytz module, which comes with a full list of time zones + UTC. Figure out what the local timezone is, construct a timezone object from it, and manipulate and attach it to the iso_format datetime.
Source code, using local timezone Asia/Kolkata, for the string 2020-7-23 10:11:12:
import pytz, datetime
time_zone = pytz.timezone("Asia/Kolkata")
iso_format = datetime.datetime.strptime("2020-7-23 10:11:12", "%Y-%m-%d %H:%M:%S")
local_date_time = time_zone.localize(iso_format, is_dst=None)
utc_date_time = local_date_time.astimezone(pytz.utc)
To convert it to UTC timing, you need to do this:
final_data = utc_date_time.strftime("%Y-%m-%d %H:%M:%S")
print(final_data)
# OUTPUT
# >>> 2020-07-23 04:41:12
ADD
To find your specified timezone, you can do this via pytz only. Just use the below code
>>> pytz.all_timezones
>>> ['Africa/Abidjan',
'Africa/Accra',
'Africa/Addis_Ababa',
...]
Hope that answers your question
Converting to ISO format: Python - Convert string representation of date to ISO 8601
Convert from ISO to UTC: How do I translate an ISO 8601 datetime string into a Python datetime object? to convert to datetime and How to convert local time string to UTC? to convert datetime to UTC.
(I didn't mark it as duplicate as there is no single post that answeres both question. Please notice you could easily google the answer)

Python: Add or remove timezone hours from a timestamp and get actual time

In Python, I need to get the actual time of a given string timestamp converted to a different timezone without timezone information. I am using pytz to do this. But all I get is the given DateTime with the timezone information appended to it.
Base datetime : 2020-05-29 19:00:00 (A string datetime without timezone info)
Requirement: When this time is converted to (US Zipcode 90071) -0700 timezone,
it should return "2020-05-29 12:00:00", not "2020-05-29 19:00:00-0700"
Code:
import pytz
from datetime import datetime
from uszipcode import SearchEngine
from timezonefinder import TimezoneFinder
date_time_obj = datetime.strptime("2020-05-29 19:00:00", '%Y-%m-%d %H:%M:%S')
zip = "90071"
search = SearchEngine(simple_zipcode=True)
zipcode = search.by_zipcode(zip)
zipcode = zipcode.to_dict()
tf = TimezoneFinder(in_memory=True)
timezone = tf.timezone_at(lng=zipcode['lng'], lat=zipcode['lat'])
tz = pytz.timezone(timezone)
new_timestamp = tz.localize(date_time_obj)
new_timestamp_str = datetime.strftime(new_timestamp, '%m/%d/%Y %H:%M:%S')
But this returns 2020-05-29 19:00:00.000000-0700. I need to retrieve a DateTime object/string with the actual time shown in that timezone without a timezone chunk attached to the end of the DateTime.
Assuming your "Base datetime" refers to UTC, you have to add a tzinfo=UTC first before you convert to another timezone. Also, avoid overwriting built-ins like zip. Example using dateutil:
from datetime import datetime
import dateutil
from uszipcode import SearchEngine
from timezonefinder import TimezoneFinder
date_time_obj = datetime.strptime("2020-05-29 19:00:00", '%Y-%m-%d %H:%M:%S')
zipcode = "90071"
search = SearchEngine(simple_zipcode=True)
zipcode = search.by_zipcode(zipcode)
zipcode = zipcode.to_dict()
tf = TimezoneFinder(in_memory=True)
timezone = tf.timezone_at(lng=zipcode['lng'], lat=zipcode['lat'])
# localize to UTC first
date_time_obj = date_time_obj.replace(tzinfo=dateutil.tz.UTC)
# now localize to timezone of the zipcode:
new_timestamp = date_time_obj.astimezone(dateutil.tz.gettz(timezone))
new_timestamp_str = datetime.strftime(new_timestamp, '%m/%d/%Y %H:%M:%S')
# '05/29/2020 12:00:00'
If you need to use pytz, make sure to use localize instead of replace (even though UTC is an exception).
Sidenote: If your "Base datetime" refers to local time (operating system), you could obtain that timezone by
import time
import dateutil
localtzname = time.tzname[time.daylight]
tz = dateutil.tz.gettz(localtzname)
It appears that your original date and time are in UTC. So for localize to work properly, you have to start with the proper timezone attached.
date_time_obj = datetime.strptime("2020-05-29 19:00:00", '%Y-%m-%d %H:%M:%S').replace(tzinfo=pytz.utc)
Then you can remove it again after the conversion:
return date_time_obj.astimezone(tz).replace(tzinfo=None)

Convert UTC datetime string to local datetime

I've never had to convert time to and from UTC. Recently had a request to have my app be timezone aware, and I've been running myself in circles. Lots of information on converting local time to UTC, which I found fairly elementary (maybe I'm doing that wrong as well), but I can not find any information on easily converting the UTC time to the end-users timezone.
In a nutshell, and android app sends me (appengine app) data and within that data is a timestamp. To store that timestamp to utc time I am using:
datetime.utcfromtimestamp(timestamp)
That seems to be working. When my app stores the data, it is being store as 5 hours ahead (I am EST -5)
The data is being stored on appengine's BigTable, and when retrieved it comes out as a string like so:
"2011-01-21 02:37:21"
How do I convert this string to a DateTime in the users correct time zone?
Also, what is the recommended storage for a users timezone information? (How do you typically store tz info ie: "-5:00" or "EST" etc etc ?) I'm sure the answer to my first question might contain a parameter the answers the second.
If you don't want to provide your own tzinfo objects, check out the python-dateutil library. It provides tzinfo implementations on top of a zoneinfo (Olson) database such that you can refer to time zone rules by a somewhat canonical name.
from datetime import datetime
from dateutil import tz
# METHOD 1: Hardcode zones:
from_zone = tz.gettz('UTC')
to_zone = tz.gettz('America/New_York')
# METHOD 2: Auto-detect zones:
from_zone = tz.tzutc()
to_zone = tz.tzlocal()
# utc = datetime.utcnow()
utc = datetime.strptime('2011-01-21 02:37:21', '%Y-%m-%d %H:%M:%S')
# Tell the datetime object that it's in UTC time zone since
# datetime objects are 'naive' by default
utc = utc.replace(tzinfo=from_zone)
# Convert time zone
central = utc.astimezone(to_zone)
Edit Expanded example to show strptime usage
Edit 2 Fixed API usage to show better entry point method
Edit 3 Included auto-detect methods for timezones (Yarin)
Here's a resilient method that doesn't depend on any external libraries:
from datetime import datetime
import time
def datetime_from_utc_to_local(utc_datetime):
now_timestamp = time.time()
offset = datetime.fromtimestamp(now_timestamp) - datetime.utcfromtimestamp(now_timestamp)
return utc_datetime + offset
This avoids the timing issues in DelboyJay's example. And the lesser timing issues in Erik van Oosten's amendment.
As an interesting footnote, the timezone offset computed above can differ from the following seemingly equivalent expression, probably due to daylight savings rule changes:
offset = datetime.fromtimestamp(0) - datetime.utcfromtimestamp(0) # NO!
Update: This snippet has the weakness of using the UTC offset of the present time, which may differ from the UTC offset of the input datetime. See comments on this answer for another solution.
To get around the different times, grab the epoch time from the time passed in. Here's what I do:
def utc2local(utc):
epoch = time.mktime(utc.timetuple())
offset = datetime.fromtimestamp(epoch) - datetime.utcfromtimestamp(epoch)
return utc + offset
See the datetime documentation on tzinfo objects. You have to implement the timezones you want to support yourself. The are examples at the bottom of the documentation.
Here's a simple example:
from datetime import datetime,tzinfo,timedelta
class Zone(tzinfo):
def __init__(self,offset,isdst,name):
self.offset = offset
self.isdst = isdst
self.name = name
def utcoffset(self, dt):
return timedelta(hours=self.offset) + self.dst(dt)
def dst(self, dt):
return timedelta(hours=1) if self.isdst else timedelta(0)
def tzname(self,dt):
return self.name
GMT = Zone(0,False,'GMT')
EST = Zone(-5,False,'EST')
print datetime.utcnow().strftime('%m/%d/%Y %H:%M:%S %Z')
print datetime.now(GMT).strftime('%m/%d/%Y %H:%M:%S %Z')
print datetime.now(EST).strftime('%m/%d/%Y %H:%M:%S %Z')
t = datetime.strptime('2011-01-21 02:37:21','%Y-%m-%d %H:%M:%S')
t = t.replace(tzinfo=GMT)
print t
print t.astimezone(EST)
Output
01/22/2011 21:52:09
01/22/2011 21:52:09 GMT
01/22/2011 16:52:09 EST
2011-01-21 02:37:21+00:00
2011-01-20 21:37:21-05:00a
If you want to get the correct result even for the time that corresponds to an ambiguous local time (e.g., during a DST transition) and/or the local utc offset is different at different times in your local time zone then use pytz timezones:
#!/usr/bin/env python
from datetime import datetime
import pytz # $ pip install pytz
import tzlocal # $ pip install tzlocal
local_timezone = tzlocal.get_localzone() # get pytz tzinfo
utc_time = datetime.strptime("2011-01-21 02:37:21", "%Y-%m-%d %H:%M:%S")
local_time = utc_time.replace(tzinfo=pytz.utc).astimezone(local_timezone)
This answer should be helpful if you don't want to use any other modules besides datetime.
datetime.utcfromtimestamp(timestamp) returns a naive datetime object (not an aware one). Aware ones are timezone aware, and naive are not. You want an aware one if you want to convert between timezones (e.g. between UTC and local time).
If you aren't the one instantiating the date to start with, but you can still create a naive datetime object in UTC time, you might want to try this Python 3.x code to convert it:
import datetime
d=datetime.datetime.strptime("2011-01-21 02:37:21", "%Y-%m-%d %H:%M:%S") #Get your naive datetime object
d=d.replace(tzinfo=datetime.timezone.utc) #Convert it to an aware datetime object in UTC time.
d=d.astimezone() #Convert it to your local timezone (still aware)
print(d.strftime("%d %b %Y (%I:%M:%S:%f %p) %Z")) #Print it with a directive of choice
Be careful not to mistakenly assume that if your timezone is currently MDT that daylight savings doesn't work with the above code since it prints MST. You'll note that if you change the month to August, it'll print MDT.
Another easy way to get an aware datetime object (also in Python 3.x) is to create it with a timezone specified to start with. Here's an example, using UTC:
import datetime, sys
aware_utc_dt_obj=datetime.datetime.now(datetime.timezone.utc) #create an aware datetime object
dt_obj_local=aware_utc_dt_obj.astimezone() #convert it to local time
#The following section is just code for a directive I made that I liked.
if sys.platform=="win32":
directive="%#d %b %Y (%#I:%M:%S:%f %p) %Z"
else:
directive="%-d %b %Y (%-I:%M:%S:%f %p) %Z"
print(dt_obj_local.strftime(directive))
If you use Python 2.x, you'll probably have to subclass datetime.tzinfo and use that to help you create an aware datetime object, since datetime.timezone doesn't exist in Python 2.x.
If using Django, you can use the timezone.localtime method:
from django.utils import timezone
date
# datetime.datetime(2014, 8, 1, 20, 15, 0, 513000, tzinfo=<UTC>)
timezone.localtime(date)
# datetime.datetime(2014, 8, 1, 16, 15, 0, 513000, tzinfo=<DstTzInfo 'America/New_York' EDT-1 day, 20:00:00 DST>)
The following worked for me in a Cloud environment for US west:
import datetime
import pytz
#set the timezone
tzInfo = pytz.timezone('America/Los_Angeles')
dt = datetime.datetime.now(tz=tzInfo)
print(dt)
Consolidating the answer from franksands into a convenient method.
import calendar
import datetime
def to_local_datetime(utc_dt):
"""
convert from utc datetime to a locally aware datetime according to the host timezone
:param utc_dt: utc datetime
:return: local timezone datetime
"""
return datetime.datetime.fromtimestamp(calendar.timegm(utc_dt.timetuple()))
You can use arrow
from datetime import datetime
import arrow
now = datetime.utcnow()
print(arrow.get(now).to('local').format())
# '2018-04-04 15:59:24+02:00'
you can feed arrow.get() with anything. timestamp, iso string etc
You can use calendar.timegm to convert your time to seconds since Unix epoch and time.localtime to convert back:
import calendar
import time
time_tuple = time.strptime("2011-01-21 02:37:21", "%Y-%m-%d %H:%M:%S")
t = calendar.timegm(time_tuple)
print time.ctime(t)
Gives Fri Jan 21 05:37:21 2011 (because I'm in UTC+03:00 timezone).
import datetime
def utc_str_to_local_str(utc_str: str, utc_format: str, local_format: str):
"""
:param utc_str: UTC time string
:param utc_format: format of UTC time string
:param local_format: format of local time string
:return: local time string
"""
temp1 = datetime.datetime.strptime(utc_str, utc_format)
temp2 = temp1.replace(tzinfo=datetime.timezone.utc)
local_time = temp2.astimezone()
return local_time.strftime(local_format)
utc_tz_example_str = '2018-10-17T00:00:00.111Z'
utc_fmt = '%Y-%m-%dT%H:%M:%S.%fZ'
local_fmt = '%Y-%m-%dT%H:%M:%S+08:00'
# call my function here
local_tz_str = utc_str_to_local_str(utc_tz_example_str, utc_fmt, local_fmt)
print(local_tz_str) # 2018-10-17T08:00:00+08:00
When I input utc_tz_example_str = 2018-10-17T00:00:00.111Z, (UTC +00:00)
then I will get local_tz_str = 2018-10-17T08:00:00+08:00 (My target timezone +08:00)
parameter utc_format is a format determined by your specific utc_tz_example_str.
parameter local_fmt is the final desired format.
In my case, my desired format is %Y-%m-%dT%H:%M:%S+08:00 ( +08:00 timezone). You should construct the format you want.
This worked for me:
from django.utils import timezone
from datetime import timedelta,datetime
ist_time = timezone.now() + timedelta(hours=5,minutes=30)
#second method
ist_time = datetime.now() + timedelta(hours=5,minutes=30)
I traditionally defer this to the frontend -- send times from the backend as timestamps or some other datetime format in UTC, then let the client figure out the timezone offset and render this data in the proper timezone.
For a webapp, this is pretty easy to do in javascript -- you can figure out the browser's timezone offset pretty easily using builtin methods and then render the data from the backend properly.
From the answer here, you can use the time module to convert from utc to the local time set in your computer:
utc_time = time.strptime("2018-12-13T10:32:00.000", "%Y-%m-%dT%H:%M:%S.%f")
utc_seconds = calendar.timegm(utc_time)
local_time = time.localtime(utc_seconds)
Here is a quick and dirty version that uses the local systems settings to work out the time difference. NOTE: This will not work if you need to convert to a timezone that your current system is not running in. I have tested this with UK settings under BST timezone
from datetime import datetime
def ConvertP4DateTimeToLocal(timestampValue):
assert isinstance(timestampValue, int)
# get the UTC time from the timestamp integer value.
d = datetime.utcfromtimestamp( timestampValue )
# calculate time difference from utcnow and the local system time reported by OS
offset = datetime.now() - datetime.utcnow()
# Add offset to UTC time and return it
return d + offset
Short and simple:
from datetime import datetime
t = "2011-01-21 02:37:21"
datetime.fromisoformat(t) + (datetime.now() - datetime.utcnow())

Categories