Timestamp string to datetime (python) - python

I have this timestamp string:
x = 2021-11-24T16:05:51.399+0000
I am struggling to get the parsing to pass after the decimal point. Here is my attempt so far:
datetime.datetime.strptime(x,"%Y-%m-%dT%H:%M:%S.%f+")
Current error running this line:
ValueError: unconverted data remains: 0000

Add %z at the end of the format to match UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive).
>>> from datetime import datetime
>>>
>>> x = '2021-11-24T16:05:51.399+0000'
>>> datetime.strptime(x,"%Y-%m-%dT%H:%M:%S.%f%z")
datetime.datetime(2021, 11, 24, 16, 5, 51, 399000, tzinfo=datetime.timezone.utc)

Related

convert 2021-01-18T11:18:10.833876+00:00 to datetime python

I am trying to convert the following string to datetime in python. After referring to datetime.strptime(‘2017-01-12T14:12:06.000-0500’,'%Y-%m-%dT%H:%M:%S.%f%Z') I am trying the below mentioned format.
config_current_ts = datetime.strptime(internal_config["timestamp_str"], "%Y:%m:%dT%H:%M:%S.%f%z")
Yet I get an error stating :
ValueError: time data '2021-01-18T11:18:10.833876+00:00' does not
match format '%Y:%m:%dT%H:%M:%S.%f%z'
I am using python3.7.4. Can someone tell me how to convert this to datetime? I want to basically compare the string and current time to see which is ahead.
With Python 3.7+, use fromisoformat - since you have ISO 8601 format, it is appropriate and as a benefit also more efficient. Ex:
from datetime import datetime
s = '2021-01-18T11:18:10.833876+00:00'
dt = datetime.fromisoformat(s)
print(dt)
# 2021-01-18 11:18:10.833876+00:00
print(repr(dt))
# datetime.datetime(2021, 1, 18, 11, 18, 10, 833876, tzinfo=datetime.timezone.utc)
the format string should be '%Y-%m-%dT%H:%M:%S.%f%z'
>>> from datetime import datetime
>>> d = '2021-01-18T11:18:10.833876+00:00'
>>> datetime.strptime(d, '%Y-%m-%dT%H:%M:%S.%f%z')
datetime.datetime(2021, 1, 18, 11, 18, 10, 833876, tzinfo=datetime.timezone.utc)

convert str date to datetime for timezone

I have an API that return this date string
strdate = '2019-10-07T06:09:28.984Z'
How do I convert it to a datetime object?
dt = datetime.datetime.strptime(strdate, '%Y-%m-%d')
If I use strptime like above i get "ValueError: unconverted data remains: T06:09:54.346Z"
What do I do if there is "T" and "Z" included in the string date? I want the data in local time. But is the string really timezone aware so I can convert it properly? In this case I know the timezone, but what if I did not know?
The error is because you're not including the time part in the format string. Do that:
datetime.strptime('2019-10-07T06:09:28.984Z', '%Y-%m-%dT%H:%M:%S.%f%z')
This results in:
datetime.datetime(2019, 10, 7, 6, 9, 28, 984000, tzinfo=datetime.timezone.utc)
If you want to convert this to a local timezone, do that:
from pytz import timezone
dt = datetime.strptime(strdate, '%Y-%m-%dT%H:%M:%S.%f%z')
local_dt = dt.astimezone(timezone('Asia/Tokyo'))
Demo: https://repl.it/repls/RotatingSqueakyCertifications
import datetime
strdate = '2019-10-07T06:09:28.984Z'
dt=datetime.datetime.strptime(strdate, "%Y-%m-%dT%H:%M:%S.%fZ")
print(dt)
# output - 2019-10-07 06:09:28.984000

I want to parse string to timestamp-with-timezone in python

I try to parse string to time-stamp with timezone format.
here is an example
"2016-02-18 16:13:07+09"
i want to know parsing this string format to time-stamp format in python.
how can i do that?
Is the UTC offset format in your string +09 or +0900 ?
If the offset in your string is 0900 you can use the below .If your UTC offset is only +09 as you mentioned in your question , you can pad the string with 00 and get the below code to work .
Code:
import datetime
time="2016-02-18 16:13:07+0900"
new_time=datetime.datetime.strptime(time,"%Y-%m-%d %H:%M:%S%z")
print(new_time)
new_time_python=datetime.datetime.strftime(new_time,"%m-%d-%y")
print(new_time_python)
Output
2016-02-18 16:13:07+09:00
02-18-16
dateutil might be a suitable library for your purposes:
from dateutil.parser import parser
p = parser()
d = p.parse('2016-02-18 16:13:07+09'.decode('utf-8')) # must be unicode string
d
>>> datetime.datetime(2016, 2, 18, 16, 13, 7, tzinfo=tzoffset(None, 32400))
If the UTC offset may be specified both as +HH and +HHMM format then you could use str.ljust() method to normalize the input time string. Then you could use .strptime() to parse it:
#!/usr/bin/env python3
from datetime import datetime
time_string = "2016-02-18 16:13:07+09"
dt = datetime.strptime(time_string.ljust(24, "0"), "%Y-%m-%d %H:%M:%S%z")
# -> datetime.datetime(2016, 2, 18, 16, 13, 7,
# tzinfo=datetime.timezone(datetime.timedelta(0, 32400)))
If your Python version doesn't support %z, see How to parse dates with -0400 timezone string in python?

Inserting python datetime into the mysql table

I have a Python date like
2013-04-04T18:56:21Z
I want to store this into my mysql database .
I tried like
self.start_at = datetime.strptime(self.start_at.split(".")[0], "%Y-%m-%dT%H:%M:%S")
But i am getting an error
ValueError: unconverted data remains: Z
PLease tell me how to convert the above date in mysql acceptable format .
In this instance this will work.
>>> from datetime import datetime
>>> start_at = '2013-04-04T18:56:21Z'
>>> datetime.strptime(start_at , "%Y-%m-%dT%H:%M:%SZ")
datetime.datetime(2013, 4, 4, 18, 56, 21)
Are the T & Z characters always in your date format or do they every change?
If the is some variation in these seperator characters, you should do something like this:
>>> from datetime import datetime
>>> start_at = '2013-04-04T18:56:21Z'
>>> datetime.strptime(start_at[0:10] + ' ' + start_at[11:19], "%Y-%m-%d %H:%M:%S")
datetime.datetime(2013, 4, 4, 18, 56, 21)

Convert UTC time to python datetime

I have numerous UTC time stamps in the following format:
2012-04-30T23:08:56+00:00
I want to convert them to python datetime objects but am having trouble.
My code:
for time in data:
pythondata[i]=datetime.strptime(time,"%y-%m-%dT%H:%M:%S+00:00")
I get the following error:
ValueError: time data '2012-03-01T00:05:55+00:00' does not match format '%y-%m-%dT%H:%M:%S+00:00'
It looks like I have the proper format, so why doesn't this work?
Change the year marker in your time format string to %Y:
time = '2012-03-01T00:05:55+00:00'
datetime.strptime(time, "%Y-%m-%dT%H:%M:%S+00:00")
# => datetime.datetime(2012, 3, 1, 0, 5, 55)
See strftime() and strptime() behavior.
I highly recommend python-dateutil library, it allows conversion of multiple datetime formats from raw strings into datetime objects with/without timezone set
>>> from dateutil.parser import parse
>>> parse('2012-04-30T23:08:56+00:00')
datetime.datetime(2012, 4, 30, 23, 8, 56, tzinfo=tzutc())

Categories