lcast15

Python Seconds to Pretty String

Apr 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.83 KB | None | 0 0
  1. def Time2String(Duration, Names = [[' minute', ' minutes'], [' hour', ' hours'], [' day', ' days'], [' week', ' weeks'], [' year', ' years'], ', ']):
  2.     Duration = int(Duration)
  3.     DurValue = Duration
  4.     if Duration < 3600:
  5.         if math.floor(DurValue/60) == 1:
  6.             Duration = str(math.floor(DurValue/60)) + Names[0][0]
  7.         else:
  8.             Duration = str(math.floor(DurValue/60)) + Names[0][1]
  9.     elif Duration < 86400:
  10.         if math.floor(DurValue/60/60) == 1:
  11.             Duration = str(math.floor(DurValue/60/60)) + Names[1][0] + Names[5]
  12.         else:
  13.             Duration = str(math.floor(DurValue/60/60)) + Names[1][1] + Names[5]
  14.            
  15.         if math.floor(DurValue/60%60) == 1:
  16.             Duration = Duration + str(math.floor(DurValue/60%60)) + Names[0][0]
  17.         else:
  18.             Duration = Duration + str(math.floor(DurValue/60%60)) + Names[0][1]
  19.     elif Duration < 604800:
  20.         if math.floor(DurValue/60/60/24) == 1:
  21.             Duration = str(math.floor(DurValue/60/60/24)) + Names[2][0] + Names[5]
  22.         else:
  23.             Duration = str(math.floor(DurValue/60/60/24)) + Names[2][1] + Names[5]
  24.            
  25.         if math.floor(DurValue/60/60%24) == 1:
  26.             Duration = Duration + str(math.floor(DurValue/60/60%24)) + Names[1][0] + Names[5]
  27.         else:
  28.             Duration = Duration + str(math.floor(DurValue/60/60%24)) + Names[1][1] + Names[5]
  29.    
  30.         if math.floor(DurValue/60%60) == 1:
  31.             Duration = Duration + str(math.floor(DurValue/60%60)) + Names[0][0]
  32.         else:
  33.             Duration = Duration + str(math.floor(DurValue/60%60)) + Names[0][1]
  34.     elif Duration < 31449600:
  35.         if math.floor(DurValue/60/60/24/7) == 1:
  36.             Duration = str(math.floor(DurValue/60/60/24/7)) + Names[3][0] + Names[5]
  37.         else:
  38.             Duration = str(math.floor(DurValue/60/60/24/7)) + Names[3][1] + Names[5]
  39.        
  40.         if math.floor(DurValue/60/60/24%7) == 1:
  41.             Duration = Duration + str(math.floor(DurValue/60/60/24%7)) + Names[2][0] + Names[5]
  42.         else:
  43.             Duration = Duration + str(math.floor(DurValue/60/60/24%7)) + Names[2][1] + Names[5]
  44.            
  45.         if math.floor(DurValue/60/60%24) == 1:
  46.             Duration = Duration + str(math.floor(DurValue/60/60%24)) + Names[1][0] + Names[5]
  47.         else:
  48.             Duration = Duration + str(math.floor(DurValue/60/60%24)) + Names[1][1] + Names[5]
  49.    
  50.         if math.floor(DurValue/60%60) == 1:
  51.             Duration = Duration + str(math.floor(DurValue/60%60)) + Names[0][0]
  52.         else:
  53.             Duration = Duration + str(math.floor(DurValue/60%60)) + Names[0][1]
  54.     else:
  55.         if math.floor(DurValue/60/60/24/7/52) == 1:
  56.             Duration = str(math.floor(DurValue/60/60/24/7/52)) + Names[4][0] + Names[5]
  57.         else:
  58.             Duration = str(math.floor(DurValue/60/60/24/7/52)) + Names[4][1] + Names[5]
  59.        
  60.         if math.floor(DurValue/60/60/24/7%52) == 1:
  61.             Duration = Duration + str(math.floor(DurValue/60/60/24/7%52)) + Names[3][0] + Names[5]
  62.         else:
  63.             Duration = Duration + str(math.floor(DurValue/60/60/24/7%52)) + Names[3][1] + Names[5]
  64.        
  65.         if math.floor(DurValue/60/60/24%7) == 1:
  66.             Duration = Duration + str(math.floor(DurValue/60/60/24%7)) + Names[2][0] + Names[5]
  67.         else:
  68.             Duration = Duration + str(math.floor(DurValue/60/60/24%7)) + Names[2][1] + Names[5]
  69.            
  70.         if math.floor(DurValue/60/60%24) == 1:
  71.             Duration = Duration + str(math.floor(DurValue/60/60%24)) + Names[1][0] + Names[5]
  72.         else:
  73.             Duration = Duration + str(math.floor(DurValue/60/60%24)) + Names[1][1] + Names[5]
  74.    
  75.         if math.floor(DurValue/60%60) == 1:
  76.             Duration = Duration + str(math.floor(DurValue/60%60)) + Names[0][0]
  77.         else:
  78.             Duration = Duration + str(math.floor(DurValue/60%60)) + Names[0][1]
  79.            
  80.     return str(Duration)
  81.  
  82. #you can use this function with custom names and separator by using a list based off this:
  83. #[['single minute', 'multiple minutes'], ['single hour', 'multiple hours'], ['single day', 'multiple days'], ['single week', 'multiple weeks'], ['single year', 'multiple years'], 'separator (", " is the default one)']
  84. #just pass the list after the seconds value e.g: func(120, [['m', 'm'], ['h', 'h'], ['d', 'd'], ['w', 'w'], ['y', 'y'], ', '])
Advertisement
Add Comment
Please, Sign In to add comment