Replacing the hour
parameter
Since a time
object has a range from 00:00
to 23:59
. In case you add one hour, it can thus get out of range, and hence it is no longer a time
object.
We can however implement this with wraparound (this means that for example 23:59
and two minutes is 00:01
) ourselves, like:
a = start.replace(hour=(start.hour+1) % 24)
We here thus replace the hour
s of the start
(we are not replacing it on the start
object, but create a copy) with (start.hour + 1) % 24
. The modulo 24
is necessary to make perform the wraparound.
Storing a timestamp in a DateTimeField
The above is however not a nice way to do this: typically time depends on the timezone (location) and the specific date (for example daylight saving time, some countries have changed the timezone, etc.).
Therfore I advice you to use a DateTimeField
instead of a TimeField
. For example in october 28, 2018 some parts of the world enter daylight saving time, and that means that if you add one hour, the clock can in some cases, still display the same numbers. With a TimeField
, this context is lost: we will change the numbers of the clock, regardless what the time is, and what the specific laws of the culture of the user are saying.