Python timestamp to time since conversion - python

I have a database column the holds timestamps in UNIX format, I am looking for a way to compare those timestamps to a timestamp from the time right now and print how many seconds/hours/days since the original timestamp.
Just to confirm I am NOT looking for a conversion from 1489757456 to 03/17/2017 # 1:30pm. I AM looking for a conversion from 1489757456 to 1m ago/2hr ago/3d ago ect.

datetime.datetime.fromtimestamp()
to convert your unix timestamp to a datetitme
datetime.datetime.now()
to get the current datetime
dateutil.relativedelta.relativedelta(dt1, dt2, ...)
to get the difference with respect to leap years.
References:
https://docs.python.org/3.6/library/datetime.html
http://dateutil.readthedocs.io/en/stable/relativedelta.html

Function, like this will generate output for hours, mins and seconds from seconds number.
def secondsToHms(d):
d = int(d);
h = math.floor(d / 3600)
m = math.floor(d % 3600 / 60)
s = math.floor(d % 3600 % 60)
htext = " hour, " if h == 1 else " hours, "
hDisplay = str(h) + htext if h > 0 else ""
mtext = " minute, " if m == 1 else " minutes, "
mDisplay = str(m) + mtext if m > 0 else ""
stext = " second" if s == 1 else " seconds"
sDisplay = str(s) + stext if s > 0 else ""
return hDisplay + mDisplay + sDisplay;
For example:
secondsToHms(344334)
>> 95.0 hours, 38.0 minutes, 54.0 seconds
So you can add your preferred formatting and also add days/months if needed in similar fashion.

Related

Comparing 2 times and getting the difference in minutes

i am trying to get the time difference using two times. i am getting 2 epoch timestamps from an api, converting them to a datetime, and then trying to compare them and get the time difference in minutes.
No errors in console.. the minutes just stay at 0.0 when i return it.
onlinestatus = (data["session"]["online"])
if onlinestatus is False:
theNewLineString = "\n"
lastLogout_string = "LastLogout: "
log_in = int(data2["player"]["lastLogin"])
log_out = int(data2["player"]["lastLogout"])
log_in_converted = timedate = time.strftime('%Y-%m-%d\n%I:%M %p', time.localtime(log_in / 1000))
log_out_converted = timedate = time.strftime('%Y-%m-%d\n%I:%M %p', time.localtime(log_out / 1000))
diff = datetime.strptime(log_in_converted, '%Y-%m-%d\n%I:%M %p') - datetime.strptime(log_out_converted, '%Y-%m-%d\n%I:%M %p')
return str("Online: ") + "`" + "False" + "`" + theNewLineString + theNewLineString + lastLogout_string + "`" + log_out_converted + theNewLineString + "`" + "Minutes Since Last Logout: " + "`" + str(diff.seconds/60) + "`"
I know everything else works. I am using a discord bot to return everything and here is what it returns:
Online: False
LastLogout: 2020-05-16
12:27 PM
Minutes Since Last Logout: 0.0
Any help is appreciated!
Let's say you have 2 datetime objects:
d1 = datetime.datetime.now()
d2 = datetime.datetime.now()
Now you can substract one from the other:
(d2 - d1)
This gives the result:
datetime.timedelta(seconds=3, microseconds=516614)
Then you can call the 'seconds' item and convert it to minutes:
(d2 - d1).seconds / 60
Hope this helps.

How to validate time format input?

So basically, i needed to write a program that takes the time of how fast you run a kilometer, and turn it into how long it takes to run a marathon. I got that done, but the problem is that it is supposed to handle if someone inputs the time incorrectly (ex 888 or 5:2555 instead of 8:30)
print "Marathon Time Calculator"
str_pace = raw_input("At what pace do you run a km? (e.g. 5:30): ")
if str_pace.isalpha():
print 'invalid time'
split_pace = str_pace.split(":")
minutes = int(split_pace[0])
seconds = int(split_pace[1])
str_pace = minutes * 60 + seconds
totalTime = str_pace * 42
hours = totalTime // 3600
timeLeft = totalTime % 3600
minutes = timeLeft // 60
seconds = timeLeft % 60
if len(split_pace[1]) >= 3:
print "Too many digits"
else:
print "You should complete a marathon in " + str(hours) + ":" + str(minutes) + ":" + str(seconds)
I have done this in python3
import re
import sys
print("Marathon Time Calculator")
str_pace = input("At what pace do you run a km? (e.g. 5:30): ")
# this is looking for a decimal value of 1 number, then a colon then another decimal value of 1 - 2 numbers
pattern = '\d{1}\:\d{1,2}'
# this is doing the actual check against the pattern for us
match = re.match(pattern, str_pace)
# we now check if there is a match to our pattern
if not match:
print('Invalid Time Input Try Again')
sys.exit
else:
# yes we are entering the value correctly continue the code
split_pace = str_pace.split(":")
minutes = int(split_pace[0])
seconds = int(split_pace[1])
str_pace = minutes * 60 + seconds
totalTime = str_pace * 42
hours = totalTime // 3600
timeLeft = totalTime % 3600
minutes = timeLeft // 60
seconds = timeLeft % 60
print("You should complete a marathon in {}:{}:{}".format(hours, minutes, seconds))

Python Time Math

I want to write a program that allows the user to enter in a start time hour, end time hour, and number of divisions.
So they might enter 9, 10, and 4 which should mean a start time of 9:00AM, end of 10:00AM and to split the range 4 times, resulting in an output of 9:00, 9:15, 9:30, 9:45.
I've tried using the time module and datetime, but cannot get the addition of time to work. I do not care about date.
I can calculate the time split, but the actual addition to the start time is evading me.
I have a hodge-podge of code, and the following is mostly me experimenting trying to figure out how to make this work. I've tried adding the minutes, tried converting to seconds, delved into datetime, tried the time module, but can't seem to get it to work. There are plenty of examples of how to "add 15 minutes to now" but the issue is I don't want to start at the "now", but rather let the user decide start time.
Thank you.
time_start = "9"
time_end = "10"
time_split = "4"
if len(time_start) == 1:
time_start = "0" + str(time_start) + ":00"
else:
time_start = str(time_start) + ":00"
if len(time_end) == 1:
time_end = "0" + str(time_end) + ":00"
else:
time_end = str(time_end) + ":00"
print time_start
print time_end
s1 = time_start + ':00'
s2 = time_end + ':00'
FMT = '%H:%M:%S'
tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)
divided = tdelta / int(time_split)
print tdelta
print divided
s3 = str(divided)
print "s1 time start: " + str(s1)
print "s2 time end: " + str(s2)
print "s3 time divided: " + str(s3)
ftr = [3600,60,1]
add_seconds = sum([a*b for a,b in zip(ftr, map(int,s3.split(':')))])
print "s3 time divided seconds: " + str(add_seconds)
print "time delta: " + str(tdelta)
EDIT: I did a small bit of research and found a much better solution that elegantly handles resolution to the millisecond. Please implement this code instead (though I will save the old code for posterity)
import datetime
start_time = 9 # per user input
end_time = 10 # per user input
divisions = 7 # per user input
total_time = end_time - start_time
start_time = datetime.datetime.combine(datetime.date.today(),datetime.time(start_time))
end_time = start_time + datetime.timedelta(hours=total_time)
increment = total_time*3600000//divisions # resolution in ms
times = [(start_time+datetime.timedelta(milliseconds=increment*i)).time()
for i in range(divisions)]
from pprint import pprint
pprint(list(map(str,times)))
# ['09:00:00',
# '09:08:34.285000',
# '09:17:08.570000',
# '09:25:42.855000',
# '09:34:17.140000',
# '09:42:51.425000',
# '09:51:25.710000']
If I were you, I'd do my math as raw minutes and use datetime.time only to save the results as something more portable.
Try this:
import datetime
start_time = 9 # per user input
end_time = 10 # per user input
divisions = 4 # per user input
total_minutes = (end_time-start_time)*60
increment = total_minutes // divisions
minutes = [start_time*60]
while minutes[-1] < end_time*60:
# < end_time*60 - increment to exclude end_time from result
minutes.append(minutes[-1] + increment)
times = [datetime.time(c//60,c%60) for c in minutes]
# [09:00:00,
# 09:15:00,
# 09:30:00,
# 09:45:00,
# 10:00:00]

Converting fractional days to H:M:S.MS - Two examples

Of the two methods of calculating a fractional day to local time, which one would you consider the best way and why?
Edit: 'Fractional day' means here the decimal part of a Julian day jd: jd - (math.floor(jd - 0.5) + 0.5) (this is because 0:00:00 is at jd.5)
#classmethod
def fromfractional(cls, frac, **kwargs):
changed = False
f = lambda x: decimal.dec(floor(x))
if not isinstance(frac, decimal.Decimal):
frac = decimal.dec(frac)
hours = decimal.dec(D24 * (frac - f(frac)))
if hours < 1:
hours += 1 # Or else microseconds won't be calculated correctly
changed = True
minutes = decimal.dec(D60 * (hours - f(hours)))
seconds = decimal.dec(D60 * (minutes - f(minutes)))
ms = decimal.dec(DKS * (seconds - f(seconds)))
if changed:
hours -= 1
return int(hours), int(minutes), int(seconds), int(ms)
#classmethod
def fromfractional2(cls, x):
d = lambda x: decimal.Decimal(str(x))
total = d(x) * d(86400000000000)
hours = (total - (total % d(3600000000000))) / d(3600000000000)
total = total % d(3600000000000)
minutes = (total - (total % d(60000000000))) / d(60000000000)
total = total % d(60000000000)
seconds = (total - (total % d(1000000000))) / d(1000000000)
total = total % d(1000000000)
ms = (total - (total % d(1000000))) / d(1000000)
total = total % d(1000000)
mics = (total - (total % d(1000))) / d(1000)
return int(hours), int(minutes), int(seconds), int(ms)
D24 = decimal.Decimal('24')
DMS = decimal.Decimal('86400000.0')
D60 = decimal.Decimal('60')
D3600 = decimal.Decimal('3600')
D1440=decimal.Decimal('1400')
DKS=decimal.Decimal('1000')
DTS=decimal.Decimal('86400')
I think you are trying to get from something like:
1.2256 days
To:
1 day, 5 hours, 24 minutes, 51 seconds
but with microseconds, too?
Here's how I generated the above response:
def nice_repr(timedelta, display="long"):
"""
Turns a datetime.timedelta object into a nice string repr.
display can be "minimal", "short" or "long" [default].
>>> from datetime import timedelta as td
>>> nice_repr(td(days=1, hours=2, minutes=3, seconds=4))
'1 day, 2 hours, 3 minutes, 4 seconds'
>>> nice_repr(td(days=1, seconds=1), "minimal")
'1d, 1s'
"""
assert isinstance(timedelta, datetime.timedelta), "First argument must be a timedelta."
result = ""
weeks = timedelta.days / 7
days = timedelta.days % 7
hours = timedelta.seconds / 3600
minutes = (timedelta.seconds % 3600) / 60
seconds = timedelta.seconds % 60
if display == 'minimal':
words = ["w", "d", "h", "m", "s"]
elif display == 'short':
words = [" wks", " days", " hrs", " min", " sec"]
else:
words = [" weeks", " days", " hours", " minutes", " seconds"]
values = [weeks, days, hours, minutes, seconds]
for i in range(len(values)):
if values[i]:
if values[i] == 1 and len(words[i]) > 1:
result += "%i%s, " % (values[i], words[i].rstrip('s'))
else:
result += "%i%s, " % (values[i], words[i])
return result[:-2]

Python summing up time

In python how do I sum up the following time?
0:00:00
0:00:15
9:30:56
It depends on the form you have these times in, for example if you already have them as datetime.timedeltas, then you could just sum them up:
>>> s = datetime.timedelta(seconds=0) + datetime.timedelta(seconds=15) + datetime.timedelta(hours=9, minutes=30, seconds=56)
>>> str(s)
'9:31:11'
Using timedeltas (tested in Python 3.9):
import datetime
timeList = ['0:00:00', '0:00:15', '9:30:56']
mysum = datetime.timedelta()
for i in timeList:
(h, m, s) = i.split(':')
d = datetime.timedelta(hours=int(h), minutes=int(m), seconds=int(s))
mysum += d
print(str(mysum))
Result:
9:31:11
As a list of strings?
timeList = [ '0:00:00', '0:00:15', '9:30:56' ]
totalSecs = 0
for tm in timeList:
timeParts = [int(s) for s in tm.split(':')]
totalSecs += (timeParts[0] * 60 + timeParts[1]) * 60 + timeParts[2]
totalSecs, sec = divmod(totalSecs, 60)
hr, min = divmod(totalSecs, 60)
print "%d:%02d:%02d" % (hr, min, sec)
Result:
9:31:11
I'm really disappointed if there is not any more pythonic solution... :(
Horrible one ->
timeList = [ '0:00:00', '0:00:15', '9:30:56' ]
ttt = [map(int,i.split()[-1].split(':')) for i in timeList]
seconds=reduce(lambda x,y:x+y[0]*3600+y[1]*60+y[2],ttt,0)
#seconds == 34271
This one looks horrible too ->
zero_time = datetime.datetime.strptime('0:0:0', '%H:%M:%S')
ttt=[datetime.datetime.strptime(i, '%H:%M:%S')-zero_time for i in timeList]
delta=sum(ttt,zero_time)-zero_time
# delta==datetime.timedelta(0, 34271)
# str(delta)=='9:31:11' # this seems good, but
# if we have more than 1 day we get for example str(delta)=='1 day, 1:05:22'
Really frustrating is also this ->
sum(ttt,zero_time).strftime('%H:%M:%S') # it is only "modulo" 24 :(
I really like to see one-liner so, I tried to make one in python3 :P (good result but horrible look)
import functools
timeList = ['0:00:00','0:00:15','9:30:56','21:00:00'] # notice additional 21 hours!
sum_fnc=lambda ttt:(lambda a:'%02d:%02d:%02d' % (divmod(divmod(a,60)[0],60)+(divmod(a,60)[1],)))((lambda a:functools.reduce(lambda x,y:x+y[0]*3600+y[1]*60+y[2],a,0))((lambda a:[list(map(int,i.split()[-1].split(':'))) for i in a])(ttt)))
# sum_fnc(timeList) -> '30:40:11'
lines = ["0:00:00", "0:00:15", "9:30:56"]
total = 0
for line in lines:
h, m, s = map(int, line.split(":"))
total += 3600*h + 60*m + s
print "%02d:%02d:%02d" % (total / 3600, total / 60 % 60, total % 60)
Assuming you want to add up the seconds for a total time:
def parse_time(s):
hour, min, sec = s.split(':')
try:
hour = int(hour)
min = int(min)
sec = int(sec)
except ValueError:
# handle errors here, but this isn't a bad default to ignore errors
return 0
return hour * 60 * 60 + min * 60 + sec
print parse_time('0:00:00') + parse_time('0:00:15') + parse_time('9:30:56')
from datetime import timedelta
h = ['3:00:00','1:07:00', '4:00:00', '4:05:00', '4:10:00', '4:03:00']
def to_td(h):
ho, mi, se = h.split(':')
return timedelta(hours=int(ho), minutes=int(mi), seconds=int(se))
print(str(sum(map(to_td, h), timedelta())))
# Out[31]: 20:25:00
Naive approach (without exception handling):
#!/usr/bin/env python
def sumup(*times):
cumulative = 0
for t in times:
hours, minutes, seconds = t.split(":")
cumulative += 3600 * int(hours) + 60 * int(minutes) + int(seconds)
return cumulative
def hms(seconds):
"""Turn seconds into hh:mm:ss"""
hours = seconds / 3600
seconds -= 3600*hours
minutes = seconds / 60
seconds -= 60*minutes
return "%02d:%02d:%02d" % (hours, minutes, seconds)
if __name__ == '__main__':
print hms(sumup(*("0:00:00", "0:00:15", "9:30:56")))
# will print: 09:31:11
Bellow is a solution using list comprehension:
from datetime import timedelta
def time_sum(time: List[str]) -> timedelta:
"""
Calculates time from list of time hh:mm:ss format
"""
return sum(
[
timedelta(hours=int(ms[0]), minutes=int(ms[1]), seconds=int(ms[2]))
for t in time
for ms in [t.split(":")]
],
timedelta(),
)
Example:
time_list = ["0:00:00", "0:00:15", "9:30:56"]
total = time_sum(time_list)
print(f"Total time: {total}")

Categories

Resources