Python function to convert seconds into minutes, hours, and days

前端 未结 16 1326
不思量自难忘°
不思量自难忘° 2020-11-28 07:27

Question: Write a program that asks the user to enter a number of seconds, and works as follows:

  • There are 60 seconds in a minute. If the number of seconds

相关标签:
16条回答
  • 2020-11-28 07:52

    The "timeit" answer above that declares divmod to be slower has seriously flawed logic.

    Test1 calls operators.

    Test2 calls the function divmod, and calling a function has overhead.

    A more accurate way to test would be:

    import timeit
    
    def moddiv(a,b):
      q= a/b
      r= a%b
      return q,r
    
    a=10
    b=3
    md=0
    dm=0
    for i in range(1,10):
      c=a*i
      md+= timeit.timeit( lambda: moddiv(c,b))
      dm+=timeit.timeit( lambda: divmod(c,b))
    
    print("moddiv ", md)
    print("divmod ", dm)
    
    
    
    
    moddiv  5.806157339000492
    
    divmod  4.322451676005585
    

    divmod is faster.

    0 讨论(0)
  • 2020-11-28 07:54
    def convertSeconds(seconds):
        h = seconds//(60*60)
        m = (seconds-h*60*60)//60
        s = seconds-(h*60*60)-(m*60)
        return [h, m, s]
    

    The function input is a number of seconds, and the return is a list of hours, minutes and seconds which that amount of seconds represent.

    0 讨论(0)
  • 2020-11-28 07:57

    Although divmod() has been mentioned, I didn't see what I considered to be a nice example. Here's mine:

    q=972021.0000  # For example
    days = divmod(q, 86400) 
    # days[0] = whole days and
    # days[1] = seconds remaining after those days
    hours = divmod(days[1], 3600)
    minutes = divmod(hours[1], 60)
    print "%i days, %i hours, %i minutes, %i seconds" % (days[0], hours[0], minutes[0], minutes[1])
    

    Which outputs:

    11 days, 6 hours, 0 minutes, 21 seconds
    
    0 讨论(0)
  • 2020-11-28 07:57
    def seconds_to_dhms(time):
        seconds_to_minute   = 60
        seconds_to_hour     = 60 * seconds_to_minute
        seconds_to_day      = 24 * seconds_to_hour
    
        days    =   time // seconds_to_day
        time    %=  seconds_to_day
    
        hours   =   time // seconds_to_hour
        time    %=  seconds_to_hour
    
        minutes =   time // seconds_to_minute
        time    %=  seconds_to_minute
    
        seconds = time
    
        print("%d days, %d hours, %d minutes, %d seconds" % (days, hours, minutes, seconds))
    
    
    time = int(input("Enter the number of seconds: "))
    seconds_to_dhms(time)
    

    Output: Enter the number of seconds: 2434234232

    Result: 28174 days, 0 hours, 10 minutes, 32 seconds

    0 讨论(0)
  • 2020-11-28 08:03

    This tidbit is useful for displaying elapsed time to varying degrees of granularity.

    I personally think that questions of efficiency are practically meaningless here, so long as something grossly inefficient isn't being done. Premature optimization is the root of quite a bit of evil. This is fast enough that it'll never be your choke point.

    intervals = (
        ('weeks', 604800),  # 60 * 60 * 24 * 7
        ('days', 86400),    # 60 * 60 * 24
        ('hours', 3600),    # 60 * 60
        ('minutes', 60),
        ('seconds', 1),
        )
    
    def display_time(seconds, granularity=2):
        result = []
    
        for name, count in intervals:
            value = seconds // count
            if value:
                seconds -= value * count
                if value == 1:
                    name = name.rstrip('s')
                result.append("{} {}".format(value, name))
        return ', '.join(result[:granularity])
    

    ..and this provides decent output:

    In [52]: display_time(1934815)
    Out[52]: '3 weeks, 1 day'
    
    In [53]: display_time(1934815, 4)
    Out[53]: '3 weeks, 1 day, 9 hours, 26 minutes'
    
    0 讨论(0)
  • 2020-11-28 08:03
    def normalize_seconds(seconds: int) -> tuple:
        (days, remainder) = divmod(seconds, 86400)
        (hours, remainder) = divmod(remainder, 3600)
        (minutes, seconds) = divmod(remainder, 60)
    
        return namedtuple("_", ("days", "hours", "minutes", "seconds"))(days, hours, minutes, seconds)
    
    0 讨论(0)
提交回复
热议问题