How can I apply an offset on the current time in python?
In other terms, be able to get the current time minus x hours and/or minus m minutes and/or minus s secondes and/or minus ms milliseconds
for instance
curent time = 18:26:00.000
offset = 01:10:00.000
=>resulting time = 17:16:00.000
Use a
datetime.datetime()
, then add or subtractdatetime.timedelta()
instances.timedelta()
arithmetic is not supported fordatetime.time()
objects; if you need to use offsets from an existingdatetime.time()
object, just usedatetime.datetime.combine()
to form adatetime.datetime()
instance, do your calculations, and 'extract' the time again with the.time()
method: