yazdmich

Untitled

Dec 2nd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.73 KB | None | 0 0
  1. # A1. This function is only useful for returning a value to 'timeelapsed'. Return the number and unit, using the plural of the unit if the amount is greater than 1.
  2. # Return an empty string if the amount is 0. This function takes advantage of all units in the parent 'timeelapsed' function using 's'.
  3. # Input example: 1, 'seconds'. Abbreviations are not accepted.
  4. # Output example: '1 second'
  5. def handle_plurals(amount, unit):
  6.     if amount == 1:
  7.         return '1 '+unit[0:len(unit)-1]
  8.     elif amount == 0:
  9.         return ''
  10.     else:
  11.         return str(amount) + ' ' + unit
  12.  
  13. # A0. Format an amount of elapsed time.
  14. # Input: value, an int or float.
  15. # unit, a string for the unit used by the value. that can be the full name ("milliseconds") or an abbreviation ("ms").
  16. # Only milliseconds and seconds are supported, because these are the only common output units. Milliseconds will be converted to seconds.
  17. #
  18. # Output: The formatting and conversion ratios are the same as the built-in timesince and timeuntil filters: a human-readable string such as '5
  19. # seconds' or '1 month, 17 days'. The only behavioral difference is that seconds will be shown instead of rounded to 0 minutes, and if the value is
  20. # less than a second, there will be up to three decimal places. Only the two largest adjacent units are shown. Weeks, however, will be shown only when
  21. # they are the largest unit. This function uses 365-day years and 30-day months.
  22. #
  23. # Template syntax example: {{ stopwatch_result | timeelapsed:"ms" }}
  24. def timeelapsed(value, unit):
  25.     # Convert to seconds. If the value is more than 1 second, then round down to integer seconds.
  26.     if (unit == "milliseconds" or unit == "ms") and value > 1000:
  27.         value //= 1000
  28.     else:
  29.         if (unit == "milliseconds" or unit == "ms") and value < 1000:
  30.             value /= 1000
  31.             return str(value) + ' seconds'
  32.     seconds = value % 60
  33.     value //= 60
  34.     minutes = value % 60
  35.     value //= 60
  36.     hours = value % 24
  37.     value //= 24
  38.     # If weeks are the largest unit, then use mod 7. If months or years are the largest unit, then use mod 30.
  39.     if value < 30:
  40.         days = value % 7
  41.         value //= 7
  42.         weeks = value % 7
  43.         months = 0
  44.         years = 0
  45.     else:
  46.         days = value % 30
  47.         value //= 30
  48.         weeks = 0
  49.         months = value % 12
  50.         years = value // 12
  51.     years_string = handle_plurals(years, 'years')
  52.     months_string = handle_plurals(months, 'months')
  53.     weeks_string = handle_plurals(weeks, 'weeks')
  54.     days_string = handle_plurals(days, 'days')
  55.     hours_string = handle_plurals(hours, 'hours')
  56.     minutes_string = handle_plurals(minutes, 'minutes')
  57.     seconds_string = handle_plurals(seconds, 'seconds')
  58.     if years_string != '':
  59.         if months_string != '':
  60.             return years_string + ', ' + months_string
  61.         else:
  62.             return years_string
  63.     elif months_string != '':
  64.         if days_string != '':
  65.             return months_string + ', ' + days_string
  66.         else:
  67.             return months_string
  68.     elif weeks_string != '':
  69.         if days_string != '':
  70.             return weeks_string + ', ' + days_string
  71.         else:
  72.             return weeks_string
  73.     elif days_string != '':
  74.         if hours_string != '':
  75.             return days_string + ', ' + hours_string
  76.         else:
  77.             return days_string
  78.     elif hours_string != '':
  79.         if minutes_string != '':
  80.             return hours_string + ', ' + minutes_string
  81.         else:
  82.             return hours_string
  83.     elif minutes_string != '':
  84.         if seconds_string != '':
  85.             return minutes_string + ', ' + seconds_string
  86.         else:
  87.             return minutes_string
  88.     else:
  89.         return seconds_string
Add Comment
Please, Sign In to add comment