代码之家  ›  专栏  ›  技术社区  ›  Paul Stephenson

在python中,向datetime.time添加n秒的标准方法是什么?

  •  295
  • Paul Stephenson  · 技术社区  · 16 年前

    给定一个 datetime.time 在python中,是否有一种标准的方法向它添加整数秒,以便 11:34:59 + 3 = 11:35:02 比如说?

    这些显而易见的想法行不通:

    >>> datetime.time(11, 34, 59) + 3
    TypeError: unsupported operand type(s) for +: 'datetime.time' and 'int'
    >>> datetime.time(11, 34, 59) + datetime.timedelta(0, 3)
    TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.timedelta'
    >>> datetime.time(11, 34, 59) + datetime.time(0, 0, 3)
    TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.time'
    

    最后我写了这样的函数:

    def add_secs_to_time(timeval, secs_to_add):
        secs = timeval.hour * 3600 + timeval.minute * 60 + timeval.second
        secs += secs_to_add
        return datetime.time(secs // 3600, (secs % 3600) // 60, secs % 60)
    

    不过,我忍不住想,我错过了一个更简单的方法。

    相关的

    8 回复  |  直到 16 年前
        1
  •  412
  •   paxdiablo    12 年前

    datetime timedelta time

    import datetime
    a = datetime.datetime(100,1,1,11,34,59)
    b = a + datetime.timedelta(0,3) # days, seconds, then other fields.
    print a.time()
    print b.time()
    

    11:34:59
    11:35:02
    

    b = a + datetime.timedelta(seconds=3)
    


    addSecs

    import datetime
    
    def addSecs(tm, secs):
        fulldate = datetime.datetime(100, 1, 1, tm.hour, tm.minute, tm.second)
        fulldate = fulldate + datetime.timedelta(seconds=secs)
        return fulldate.time()
    
    a = datetime.datetime.now().time()
    b = addSecs(a, 300)
    print a
    print b
    

     09:11:55.775695
     09:16:55
    
        2
  •  44
  •   Eli Courtwright    16 年前

    sometime = get_some_time() # the time to which you want to add 3 seconds
    later = (datetime.combine(date.today(), sometime) + timedelta(seconds=3)).time()
    

        3
  •  18
  •   unmounted    16 年前

    >>> b = a + datetime.timedelta(seconds=3000)
    >>> b
    datetime.datetime(1, 1, 1, 12, 24, 59)
    
        4
  •  9
  •   Paul Stephenson    16 年前

    add_secs_to_time()

    def add_secs_to_time(timeval, secs_to_add):
        dummy_date = datetime.date(1, 1, 1)
        full_datetime = datetime.datetime.combine(dummy_date, timeval)
        added_datetime = full_datetime + datetime.timedelta(seconds=secs_to_add)
        return added_datetime.time()
    

    (datetime.datetime.combine(datetime.date(1, 1, 1), timeval) + datetime.timedelta(seconds=secs_to_add)).time()
    

        5
  •  6
  •   rescdsk    12 年前

    datetime.time

    >>> import datetime
    >>> import nptime
    >>> nptime.nptime(11, 34, 59) + datetime.timedelta(0, 3)
    nptime(11, 35, 2)
    

    nptime

    https://github.com/tgs/nptime

        6
  •  4
  •   George Sovetov    6 年前

    datetime

    timedelta

    >>> from datetime import datetime, timedelta
    >>> t = datetime.now() + timedelta(seconds=3000)
    >>> print(t)
    datetime.datetime(2018, 1, 17, 21, 47, 13, 90244)
    

    std::chrono::duration

        7
  •  1
  •   Tshilidzi Mudau Simon Müller    7 年前

    datetime.datetime datetime.timedelta time()

        8
  •  1
  •   Bart Van Loon    6 年前

    arrow

    sometime = arrow.now()
    abitlater = sometime.shift(seconds=3)