Comment detail

年間カレンダー (Nested Flatten)
 Lua始めました。
 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
-- 閏年かどうか判定
L = 
function (y)
    if y % 400 == 0 then return 1 end
    if y % 100 == 0 then return 0 end
    if y %   4 == 0 then return 1 end
    return 0
end

-- 指定された日の曜日を取得
W = 
function (y, m, d)
    return tonumber(os.date('%w', os.time({ year = y, month = m, day = d })))
end

(function (v)
    local d = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
    local w = { 'Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa' }
    local m, t, y

    if #v ~= 2 then
        io.stderr:write('usage: ' .. v[0] .. ' [month] [year]\n')
        return 1
    end

    m, y = tonumber(v[1]), tonumber(v[2])

    if m == nil or y == nil then
        io.stderr:write('usage: ' .. v[0] .. ' [month] [year]\n')
        return 1
    end

    if m < 1 or m > 12 then
        io.stderr:write('cal: illegal month value: use 1-12\n')
        return 1
    end
    if y < 1 or y > 9999 then
        io.stderr:write('cal: illegal year value: use 1-9999\n')
        return 1
    end

    -- 閏日を補正
    d[2] = d[2] + L(y)

    t = string.format('%4d/%2d', y, m) .. '\n'
     .. table.concat(w, ' ') .. '\n'
     .. string.rep('   ', W(y, m, 1))
    for i = 1, d[m] do
        t = t .. string.format('%2d', i)
        t = W(y, m, i) < 6 and t .. ' ' or t .. '\n'
    end
    print(t)

    return 0
end)(arg)

Index

Feed

Other

Link

Pathtraq

loading...