Add USE_24_HOUR_TIME_FORMAT setting (#148)

This commit is contained in:
Christopher C. Wells 2020-07-21 20:46:46 -07:00
parent c5e5589c22
commit d5dc329cf8
2 changed files with 29 additions and 24 deletions

View File

@ -1,24 +1,20 @@
# This files adds two new date input formats to support Baby Buddy's frontend
# library formats:
# - %m/%d/%Y %I:%M:%S %p
# - %m/%d/%Y %I:%M %p
#
# The remaining formats come from the base Django formats.
#
# See django.cong.locale.en.
DATETIME_INPUT_FORMATS = [
'%m/%d/%Y %I:%M:%S %p', # '10/25/2006 2:30:59 PM' (new)
'%m/%d/%Y %I:%M %p', # '10/25/2006 2:30 PM' (new)
'%Y-%m-%d %H:%M:%S', # '2006-10-25 14:30:59'
'%Y-%m-%d %H:%M:%S.%f', # '2006-10-25 14:30:59.000200'
'%Y-%m-%d %H:%M', # '2006-10-25 14:30'
'%Y-%m-%d', # '2006-10-25'
'%m/%d/%Y %H:%M:%S', # '10/25/2006 14:30:59'
'%m/%d/%Y %H:%M:%S.%f', # '10/25/2006 14:30:59.000200'
'%m/%d/%Y %H:%M', # '10/25/2006 14:30'
'%m/%d/%Y', # '10/25/2006'
'%m/%d/%y %H:%M:%S', # '10/25/06 14:30:59'
'%m/%d/%y %H:%M:%S.%f', # '10/25/06 14:30:59.000200'
'%m/%d/%y %H:%M', # '10/25/06 14:30'
'%m/%d/%y', # '10/25/06'
]
from django.conf import settings
from django.conf.locale.en import formats
# Override the regular locale settings to support 24 hour time.
if settings.USE_24_HOUR_TIME_FORMAT:
DATETIME_FORMAT = 'N j, Y, H:i:s'
TIME_FORMAT = 'H:i:s'
DATETIME_INPUT_FORMATS = [
'%m/%d/%Y %H:%I:%S' # '10/25/2006 14:30:59'
]
else:
# These formats are added to support the locale style of Baby Buddy's
# frontend library, which uses momentjs.
DATETIME_INPUT_FORMATS = [
'%m/%d/%Y %I:%M:%S %p', # '10/25/2006 2:30:59 PM'
'%m/%d/%Y %I:%M %p', # '10/25/2006 2:30 PM'
]
# Append all other input formats from the base locale.
DATETIME_INPUT_FORMATS.append(formats.DATETIME_INPUT_FORMATS)

View File

@ -1,4 +1,5 @@
import os
from distutils.util import strtobool
from django.utils.translation import gettext_lazy as _
from dotenv import load_dotenv, find_dotenv
@ -166,6 +167,14 @@ LANGUAGES = [
USE_L10N = True
# Custom setting that can be used to override the locale-based time set by
# USE_L10N _for specific locales_ to use 24-hour format. In order for this to
# work with a given locale it must be set at the FORMAT_MODULE_PATH with
# conditionals on this setting. See babybuddy/forms/en/formats.py for an example
# implementation for the English locale.
USE_24_HOUR_TIME_FORMAT = strtobool(os.environ.get('USE_24_HOUR_TIME_FORMAT'))
FORMAT_MODULE_PATH = ['babybuddy.formats']