I have two char fields, data import from excel or csv in odoo.
time_1= fields.Char(string = 'Time 1')
time_2= fields.Char(string = 'Time 2')
result= fields.Float(string = 'Result Time 2 - Time 1') #Need result 06:00
time_1 = 10:00:00,
time_2 = 16:00:00 (data from external source)
How with #api.onchange('time_1', 'time_2') or #api.depends('time_1', 'time_2')
convert char to time and subtract time_2 - time_1 and put result in result field?
It should be like that,
from datetime import datetime
#api.multi
#api.onchange('time_1', 'time_2')
def onchange_time(self):
for rec in self:
time1 = datetime.strptime(rec.time1, "%Y-%m-%d %H:%M:%S")
time2 = datetime.strptime(rec.time2, "%Y-%m-%d %H:%M:%S")
rec.result = (time2 - time1).seconds / float(60*60)
Two datetime objects will return timedelta obect in result
while you perform any arithmetic operations on it. timedelta has
property while will give you difference in seconds, so you can convert
that seconds to hours.
And then in view set
<field name="result" widget="float_time" />
Related
I want to specify the date of the desired format as a file name in this way
;
During [2019-10-01 09:00 ~ 2019-10-02 08:59:59]
Save data to 191001-09.txt
I have no idea about this. I could only follow simple code.
Please let me know how to fix it :
def timeStamped (fname, fmt = '19% m% d-% H {fname} '):
return datetime.datetime.now (). strftime (fmt) .format (fname = fname)
with open (timeStamped ('. txt'), 'a') as f_last:
f_last.write ('data')
Question: Conditional file name from datetime.now()
Import the required objects
datetime
timedelta
from datetime import datetime, timedelta
Define a function that gets the desired file name from a datetime.date object:
def fname_from_date(date):
# Rule 09:00:00 ~ day + 1 08:59:59
midnight = date.replace(hour=0, minute=0, second=0)
begin_of_day = date.replace(hour=9, minute=0, second=0)
end_of_day = date.replace(hour=8, minute=59, second=59)
# Are we between 'midnight' and 'end_of_day'
if date >= midnight and date <= end_of_day:
date = date - timedelta(days=1)
print('\tNext day -1: {}'.format(date))
# 191001-09.txt
fname = date.strftime('%Y%m%d-09')
return fname
Test the function def fname_from_date(... with static dates.
This requires to create a datetime.date object from datestr.
for datestr in ['2019-10-01 09:00:00',
'2019-10-01 11:01:11',
'2019-10-02 07:07:07',
'2019-10-02 08:59:59']:
date = datetime.strptime(datestr, '%Y-%m-%d %H:%M:%S')
print(date)
fname = '{}.txt'.format(fname_from_date(date))
print('\t{}'.format(fname))
Output:
2019-10-01 09:00:00
20191001-09.txt
2019-10-01 11:01:11
20191001-09.txt
2019-10-02 07:07:07
Next day -1: 2019-10-01 07:07:07
20191001-09.txt
2019-10-02 08:59:59
Next day -1: 2019-10-01 08:59:59
20191001-09.txt
Usage:
fname = '{}.txt'.format(fname_from_date(datetime.now()))
I will compare a dataset server time and the actual time. If the different greater than 1 minute, the script is continued. Else a waiting loop for example 10 sec. will start.
But I can't execute the if function. Thanks for your help!
That's the output:
last importtime: 2019-07-05 14:16:07
actual time: 2019-07-05 18:30:21
1 day, 4:14:14
Error:
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'datetime.datetime'
Code:
from datetime import datetime
import mysql.connector
import pandas as pd
#db-connection#
mydb = mysql.connector.connect(host="localhost",port="xx",user="xx",passwd="xxx",database="traiding")
mycursor = mydb.cursor()
mycursor.execute("select max(imported) from import_log")
data_sql=mycursor.fetchall()
#last import from database'
data=pd.DataFrame(data_sql, columns=['date'])
#close connection#
mycursor.close()
mydb.close()
#last import date#
lastimported=datetime.strftime(data_sql[0][0], "%Y-%m-%d %H:%M:%S")
print("last importtime:",lastimported)
#lastimport=datetime.strptime(lastimported, "%Y-%m-%d %H:%M:%S")
current_time=datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
print ("actual time: ", current_time)
s1 = lastimported
s2 = current_time
FMT = '%Y-%m-%d %H:%M:%S'
tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)
print (tdelta)
min_time=datetime.strptime('00:01:00', "%H:%M:%S")
if tdelta > min_time :
print (">0") # Do Something
else:
print ("else") # Waiting loop, for example 10sec
You have to compare the timedelta object to another timedelta object.
import datetime as dt
#... Your code
if tdelta > dt.timedelta(minutes=1):
print (">0") # Do Something
else:
print ("else") # Waiting loop, for example 10sec
i am trying to find difference of two datetime variable in python
i tried using direct difference of two variables, tried using strptime() function
from datetime import datetime
time = datetime.now()
#timestamp variable gets time value from database
print "time 1:" + str(timestamp)
print "time 2:" + str(time)
#diff = timestamp-time; #already tried
#diff = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S") - datetime.strtime(time, "%Y-%m-%d %H:%M:%S") # already tried
diff= datetime.datetime(timestamp, "%Y-%m-%d %H:%M:%S") - datetime.datetime(time, "%Y-%m-%d %H:%M:%S")
print diff
Expect the output in seconds and actual result is type error
You can use total_seconds() on datetime.timedelta to get difference in seconds:
diff = (timestamp-time).total_seconds()
Code:
from datetime import datetime
timestamp = '2019-04-01 07:12:00'
time = '2019-04-01 07:11:0'
print("time 1:" + str(timestamp))
print("time 2:" + str(time))
timestamp = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S")
time = datetime.strptime(time, "%Y-%m-%d %H:%M:%S")
diff = (timestamp-time).total_seconds()
print(diff)
# 60.0
I am trying to solve a problem. where I am defining date and time as string in below mentioned format.
date1 = '2016-12-1'
time1 = '10:30 AM'
time2 = '11:30 PM'
I have another string add_time = '01:50' type of variables is string.
what I'm looking for is:
new_date_and_time = time1 + add_time
so that it returns
date1 and new_date_and_time
> 2016-12-1, 12:20 PM (10:30 AM + 1:50)
but if I am adding time2 + add_time the date is also changed,
so it should print 2016-12-2, 1:20 AM
Is there any package which can do this?
Convert strings to datetime object, then add time using timedelta
from datetime import datetime, timedelta
date1 = '2016-12-1'
time1 = '10:30 AM'
date_object = datetime.strptime(date1 + ' ' + time1, '%Y-%m-%d %I:%M %p')
new_date_object = date_object + timedelta(hours=1) + timedelta(minutes=50)
SOAP client return seconds to end event.
How can I get from this seconds date in format "yyy-mm-dd hh:ii:ss"
A quick example (add 50000 seconds from now with datetime.timedelta):
>>> import datetime
>>> time_now = datetime.datetime.now()
>>> time_event = time_now + datetime.timedelta(seconds=50000)
>>> time_event.strftime("%Y-%m-%d %H:%M:%S")
'2010-05-08 12:07:05'