Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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.
- # Return an empty string if the amount is 0. This function takes advantage of all units in the parent 'timeelapsed' function using 's'.
- # Input example: 1, 'seconds'. Abbreviations are not accepted.
- # Output example: '1 second'
- def handle_plurals(amount, unit):
- if amount == 1:
- return '1 '+unit[0:len(unit)-1]
- elif amount == 0:
- return ''
- else:
- return str(amount) + ' ' + unit
- # A0. Format an amount of elapsed time.
- # Input: value, an int or float.
- # unit, a string for the unit used by the value. that can be the full name ("milliseconds") or an abbreviation ("ms").
- # Only milliseconds and seconds are supported, because these are the only common output units. Milliseconds will be converted to seconds.
- #
- # Output: The formatting and conversion ratios are the same as the built-in timesince and timeuntil filters: a human-readable string such as '5
- # 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
- # 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
- # they are the largest unit. This function uses 365-day years and 30-day months.
- #
- # Template syntax example: {{ stopwatch_result | timeelapsed:"ms" }}
- def timeelapsed(value, unit):
- # Convert to seconds. If the value is more than 1 second, then round down to integer seconds.
- if (unit == "milliseconds" or unit == "ms") and value > 1000:
- value //= 1000
- else:
- if (unit == "milliseconds" or unit == "ms") and value < 1000:
- value /= 1000
- return str(value) + ' seconds'
- seconds = value % 60
- value //= 60
- minutes = value % 60
- value //= 60
- hours = value % 24
- value //= 24
- # If weeks are the largest unit, then use mod 7. If months or years are the largest unit, then use mod 30.
- if value < 30:
- days = value % 7
- value //= 7
- weeks = value % 7
- months = 0
- years = 0
- else:
- days = value % 30
- value //= 30
- weeks = 0
- months = value % 12
- years = value // 12
- years_string = handle_plurals(years, 'years')
- months_string = handle_plurals(months, 'months')
- weeks_string = handle_plurals(weeks, 'weeks')
- days_string = handle_plurals(days, 'days')
- hours_string = handle_plurals(hours, 'hours')
- minutes_string = handle_plurals(minutes, 'minutes')
- seconds_string = handle_plurals(seconds, 'seconds')
- if years_string != '':
- if months_string != '':
- return years_string + ', ' + months_string
- else:
- return years_string
- elif months_string != '':
- if days_string != '':
- return months_string + ', ' + days_string
- else:
- return months_string
- elif weeks_string != '':
- if days_string != '':
- return weeks_string + ', ' + days_string
- else:
- return weeks_string
- elif days_string != '':
- if hours_string != '':
- return days_string + ', ' + hours_string
- else:
- return days_string
- elif hours_string != '':
- if minutes_string != '':
- return hours_string + ', ' + minutes_string
- else:
- return hours_string
- elif minutes_string != '':
- if seconds_string != '':
- return minutes_string + ', ' + seconds_string
- else:
- return minutes_string
- else:
- return seconds_string
Add Comment
Please, Sign In to add comment