so i am taking the difference between two times (2022-07-20 23:10:00.990000) and 2022-07-20 23:10:02.100000. that gives me back 0:00:01.110000. i want that to transform to HH:MM:SS without the microseconds. the easiest way to take off microseconds is to do "avg_inqueue_time = str(avg_inqueue_time).split(".")[0]" that will give me 0:00:01. then i try to do avg_inqueue_time_transformed = datetime.strptime('%H:%M:%S', avg_inqueue_time) but gives an error ValueError: time data '%H:%M:%S' does not match format '0:02:07'.
any ideas how to transform that?
I believe a fast approach is to take a different time delta object where you only pick the seconds.
import datetime
time_1 = datetime.datetime(2022,7,20,23,10,00,990000)
time_2 = datetime.datetime(2022,7,20,23,10,2,100000)
timediff=(time_2-time_1)
timediff_wo_microseconds = datetime.timedelta(days=timediff.days, seconds=timediff.seconds)
print(timediff_wo_microseconds)
Related
I have 1000 of UYC timestamps in csv file, I want to convert it into date and time but I am only interested in second like
Timestamps= 1666181576.26295,
1666181609.54292
19/10/2022 15:45:25.34568
from that I only have interest in 25.34568 seconds, also the numbers after points. How can I get this type of conversion in python? Mostly the search on the internet is interested in conversation from UTC to time and date but I also want precision in seconds.
from datetime import datetime
from decimal import Decimal
ts = 1666181576.26295
timestamp = datetime.fromtimestamp(ts)
result = timestamp.second + Decimal(timestamp.microsecond)/1000000
print(result)
Will result in 56.26295
You can use datetime,
from datetime import datetime
ts = 1666181576.26295
mseconds = datetime.utcfromtimestamp(ts).microsecond
Simplest way I can see to do this is by splitting the timestamp to output everything from seconds onwards
timestamp = 1666181609.54292
temp = datetime.utcfromtimestamp(timestamp)
output = str(temp)
print(output[17:])
I have a time string obtained from API, it's UTC+0. I would like to change to other time zone.
I have tried below but it doesn't work. Could you please give me some idea ? many thanks.
utc0time='2021-04-17T15:50:14.614646+00:00'
dt = datetime.strptime('utc0time', '%Y-%m-%dT%H:%M:%S%z'). #it results as an error, not match the format
time.mktime(dt.timetuple())
calendar.timegm(dt.timetuple())
You could actually use timedelta in datetime module to +/- number of hours to achieve the time in other timezone you wish.
Here is an example where you can use timedelta:
https://www.geeksforgeeks.org/python-datetime-timedelta-function/
thanks for the comments and it gave me the idea. Because i only need to convert from one time zone to another one, i don't need to convert to multi-timezone. I don't use pytz this time. I used a silly method, changed the str to timestamp first, then used timedelta to adjust the hours. Below is my final code.
utc0time='2021-04-17T15:50:14.614646+00:00'
utc0time = utc0time[:-13]
timestamp = time.mktime(time.strptime(utc0time, '%Y-%m-%dT%H:%M:%S'))
datatimeformat = datetime.fromtimestamp(timestamp)
utc8time = datatimeformat + timedelta(hours = 8)
As I need to transform the time format hh:mm to the count of minutes. Doing the maths would be:
hh*60+mm
I came across with the method mktime, which I thought it was to get the EPOCH time in minutes of a given datetimeobject. In this way I convert from datetimeto integer.
I wrote this in order to calculate the time of the day in minutes:
(the outputs in the boxes below)
a = datetime.now()
print type(a)
print a
b= '2017-07-05 00:00:00.0'
b = datetime.strptime(b, "%Y-%m-%d %H:%M:%S.%f")
type(b)
out: type 'datetime.datetime'
out: type 'datetime.datetime'
As both objects are datetime I used the method mktime that I found searching for a way to transform from timestamp to integer
timestamp = int(time.mktime(a.timetuple()))-int(time.mktime(b.timetuple()))
type(timestamp)
print timestamp
print a
print b
And the output is:
out: 62189
out: 2017-07-05 17:16:29.134026
out: 2017-07-05 00:00:00
So the value 62189is seconds? Am I using EPOCH time?
(sorry if the question is bit fool, I am quite beginner, that's why I need help)
Thanks!
I would come with a solution like:
import time
t = time.strptime("02:24", "%H:%M") # Replace "02:24" with your string parameter
nb_minutes = t.tm_hour * 60 + t.tm_min
This transforms the time format "hh:mm" to the count of minutes as requested in your OP.
Note that there is a distrinction between a date, a datetime and a time. Python offers more flexibility than Javascript Date() on this.
Basically:
date = YYYY-MM-DD
time = HH:MM:ss
datetime = YYYY-MM-DD HH:MM:ss
Depending of your calculations, using the correct object in the python library will save you efforts all while making your source code more semantically readable.
My time format is screwy, but it seemed workable, as a string with the following format:
'47:37:00'
I tried to set a variable where:
DT = '%H:%M:%S'
So I could find the difference between two times, but it's given me the following error:
ValueError: time data '47:37:00' does not match format '%H:%M:%S'
Is it possible there are more elements to my time stamps than I thought? Or that it's formatted in minutes/seconds/milliseconds? I can't seem to find documentation that would help me determine my time format so I could set DT and do arithmetic on it.
It's because you set 47 to %H, that is not a proper value.
Here is an example:
import datetime
dt = datetime.datetime.strptime('2016/07/28 12:37:00','%Y/%m/%d %H:%M:%S')
print dt
Output: 2016-07-28 12:37:00
You wrote "I can't seem to find documentation that would help me determine my time format so I could set DT and do arithmetic on it"
Try this: https://docs.python.org/3/library/datetime.html
Way down to the bottom.
And yes, when the %H is matched with 47, you get boom the error.
I want to compare only time part in datetime. I have different dates with only time field to compare. Since dates are different and only time part i want to consider So i think creating two datetime object will not help.
my string as
start="22:00:00"
End="03:00:00"
Tocompare="23:30:00"
Above are strings when i convert them with datetime as
dt=datetime.strptime(start,"%H:%M:%S")
it gives
1900-01-01 22:00:00
which is default date in python.
So i need to avoid all this and want only time part. I simply need to check does my Tocompare falls between start and End
Just call the .time() method of the datetime objects to get their hours, minutes, seconds and microseconds.
dt = datetime.strptime(start,"%H:%M:%S").time()
Compare their times using datetime.time().
import datetime
start = datetime.datetime.strptime(start, '%H:%M:%S')
start = datetime.time(start.hour, start.minute,start.second)
tocompare = datetime.datetime.strptime(tocompare, '%H:%M:%S')
tocompare = datetime.time(tocompare.hour, tocompare.minute, tocompare.second)
start > tocompare # False