How to convert date/time string to epoch? - python

Have the following string:
"date Thursday June 03 12:02:56 2017"
What would be the proper way of convert it to epoch time?

You can use datetime.strptime() to parse your date and then just do delta with the epoch:
from datetime import datetime as dt
epoch = dt(1970, 1, 1)
date = "date Thursday June 03 12:02:56 2017"
epoch_time = int((dt.strptime(date, "date %A %B %d %H:%M:%S %Y") - epoch).total_seconds())
# 1496491376

Another solution is to create a time.struct_time structure by parsing with time.strptime() and then pass it into calendar.timegm() to convert to epoch time.
import time
import calendar
timestr = "date Thursday June 03 12:02:56 2017"
calendar.timegm(time.strptime(timestr, "date %A %B %d %H:%M:%S %Y"))
# returns 1496491376

Related

strptime() gives ValueError: time data 'AM CDT' does not match format '%p %Z'

I have a str that I want to convert to a datetime. This str is this: 'Thursday, September 9, 2021 at 11:50 AM CDT. I am using the datetime.strptime() function, but it seems like the AM or time zone is not being recognized.
When I use the code
time = 'Thursday, September 9, 2021 at 11:50 AM CDT'
time = datetime.strptime(time, '%A, %B %d, %Y at %I:%M %p %Z')
I get the following:
ValueError: time data 'Thursday, September 9, 2021 at 11:50 AM CDT' does not match format '%A, %B %d, %Y at %I:%M %p %Z:%M %p %Z'
I've been able to convert the first part up until the %p %Z part, at which I get the following error:
ValueError: time data 'AM CDT' does not match format '%p %Z'
Any ideas on how dt.strptime() can recognize AM/PM and the time zone correctly?
Not sure of the best approach, but since Python 3.9 you can use the ZoneInfo module for this:
from datetime import datetime
from zoneinfo import ZoneInfo
CDT = ZoneInfo('CST6CDT')
# or alternatively:
# CDT = ZoneInfo('US/Central')
time = 'Thursday, September 9, 2021 at 1:50 PM'
time = datetime.strptime(time, '%A, %B %d, %Y at %I:%M %p')
time = time.replace(tzinfo=CDT)
print(time) # 2021-09-09 13:50:00-05:00
#MrFuppes made a good point about an ambiguity between the CST6EDT and US/Central zones. However, when I tried a quick test in DST and outside of DST, I couldn't see any noticeable difference, as the time zone seemed to adjust automatically - which indicates that either of those zone values appear to be DST- aware (unless I'm missing something of course).
I added an example below:
from datetime import datetime
from zoneinfo import ZoneInfo
# CDT = ZoneInfo('CST6CDT')
CDT = ZoneInfo('US/Central')
# Note: DST in 2021, ends on November 7th
time_dst = 'Saturday, November 6, 2021 at 1:50 PM'
time_st = 'Monday, November 8, 2021 at 1:50 PM'
time_dst = datetime.strptime(time_dst, '%A, %B %d, %Y at %I:%M %p')
time_dst = time_dst.replace(tzinfo=CDT)
time_st = datetime.strptime(time_st, '%A, %B %d, %Y at %I:%M %p')
time_st = time_st.replace(tzinfo=CDT)
print('DST: ', time_dst)
print('ST: ', time_st)
Output appears to be the same despite which ZoneInfo object is used:
DST: 2021-11-06 13:50:00-05:00
ST: 2021-11-08 13:50:00-06:00

Python datetime strptime() does not match format

I get the following error in which you can see the time data and the format I am using
time data '20:07:35 EEDT Wed Mar 31 2021' does not match format '%H:%M:%S %Z %a %b %d %Y'
I used the directives from here and I see that the format matches the description of each directive.
Can you see what is the issue here?
import datetime
time = '20:07:35 EEDT Wed Mar 31 2021'
time = time.replace('EEDT', '+0300')
datetime.datetime.strptime(time, '%H:%M:%S %z %a %b %d %Y')
you can map the abbreviated time zone to a IANA time zone name by dateutil's parser:
import dateutil
s = '20:07:35 EEDT Wed Mar 31 2021'
tzmapping = {"EEDT": dateutil.tz.gettz('Europe/Athens'),
"EEST": dateutil.tz.gettz('Europe/Athens')} # add more if needed...
dtobj = dateutil.parser.parse(s, tzinfos=tzmapping)
that will give you
dtobj
# >>> datetime.datetime(2021, 3, 31, 20, 7, 35, tzinfo=tzfile('Europe/Athens'))
dtobj.utcoffset()
# >>> datetime.timedelta(seconds=10800) # UTC+3
Note that timedelta arithmetic works correctly, i.e. includes DST changes:
from datetime import timedelta
dtobj -= timedelta(7) # DST change: dtobj is now EEST, UTC+2
dtobj.utcoffset()
# >>> datetime.timedelta(seconds=7200)
Problem is with EEDT. If you ignore EEDT(quickfix, not ideal), then your code may look like:
text = '20:07:35 EEDT Wed Mar 31 2021';
fmt = '%H:%M:%S EEDT %a %b %d %Y';
datetime.strptime(text, fmt)
--edit--
parsing datetime with timezone is difficult to pure datetime module. I'm not big expert, but pytz or python-datetutil should be good choice, according to this page: https://medium.com/#nqbao/python-timezone-and-daylight-savings-e511a0093d0
For those who are interested in different approach for similar, like GMT and BST or EEST and EEDT, it can be represented like this:
import datetime
try:
Time1 = datetime.datetime.strptime(DropTm,"%a %b %d %H:%M:%S GMT %Y")
except:
Time1 = datetime.datetime.strptime(DropTm,"%a %b %d %H:%M:%S BST %Y")
In your situation it will be:
import datetime
try:
Time1 = datetime.datetime.strptime(SomeValue,"%H:%M:%S EEDT %a %b %d %Y")
except:
Time1 = datetime.datetime.strptime(SomeValue,"%H:%M:%S EEST %a %b %d %Y")
Where is "SomeValue" your data!!
It did worked for me and do not need any other libraries! Good Luck with coding!!!

What are the limits of %z in Python datetime?

I am trying to parse the mailing list of Apache Pig. I use the following function while parsing the dates.
from datetime import datetime
def str_to_date(date_str):
# First, remove the (UTC) type of parts at the end
try:
date_str = date_str[: date_str.index("(") - 1]
except ValueError:
pass
# Then, try different date formats
for date_format in [
"%a, %d %b %Y %H:%M:%S %z",
"%a %b %d %Y %H:%M:%S %z",
"%a %b %d %H:%M:%S %Y %z",
"%d %b %Y %H:%M:%S %z",
]:
try:
return datetime.strptime(date_str, date_format)
except ValueError:
pass
raise ValueError("No valid date format found for {}".format(date_str))
In the 201201.mbox, the following error raises:
ValueError: No valid date format found for Fri, 20 Jan 2012 16:31:14 +0580
When I inspect the mbox, I realized that it includes Date: Fri, 20 Jan 2012 16:31:14 +0580 line. So, it does not match any of the date formats in the function but the problem is +0580 should be "a 5-character string of the form +HHMM or -HHMM, where HH is a 2-digit string giving the number of UTC offset hours, and MM is a 2-digit string giving the number of UTC offset minutes" (docs)
According to the mbox, the offset of the mail date is +0580, which means plus 5 hours and 80 minutes. Isn't that wrong? Or, do I miss something?
There are only 60 minutes in an hour, so MM can't be more than 59. +0580 should be +0620.

How to fix ValueError time date does not match format when converting from string to datetime

Im trying to convert a string to datetime and keep getting the error: ValueError: time data 'Mon, 22 Apr 2019 17:04:38 +0200 (CEST)' does not match format '%a, %d %b %Y %H:%M:%S %z %Z'
from datetime import datetime
s = "Mon, 22 Apr 2019 17:04:38 +0200 (CEST)"
d = datetime.strptime(s, '%a, %d %b %Y %H:%M:%S %z %Z')
What am i missing?
%Z is generally used for converting into string format. In any case, it is the offset, not the name of the time zone.
The rest of your code is valid, however:
s = "Mon, 22 Apr 2019 17:04:38 +0200"
d = datetime.strptime(s, '%a, %d %b %Y %H:%M:%S %z')
datetime only comes with the ability to parse UTC and whatever local time zone is listed in time.tzname. It can't match (CEST) because it doesn't know what timezone that is (It would also be redundant because you defined the timezone using the offset +0200).
You will need to implement your own (CEST) using datetime.tzinfo or by importing an external library like pytz or pendulum in order to parse (CEST) from a string into a datetime.timezone.
Also, don't forget to include parenthesis() in your match string.
This code passes, however, I do not know what happens to 'CEST' once it is converted into the string.
from datetime import datetime
tz = 'CEST'
s = "Mon, 22 Apr 2019 17:04:38 +0200 " + tz
d = datetime.strptime(s, '%a, %d %b %Y %H:%M:%S %z ' + tz)

Year wrong on Epoch time conversion

I'm trying to convert epoch time using Python. However, the year always seems to be wrong. In the below example, it should be 2014.
import time
timestamp = time.strftime("%a, %d %b %Y %H:%M:%S +0000",
time.localtime(1415219530834))
What am I doing wrong?
I get this result:
Sat, 09 Jul 46816 16:20:34 +0000
You are passing time in milliseconds, but it should be in seconds. Divide it by 1000
import time
timestamp = time.strftime("%a, %d %b %Y %H:%M:%S +0000",
time.localtime(1415219530))
result:
'Wed, 05 Nov 2014 15:32:10 +0000'
You've passed a timestamp in milliseconds, but localtime expects a value in seconds.
time.localtime(1415219530834 / 1000)
Try this also
import datetime
datetime.datetime.fromtimestamp(your time in epoch/1000)
Convert time in epoch
int(yourtime.strftime("%s"))*1000
You are getting the Epoch in Milliseconds in Python. You need to get it in seconds for it to work correctly.
Something like
import time
mytime = 1415219530834
seconds = int(mytime/1000)
timestamp = time.strftime("%a, %d %b %Y %H:%M:%S +0000",time.localtime(seconds))

Categories