Skip to content

Commit

Permalink
Remove support for 24H time format override
Browse files Browse the repository at this point in the history
  • Loading branch information
cdubz committed Mar 25, 2023
1 parent decc97a commit 5875ef9
Show file tree
Hide file tree
Showing 13 changed files with 16 additions and 247 deletions.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions babybuddy/formats/en/formats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-

# Add custom "short" version of `MONTH_DAY_FORMAT`. This customization will
# only work with the locale format locale specified by this file.
SHORT_MONTH_DAY_FORMAT = 'M j'
11 changes: 0 additions & 11 deletions babybuddy/formats/pt/formats.py

This file was deleted.

9 changes: 0 additions & 9 deletions babybuddy/formats/tr/formats.py

This file was deleted.

64 changes: 0 additions & 64 deletions babybuddy/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,9 @@

from django.conf import settings
from django.utils import timezone, translation
from django.conf.locale.en import formats as formats_en_us
from django.conf.locale.en_GB import formats as formats_en_gb
from django.contrib.auth.middleware import RemoteUserMiddleware


def update_en_us_date_formats():
"""
Update the datetime formats for the en-US locale. This is handled here and
not using `FORMAT_MODULE_PATH` because the processing of format modules
does not allow us to distinguish appropriately between en-US and en-GB
based on user settings.
"""
if settings.USE_24_HOUR_TIME_FORMAT:
formats_en_us.DATETIME_FORMAT = "N j, Y, H:i:s"
custom_input_formats = [
"%m/%d/%Y %H:%M:%S", # '10/25/2006 14:30:59'
"%m/%d/%Y %H:%M", # '10/25/2006 14:30'
]
formats_en_us.SHORT_DATETIME_FORMAT = "m/d/Y G:i:s"
formats_en_us.TIME_FORMAT = "H:i:s"
else:
# These formats are added to support the locale style of Baby Buddy's
# frontend library, which uses momentjs.
custom_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'
]

# Add custom "short" version of `MONTH_DAY_FORMAT`.
formats_en_us.SHORT_MONTH_DAY_FORMAT = "M j"

# Append all other input formats from the base locale.
formats_en_us.DATETIME_INPUT_FORMATS = (
custom_input_formats + formats_en_us.DATETIME_INPUT_FORMATS
)


def update_en_gb_date_formats():
if settings.USE_24_HOUR_TIME_FORMAT:
# 25 October 2006 14:30:00
formats_en_gb.DATETIME_FORMAT = "j F Y H:i:s"
custom_input_formats = [
"%d/%m/%Y %H:%M:%S", # '25/10/2006 14:30:59'
"%d/%m/%Y %H:%M", # '25/10/2006 14:30'
]
formats_en_gb.SHORT_DATETIME_FORMAT = "d/m/Y H:i"
formats_en_gb.TIME_FORMAT = "H:i"
else:
formats_en_gb.DATETIME_FORMAT = "j F Y f a" # 25 October 2006 2:30 p.m
# These formats are added to support the locale style of Baby Buddy's
# frontend library, which uses momentjs.
custom_input_formats = [
"%d/%m/%Y %I:%M:%S %p", # '25/10/2006 2:30:59 PM'
"%d/%m/%Y %I:%M %p", # '25/10/2006 2:30 PM'
]

# Append all other input formats from the base locale.
formats_en_gb.DATETIME_INPUT_FORMATS = (
custom_input_formats + formats_en_gb.DATETIME_INPUT_FORMATS
)


class UserLanguageMiddleware:
"""
Customizes settings based on user language setting.
Expand All @@ -85,11 +26,6 @@ def __call__(self, request):
language = settings.LANGUAGE_CODE

if language:
if language == "en-US":
update_en_us_date_formats()
elif language == "en-GB":
update_en_gb_date_formats()

# Set the language before generating the response.
translation.activate(language)

Expand Down
10 changes: 0 additions & 10 deletions babybuddy/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,6 @@

FORMAT_MODULE_PATH = ["babybuddy.formats"]

# 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 = bool(
strtobool(os.environ.get("USE_24_HOUR_TIME_FORMAT") or "False")
)


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/
Expand Down
11 changes: 11 additions & 0 deletions babybuddy/tests/formats/tests_en.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
import datetime

from django.test import TestCase
from django.utils.formats import date_format


class FormatsTestCase(TestCase):
def test_short_month_day_format(self):
dt = datetime.datetime(year=2021, month=7, day=31, hour=5, minute=5, second=5)
self.assertEqual(date_format(dt, "SHORT_MONTH_DAY_FORMAT"), "Jul 31")
70 changes: 0 additions & 70 deletions babybuddy/tests/formats/tests_en_gb.py

This file was deleted.

67 changes: 0 additions & 67 deletions babybuddy/tests/formats/tests_en_us.py

This file was deleted.

6 changes: 0 additions & 6 deletions core/tests/tests_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,3 @@ def test_datetime_short(self):
formats.date_format(date, format="TIME_FORMAT"),
),
)

date = timezone.localtime() - timezone.timedelta(days=500)
self.assertEqual(
datetime.datetime_short(date),
formats.date_format(date, format="SHORT_DATETIME_FORMAT"),
)
9 changes: 0 additions & 9 deletions docs/configuration/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,3 @@ the user settings form.
**See also**

[List of tz database time zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)

## `USE_24_HOUR_TIME_FORMAT`

*Default:* `False`

Whether to force 24-hour time format for locales that do not ordinarily use it
(e.g. `en`). Support for this feature must be implemented on a per-locale basis.
See format files under [`babybuddy/formats`](https://github.com/babybuddy/babybuddy/tree/master/babybuddy/formats)
for supported locales.
1 change: 0 additions & 1 deletion gulpfile.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ module.exports = {
},
testsConfig: {
isolated: [
'babybuddy.tests.formats.tests_en_us.FormatsTestCase.test_use_24_hour_time_format',
'babybuddy.tests.tests_views.ViewsTestCase.test_password_reset'
],
},
Expand Down

0 comments on commit 5875ef9

Please sign in to comment.