Login

Seconds-to-Duration Template Tag

Author:
danward
Posted:
March 27, 2009
Language:
Python
Version:
1.0
Score:
1 (after 2 ratings)

Does exactly what it says on the tin!

This template tag, when implemented, converts a duration (in seconds) to a more meaningful format. It has a short and long setting, which is easy to manipulate for your needs. Apologies if something already exists like this, however I felt that writing this would be quicker than trying to find it online.

As an example, given the duration 84658:

Short (default): 23 hrs 30 mins 58 secs

Long: 23 hours, 30 minutes and 58 seconds

All the best,

Dan Ward.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Import template library
from django import template

# Set register
register = template.Library()

# Register filter
@register.filter
def sectodur(value, arg = ''):
	
	"""
	#######################################################
	#                                                     #
	#   Seconds-to-Duration Template Tag                  #
	#   Dan Ward 2009 (http://d-w.me)                     #
	#                                                     #
	#######################################################
	
	Usage: {{ VALUE|sectodur[:"long"] }}
	
	NOTE: Please read up 'Custom template tags and filters'
	      if you are unsure as to how the template tag is
	      implemented in your project.
	"""
		
	# Place seconds in to integer
	secs = int(value)
	
	# If seconds are greater than 0
	if secs > 0:
		
		# Import math library
		import math
		
		# Place durations of given units in to variables
		daySecs = 86400
		hourSecs = 3600
		minSecs = 60
		
		# If short string is enabled
		if arg != 'long':
			
			# Set short names
			dayUnitName = ' day'
			hourUnitName = ' hr'
			minUnitName = ' min'
			secUnitName = ' sec'
			
			# Set short duration unit splitters
			lastDurSplitter = ' '
			nextDurSplitter = lastDurSplitter
		
		# If short string is not provided or any other value
		else:
			
			# Set long names
			dayUnitName = ' day'
			hourUnitName = ' hour'
			minUnitName = ' minute'
			secUnitName = ' second'
			
			# Set long duration unit splitters
			lastDurSplitter = ' and '
			nextDurSplitter = ', '
		
		# Create string to hold outout
		durationString = ''
		
		# Calculate number of days from seconds
		days = int(math.floor(secs / int(daySecs)))
		
		# Subtract days from seconds
		secs = secs - (days * int(daySecs))
		
		# Calculate number of hours from seconds (minus number of days)
		hours = int(math.floor(secs / int(hourSecs)))
		
		# Subtract hours from seconds
		secs = secs - (hours * int(hourSecs))
		
		# Calculate number of minutes from seconds (minus number of days and hours)
		minutes = int(math.floor(secs / int(minSecs)))
		
		# Subtract days from seconds
		secs = secs - (minutes * int(minSecs))
		
		# Calculate number of seconds (minus days, hours and minutes)
		seconds = secs
		
		# If number of days is greater than 0				
		if days > 0:
			
			# Add multiple days to duration string
			durationString += ' ' + str(days) + dayUnitName + (days > 1 and 's' or '')
		
		# Determine if next string is to be shown
		if hours > 0:

			# If there are no more units after this
			if minutes <= 0 and seconds <= 0:

				# Set hour splitter to last
				hourSplitter = lastDurSplitter

			# If there are unit after this
			else:

				# Set hour splitter to next
				hourSplitter = (len(durationString) > 0 and nextDurSplitter or '')
		
		# If number of hours is greater than 0				
		if hours > 0:

			# Add multiple days to duration string
			durationString += hourSplitter + ' ' + str(hours) + hourUnitName + (hours > 1 and 's' or '')
		
		# Determine if next string is to be shown
		if minutes > 0:
			
			# If there are no more units after this
			if seconds <= 0:
			
				# Set minute splitter to last
				minSplitter = lastDurSplitter
			
			# If there are unit after this
			else:
				
				# Set minute splitter to next
				minSplitter = (len(durationString) > 0 and nextDurSplitter or '')
		
		# If number of minutes is greater than 0				
		if minutes > 0:

			# Add multiple days to duration string
			durationString += minSplitter + ' ' + str(minutes) + minUnitName + (minutes > 1 and 's' or '')
		
		# Determine if next string is last
		if seconds > 0:
			
			# Set second splitter
			secSplitter = (len(durationString) > 0 and lastDurSplitter or '')
		
		# If number of seconds is greater than 0				
		if seconds > 0:

			# Add multiple days to duration string
			durationString += secSplitter + ' ' + str(seconds) + secUnitName + (seconds > 1 and 's' or '')
			
		# Return duration string
		return durationString.strip()
		
	# If seconds are not greater than 0
	else:
		
		# Provide 'No duration' message
		return 'No duration'
	

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.