Repository: joke2k/faker
Pull request #1888 (2023-07-12 07:52:12)

add ja_JP datetime function
### What does this change
Add `MONTH_NAMES`, `DAY_NAMES` and 'TRADITIONAL_MONTH_NAMES` to ja_JP locale.

### What was wrong
Not implemented before.

### How this fixes it
Not implemented before.


Patch:
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 00000000..6ea489d0
--- /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]


Test patch:
diff --git a/tests/providers/test_date_time.py b/tests/providers/test_date_time.py
index 50389be9..492b3ec3 100644
--- a/tests/providers/test_date_time.py
+++ b/tests/providers/test_date_time.py
@@ -28,6 +28,7 @@ from faker.providers.date_time.es_ES import Provider as EsEsProvider
 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 @@ class TestFrCa(unittest.TestCase):
     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()