-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstrftime.lua
More file actions
366 lines (295 loc) · 10.9 KB
/
Copy pathstrftime.lua
File metadata and controls
366 lines (295 loc) · 10.9 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
-- ***************************************************************
--
-- Copyright 2018 by Sean Conner. All Rights Reserved.
--
-- This library is free software; you can redistribute it and/or modify it
-- under the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation; either version 3 of the License, or (at your
-- option) any later version.
--
-- This library is distributed in the hope that it will be useful, but
-- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
-- License for more details.
--
-- You should have received a copy of the GNU Lesser General Public License
-- along with this library; if not, see <http://www.gnu.org/licenses/>.
--
-- Comments, questions and criticisms can be sent to: sean@conman.org
--
-- ********************************************************************
-- luacheck: ignore 611
local os = require "os"
local lpeg = require "lpeg"
local tonumber = tonumber
local error = error
local string = string
local Cmt = lpeg.Cmt
local Cg = lpeg.Cg
local Ct = lpeg.Ct
local Cf = lpeg.Cf
local Cs = lpeg.Cs
local C = lpeg.C
local P = lpeg.P
local S = lpeg.S
local R = lpeg.R
-- ***********************************************************************
-- The following could be done easier and simpler with nl_langinfo(),
-- but that isn't portable (being a GNU extension). This *is* portable,
-- although we cannot get the formats used for "%c", "%x" or "%X".
-- ***********************************************************************
local short_months = {}
local long_months = {}
local short_days = {}
local long_days = {}
local am_pm = {}
do
local key
local now = { year = 2013 , month = 1 , day = 1 , hour = 0 }
for m = 1 , 12 do
now.month = m
key = os.date("%b", os.time(now))
short_months[key] = m
key = os.date("%B", os.time(now))
long_months[key] = m
end
for d = 1 , 7 do
now.day = d
key = os.date("%a", os.time(now))
short_days[key] = d
key = os.date("%A", os.time(now))
long_days[key] = d
end
now.hour = 0
key = os.date("%p", os.time(now))
am_pm[key] = 0
am_pm[key:lower()] = 0 -- GNU extension
now.hour = 12
key = os.date("%p", os.time(now))
am_pm[key] = 12
am_pm[key:lower()] = 12 -- GNU extension
end
-- ********************************************************************
local function chkrange(min,max)
return function(_,position,capture)
local val = tonumber(capture)
if val < min or val > max then
return false
else
return position,val
end
end
end
local utf8c = R"\128\191"
local utf8 = R"\192\207" * utf8c
+ R"\208\223" * utf8c * utf8c
+ R"\224\247" * utf8c * utf8c * utf8c
+ R"\248\251" * utf8c * utf8c * utf8c * utf8c
+ R"\252\253" * utf8c * utf8c * utf8c * utf8c * utf8c
local char = R("AZ","az") + utf8
local digit = R"09"
local dday = Cmt(digit * digit, chkrange(1, 31))
local d24hour = Cmt(digit * digit, chkrange(0, 23))
local d12hour = Cmt(digit * digit, chkrange(1, 12))
local ddyear = Cmt(digit * digit * digit,chkrange(1,366))
local dmonth = Cmt(digit * digit, chkrange(1, 12))
local dminute = Cmt(digit * digit, chkrange(0, 59))
local dsecond = Cmt(digit * digit, chkrange(0, 61))
local dweeknum = Cmt(digit * digit, chkrange(0, 53))
local dweek = Cmt(digit, chkrange(0, 6))
local d2year = (digit * digit) / function(c) return c + 1900 end
local d4year = (digit * digit * digit * digit) / tonumber
local number = (S"-+" * digit * digit * digit * digit)
local dweek1 = Cmt(digit, function(_,position,capture)
local v = tonumber(capture)
if v < 1 or v > 7 then
return false
else
if v == 7 then
v = 0
end
return position,v
end
end)
-- ********************************************************************
local oct = R"07"
local hex = R("09","AF","af")
local normal = C(P(1) - (P[[\]] + P"%"))
local escaped = P[[\n]] / "\n"
+ P[[\t]] / "\t"
+ P[[\v]] / "\v"
+ P[[\b]] / "\b"
+ P[[\r]] / "\r"
+ P[[\f]] / "\f"
+ P[[\a]] / "\a"
+ P[[\\]] / "\\"
+ P[[\?]] / "?"
+ P[[\']] / "'"
+ P[[\"]] / '"'
+ P[[\]] * C(oct * oct * oct) / function(c) return tonumber(c,8) end
+ P[[\x]] * C(hex * hex) / function(c) return tonumber(c,16) end
local text = Cs((escaped + normal)^1)
/ function(c) return P(c) end
-- -------------------------------------------------------------------
local directives = P"a" / function()
return Cg(char^1 / short_days,"wday")
end
+ P"A" / function()
return Cg(char^1 / long_days,"wday")
end
+ P"b" / function()
return Cg(char^1 / short_months,"month")
end
+ P"B" / function()
return Cg(char^1 / long_months,"month")
end
+ P"c" / function()
error("%c format specifier not supported")
end
+ P"d" / function()
return Cg(dday,"day")
end
+ P"H" / function()
return Cg(d24hour,"hour")
end
+ P"I" / function()
return Cg(d12hour,"hour")
end
+ P"j" / function()
return Cg(ddyear,"yday")
end
+ P"m" / function()
return Cg(dmonth,"month")
end
+ P"M" / function()
return Cg(dminute,"min")
end
+ P"p" / function()
return Cg(char^1 / am_pm,"pm")
end
+ P"S" / function()
return Cg(dsecond,"sec")
end
+ P"U" / function()
return Cg(dweeknum,"weeknums")
end
+ P"w" / function()
return Cg(dweek,"wday")
end
+ P"W" / function()
return Cg(dweeknum,"weeknumm")
end
+ P"x" / function()
error("%x format specifier not supported")
end
+ P"X" / function()
error("%X format specifier not supported")
end
+ P"y" / function()
return Cg(d2year,"year")
end
+ P"Y" / function()
return Cg(d4year,"year")
end
+ P"Z" / function()
return Cg(char^1,"zone")
end
+ P"%" / function()
return P"%"
end
-- -------------------------------------------------------------------
-- C99 extensions
-- -------------------------------------------------------------------
+ P"C" / function()
return Cg(d2year,"year")
end
+ P"D" / function()
return Cg(dmonth,"month") * P"/"
* Cg(dday,"day") * P"/"
* Cg(d2year,"year")
end
+ P"e" / function()
return P" "^-1
* Cg(Cmt(digit^1,chkrange(1,31)),"day")
end
+ P"F" / function()
return Cg(d4year,"year") * P"-"
* Cg(dmonth,"month") * P"-"
* Cg(dday,"day")
end
+ P"g" / function()
return Cg(d2year,"year")
end
+ P"G" / function()
return Cg(d4year,"year")
end
+ P"h" / function()
return Cg(char^1 / short_months,"month")
end
+ P"n" / function()
return P"\n"
end
+ P"r" / function()
return Cg(d12hour,"hour") * P":"
* Cg(dminute,"min") * P":"
* Cg(dsecond,"sec") * P" "
* Cg(char^1 / am_pm,"pm")
end
+ P"R" / function()
return Cg(d24hour,"hour") * P":"
* Cg(dminute,"min")
end
+ P"t" / function()
return P"\t"
end
+ P"T" / function()
return Cg(d24hour,"hour") * P":"
* Cg(dminute,"min") * P":"
* Cg(dsecond,"sec")
end
+ P"u" / function()
return Cg(dweek1,"wday")
end
+ P"V" / function()
return Cg(dweeknum,"weeknumm4")
end
+ P"z" / function()
return Cg(number,"zone")
end
+ P"E" / function()
error("%E format modifier not supported")
end
+ P"O" / function()
error("%O format modifier not supported")
end
-- -------------------------------------------------------------------
-- others
-- -------------------------------------------------------------------
+ P"k" / function()
return P" "^-1
* Cg(Cmt(digit^1,chkrange(1,23)),"hour")
end
+ P"l" / function()
return P" "^-1
* Cg(Cmt(digit^1,chkrange(1,12)),"hour")
end
+ P"P" / function()
return Cg(char^1 / am_pm,"pm")
end
+ P"s" / function()
return Cg(digit^1 / tonumber,"epoch")
end
+ P"+" / function()
error("%+ format specifier not supported")
end
-- -------------------------------------------------------------------
-- Catch all
-- -------------------------------------------------------------------
+ P(1) / function(c)
error(string.format("%%%s format specifier not supported",c))
end
local directive = P"%" * directives
return Cf((text + directive)^0,function(a,b) return a * b end)
/ function(c)
return Ct(c)
end