1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | from django import template
from bisect import bisect
register = template.Library()
@register.filter
def fuzzy_time(time):
"""
Formats a time as fuzzy periods of the day.
Accepts a datetime.time or datetime.datetime object.
"""
periods = ["Early-Morning", "Morning", "Mid-day", \
"Afternoon", "Evening", "Late-Night"]
breakpoints = [4, 10, 13, 17, 21]
try:
return periods[bisect(breakpoints, time.hour)]
except AttributeError: # Not a datetime object
return '' #Fail silently
|
More like this
- Fuzzy Date Diff Template Filter by zain 3 years, 2 months ago
- Smart i18n date diff (twitter like) by Batiste 3 years, 1 month ago
- Date/time util template filters by marinho 4 years, 6 months ago
- Days Since Filter by joe4444 5 years, 2 months ago
- FuzzyDateTimeField by japerk 3 years, 1 month ago
Comments
hi waylan,
nice snippet. In your description there is a little typo:
a closing bracket too much :)
#