From d5dc329cf8e17def1ced61dd8b94b8848d9c5319 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Tue, 21 Jul 2020 20:46:46 -0700 Subject: [PATCH] Add USE_24_HOUR_TIME_FORMAT setting (#148) --- babybuddy/formats/en/formats.py | 44 +++++++++++++++------------------ babybuddy/settings/base.py | 9 +++++++ 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/babybuddy/formats/en/formats.py b/babybuddy/formats/en/formats.py index 67c4c5c2..8f651240 100644 --- a/babybuddy/formats/en/formats.py +++ b/babybuddy/formats/en/formats.py @@ -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) diff --git a/babybuddy/settings/base.py b/babybuddy/settings/base.py index f86275a8..f10cb7dd 100644 --- a/babybuddy/settings/base.py +++ b/babybuddy/settings/base.py @@ -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']