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'
Related
from datetime import datetime, timedelta
now = datetime.now()
start = now + timedelta(minutes = 15)
finish = start + timedelta(minutes = 30)
How can I find the number of 15 minutes intervals exist between start and finish?
from datetime import datetime, timedelta
now = datetime.now()
start = now + timedelta(minutes = 15)
finish = start + timedelta(minutes = 30)
elapsed = finish - start
number_of_intervals = elapsed / timedelta(minutes=15)
elapsed is the timedelta between start and finish. Divide by 15 minutes to calculate how many 15 minute intervals fit in there.
Note that this returns a float, so includes fractional intervals. Round as appropriate.
You need to find the difference between start and finish in minutes, divide by 15, and make that an int:
now = datetime.now()
start = now + timedelta(minutes = 15)
finish = start + timedelta(minutes = 30)
difference = (finish - start).total_seconds()/60
quarters = int(difference/15)
i would write something similar to this:
from datetime import datetime, timedelta
DATE_TIME_STRING_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'
from_date_time = datetime.strptime('2016-06-06T05:00:00.000Z',
DATE_TIME_STRING_FORMAT)
to_date_time = datetime.strptime('2016-06-06T06:00:00.000Z',
DATE_TIME_STRING_FORMAT)
date_times = [from_date_time.strftime(DATE_TIME_STRING_FORMAT)]
date_time = from_date_time
while date_time < to_date_time:
date_time += timedelta(minutes=15)
date_times.append(date_time.strftime(DATE_TIME_STRING_FORMAT))
print(date_times)
Output:
['2016-06-06T05:00:00.000000Z', '2016-06-06T05:15:00.000000Z', '2016-06-06T05:30:00.000000Z', '2016-06-06T05:45:00.000000Z', '2016-06-06T06:00:00.000000Z']
Edit:
If you are interested in just the number of 15 minute intervals you can use something like:
from datetime import datetime, timedelta
DATE_TIME_STRING_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'
from_date_time = datetime.strptime('2016-06-06T05:00:00.000Z',
DATE_TIME_STRING_FORMAT)
to_date_time = datetime.strptime('2016-06-06T06:00:00.000Z',
DATE_TIME_STRING_FORMAT)
print((to_date_time-from_date_time) / timedelta(minutes=15))
You can use time library instead of date time. time works with seconds and you should convert minutes to seconds:
import time
interval = 45*60
start = time.time()
finish = time.time() + interval
diff = finish - start
print(diff // (15*60))
Simply compare start and finish like so:
from datetime import datetime, timedelta
now = datetime.now()
start = now + timedelta(minutes = 15)
finish = start + timedelta(minutes = 30)
elapsed = finish - start # This is a timedelta object
reference_interval = 15*60 # interval in seconds
number_of_intervals = elapsed.seconds/reference_interval
As pointed out by Sören, this will not work if 'elapsed' is more than one day, in which case, simply compute the number as follow:
number_of_intervals = (elapsed.days*86400+elapsed.seconds)/reference_interval
# (86400 seconds in a day)
Using Rouble's suggestion and Maulik Gangani's example based on that suggestion:
How do I determine if current time is within a specified range using Python's datetime module?
I have two time strings that have been converted into a datetime object using .strptime as follows:
timeStart = '0300'
timeStart = datetime.strptime(timeStart, '%H%M')
I have a datetime object for time now:
timeNow = datetime.now()
My problem is that unless I convert timeNow (datetime object) into a string and then back again using .strptime , the script will not work:
timeNow = datetime.now()
timeNow = datetime.strftime(timeNow, '%H%M')
timeNow = datetime.strptime(timeNow, '%H%M')
Full code:
from datetime import datetime
def isNowInTimePeriod(startTime, endTime, nowTime):
if startTime < endTime:
return nowTime >= startTime and nowTime <= endTime
else: #Over midnight
return nowTime >= startTime or nowTime <= endTime
timeStart = '0300'
timeEnd = '1000'
timeEnd = datetime.strptime(timeEnd, '%H%M')
timeStart = datetime.strptime(timeStart, '%H%M')
timeNow = datetime.now()
timeNow = datetime.strftime(timeNow, '%H%M')
timeNow = datetime.strptime(timeNow, '%H%M')
My question is, how do I format timeNow = datetime.now() so that I can compare it against a string in 24hr format '0000' without having to convert datetime.now() to a string and back again?
The reason why your code is not working, it's because apart from time, datetime (as the name suggests) holds also information about the date.
When you run the following code:
timeStart = '0300'
timeStart = datetime.strptime(timeStart, '%H%M')
The hour and minute is converted, but the rest (the "date" part) is assumed to be epoch:
repr(timeStart)
# Output: 'datetime.datetime(1900, 1, 1, 3, 0)'
When you run datetime.now(), however, that always assumes the current time + date:
>>> datetime.now()
datetime.datetime(2020, 7, 17, 8, 25, 18, 270848)
The reason why converting to string, and back from string works, is because when you convert your datetime to a string with the format '%H%M, you lose the date part in the resulting string. Converting back from that, there's only hour and minutes to read, so you're losing the date part.
Effectivelly, you should be using datetime.time, instead of datetime.datetime to compare time.
After reading using strptime, try using the .time() method to only extract the time part and lose the date part:
timeStart = '0300'
timeStart = datetime.strptime(timeStart, '%H%M').time()
And same for the timeNow part:
timeNow = datetime.now().time()
If you call datetime.strptime, a default date is added if you only supply a time string. datetime.now() will have today's date. You can remove that by using only the time part of the datetime object - which you get by calling the time() method, e.g.
from datetime import datetime
def isNowInTimePeriod(startTime, endTime, nowTime):
if startTime < endTime:
return nowTime >= startTime and nowTime <= endTime
else: #Over midnight
return nowTime >= startTime or nowTime <= endTime
timeStart = '0300'
timeEnd = '1000'
timeEnd = datetime.strptime(timeEnd, '%H%M').time()
timeStart = datetime.strptime(timeStart, '%H%M').time()
timeNow = datetime.now().time()
print(isNowInTimePeriod(timeStart, timeEnd, timeNow))
# True (my current time is 0823)
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 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" />
I'm trying to work out a way to make a new variable AUTOMATIC_END_TIME based on adding the minimum amount of time onto the start time but I can't figure out the way to allow START_TIME to be turned into a time that can then have time added onto it.
So far my script has the following:
from time import sleep
from datetime import datetime, timedelta
START_TIME = "19:18"
END_TIME = "19:25"
LOGA = ["one", "two"]
TIME_DIFFERENCE = datetime.strptime(END_TIME, "%H:%M") - datetime.strptime(START_TIME, "%H:%M")
TIME_DIFFERENCE = TIME_DIFFERENCE.seconds
if len(LOGA) * (60 * (5 + 1)) >= TIME_DIFFERENCE:
print "Show minimum end time"
AUTOMATIC_END_TIME = "" # Should come out as 19:30
The current script shouldn't change at all except for AUTOMATIC_END_TIME which should be START_TIME + (60 * (5 + 1) It should come out as 19:30
>>> (datetime.strptime(START_TIME, "%H:%M") + timedelta(minutes=12)).strftime('%H:%M')
'19:30'
from time import sleep
from datetime import datetime, timedelta
START_TIME = "19:18"
END_TIME = "19:25"
LOGA = ["one", "two"]
TIME_DIFFERENCE = datetime.strptime(END_TIME, "%H:%M") - datetime.strptime(START_TIME, "%H:%M")
TIME_DIFFERENCE = TIME_DIFFERENCE.seconds
if len(LOGA) * (60 * (5 + 1)) >= TIME_DIFFERENCE:
print "Show minimum end time"
AUTOMATIC_END_TIME = (datetime.strptime(START_TIME, "%H:%M") + timedelta(minutes=12)).strftime('%H:%M')
print AUTOMATIC_END_TIME