Rounding time in Python

前端 未结 8 1895
清酒与你
清酒与你 2020-12-24 13:34

What would be an elegant, efficient and Pythonic way to perform a h/m/s rounding operation on time related types in Python with control over the rounding resolution?

相关标签:
8条回答
  • 2020-12-24 13:45

    Here is a lossy* version of hourly rounding:

    dt = datetime.datetime
    now = dt.utcnow()
    rounded = dt.utcfromtimestamp(round(now.timestamp() / 3600, 0) * 3600)
    

    Same principle can be applied to different time spans.

    *The above method assumes UTC is used, as any timezone information will be destroyed in conversion to timestamp.

    0 讨论(0)
  • 2020-12-24 13:45
    def round_dt_to_seconds(dt):
        datetime.timedelta(seconds=dt.seconds)
    
    0 讨论(0)
  • 2020-12-24 13:53

    For a datetime.datetime rounding, see this function: https://stackoverflow.com/a/10854034/1431079

    Sample of use:

    print roundTime(datetime.datetime(2012,12,31,23,44,59,1234),roundTo=60*60)
    2013-01-01 00:00:00
    
    0 讨论(0)
  • 2020-12-24 13:53

    How about use datetime.timedeltas:

    import time
    import datetime as dt
    
    hms=dt.timedelta(hours=20,minutes=11,seconds=13)
    
    resolution=dt.timedelta(seconds=10)
    print(dt.timedelta(seconds=hms.seconds%resolution.seconds))
    # 0:00:03
    
    resolution=dt.timedelta(minutes=10)
    print(dt.timedelta(seconds=hms.seconds%resolution.seconds))
    # 0:01:13
    
    0 讨论(0)
  • 2020-12-24 13:58

    This will round up time data to a resolution as asked in the question:

    import datetime as dt
    
    current = dt.datetime.now()
    current_td = dt.timedelta(hours=current.hour, minutes=current.minute, seconds=current.second, microseconds=current.microsecond)
    
    # to seconds resolution
    to_sec = dt.timedelta(seconds=round(current_td.total_seconds()))
    print(dt.datetime.combine(current, dt.time(0)) + to_sec)
    
    # to minute resolution
    to_min = dt.timedelta(minutes=round(current_td.total_seconds() / 60))
    print(dt.datetime.combine(current, dt.time(0)) + to_min)
    
    # to hour resolution
    to_hour = dt.timedelta(hours=round(current_td.total_seconds() / 3600))
    print(dt.datetime.combine(current, dt.time(0)) + to_hour)
    
    0 讨论(0)
  • 2020-12-24 13:58

    You can convert both times to seconds, do the modulo operati

    from datetime import time
    
    def time2seconds(t):
        return t.hour*60*60+t.minute*60+t.second
    
    def seconds2time(t):
        n, seconds = divmod(t, 60)
        hours, minutes = divmod(n, 60)
        return time(hours, minutes, seconds)
    
    def timemod(a, k):
        a = time2seconds(a)
        k = time2seconds(k)
        res = a % k
        return seconds2time(res)
    
    print(timemod(time(20, 11, 13), time(0,0,10)))
    print(timemod(time(20, 11, 13), time(0,10,0)))
    

    Outputs:

    00:00:03
    00:01:13
    
    0 讨论(0)
提交回复
热议问题