Calculate difference between two datetimes odoo 10 [duplicate] - python

This question already has answers here:
Date difference in minutes in Python
(12 answers)
Closed 5 years ago.
Working on Odoo10, i need to calculate the difference between two fields of datetime type, start and finish, i need the difference to be in minutes. how can i do that ?

Try with this example:
from dateutil.relativedelta import relativedelta
#api.one
#api.depends('start_field','finish_field')
def _total_minutes(self):
if self.start_field and self.finish_field:
start_dt = fields.Datetime.from_string(self.start_field)
finish_dt = fields.Datetime.from_string(self.finish_field)
difference = relativedelta(finish_dt, start_dt)
days = difference.days
hours = difference.hours
minutes = difference.minutes
seconds = 0

Related

Generate random time in hour and minute format python [duplicate]

This question already has answers here:
Formatting timedelta objects [duplicate]
(7 answers)
Closed 5 months ago.
I want to generate a timestamp only in hour and minutes format, like this:
154h 27m
Please check my logic below
planned_downtime = timedelta(hours=random.randrange(150))
However, the result is coming in seconds. To convert it to above mentioned format, I applied this:
planned_downtime.strftime("%H %M")
But I'm getting errors. How can I convert this randomly generated time in above format?
Like this maybe:
m = random.randint(0,60)
h = random.randint(0,24)
ts = f'{h}h {m}m'
print(ts)
15h 48m

Add time with integer number in python [duplicate]

This question already has answers here:
How to add hours to current time in python
(4 answers)
Closed 3 years ago.
I want to get current time and add it with an integer of hours. Example now is 11.00pm, May 12, 2019. I want to add 3 hours more. So the result would be 2.00 am May 13, 2019. Please help me to datetime + hours(integer type)
import datetime
currentDT = datetime.datetime.now()
print('Now is: '+ str(currentDT))
hours = int(input()) #any hours you want
result = currentDT + hours #it will get the errors here
Use datetime.now to obtain the current time, and add a datetime.timedelta:
from datetime import datetime, timedelta
n_hours = 3
date = datetime.now() + timedelta(hours=n_hours)
print(datetime.now())
# 2019-05-12 19:16:51.651376
print(date)
# 2019-05-12 22:16:51.464890

Adding datetime.time objects to each other in python [duplicate]

This question already has answers here:
What is the standard way to add N seconds to datetime.time in Python?
(11 answers)
Closed 6 years ago.
How can i add datetime.time objects to each other? Lets say i have:
import datetime as dt
a = dt.time(hour=18, minute=15)
b = dt.time(hour=0, minute=15)
#c = a+b???
c should be equal to datetime.time(hour=18, minute=30)
Edit:
I have a function that gets as arguments datetime.time objects and should return datetime.time object that is sum of passed arguments. As i am only dealing with hours and minutes i wrote this:
def add_times(t1, t2):
hours = t1.hour + t2.hour
minutes = t1.minute + t2.minute
hours += minutes // 60
minutes %= 60
new_time = datetime.time(hour=hours, minute=minutes)
return new_time
But it is a dirty way and i am sure there is a legit way of doing it.
How do i achieve that?
Adding timedeltas
You can add dt.timedeltas
import datetime as dt
a = dt.timedelta(hours=18, minutes=15)
b = dt.timedelta(hours=0, minutes=15)
a + b
datetime.timedelta(0, 66600)

How to change the time on date time into 12 hour time? [duplicate]

This question already has answers here:
How can I convert 24 hour time to 12 hour time?
(6 answers)
Closed 7 years ago.
import time
timestr = time.strftime("%Y/%m/%d-%H:%M")
print timestr
When I run this code it comes out with 2015/10/27-20:36 but how would I make the time 8:36 instead
timestr = time.strftime("%Y/%m/%d-%I:%M")

In Python, how do I make a datetime that is 15 minutes from now? 1 hour from now? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Python - easy way to add N seconds to a datetime.time?
How to create a DateTime equal to 15 minutes ago?
what's the best way to do this?
d1 = datetime.datetime.now() + datetime.timedelta(minutes=15)
d2 = datetime.datetime.now() + datetime.timedelta(hours=1)

Categories

Resources