convert string having timezone into to utc datetime object python - python-3.x

I have a timezone aware string like this and would like to convert it to utc timezone. I am using python 3.4 version
My string is like -
2018-05-04T05:22:52.3272611-04:00
I am not able to get much info on converting the given datetime to utc.

That looks like an ISO 8601 string (though with anomalously high precision on the sub-second component); if the fact that you have 7 digits instead of 6 after the decimal point is a typo, you can use dateutil.parser.isoparse on it:
from dateutil.parser import isoparse
from dateutil.tz import UTC
dt = isoparse('2018-05-04T05:22:52.327261-04:00')
dt.astimezone(UTC)
Otherwise you can use dateutil.parser.parse, which is slower but allows for more unusual formats:
from dateutil.parser import parse
from dateutil.tz import UTC
dt = parse('2018-05-04T05:22:52.3272611-04:00')
dt.astimezone(UTC)
You should also use parse if you are not certain what the format will be.

Related

converting string date time with microseconds to date object

I have a datetime coming from json data as 2017-03-28T00:00:00.000Z
I have tried this
datetime.datetime.strptime(record_date, "%Y-%m-%d%H:%M:%S%f")
but this gives error:
*** ValueError: time data '2017-03-28T00:00:00.000Z' does not match format '%Y-%m-%d%H:%M:%S%f'
I have to compare the read date with user input date as 2017-03-28 format
Your time is in isoformat, you need to use datetime's fromisoformat to convert it into a datetime object.
The [:-1] is to exclude the Z because datetime does not understand what that is. (2017-03-28T00:00:00.000Z is actually RFC 3339)
Z basically means UTC timezone (+00:00 GMT)
from datetime import datetime
foo = "2017-03-28T00:00:00.000Z"
bar = datetime.fromisoformat(foo[:-1])
print(bar)
print(type(bar))
Gives you the result of, but this is now naive (unaware of timezone)
2017-03-28 00:00:00
<class 'datetime.datetime'>
A quick workaround to make it timezone aware is
bar = datetime.fromisoformat(foo.replace('Z', '+00:00'))
print(bar)
2017-03-28 00:00:00+00:00
#If milliseconds are of concern then just strftime
bar = datetime.fromisoformat(foo.replace('Z', '+00:00')).strftime("%Y-%m-%d %H:%M:%S:%f %Z")
# the Z displays UTC or equivalent time zone
2017-03-28 00:00:00:000000 UTC
The dateutil module is even better at handling this as it has native support for RFC 3339.
import dateutil.parser
foo = "2017-03-28T00:00:00.000Z"
bar = dateutil.parser.isoparse(foo)
print(bar)
print(type(bar))
2017-03-28 00:00:00+00:00
<class 'datetime.datetime'>

Convert date and time from UTC to local in specifed format

I have UTC date time as 2021-02-24 12:41:40.
I want to get it converted into local timezone in 24 hour format i.e. "IST" in same format i.e. 2021-02-24 18:11:40.
I referred many answers on Stackoverflow, but I am not able to get the result in desired format.
How can it be achieved in Python3?
what I linked applied specifically to your question:
from datetime import datetime, timezone
from dateutil.tz import gettz
# the given info:
ist = gettz("Asia/Kolkata")
s = "2021-02-24 12:41:40"
# now parse to datetime and set tzinfo to UTC
dt = datetime.fromisoformat(s).replace(tzinfo=timezone.utc)
# convert to IST time zone
dt = dt.astimezone(ist)
# output to isoformat string, but without time zone info
s = dt.replace(tzinfo=None).isoformat(' ')
print(s)
# 2021-02-24 18:11:40
Solution for Python 3.6.*
datetime = "2021-02-24 12:41:40"
from_zone = tz.tzutc()
to_zone = tz.tzlocal()
utc = datetime.strptime( datetime,'%Y-%m-%d %H:%M:%S')
utc = utc.replace(tzinfo=from_zone)
local = utc.astimezone(to_zone)
localTime = local.replace(tzinfo=None).isoformat(' ')

python get previous day date in UTC using datetime(datetime.date) and timedelta

i wanted to get previous day date in UTC .
from datetime import datetime, timedelta
month_now = str(datetime.date(datetime.utcnow()))[:-3]
>>> print(month_now)
2021-01
And now
previous_day = str(datetime.date(datetime.utcnow()-datetime.timedelta(days=1)))
AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'
Please help.All related posts was suggesting some thing like but need something with datetime.date
Previous_Date = datetime.datetime.today() - datetime.timedelta(days=1)
Stop using utcnow and utcfromtimestamp - be specific instead by using timezone.utc.
from datetime import datetime, timedelta, timezone
todayUTC = datetime.now(timezone.utc).date()
yesterdayUTC = today - timedelta(1)
print(todayUTC, yesterdayUTC)
# 2021-01-08 2021-01-07
Also not that classes datetime, timedelta etc. are classes from the datetime module. If you import them explicitly (from datetime import ...), you have to use them accordingly, hence the error. The other option is to import the module, so you can then call all its classes like e.g. datetime.timedelta, datetime.datetime and so on. docs. Your original code then would have to look something like
import datetime
month_now = str(datetime.datetime.utcnow().date())[:-3]
previous_day = str((datetime.datetime.utcnow()-datetime.timedelta(days=1)).date())
print(month_now, previous_day)
# 2021-01 2021-01-07

PyMongo/Python/Airflow - Convert CST Date/DateTime to UTC and store in MongoDB in ISO format?

Which is the correct or ideal or preferred method to convert a CST Date and/or Datetime field to UTC along with DST aware settings and store in MongoDB in ISO format in Python/PyMongo ? The source date/datetime field can come from any timezone (right now we know its CST), I need to convert all of them to UTC and store into target MongoDB.
As per MongoDB docs, MongoDB stores times in UTC by default, and will convert any local time representations into this form. Applications that must operate or report on some unmodified local time value may store the time zone alongside the UTC timestamp, and compute the original local time in their application logic.
Examples:
Method#1: with Timestamp (local timezone defined)
from datetime import datetime
import pytz
local_timezone = pytz.timezone("US/Central")
utc_datetime = local_timezone.localize(datetime.strptime ("1/2/2017 12:43 pm",'%m/%d/%Y %H:%M %p'),is_dst=True).astimezone(pytz.utc)
print(utc_datetime)
print(type(utc_datetime))
2017-01-02 18:43:00+00:00
<class 'datetime.datetime'>
without timestamp i.e. just date: - it adds an offset value of 6 hours in timestamp and during DST 5 hours. Removing or without astimezone(pytz.utc) , it returns date/time like 2017-01-02 00:00:00-06:00 i.e. showing -6 hours offset difference. Should we really be using astimezeon(pytz.utc) ??
from datetime import datetime
import pytz
local_timezone = pytz.timezone("US/Central")
utc_datetime = local_timezone.localize(datetime.strptime ("1/2/2017",'%m/%d/%Y'),is_dst=True).astimezone(pytz.utc)
print(utc_datetime)
print(type(utc_datetime))
2017-01-02 06:00:00+00:00
<class 'datetime.datetime'>
Method#2: with Timestamp (local timezone NOT defined)
from datetime import datetime, timezone
utc_datetime=datetime.utcfromtimestamp(datetime.strptime ("1/2/2017 12:43 pm",'%m/%d/%Y %H:%M %p').replace(tzinfo = timezone.utc).timestamp())
print(utc_datetime)
print(type(utc_datetime))
2017-01-02 12:43:00
<class 'datetime.datetime'>
without Timestamp i.e. just date part - no offset
from datetime import datetime, timezone
utc_datetime=datetime.utcfromtimestamp(datetime.strptime ("1/2/2017",'%m/%d/%Y').replace(tzinfo = timezone.utc).timestamp())
print(utc_datetime)
print(type(utc_datetime))
2017-01-02 00:00:00
<class 'datetime.datetime'>
After loading into MongoDB - it adds a "Z" at the end of the date/timestamp. Should I also add "tz_aware=True" when initiating connection with MongoClient ?
ISOFormat - changing above utc timestamp to isoformat() returns and gets loaded as string in MongoDB instead of a Date. So, how do we ensure it is still stored in ISO Date format in MongoDB ?
utc_datetime_iso=datetime.utcfromtimestamp(datetime.strptime ("1/2/2017",'%m/%d/%Y').replace(tzinfo = timezone.utc).timestamp()).**isoformat()**
print(utc_datetime_iso)
print(type(utc_datetime_iso))
2017-01-02T00:00:00
<class 'str'>
I never worked with python, so I can give only some general notes.
Never store date/time values as string, use proper Date object. Storing date/time values as strings is usually a design failure.
All Date values in MongoDB are stored in UTC - always and only. Some client applications implicitly converts UTC to local times and display local values, however internally in MongoDB it is always UTC.
If you run db.collection.insertOne({ts: ISODate("2020-09-07T14:00:00+02:00")}) then MongoDB stores ISODate("2020-09-07T12:00:00Z"), the original time zone information is lost. If you need to preserve the original time zone, then you have to store it in a separate field.
ISODate is just an alias for new Date. However, there is a difference. If you don't specify any time zone (e.g. "2020-09-07T14:00:00") then new Date() assumes local time but ISODate() assumes UTC time. I don't know which method is internally used by python.
So, new Date("2020-09-07T14:00:00") results in 2020-09-07 12:00:00Z whereas ISODate("2020-09-07T14:00:00") results in 2020-09-07 14:00:00Z

How to convert zulu datetime format to user defined time format

Hi I have this DateTime format in our log "2019-09-19T15:12:59.943Z"
I want to convert this to custom DateTime format 2019-09-19 15:12:59
from datetime import datetime
timestamp = "2019-09-19T15:12:59.943Z"
dt_object = datetime.fromtimestamp(timestamp)
print("dt_object =", dt_object)
print("type(dt_object) =", type(dt_object))
which function shall I use for this
thanks
okay
This issue is related to custom DateTime formatting not related to timestamp.
because timestamp in python is an integer value, not a string value.
So you have a custom DateTime format which contains Zulu time format.
and you need to convert this Zulu DateTime format to custom DateTime format.
so, try this python script and its working fine on Python version 3.6
import datetime
d = datetime.datetime.strptime("2019-09-19T15:12:59.943Z","%Y-%m-%dT%H:%M:%S.%fZ")
new_format = "%Y-%m-%d"
d.strftime(new_format)
print(d)
or you can use this online fiddle to check the result
https://pyfiddle.io/fiddle/c7b8e849-c31a-41ba-8bc9-5436d6faa4e9/?i=true

Resources