-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday-format
More file actions
executable file
·152 lines (123 loc) · 3.21 KB
/
day-format
File metadata and controls
executable file
·152 lines (123 loc) · 3.21 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env lua
local R = require 're'
local format_pattern = R.compile [[
segs <- {| seg* |}
seg <- {|
{}
( epoch
/ year
/ day_long_ordinal
/ day_short_ordinal
/ day_long
/ month
/ day_short
/ month_en_long
/ day_en_long
/ month_en_short
/ day_en_short
)
{}
|} / . seg
-- 1 to 9
day_short <-
{~ [1-9] -> 'day_short' ~}
-- 10 11 12
month <-
{~ ('1'[0-2]) -> 'month' ~}
-- 13 to 31
day_long <-
{~ ('1'[3-9] / '2'%d / '3'[01]) -> 'day_long' ~}
-- 2000 to 9999
year <-
{~ ([2-9]%d^3) -> 'year' ~}
-- 10000 and above
epoch <-
{~ ([1-9]%d^+4) -> 'epoch' ~}
-- day with ordinal
day_short_ordinal <-
{~ (day_short ordinal_suffix) -> 'day_short_ordinal' ~}
day_long_ordinal <-
{~ (([12]%d / '3'[01]) ordinal_suffix) -> 'day_long_ordinal' ~}
ordinal_suffix <-
'st' / 'nd' / 'rd' / 'th'
-- words
month_en_long <- {~
( [Jj]'anuary' / [Ff]'ebruary' / [Mm]'arch' / [Aa]'pril'
/ [Mm]'ay' / [Jj]'une' / [Jj]'uly' / [Aa]'ugust'
/ [Ss]'eptember' / [Oo]'ctober' / [Nn]'ovember' / [Dd]'ecember'
) -> 'month_en_long'
~}
month_en_short <- {~
( [Jj]'an' / [Ff]'eb' / [Mm]'ar' / [Aa]'pr'
/ [Mm]'ay' / [Jj]'un' / [Jj]'ul' / [Aa]'ug'
/ [Ss]'ep' / [Oo]'ct' / [Nn]'ov' / [Dd]'ec'
) -> 'month_en_short'
~}
day_en_long <- {~
( [Ss]'unday' / [Mm]'onday' / [Tt]'uesday' / [Ww]'ednesday'
/ [Tt]'hursday' / [Ff]'riday' / [Ss]'aturday'
) -> 'day_en_long'
~}
day_en_short <- {~
( [Ss]'un' / [Mm]'on' / [Tt]'ue' / [Ww]'ed'
/ [Tt]'hu' / [Ff]'ri' / [Ss]'at'
) -> 'day_en_short'
~}
]]
-- formatters
function ordinal_en (i)
local suffix = 'th'
local unit = i:sub(#i)
if #i == 1 or i:sub(#i-1,1) ~= '1' then
if unit == '1' then
suffix = 'st'
elseif unit == '2' then
suffix = 'nd'
elseif unit == '3' then
suffix = 'rd'
end
end
return i .. suffix
end
function short_form (i)
return i:gsub('^0*', '')
end
local formatter = {
day_en_long = function (t) return os.date('%A', t) end,
day_en_short = function (t) return os.date('%a', t) end,
day_long = function (t) return os.date('%d', t) end,
day_long_ordinal = function (t) return ordinal_en(os.date('%d', t)) end,
day_short = function (t) return short_form(os.date('%d', t)) end,
day_short_ordinal = function (t) return ordinal_en(short_form(os.date('%d', t))) end,
epoch = function (t) return os.date('%s', t) end,
month_en_long = function (t) return os.date('%B', t) end,
month_en_short = function (t) return os.date('%b', t) end,
month = function (t) return os.date('%m', t) end,
year = function (t) return os.date('%Y', t) end,
}
-- main
local input = table.concat(arg, ' ')
local parsed = format_pattern:match(input)
-- print((require'cjson').encode({input=input,parsed=parsed}))os.exit()
while true do
local d = io.read()
if d == nil then break end
local t = os.time{
year = 0 + d:sub(1, 4),
month = 0 + d:sub(6, 7),
day = 0 + d:sub(9, 10),
}
local out = {}
local cur = 1
for _, seg in ipairs(parsed) do
if cur < seg[1] then
out[#out+1] = input:sub(cur, seg[1]-1)
end
out[#out+1] = formatter[seg[2]](t)
cur = seg[3]
end
if cur < #input-1 then
out[#out+1] = input:sub(cur, #input)
end
print(table.concat(out))
end