0

I need, passing a datetime value, the next moment.

For example:

input: 2022-11-15 10:23:00 --> output: 2022-11-15 10:23:01

input 2022-11-15 10:59:59 --> output: 2022-11-15 11:00:00

input: 2022-12-31 23:59:59 --> output: 2023-01-01 00:00:00

I tried doing this

    date = dateEx + datetime.timedelta(days=1)  # not always, only if the 
                                                # day ended(?)
    timeStr = timeEx.strftime("%H:%M:%S")
    timeStr = timeStr.split(":")
    timeEx = datetime.time(int(timeStr[0]), int(timeStr[1]), int(timeStr[2]))
    StartTime = timeEx + datetime.time(second=1) # trying to get the next moment

Where dateEx is the previous generated date (format: YYYY-MM-DD) and timeEx is the previous generated time (format: YYYY-MM-DD HH-MM-SS).

At StartTime = timeEx + datetime.time(second=1) I get an error: TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.time'

  • That's not the next moment, that's exactly one second later. I know this sounds like the same, but it isn't - your clock can have arbitrary resolution. There's is also cases where your time increase quantum ends up being larger than a second. With that out of the world: – Marcus Müller Nov 15 '22 at 10:08
  • How do you intend to work with slip seconds and daylights time saving clock adjustments? – Marcus Müller Nov 15 '22 at 10:09
  • Other than that, if you want to handle it in a human-sensible way, Datetime is the right type, but for a time increase you need to use Time Delta, instead – Marcus Müller Nov 15 '22 at 10:10
  • @MarcusMüller Python's datetime/time classes do not consider leap seconds, if that is what you mean by "slip second" - see [technical detail, #4](https://docs.python.org/3/library/datetime.html#technical-detail). Other than that, the question requires clarification. *It seems like* OP is looking for rounding? See e.g. [How to round the minute of a datetime object](https://stackoverflow.com/q/3463930/10197418). – FObersteiner Nov 15 '22 at 10:16
  • @MarcusMüller omg i think it's more difficult than i thought for a Python-newies like me... clock adjustments don't bother me, i just don't consider them – pollettoRinforzante Nov 15 '22 at 10:26
  • @MarcusMüller could i generate random timedelta values in a while loop untill the generated value is = timeEx + 1? – pollettoRinforzante Nov 15 '22 at 10:26
  • yes, you could. – Marcus Müller Nov 15 '22 at 10:31

0 Answers0