Rounding time in Python - python

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)

Related

How to find the difference between two times

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}")

Generate random list of timestamps in python

I'm trying to generate random list of 24hr timestamps. I can generate one sample of date and time between the set range using the code below. I'm hoping to generate multiple samples (e.g. 10 samples)
Also, the date component isn't a priority for me. If i could drop that and just generate random 24hr timestamps that would be good.
Most threads I've found only consider generate random dates. I can find anything that concerns time.
import random
import time
from datetime import datetime
def randomDate(start, end):
frmt = '%d-%m-%Y %H:%M:%S'
stime = time.mktime(time.strptime(start, frmt))
etime = time.mktime(time.strptime(end, frmt))
ptime = stime + random.random() * (etime - stime)
dt = datetime.fromtimestamp(time.mktime(time.localtime(ptime)))
return dt
random_datetime = randomDate("20-01-2018 13:30:00", "23-01-2018 04:50:34")
print(random_datetime)
Output:
2018-01-21 03:33:55
The whole point of the datetime library: datetime, timedelta, etc. objects act as much like numbers as possible, so you can just do arithmetic on them.
So, to generate a uniform distribution over a day, just take a uniform distribution from 0.0 to 1.0, and multiply it by a day:1
td = random.random() * datetime.timedelta(days=1)
To generate a uniform random time on some particular day, just add that to midnight on that day:
dt = datetime.datetime(2018, 5, 1) + random.random() * datetime.timedelta(days=1)
To generate a random timestamp between two timestamps:
dt = random.random() * (end - start) + start
And if you want 10 of those:
[random.random() * (end - start) + start for _ in range(10)]
That's all there is to it. Stay away from all those other time formats from the time module; they're only needed if you need compatibility with stuff like C libraries and filesystem data. Just use datetime in the first place:
def randomtimes(stime, etime, n):
frmt = '%d-%m-%Y %H:%M:%S'
stime = datetime.datetime.strptime(start, frmt)
etime = datetime.datetime.strptime(end, frmt)
td = etime - stime
return [random.random() * td + stime for _ in range(n)]
1. However, keep in mind that if you're dealing with local rather than UTC times, some days are actually 23 or 25 hours long, because of Daylight Saving Time. A timedelta doesn't understand that.
Depending on your needs, it might be well worth the trouble to learn the Python datetime and time modules. If your code will do lots of different manipulations, then go with #abarnert's answer. If all you need is a time string (rather than a Python timestamp), this function will crank it out for you:
import random
def randomTime():
# generate random number scaled to number of seconds in a day
# (24*60*60) = 86,400
rtime = int(random.random()*86400)
hours = int(rtime/3600)
minutes = int((rtime - hours*3600)/60)
seconds = rtime - hours*3600 - minutes*60
time_string = '%02d:%02d:%02d' % (hours, minutes, seconds)
return time_string
for i in range(10):
print(randomTime())
this outputs:
19:07:31
16:32:00
02:01:30
20:31:21
20:20:26
09:49:12
19:38:42
10:49:32
13:13:36
15:02:54
But if you don't want 24 hour time, then you can intercept the 'hours' variable before you stuff it in into the string:
import random
def randomTime():
# generate random number scaled to number of seconds in a day
# (24*60*60) = 86,400
rtime = int(random.random()*86400)
hours = int(rtime/3600)
minutes = int((rtime - hours*3600)/60)
seconds = rtime - hours*3600 - minutes*60
# figure out AM or PM
if hours >= 12:
suffix = 'PM'
if hours > 12:
hours = hours - 12
else:
suffix = 'AM'
time_string = '%02d:%02d:%02d' % (hours, minutes, seconds)
time_string += ' ' + suffix
return time_string
for i in range(10):
print(randomTime())
which gives :
05:11:45 PM
02:28:44 PM
08:09:19 PM
02:52:30 PM
07:40:02 PM
03:55:16 PM
03:48:44 AM
12:35:43 PM
01:32:51 PM
07:54:26 PM
In case you need continuous times:
from datetime import datetime,timedelta
time_starter = datetime.strptime("12:00:00","%H:%M:%S")
for i in range(1,10):
time = time_starter + timedelta(hours=i)
time = time.strftime("%H:%M:%S")
print(time)
if you need random or continuous minutes use:
time = time_starter + timedelta(minutes=i) #continuous
time = time_starter + timedelta(minutes=randint(0,60)) #random
import random
import time
from datetime import datetime
dates = []
def randomDate(start, end):
frmt = '%d-%m-%Y %H:%M:%S'
stime = time.mktime(time.strptime(start, frmt))
etime = time.mktime(time.strptime(end, frmt))
ptime = stime + random.random() * (etime - stime)
dt = datetime.fromtimestamp(time.mktime(time.localtime(ptime)))
return dt
for i in range(0 , 10)
dates.append(randomDate("20-01-2018 13:30:00", "23-01-2018 04:50:34"))
Your dates will have a list of 10 sample date :)
Good Luck

Convert Date (YYYY MM DD HH:MM:SS) to Decimal Day (YYYY MM DD.ddddd)

I have a script built that takes a date and time of an observation at a telescope and converts it to just a date with a decimal day. This script also takes in half the exposure time and adds it to the decimal day so that I can get the most accurate astrometric calculation. This is then submitted to The Minor Planet Center that only accepts the time of observation in Year Month Day.XXXXX What is the most accurate way to 5 decimal points to do this? This is the current way I use, yes it is very messy but it does get me the decimal day. Occasionally it is off by one second in the time conversion.
expmpc = float.("10")
utcstartmpc = "05:45:19.03"
datempc = "2015-02-14"
ddexposure = expmpc/(60.0*60.0*24.0)
ddexposure = round(ddexposure, 5)
seconds = int(utcstartmpc[6:8]) / 60.0
minutes = (int(utcstartmpc[3:5]) + seconds) / 60
hours = (int(utcstartmpc[:2]) + minutes) / 24
hours = round(hours, 5)
expadd = str(hours + ddexposure)
datempc = datempc.replace("-", " ")
utcmpc = "C%s%s" % (datempc, expadd[1:])
utcmpc = utcmpc.ljust(17, "0")
As you can see this is very messy and it involves rounding a lot of data and I believe I am losing accuracy with the rounding. The finished outcome of the code leaves a time such as this:
C2015 02 14.23986
Is there a module that works better?
Thank you for the help.
Here's something that uses more of the built-in time/date modules (and I think it does what you want):
import time
import datetime
def my_date_converter(date_in):
parse_str, _ = date_in.split(".")
date_str, time_str = parse_str.split(" ")
parsed_time = time.strptime(time_str, "%H:%M:%S")
total_seconds = datetime.timedelta(hours=parsed_time.tm_hour,minutes=parsed_time.tm_min,seconds=parsed_time.tm_sec).total_seconds()
seconds_in_day = 86400.0
decimal_seconds = total_seconds / seconds_in_day
utcmpc = "C%s%s" % (date_str.replace("-", " "), str(round(decimal_seconds, 5))[1:])
utcmpc = utcmpc.ljust(17, "0")
return utcmpc
def main():
to_convert = "2015-02-14 05:45:19.03"
converted = my_date_converter(to_convert)
print "%s => %s" % (to_convert, converted)
if __name__ == '__main__':
main()
Example output: 2015-02-14 05:45:19.03 => C2015 02 14.23980
Have you tried the excellent astropy library? They have a package to deal with time and conversions: astropy.time
You could separate your problem into 2 tasks:
get datetime object that represent UTC time from the input expmpc, utcstartmpc, datempc
convert datetime object to string in the Year Month Day.XXXXX format.
Get datetime object from the input expmpc, utcstartmpc, datempc
from datetime import datetime, timedelta
expmpc = "10"
utcstartmpc = "05:45:19.03"
datempc = "2015-02-14"
dt = datetime.strptime(datempc + " " + utcstartmpc, "%Y-%m-%d %H:%M:%S.%f")
dt += timedelta(seconds=int(expmpc))
Convert datetime object to string in the Year Month Day.XXXXX format
from datetime import datetime, time, timedelta
s = dt.strftime("C%Y %m %d.")
day_fraction = dt - datetime.combine(dt, time.min)
s += ("%.0f" % (100000*day_fraction / timedelta(days=1)))
# -> C2015 02 14.23992
Note: the result is slightly different from the one in your question (92 vs. 86 at the end).
In Python 2, you need to replace td / timedelta(1) with td.total_seconds() / 86400.
Here's another way:
>>> from __future__ import division
>>> assert day_fraction.days == 0
>>> '%.0f' % ((day_fraction.seconds*10**6 + day_fraction.microseconds) / 864000)
'23992'
All arithmetic operations before the true division are performed with infinite precision.
Yet another way also produces 92:
>>> from datetime import timezone
>>> ('%.5f' % ((dt.replace(tzinfo=timezone.utc).timestamp() % 86400) / 86400))[2:]
'23992'

How do I check the difference, in seconds, between two dates?

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())

Convert a timedelta to days, hours and minutes

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.

Categories

Resources