Hướng dẫn convert second to minute python - chuyển đổi thứ hai sang phút python

Đây là cách tôi có được nó.

Show
def sec2time(sec, n_msec=3):
    ''' Convert seconds to 'D days, HH:MM:SS.FFF' '''
    if hasattr(sec,'__len__'):
        return [sec2time(s) for s in sec]
    m, s = divmod(sec, 60)
    h, m = divmod(m, 60)
    d, h = divmod(h, 24)
    if n_msec > 0:
        pattern = '%%02d:%%02d:%%0%d.%df' % (n_msec+3, n_msec)
    else:
        pattern = r'%02d:%02d:%02d'
    if d == 0:
        return pattern % (h, m, s)
    return ('%d days, ' + pattern) % (d, h, m, s)

Vài ví dụ:

$ sec2time(10, 3)
Out: '00:00:10.000'

$ sec2time(1234567.8910, 0)
Out: '14 days, 06:56:07'

$ sec2time(1234567.8910, 4)
Out: '14 days, 06:56:07.8910'

$ sec2time([12, 345678.9], 3)
Out: ['00:00:12.000', '4 days, 00:01:18.900']

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    ĐọcExamples:

    Input : 12345
    Output : 3:25:45
    
    Input : 3600
    Output : 1:00:00

    Bàn luậnNaive This approach is simply a naive approach to get the hours, minutes and seconds by simple mathematical calculations. 

    Python3

    def convert(seconds):

    Cho một số nguyên n (tính bằng giây), chuyển đổi thành giờ, phút và giây. Ví dụ:

    Cách tiếp cận số 1: Naive Cách tiếp cận này chỉ đơn giản là một cách tiếp cận ngây thơ để có được giờ, phút và giây bằng các tính toán toán học đơn giản. & NBSP;

        seconds = seconds %

        

    $ sec2time(10, 3)
    Out: '00:00:10.000'
    
    $ sec2time(1234567.8910, 0)
    Out: '14 days, 06:56:07'
    
    $ sec2time(1234567.8910, 4)
    Out: '14 days, 06:56:07.8910'
    
    $ sec2time([12, 345678.9], 3)
    Out: ['00:00:12.000', '4 days, 00:01:18.900']
    
    6= seconds
    $ sec2time(10, 3)
    Out: '00:00:10.000'
    
    $ sec2time(1234567.8910, 0)
    Out: '14 days, 06:56:07'
    
    $ sec2time(1234567.8910, 4)
    Out: '14 days, 06:56:07.8910'
    
    $ sec2time([12, 345678.9], 3)
    Out: ['00:00:12.000', '4 days, 00:01:18.900']
    
    9
    $ sec2time(10, 3)
    Out: '00:00:10.000'
    
    $ sec2time(1234567.8910, 0)
    Out: '14 days, 06:56:07'
    
    $ sec2time(1234567.8910, 4)
    Out: '14 days, 06:56:07.8910'
    
    $ sec2time([12, 345678.9], 3)
    Out: ['00:00:12.000', '4 days, 00:01:18.900']
    
    9
    $ sec2time(10, 3)
    Out: '00:00:10.000'
    
    $ sec2time(1234567.8910, 0)
    Out: '14 days, 06:56:07'
    
    $ sec2time(1234567.8910, 4)
    Out: '14 days, 06:56:07.8910'
    
    $ sec2time([12, 345678.9], 3)
    Out: ['00:00:12.000', '4 days, 00:01:18.900']
    
    3

        seconds %__

    $ sec2time(10, 3)
    Out: '00:00:10.000'
    
    $ sec2time(1234567.8910, 0)
    Out: '14 days, 06:56:07'
    
    $ sec2time(1234567.8910, 4)
    Out: '14 days, 06:56:07.8910'
    
    $ sec2time([12, 345678.9], 3)
    Out: ['00:00:12.000', '4 days, 00:01:18.900']
    
    3

        

    Input : 12345
    Output : 3:25:45
    
    Input : 3600
    Output : 1:00:00
    8= seconds
    $ sec2time(10, 3)
    Out: '00:00:10.000'
    
    $ sec2time(1234567.8910, 0)
    Out: '14 days, 06:56:07'
    
    $ sec2time(1234567.8910, 4)
    Out: '14 days, 06:56:07.8910'
    
    $ sec2time([12, 345678.9], 3)
    Out: ['00:00:12.000', '4 days, 00:01:18.900']
    
    9
    $ sec2time(10, 3)
    Out: '00:00:10.000'
    
    $ sec2time(1234567.8910, 0)
    Out: '14 days, 06:56:07'
    
    $ sec2time(1234567.8910, 4)
    Out: '14 days, 06:56:07.8910'
    
    $ sec2time([12, 345678.9], 3)
    Out: ['00:00:12.000', '4 days, 00:01:18.900']
    
    9 def3

        seconds %= def3

    convert(seconds):7convert(seconds):8

    Độ phức tạp về thời gian: O (1) Không gian phụ trợ: O (1)O(1)
    Auxiliary Space: O(1)

    Xem thảo luậnApproach #2 : Alternate to the Naive approach By using the divmod() function, which does only a single division to produce both the quotient and the remainder, you can have the result very quickly with only two mathematical operations. 

    Cải thiện bài viết

    def convert(seconds):

    Lưu bài viết

    Đọc

    Bàn luận

        seconds %= def3

    convert(seconds):7convert(seconds):8

    Độ phức tạp về thời gian: O (1) Không gian phụ trợ: O (1)O(1)
    Auxiliary Space: O(1)

    Xem thảo luậnApproach #3 : Using timedelta (Object of datetime module) Datetime module provides timedelta object which represents a duration, the difference between two dates or times. datetime.timedelta can be used to represent seconds into hours, minutes and seconds format. 

    Python3

    Cải thiện bài viết

    Lưu bài viết

    Đọc

        seconds %= def3

    convert(seconds):7convert(seconds):8

    Độ phức tạp về thời gian: O (1) Không gian phụ trợ: O (1)O(1)
    Auxiliary Space: O(1)

    Xem thảo luậnApproach #4 : Using time.strftime() time.strftime() gives more control over formatting. The format and time.gmtime() is passed as argument. gmtime is used to convert seconds to special tuple format that strftime() requires. 

    Python3

    seconds 1 %7

    def convert(seconds):

        convert(seconds):0

    $ sec2time(10, 3)
    Out: '00:00:10.000'
    
    $ sec2time(1234567.8910, 0)
    Out: '14 days, 06:56:07'
    
    $ sec2time(1234567.8910, 4)
    Out: '14 days, 06:56:07.8910'
    
    $ sec2time([12, 345678.9], 3)
    Out: ['00:00:12.000', '4 days, 00:01:18.900']
    
    022____103
    $ sec2time(10, 3)
    Out: '00:00:10.000'
    
    $ sec2time(1234567.8910, 0)
    Out: '14 days, 06:56:07'
    
    $ sec2time(1234567.8910, 4)
    Out: '14 days, 06:56:07.8910'
    
    $ sec2time([12, 345678.9], 3)
    Out: ['00:00:12.000', '4 days, 00:01:18.900']
    
    04

    convert(seconds):4= convert(seconds):6

    convert(seconds):7convert(seconds):8

    Độ phức tạp về thời gian: O (1) Không gian phụ trợ: O (1)O(1)
    Auxiliary Space: O(1)