Skip to content

Commit

Permalink
add support format "昭53.5.31"
Browse files Browse the repository at this point in the history
  • Loading branch information
taisukef committed Nov 23, 2023
1 parent 7b7a8d1 commit 1d1b988
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
18 changes: 15 additions & 3 deletions Day.js
Expand Up @@ -46,18 +46,30 @@ class Day {
}
if (!flg) {
year = toHalfNum(year);
const n2 = year.match(/(..)\s*(\d+|元)\s*年\s*(\d+)\s*月\s*(\d+)\s*日/);
const n2 = year.match(/(..?)\s*(\d+|元)\s*年\s*(\d+)\s*月\s*(\d+)\s*日/);
if (n2) {
const wa = WAREKI_JA[n2[1]];
if (!wa) {
throw new Error("illegal date");
throw new Error("illegal date: " + n2[1]);
}
const y = n2[2] == "元" ? 1 : n2[2];
year = wa + parseInt(y, 10) - 1;
month = parseInt(n2[3], 10);
day = parseInt(n2[4], 10);
} else {
throw new Error("illegal date");
const n2 = year.match(/(\D\D?)\s*(\d+)\s*.\s*(\d+)\s*.\s*(\d+)\s*/);
if (n2) {
const wa = WAREKI_JA[n2[1].trim()];
if (!wa) {
throw new Error("illegal date: [" + n2[1] + "]");
}
const y = n2[2];
year = wa + parseInt(y, 10) - 1;
month = parseInt(n2[3], 10);
day = parseInt(n2[4], 10);
} else {
throw new Error("illegal date");
}
}
}
} else if (year instanceof Date) {
Expand Down
5 changes: 5 additions & 0 deletions WAREKI.js
Expand Up @@ -17,6 +17,11 @@ const WAREKI_JA = {
"昭和": 1926,
"平成": 1989,
"令和": 2019,
"明": 1868,
"大": 1912,
"昭": 1926,
"平": 1989,
"令": 2019,
};


Expand Down
6 changes: 6 additions & 0 deletions test/Day.test.js
Expand Up @@ -289,3 +289,9 @@ Deno.test("constructor with TimeZone", () => {
t.assertEquals(new Day(new TimeZone(9 + 24)), new Day().dayAfter(1));
t.assertEquals(new Day(new TimeZone(9 - 24)), new Day().dayBefore(1));
});
Deno.test("wareki", () => {
t.assertEquals(new Day("平成1年1月1日"), new Day("1989-01-01"));
t.assertEquals(new Day("平1年1月1日"), new Day("1989-01-01"));
t.assertEquals(new Day("昭53.5.31"), new Day("1978-05-31"));
t.assertEquals(new Day("令 2.12.23"), new Day("2020-12-23"));
});
2 changes: 1 addition & 1 deletion test/WAREKI.test.js
Expand Up @@ -11,4 +11,4 @@ Deno.test("simple", () => {
t.assertEquals(wareki2year("令和4年"), 2022);
t.assertEquals(year2wareki(1978), "昭和53年");
t.assertEquals(wareki2year("昭和53年"), 1978);
});
});

0 comments on commit 1d1b988

Please sign in to comment.