Comment detail

西暦 to 和暦 (Nested Flatten)
明治 6 年より前は範囲外で。あまり美しくないですが。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import std.stdio;
import std.date;
import std.string;

void printWareki(string date){
    auto d = parse(date);
    if(d == d_time_nan){
        writefln("範囲外");
        return;
    }

    auto meiji6 = parse("1873/1/1");
    auto taisho = parse("1912/7/30");
    auto showa = parse("1926/12/25");
    auto heisei = parse("1989/1/8");

    auto dateParts = date.split("/");
    auto year = atoi(dateParts[0]);
    auto month = atoi(dateParts[1]);
    auto day = atoi(dateParts[2]);

    void output(char eraNameCap){
        auto diff = (['M' : 1867, 'T' : 1911, 'S' : 1925, 'H' : 1988]);
        auto eraName = (['M' : "明治", 'T' : "大正", 'S' : "昭和", 'H' : "平成"]);
        writef("%s%d年%d月%d日", eraName[eraNameCap],
                    year - diff[eraNameCap], month, day);
    }

    if(meiji6 <= d && d < taisho){
        output('M');
    }
    else if(d == taisho){
        output('M'); writef(" "); output('T');
    }
    else if(taisho < d && d < showa){
        output('T');
    }
    else if(d == showa){
        output('T'); writef(" "); output('S');
    }
    else if(showa < d && d < heisei){
        output('S');
    }
    else if(heisei <= d){
        output('H');
    }
    else{
        writef("範囲外");
    }
    writefln();
}

void main(){
    printWareki("1868/12/2");   // 範囲外
    printWareki("1926/12/24");  // 大正15年12月24日
    printWareki("2007/12/01");  // 平成19年12月1日
    printWareki("1926/12/25");  // 大正15年12月25日 昭和1年12月25日
    printWareki("1868/1/2");    // 範囲外
    printWareki("1868/100/2");  // 範囲外
    printWareki("1904/02/06");  // 明治37年2月6日
}

Index

Feed

Other

Link

Pathtraq

loading...