Skip to content

Commit 0eac738

Browse files
authored
Merge pull request #6 from taichihub/02-my-calendar
20240906 カレンダーのプログラム(JavaScript)
2 parents 7b30787 + fc6c339 commit 0eac738

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

02.calendar/cal.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env node
2+
3+
import { Command } from "commander";
4+
import { DateTime } from "luxon";
5+
6+
function printCalendar(year, month) {
7+
if (year < 1900 || year > 2100 || month < 1 || month > 12) {
8+
console.error(
9+
"指定された年または月が無効です。(年:1900〜2100,月:1〜12が有効)",
10+
);
11+
process.exit(1);
12+
}
13+
14+
console.log(` ${month}${year}`);
15+
console.log("日 月 火 水 木 金 土");
16+
17+
const firstDay = DateTime.local(year, month, 1);
18+
const lastDay = firstDay.endOf("month");
19+
20+
const padding = " ".repeat((firstDay.weekday % 7) * 3);
21+
process.stdout.write(padding);
22+
23+
for (let day = firstDay; day <= lastDay; day = day.plus({ days: 1 })) {
24+
const formattedDay = day.day.toString().padStart(2);
25+
const isLastDay = day.hasSame(lastDay, "day");
26+
const isSaturday = day.weekday === 6;
27+
if (isLastDay) {
28+
console.log(formattedDay);
29+
console.log();
30+
} else if (isSaturday) {
31+
console.log(formattedDay);
32+
} else {
33+
process.stdout.write(`${formattedDay} `);
34+
}
35+
}
36+
}
37+
38+
function main() {
39+
const program = new Command();
40+
41+
program
42+
.option("-y, --year <year>", "年を指定", parseInt)
43+
.option("-m, --month <month>", "月を指定", parseInt)
44+
.parse(process.argv);
45+
46+
const options = program.opts();
47+
const now = DateTime.now();
48+
const year = options.year ?? now.year;
49+
const month = options.month ?? now.month;
50+
51+
printCalendar(year, month);
52+
}
53+
54+
main();

02.calendar/package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

02.calendar/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"fix": "prettier --write . && eslint --fix .",
44
"lint": "prettier --check . && eslint ."
55
},
6+
"dependencies": {
7+
"commander": "^12.1.0",
8+
"luxon": "^3.5.0"
9+
},
610
"devDependencies": {
711
"@eslint/js": "^9.9.0",
812
"eslint": "^9.9.0",

0 commit comments

Comments
 (0)