Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce an option to template tag to show 24h instead #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions openinghours/templates/openinghours/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ <h2>Display opening hours</h2>

{% opening_hours location %}
<hr>
<p>Time-formatted opening hours (24h):</p>
{% verbatim %}
<pre>{% opening_hours location timeformat=24h %}</pre>
{% endverbatim %}
{% opening_hours location timeformat="24h" %}
<hr>
<p>Concise opening hours:</p>
{% verbatim %}
<pre>{% opening_hours location concise=1 %}</pre>
Expand Down
41 changes: 27 additions & 14 deletions openinghours/templatetags/openinghours_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def get_closing_rule_for_now(location, attr=None):


@register.simple_tag
def opening_hours(location=None, concise=False):
def opening_hours(location=None, concise=False,timeformat="12h"):
"""
Creates a rendered listing of hours.
"""
Expand All @@ -115,19 +115,32 @@ def opening_hours(location=None, concise=False):

ohrs.order_by('weekday', 'from_hour')

for o in ohrs:
days.append({
'day_number': o.weekday,
'name': o.get_weekday_display(),
'from_hour': o.from_hour,
'to_hour': o.to_hour,
'hours': '%s%s - %s%s' % (
o.from_hour.strftime('%I:%M').lstrip('0'),
o.from_hour.strftime('%p').lower(),
o.to_hour.strftime('%I:%M').lstrip('0'),
o.to_hour.strftime('%p').lower()
)
})
if timeformat == "24h":
for o in ohrs:
days.append({
'day_number': o.weekday,
'name': o.get_weekday_display(),
'from_hour': o.from_hour,
'to_hour': o.to_hour,
'hours': '%s - %s' % (
o.from_hour.strftime('%H:%M'),
o.to_hour.strftime('%H:%M'),
)
})
else:
for o in ohrs:
days.append({
'day_number': o.weekday,
'name': o.get_weekday_display(),
'from_hour': o.from_hour,
'to_hour': o.to_hour,
'hours': '%s%s - %s%s' % (
o.from_hour.strftime('%I:%M').lstrip('0'),
o.from_hour.strftime('%p').lower(),
o.to_hour.strftime('%I:%M').lstrip('0'),
o.to_hour.strftime('%p').lower()
)
})

open_days = [o.weekday for o in ohrs]
for day_number, day_name in WEEKDAYS:
Expand Down