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 4 years, 3 months ago
- UTC DateTime field by ludo 5 years, 9 months ago
- FuzzyDateTimeField by japerk 4 years, 2 months ago
- Date/time util template filters by marinho 5 years, 7 months ago
- Smart i18n date diff (twitter like) by Batiste 4 years, 2 months ago
Comments
hi waylan,
nice snippet. In your description there is a little typo:
a closing bracket too much :)
#