Skip to content

Commit c037f99

Browse files
Fixes time format error when compiling under Windows (#2228)
* Fixes encoding error when building under Windows * This will keep the formatting the same and allow it to work under Windows. * This will keep the formatting the same and allow it to work under Windows. --------- Co-authored-by: Sam <sam@writethedocs.org>
1 parent a8970f1 commit c037f99

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

docs/_ext/core.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from .utils import load_yaml
99

1010
import logging
11+
import sys
1112
from datetime import datetime, time, timedelta
1213

1314
try:
@@ -17,11 +18,23 @@
1718

1819
log = logging.getLogger(__name__)
1920

21+
22+
def get_24hour_time(dt):
23+
return dt.strftime('%H:%M')
24+
25+
26+
def get_12hour_time(dt):
27+
hour = dt.strftime('%I')
28+
hour = hour.lstrip('0')
29+
return f'{hour}:{dt.strftime("%M %p")}'
30+
31+
2032
TIME_FORMATS = {
21-
'24h': '%H:%M',
22-
'12h': '%-I:%M %p',
33+
'24h': get_24hour_time,
34+
'12h': get_12hour_time,
2335
}
2436

37+
2538
# Some DST timezones aren't "real" timezones.
2639
TIMEZONE_TRANSLATION_PYTZ = {
2740
'CEST': 'CET',
@@ -134,15 +147,15 @@ def load_conference_context_from_yaml(shortcode, year, year_str, page):
134147
)
135148
if not display_timezones:
136149
# In a single-tz schedule, render just naive time, first schedule item with TZ
137-
schedule_item['time'] = naive_item_start.strftime(TIME_FORMATS[data['time_format']])
150+
schedule_item['time'] = TIME_FORMATS[data['time_format']](naive_item_start)
138151
if not naive_next_item_default_start and not data.get('flaghasfood'):
139152
schedule_item['time'] += ' ' + data['tz']
140153
if display_timezones:
141154
# In multi-timezone, first convert to aware, then format for each timezone.
142155
# Note that we need to combine with conf date to know whether there is DST.
143156
aware_item_start = datetime.combine(conf_date, naive_item_start.time()).replace(tzinfo=conf_timezone)
144157
schedule_item['time'] = '<br>'.join([
145-
aware_item_start.astimezone(tz).strftime(TIME_FORMATS[data['time_format']]) + ' ' + tz_name
158+
TIME_FORMATS[data['time_format']](aware_item_start.astimezone(tz)) + ' ' + tz_name
146159
for tz_name, tz in display_timezones
147160
])
148161
naive_next_item_default_start = naive_item_start + duration

0 commit comments

Comments
 (0)