Python timedelta issue with negative values

前端 未结 2 2044
独厮守ぢ
独厮守ぢ 2020-11-28 11:00

Hi I need some help to understand why this is happening. I have a method to track \'time remaining\' in an event program:

def get_program_time_budget(self):
         


        
2条回答
  •  清酒与你
    2020-11-28 11:34

    Why?

    Possibly as a unintended side effect of the way // and % are defined.

    Possibly because it makes it easier to implement the datetime class. Five minutes before the epoch is 23:55, not 0:-5.

    It doesn't really matter. Just know that it's how days, seconds, and microseconds get normalized. And that it can easily be worked around.

    def format_timedelta(td):
        if td < timedelta(0):
            return '-' + format_timedelta(-td)
        else:
            # Change this to format positive timedeltas the way you want
            return str(td)
    
     >>> format_timedelta(timedelta(minutes=-5))
     '-0:05:00'
    

提交回复
热议问题