From 696d11e5a80d55228353b7d541e2903384187c6a Mon Sep 17 00:00:00 2001 From: Sean H <50758691+cyanghsieh@users.noreply.github.com> Date: Wed, 12 Jul 2023 22:08:21 +0800 Subject: [PATCH] Add `ja_JP` locale for `datetime` (#1888) --- faker/providers/date_time/ja_JP/__init__.py | 54 +++++++++++++++++++++ tests/providers/test_date_time.py | 19 ++++++++ 2 files changed, 73 insertions(+) create mode 100644 faker/providers/date_time/ja_JP/__init__.py diff --git a/faker/providers/date_time/ja_JP/__init__.py b/faker/providers/date_time/ja_JP/__init__.py new file mode 100644 index 0000000000..6ea489d024 --- /dev/null +++ b/faker/providers/date_time/ja_JP/__init__.py @@ -0,0 +1,54 @@ +from .. import Provider as DateTimeProvider + + +class Provider(DateTimeProvider): + MONTH_NAMES = { + "01": "一月", + "02": "二月", + "03": "三月", + "04": "四月", + "05": "五月", + "06": "六月", + "07": "七月", + "08": "八月", + "09": "九月", + "10": "十月", + "11": "十一月", + "12": "十二月", + } + + TRADITIONAL_MONTH_NAMES = { + "01": "睦月", + "02": "如月", + "03": "弥生", + "04": "卯月", + "05": "皐月", + "06": "水無月", + "07": "文月", + "08": "葉月", + "09": "長月", + "10": "神無月", + "11": "霜月", + "12": "師走", + } + DAY_NAMES = { + "0": "日曜日", + "1": "月曜日", + "2": "火曜日", + "3": "水曜日", + "4": "木曜日", + "5": "金曜日", + "6": "土曜日", + } + + def day_of_week(self) -> str: + day = self.date("%w") + return self.DAY_NAMES[day] + + def month_name(self) -> str: + month = self.month() + return self.MONTH_NAMES[month] + + def traditional_month_name(self) -> str: + month = self.month() + return self.TRADITIONAL_MONTH_NAMES[month] diff --git a/tests/providers/test_date_time.py b/tests/providers/test_date_time.py index 50389be9fc..492b3ec342 100644 --- a/tests/providers/test_date_time.py +++ b/tests/providers/test_date_time.py @@ -28,6 +28,7 @@ from faker.providers.date_time.fr_FR import Provider as FrFrProvider from faker.providers.date_time.hy_AM import Provider as HyAmProvider from faker.providers.date_time.it_IT import Provider as ItItProvider +from faker.providers.date_time.ja_JP import Provider as JaJpProvider from faker.providers.date_time.nl_NL import Provider as NlProvider from faker.providers.date_time.no_NO import Provider as NoNoProvider from faker.providers.date_time.pl_PL import Provider as PlProvider @@ -1264,3 +1265,21 @@ def test_day(self): def test_month(self): day = self.fake.month_name() assert day in FrFrProvider.MONTH_NAMES.values() + + +class TestJaJp(unittest.TestCase): + def setUp(self): + self.fake = Faker("ja_JP") + Faker.seed(0) + + def test_day(self): + day = self.fake.day_of_week() + assert day in JaJpProvider.DAY_NAMES.values() + + def test_month(self): + month = self.fake.month_name() + assert month in JaJpProvider.MONTH_NAMES.values() + + def test_traditional_month(self): + month = self.fake.traditional_month_name() + assert month in JaJpProvider.TRADITIONAL_MONTH_NAMES.values()