備忘メモ。
Linuxのdateコマンドで和暦を表示する。
CentOS7のdateコマンド(GNU date)で確認。確認したバージョンは8.22。
なお、ubuntu18.10でも表示を確認。Macは西暦のみで和暦は出てこなかった。
要約:
dateコマンドに「+%Ec」「+%EC」「+%Ex」「+%EX」「+%Ey」「+%EY」を指定すると、和暦に関する情報が返ってくる。
詳しくは、下記「余談1:」を参照。
和暦を表示するには、localeは「LC_TIME="ja_JP.UTF-8"」が設定されている必要がある。あと、当然日本語フォントはいる。
実行例:
$ date +%Ec 平成31年04月03日 20時08分58秒 $ date +%EC 平成 $ date +%Ex 平成31年04月03日 $ date +%EX 13時09分13秒 $ date +%Ey 31 $ date +%EY 平成31年
任意の日付も渡せる。
また、元年の場合は「元年」と表示される。
例では、平成と昭和、昭和と大正の表示をのせる。
$ date +%Ec -d "1989-01-08 00:00:00" 平成元年01月08日 00時00分00秒 $ date +%Ec -d "1989-01-07 23:59:59" 昭和64年01月07日 23時59分59秒 $ date +%Ec -d "1926-12-25 00:00:00" 昭和元年12月25日 00時00分00秒 $ date +%Ec -d "1926-12-24 23:59:59" 大正15年12月24日 23時59分59秒
なお、明治は6年からスタートしているので元年は無い。明治5年以前は西暦表示になる。
これは、明治6年に太陰太陽暦からグレゴリオ歴に変わったことが関係しているらしい。
詳しくは「余談2:」を参照。
$ date +%Ec -d "1873-01-01 00:00:00" 明治6年01月01日 00時00分00秒 $ date +%Ec -d "1872-12-31 23:59:59" 西暦1872年12月31日 23時59分59秒
それと、2019年4月3日時点では「令和対応」はまだ。
$ date +%Ec -d "2019-04-30 23:59:59"; date +%Ec -d "2019-05-01 00:00:00" 平成31年04月30日 23時59分59秒 平成31年05月01日 00時00分00秒
でも「glibc」に対してのパッチが出ているので、もう少ししたら対応されると思われるます。
https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=466afec30896585b60c2106df7a722a86247c9f3
https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=0bd545ee235dd02aa95394e62bca1faff02eeae3
余談1:
Eオプションはmanには載ってませんでした。
info dateで確認したら、以下のような説明がありました。
An optional modifier can follow the optional flag and width
specification. The modifiers are:'E'
Use the locale's alternate representation for date and time. This
modifier applies to the '%c', '%C', '%x', '%X', '%y' and '%Y'
conversion specifiers. In a Japanese locale, for example, '%Ex'
might yield a date format based on the Japanese Emperors' reigns.