I want to convert timestamps to dates and truncate minute and seconds,then, convert it back to timestamps. I know I can do that with some mathematical algorithm like timestamps/60 or timestamps/3600, but I want to use Python provided functions. My code is like below. I tried two methods:
for timeStamp in lineTimeStamps:
#first try
day1=datetime.datetime.\
fromtimestamp(float(timeStamp)).strftime("%Y-%m-%d %H")
timestamp1 = time.mktime(day1)
#second try
day2=datetime.datetime.\
fromtimestamp(float(timeStamp)).\
replace( minute=0, second=0, microsecond=0).timetuple()
timestamp2 = time.mktime(day2)
In timestamp1 = time.mktime(day1), I got the error like:
TypeError: Tuple or struct_time argument required
In timestamp2 = time.mktime(day2), I got the error like:
OverflowError: mktime argument out of range
How can I convert it back to timestamps?
Converting to string and back is very slow operation. I'd suggest to use that math algorithm anyway:
# get integer and fractional parts of modulo division
i, f = divmod(timeStamp, 3600)
# i * 3600 is hour:0:0 time
timestamp1 = i * 3600
And that's it.
strftime("%Y-%m-%d %H") returns a string.
If you want to go down this path, you should call strptime to turn it back into a time object:
day1=datetime.datetime.\
fromtimestamp(float(timeStamp)).strftime("%Y-%m-%d %H")
day1 = time.strptime(day1, "%Y-%m-%d %H")
timestamp1 = time.mktime(day1)
I would recommend #baldr approach though...
Related
I must add a given number of hours:minutes to a date, once I try to format the method string parameter to datetime I am getting the below error:
myTime = '236:22'
myTime_str = '%H:%M'
myTime_time = datetime.strptime(myTime, myTime_str )
ValueError: time data '236:22' does not match format '%H:%M'
As I couldn't find a strptime() format code that allows the hour (236) to be greater than 23 I am wondering if is there other function or library, rather than datetime, that would help me to before addressing it "in the method"
I have solved the requirement as follows:
duration_hours = int(duration.split(':')[0])
duration_minutes = int(duration.split(':')[1])
new_time = given_time + timedelta(hours=duration_hours,minutes=duration_minutes)'
The following ValueError is being raised while running the following code. The date is passed as a string from an another component, where I need to strip out the time.
ValueError: time data '2022-03-24T14:02:24.6413975' does not match format '%Y-%m-%d %H:%M:%S,%f'
The code:
from datetime import datetime
date='2022-03-24T14:02:24.6413975'
time = datetime.strptime(date, "%Y-%m-%d %H:%M:%S,%f")
if time > '09:30' :
print("do some thing")
The primary issue you're facing is the datetime format, as explained by the error message.
The .%f formatter can only accept six decimal places.
The T is missing from the format string.
There is a comma before the %f formatter, where there should be a full stop.
Therefore, this is the formatting string you need:
'%Y-%m-%dT%H:%M:%S.%f'
Additionally, time can be parsed from a datetime object simply by calling the .time() function as follows. String parsing should not be used, if at all possible.
time = dt.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%f").time()
Next, the if statement should compare datetime object to datetime object, as:
if time > dt.time(9,30):
...
Therefore, the complete solution is as follows:
import datetime as dt
# Fractional seconds reduced to 6 decimal places.
date = '2022-03-24T14:02:24.641397'
# Use the .time() function to extract the time.
time = dt.datetime.strptime(date, '%Y-%m-%dT%H:%M:%S.%f').time()
# Compare datetime object to datetime object.
if time > dt.time(9,30):
print('Do some stuff ...')
This should work:
import datetime
date='2022-03-24T14:02:24.6413975'
time = date.split('T')[1].split(':')
time = datetime.time(int(time[0]), int(time[1]), int(time[2].split('.')[0]), int(time[2].split('.')[1][:6]))
if time > datetime.time(9, 30) :
print("do some thing")
Output:
do some thing
This just takes date, splits it T, splits the second part of the resulting string at every :, and passes all of them to datetime.time. The last two arguments to datetime.time have to be split a the decimal to get the microseconds, and the last one has to be shortened because of the limit on how long datetime allows microseconds to be.
Hope this may help:
def stripTime():
date='2022-03-24T14:02:24.6413975'
date=date.split('T')
print(date[1])
stripTime()
Output:
14:02:24.6413975
I am using datetime.strptime() to convert a string containing time and date from a sensor into a datetime object.
The code sometimes fails. Minimal example:
datetime.strptime('1/9/2021 24:01:53', '%d/%m/%Y %H:%M:%S')
Output error:
ValueError: time data '1/9/2021 24:01:53' does not match format '%d/%m/%Y %H:%M:%S'
I am guessing this has to do with the fact that the time is more than 23:59:59 - which seems to me a non-realistic time (I would think that 1/9/2021 24:01:53 could potentially be 2/9/2021 00:01:53 - a time format which I have never seen).
Is this a non-standard way of representing time or possibly a hardware/software issue with the sensor acquisition system? If it is a different way of representing time, how can I convert it to a standard datetime object?
Kind regards,
D.F.
If the hour exceeds 23 in a variable representing time, a good option is to create a timedelta from it, which you can then add to a datetime object. For given example that might look like
from datetime import datetime, timedelta
def custom_todatetime(s):
"""
split date/time string formatted as 'DD/MM/YYYY hh:mm:ss' into date and time parts.
parse date part to datetime and add time part as timedelta.
"""
parts = s.split(' ')
seconds = sum(int(x) * 60 ** i for i, x in enumerate(reversed(parts[1].split(':'))))
return datetime.strptime(parts[0], "%d/%m/%Y") + timedelta(seconds=seconds)
s = '1/9/2021 24:01:53'
print(custom_todatetime(s))
# 2021-09-02 00:01:53
Note: conversion of hh:mm:ss to seconds taken from here - give a +1 there if helpful.
Hi I have two times in slightly different formats and I need to work out the difference. The first was parsed from a ISO 8601 date using dateutil.parser
I'm not sure what I need to do to parse them into the same format, but my two dates are:
2017-05-24 15:40:00+00:00
2017-05-24 14:23:44.995015
If they were both in datetime format I could just subtract one from the other, so I need to chop the milliseconds off both (coz that's not relevant to me), and tell python the new strings are both datetimes?
Since you're already using dateutil, what's wrong with just removing the timezone (or adding it to the other) and subtracting them?
import dateutil.parser
date1 = dateutil.parser.parse("2017-05-24 15:40:00+00:00").replace(tzinfo=None)
date2 = dateutil.parser.parse("2017-05-24 14:23:44.995015")
date_delta = date1 - date2 # 1:16:15.004985
You can call replace(microsecond=0) on your dates to remove the microseconds.
You could transform the second datetime (that is a timestamp) into the first one with this code:
def convert_to_timestamp(string_date):
the_datetime = datetime.strptime(string_date.decode("utf-8"), "%Y%m%d.%H%M%S.%f")
return time.mktime(the_datetime.timetuple()) * 1e6 + the_datetime.microsecond
or:
def transformTimestamps(timestamp_):
year = timestamp_[:4]
month = timestamp_[4:6]
day = timestamp_[6:8]
hour = timestamp_[9:11]
minute = timestamp_[11:13]
second = timestamp_[13:15]
microsecond = timestamp_[16:22]
myformat = year+"-"+month+"-"+day+" +hour+":"+minute+":"+second+":"+microsecond
return datetime.strptime(myformat, '%Y-%m-%d %H:%M:%S:%f')
Then, you can calculate the difference between them.
I hope this help. Greetings!
Probably you want to use this method
datetime.strptime(date_string, format)
Also remember you can get rid of elements you do not want in your date (Like milliseconds) when you declare the specified date, as in
class datetime.datetime(year, month, day, hour=0, minute=0, second=0,
microsecond=0, tzinfo=None, *, fold=0)
For more on this topic you can always read the python docs, you can find the same information I just gave you and more here:
https://docs.python.org/3/library/datetime.html
Hope it helped.
I have a log file with timestamps like "2012-05-12T13:04:35.347-07:00". I want to convert each timestamp into a number so that I sort them by ascending order based on time.
How can I do this in Python? In Java I found out that I can convert timestamps for such format with SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ") but for Python I couldn't find anything.
As py2.x has issues with the %z directive you've to do something like this:
from datetime import timedelta,datetime
strs = "2012-05-12T13:04:35.347-07:00"
#replace the last ':' with an empty string, as python UTC offset format is +HHMM
strs = strs[::-1].replace(':','',1)[::-1]
As datetime.striptime doesn't supports %z(UTC offset)(at least not in py2.x), so you need a work around:
#Snippet taken from http://stackoverflow.com/a/526450/846892
try:
offset = int(strs[-5:])
except:
print "Error"
delta = timedelta(hours = offset / 100)
Now apply formatting to : '2012-05-12T13:04:35.347'
time = datetime.strptime(strs[:-5], "%Y-%m-%dT%H:%M:%S.%f")
time -= delta #reduce the delta from this time object
print time
#2012-05-12 20:04:35.347000