#!/usr/bin/env julia
# birth.jl
using Dates
# --- ヘルパ ---
const WDAY_SHORT = ["月","火","水","木","金","土","日"]
const WDAY_LONG = ["月曜日","火曜日","水曜日","木曜日","金曜日","土曜日","日曜日"]
fmt_date_ja(d::Date; long_wday::Bool=true) = string(
Dates.format(d, dateformat"yyyy年m月d日"), "(",
(long_wday ? WDAY_LONG : WDAY_SHORT)[dayofweek(d)], ")"
)
# 引数: [YYYY MM DD [ENDYEAR]]
function parse_args()
if length(ARGS) == 0
return Date(1953, 9, 20), year(Dates.today())
elseif length(ARGS) == 3 || length(ARGS) == 4
b = Date(parse(Int, ARGS[1]), parse(Int, ARGS[2]), parse(Int, ARGS[3]))
lasty = (length(ARGS) == 4) ? parse(Int, ARGS[4]) : year(Dates.today())
return b, lasty
else
error("使い方: ./birth.jl または ./birth.jl YYYY M D [ENDYEAR]")
end
end
function list_birthdays(birth::Date, last_year::Int)
last_year = max(last_year, year(birth)) # 下限は誕生年
for y in year(birth):last_year
d = Date(y, month(birth), day(birth))
age = y - year(birth)
star = (dayofweek(d) == 7) ? "*" : "" # 7 = 日曜
println(fmt_date_ja(d; long_wday=true), " ", age, "歳 ", star)
end
end
# --- 本体 ---
birthday, list_until = parse_args()
today = Dates.today()
println("【基礎情報】")
println("今日 : ", fmt_date_ja(today; long_wday=true))
println("誕生日 : ", fmt_date_ja(birthday; long_wday=true))
println("\n【差分と年齢】")
days_elapsed = Dates.value(today - birthday)
age_years = year(today) - year(birthday) - (Date(year(today), month(birthday), day(birthday)) > today ? 1 : 0)
println("生誕からの経過日数 : ", days_elapsed, "日")
println("満年齢 : ", age_years, "歳")
println("\n【年ごとの誕生日一覧】(*は日曜) ※最終年:", list_until, "年")
list_birthdays(birthday, list_until)