I've got a timedelta. I want the days, hours and minutes from that - either as a tuple or a dictionary... I'm not fussed.
I must have done this a dozen times in a dozen languages over the years but Python usually has a simple answer to everything so I thought I'd ask here before busting out some nauseatingly simple (yet verbose) mathematics.
Mr Fooz raises a good point.
I'm dealing with "listings" (a bit like ebay listings) where each one has a duration. I'm trying to find the time left by doing when_added + duration - now
Am I right in saying that wouldn't account for DST? If not, what's the simplest way to add/subtract an hour?
If you have a datetime.timedelta value td, td.days already gives you the "days" you want. timedelta values keep fraction-of-day as seconds (not directly hours or minutes) so you'll indeed have to perform "nauseatingly simple mathematics", e.g.:
def days_hours_minutes(td):
return td.days, td.seconds//3600, (td.seconds//60)%60
This is a bit more compact, you get the hours, minutes and seconds in two lines.
days = td.days
hours, remainder = divmod(td.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
# If you want to take into account fractions of a second
seconds += td.microseconds / 1e6
days, hours, minutes = td.days, td.seconds // 3600, td.seconds // 60 % 60
As for DST, I think the best thing is to convert both datetime objects to seconds. This way the system calculates DST for you.
>>> m13 = datetime(2010, 3, 13, 8, 0, 0) # 2010 March 13 8:00 AM
>>> m14 = datetime(2010, 3, 14, 8, 0, 0) # DST starts on this day, in my time zone
>>> mktime(m14.timetuple()) - mktime(m13.timetuple()) # difference in seconds
82800.0
>>> _/3600 # convert to hours
23.0
For all coming along and searching for an implementation:
The above posts are related to datetime.timedelta, which is sadly not having properties for hours and seconds. So far it was not mentioned, that there is a package, which is having these. You can find it here:
Check the source: https://github.com/andrewp-as-is/timedelta.py
Available via pip: https://pypi.org/project/timedelta/
Example - Calculation:
>>> import timedelta
>>> td = timedelta.Timedelta(days=2, hours=2)
# init from datetime.timedelta
>>> td = timedelta.Timedelta(datetime1 - datetime2)
Example - Properties:
>>> td = timedelta.Timedelta(days=2, hours=2)
>>> td.total.seconds
180000
>>> td.total.minutes
3000
>>> td.total.hours
50
>>> td.total.days
2
I hope this could help someone...
I don't understand
days, hours, minutes = td.days, td.seconds // 3600, td.seconds // 60 % 60
how about this
days, hours, minutes = td.days, td.seconds // 3600, td.seconds % 3600 / 60.0
You get minutes and seconds of a minute as a float.
I used the following:
delta = timedelta()
totalMinute, second = divmod(delta.seconds, 60)
hour, minute = divmod(totalMinute, 60)
print(f"{hour}h{minute:02}m{second:02}s")
Here is a little function I put together to do this right down to microseconds:
def tdToDict(td:datetime.timedelta) -> dict:
def __t(t, n):
if t < n: return (t, 0)
v = t//n
return (t - (v * n), v)
(s, h) = __t(td.seconds, 3600)
(s, m) = __t(s, 60)
(micS, milS) = __t(td.microseconds, 1000)
return {
'days': td.days
,'hours': h
,'minutes': m
,'seconds': s
,'milliseconds': milS
,'microseconds': micS
}
Here is a version that returns a tuple:
# usage: (_d, _h, _m, _s, _mils, _mics) = tdTuple(td)
def tdTuple(td:datetime.timedelta) -> tuple:
def _t(t, n):
if t < n: return (t, 0)
v = t//n
return (t - (v * n), v)
(s, h) = _t(td.seconds, 3600)
(s, m) = _t(s, 60)
(mics, mils) = _t(td.microseconds, 1000)
return (td.days, h, m, s, mics, mils)
While pandas.Timedelta does not provide these attributes directly, it indeed provide a method called total_seconds, based on which days, hours, and minutes can be easily derived:
import pandas as pd
td = pd.Timedelta("2 days 12:30:00")
minutes = td.total_seconds()/60
hours = minutes/60
days = hours/ 24
print(minutes, hours, days)
I found the easiest way is using str(timedelta). It will return a sting formatted like 3 days, 21:06:40.001000, and you can parse hours and minutes using simple string operations or regular expression.
While if you are using python datetime package, you can also code like below:
import datetime
tap_in = datetime.datetime.strptime("04:12", "%H:%M")
tap_out = datetime.datetime.strptime("18:20", "%H:%M")
num_of_hour = (tap_out - tap_in).total_seconds()/3600
num_of_hour # 14.133333333333333
This is another possible approach, though a bit wordier than those already mentioned. It maybe isn't the best approach for this scenario but it is handy to be able to obtain your time duration in a specific unit that isn't stored within the object (weeks, hours, minutes, milliseconds) and without having to remember or calculate conversion factors.
from datetime import timedelta
one_hour = timedelta(hours=1)
one_minute = timedelta(minutes=1)
print(one_hour/one_minute) # Yields 60.0
I've got a timedelta. I want the days, hours and minutes from that - either as a tuple or a dictionary... I'm not fussed.
in_time_delta = timedelta(days=2, hours=18, minutes=30)
td_d = timedelta(days=1)
td_h = timedelta(hours=1)
td_m = timedelta(minutes=1)
dmh_list = [in_time_delta.days,
(in_time_delta%td_d)//td_h,
(in_time_delta%td_h)//td_m]
Which should assign [2, 18, 30] to dmh_list
If using pandas (at least version >1.0), the Timedelta class has a components attribute that returns a named tuple with all the fields nicely laid out.
e.g.
import pandas as pd
delta = pd.Timestamp("today") - pd.Timestamp("2022-03-01")
print(delta.components)
timedelta = pd.Timestamp("today") - pd.Timestamp("2022-01-01")
print(timedelta.components)
print(timedelta.components.days)
print(timedelta.components.seconds)
will return something like:
Components(days=281, hours=2, minutes=24, seconds=3, milliseconds=72, microseconds=493, nanoseconds=0)
281
3
timedeltas have a days and seconds attribute .. you can convert them yourself with ease.
Related
I get as input times (hh:mm:ss) as float - e.g. 18:52:18 is a float 0.786331018518518.
I achieved to calculate the time from a float value with this code:
val = 0.786331018518518
hour = int(val*24)
minute = int((val*24-hour)*60)
seconds = int(((val*24-hour)*60-minute)*60)
print(f"{hour}:{minute}:{seconds}")
How is it possible to calculate the float value from a time - so from 18:52:18 to 0.786331018518518?
And generally isn't there an easier way to convert in both directions between float and time (I was thinking about the datetime module but was not able to find anything for this)?
What you have is an amount of time, in day unit
You can use datetime.timedelta that represents a duration
val = 0.786331018518518
d = timedelta(days=val)
print(d) # 18:52:19
val = d.total_seconds() / 86400
print(val) # 0.7863310185185185
From your hour/minute/seconds variables it would be
print(hour / 24 + minute / 1440 + seconds / 86400)
I'm assuming the float-value is the time in days.
To get the float-value from time, you can convert each of them (hours, minutes, seconds) to days and add them all up.
So hours/24+minutes/60/24+seconds/60/60/24 would work.
I'm trying to figure out a way to take two times from the same day and figure out the difference between them. So far shown in the code below I have converted both of the given times into Int Vars and split the strings to retrieve the information. This works well but when the clock in values minute is higher than the clock out value it proceeds to give a negative value in minute slot of the output.
My current code is:
from datetime import datetime
now = datetime.now()
clocked_in = now.strftime("%H:%M")
clocked_out = '18:10'
def calc_total_hours(clockedin, clockedout):
in_hh, in_mm = map(int, clockedin.split(':'))
out_hh, out_mm = map(int, clockedout.split(':'))
hours = out_hh - in_hh
mins = out_mm - in_mm
return f"{hours}:{mins}"
print(calc_total_hours(clocked_in, clocked_out))
if the clocked in value is 12:30 and the clocked out value is 18:10
the output is:
6:-20
the output needs to be converted back into a stand time format when everything is done H:M:S
Thanks for you assistance and sorry for the lack of quality code. Im still learning! :D
First, in order to fix your code, you need to convert both time to minutes, compute the difference and then convert it back to hours and minutes:
clocked_in = '12:30'
clocked_out = '18:10'
def calc_total_hours(clockedin, clockedout):
in_hh, in_mm = map(int, clockedin.split(':'))
out_hh, out_mm = map(int, clockedout.split(':'))
diff = (in_hh * 60 + in_mm) - (out_hh * 60 + out_mm)
hours, mins = divmod(abs(diff) ,60)
return f"{hours}:{mins}"
print(calc_total_hours(clocked_in, clocked_out))
# 5: 40
Better way to implement the time difference:
import time
import datetime
t1 = datetime.datetime.now()
time.sleep(5)
t2 = datetime.datetime.now()
diff = t2 - t1
print(str(diff))
Output:
#h:mm:ss
0:00:05.013823
Probably the most reliable way is to represent the times a datetime objects, and then take one from the other which will give you a timedelta.
from datetime import datetime
clock_in = datetime.now()
clock_out = clock_in.replace(hour=18, minute=10)
seconds_diff = abs((clock_out - clock_in).total_seconds())
hours, minutes = seconds_diff // 3600, (seconds_diff // 60) % 60
print(f"{hours}:{minutes}")
I tried to calculate how many hours between 10:49 and 16:38 by the type of float.
import datetime
t1 = datetime.time(10,49,00)
t2 = datetime.time(16,38,00)
t = (t2 - t1).hours
You can calculate like that :
from datetime import datetime, timedelta
t1 = datetime.strptime("10:49:00", "%H:%M:%S")
t2 = datetime.strptime("16:38:00", "%H:%M:%S")
t = (t2 - t1)
print t.total_seconds() / 3600
print timedelta(days=0, seconds=t.seconds, microseconds=t.microseconds)
print ceil(t.total_seconds() / 3600)
Output:
5.81666666667
5:49:00
6.0
And get hour from timedelta :
x = timedelta(days=0, seconds=t.seconds, microseconds=t.microseconds)
print x.seconds//3600
Output is 5.
It's a simple typo. You're calling for .hours but actually the keyword is .hour
Try going with t = t2.hour - t1.hour
From Python interpreter:
>>> t1 = datetime.time(10,49,00)
>>> t2 = datetime.time(16,38,00)
>>> t = t2.hour - t1.hour
>>> t
6
But just so you know, if you want to use the .hour keyword, it's useless to use a float, because it will always return x.0 because whatever full hours you decide to subtract, you will never get a float that isn't .0
datetime.time does not support the subtraction operator. Since you already have the hours and minutes as integers, you might as well calculate the difference yourself.
print(((16 * 60 + 38) - (10 * 60 + 49)) / 60)
This outputs:
5.816666666666666
You can have a very similar solution to your proposal by using datetime.datetime and adding a dummy date (e.g. the first day of this year). Furthermore, you may only ask for days and seconds.
import datetime
t1 = datetime.datetime(2018,1,1,10,49,00)
t2 = datetime.datetime(2018,1,1,16,38,00)
difference = (t2 - t1)
difference.days * 24 + difference.seconds // 3600
> 5
What would be an elegant, efficient and Pythonic way to perform a h/m/s rounding operation on time related types in Python with control over the rounding resolution?
My guess is that it would require a time modulo operation. Illustrative examples:
20:11:13 % (10 seconds) => (3 seconds)
20:11:13 % (10 minutes) => (1 minutes and 13 seconds)
Relevant time related types I can think of:
datetime.datetime \ datetime.time
struct_time
For a datetime.datetime rounding, see this function:
https://stackoverflow.com/a/10854034/1431079
Sample of use:
print roundTime(datetime.datetime(2012,12,31,23,44,59,1234),roundTo=60*60)
2013-01-01 00:00:00
How about use datetime.timedeltas:
import time
import datetime as dt
hms=dt.timedelta(hours=20,minutes=11,seconds=13)
resolution=dt.timedelta(seconds=10)
print(dt.timedelta(seconds=hms.seconds%resolution.seconds))
# 0:00:03
resolution=dt.timedelta(minutes=10)
print(dt.timedelta(seconds=hms.seconds%resolution.seconds))
# 0:01:13
This will round up time data to a resolution as asked in the question:
import datetime as dt
current = dt.datetime.now()
current_td = dt.timedelta(
hours = current.hour,
minutes = current.minute,
seconds = current.second,
microseconds = current.microsecond)
# to seconds resolution
to_sec = dt.timedelta(seconds = round(current_td.total_seconds()))
print(dt.datetime.combine(current, dt.time(0)) + to_sec)
# to minute resolution
to_min = dt.timedelta(minutes = round(current_td.total_seconds() / 60))
print(dt.datetime.combine(current, dt.time(0)) + to_min)
# to hour resolution
to_hour = dt.timedelta(hours = round(current_td.total_seconds() / 3600))
print(dt.datetime.combine(current, dt.time(0)) + to_hour)
You can convert both times to seconds, do the modulo operati
from datetime import time
def time2seconds(t):
return t.hour*60*60+t.minute*60+t.second
def seconds2time(t):
n, seconds = divmod(t, 60)
hours, minutes = divmod(n, 60)
return time(hours, minutes, seconds)
def timemod(a, k):
a = time2seconds(a)
k = time2seconds(k)
res = a % k
return seconds2time(res)
print(timemod(time(20, 11, 13), time(0,0,10)))
print(timemod(time(20, 11, 13), time(0,10,0)))
Outputs:
00:00:03
00:01:13
I use following code snippet to round to the next hour:
import datetime as dt
tNow = dt.datetime.now()
# round to the next full hour
tNow -= dt.timedelta(minutes = tNow.minute, seconds = tNow.second, microseconds = tNow.microsecond)
tNow += dt.timedelta(hours = 1)
I think I'd convert the time in seconds, and use standard modulo operation from that point.
20:11:13 = 20*3600 + 11*60 + 13 = 72673 seconds
72673 % 10 = 3
72673 % (10*60) = 73
This is the easiest solution I can think about.
Here is a lossy* version of hourly rounding:
dt = datetime.datetime
now = dt.utcnow()
rounded = dt.utcfromtimestamp(round(now.timestamp() / 3600, 0) * 3600)
Same principle can be applied to different time spans.
*The above method assumes UTC is used, as any timezone information will be destroyed in conversion to timestamp.
You could also try pandas.Timestamp.round:
import datetime
import pandas as pd
t = datetime.datetime(2012,12,31,23,44,59,1234)
print(pd.to_datetime(t).round('1min'))
% Timestamp('2012-12-31 23:45:00')
You can perform the following if you want to change the result back to datetime format:
pd.to_datetime(t).round('1min').to_pydatetime()
% datetime.datetime(2012, 12, 31, 23, 45)
There has to be an easier way to do this. I have objects that want to be refreshed every so often, so I want to record when they were created, check against the current timestamp, and refresh as necessary.
datetime.datetime has proven to be difficult, and I don't want to dive into the ctime library. Is there anything easier for this sort of thing?
if you want to compute differences between two known dates, use total_seconds like this:
import datetime as dt
a = dt.datetime(2013,12,30,23,59,59)
b = dt.datetime(2013,12,31,23,59,59)
(b-a).total_seconds()
86400.0
#note that seconds doesn't give you what you want:
(b-a).seconds
0
import time
current = time.time()
...job...
end = time.time()
diff = end - current
would that work for you?
>>> from datetime import datetime
>>> a = datetime.now()
# wait a bit
>>> b = datetime.now()
>>> d = b - a # yields a timedelta object
>>> d.seconds
7
(7 will be whatever amount of time you waited a bit above)
I find datetime.datetime to be fairly useful, so if there's a complicated or awkward scenario that you've encountered, please let us know.
EDIT: Thanks to #WoLpH for pointing out that one is not always necessarily looking to refresh so frequently that the datetimes will be close together. By accounting for the days in the delta, you can handle longer timestamp discrepancies:
>>> a = datetime(2010, 12, 5)
>>> b = datetime(2010, 12, 7)
>>> d = b - a
>>> d.seconds
0
>>> d.days
2
>>> d.seconds + d.days * 86400
172800
We have function total_seconds() with Python 2.7
Please see below code for python 2.6
import datetime
import time
def diffdates(d1, d2):
#Date format: %Y-%m-%d %H:%M:%S
return (time.mktime(time.strptime(d2,"%Y-%m-%d %H:%M:%S")) -
time.mktime(time.strptime(d1, "%Y-%m-%d %H:%M:%S")))
d1 = datetime.now()
d2 = datetime.now() + timedelta(days=1)
diff = diffdates(d1, d2)
Here's the one that is working for me.
from datetime import datetime
date_format = "%H:%M:%S"
# You could also pass datetime.time object in this part and convert it to string.
time_start = str('09:00:00')
time_end = str('18:00:00')
# Then get the difference here.
diff = datetime.strptime(time_end, date_format) - datetime.strptime(time_start, date_format)
# Get the time in hours i.e. 9.60, 8.5
result = diff.seconds / 3600;
Hope this helps!
Another approach is to use timestamp values:
end_time.timestamp() - start_time.timestamp()
By reading the source code, I came to a conclusion: the time difference cannot be obtained by .seconds:
#property
def seconds(self):
"""seconds"""
return self._seconds
# in the `__new__`, you can find the `seconds` is modulo by the total number of seconds in a day
def __new__(cls, days=0, seconds=0, microseconds=0,
milliseconds=0, minutes=0, hours=0, weeks=0):
seconds += minutes*60 + hours*3600
# ...
if isinstance(microseconds, float):
microseconds = round(microseconds + usdouble)
seconds, microseconds = divmod(microseconds, 1000000)
# ! 👇
days, seconds = divmod(seconds, 24*3600)
d += days
s += seconds
else:
microseconds = int(microseconds)
seconds, microseconds = divmod(microseconds, 1000000)
# ! 👇
days, seconds = divmod(seconds, 24*3600)
d += days
s += seconds
microseconds = round(microseconds + usdouble)
# ...
total_seconds can get an accurate difference between the two times
def total_seconds(self):
"""Total seconds in the duration."""
return ((self.days * 86400 + self.seconds) * 10**6 +
self.microseconds) / 10**6
in conclusion:
from datetime import datetime
dt1 = datetime.now()
dt2 = datetime.now()
print((dt2 - dt1).total_seconds())