Skip to content

Commit ee81675

Browse files
authored
Merge pull request yuin#293 from laowong/master
Issue yuin#292 : os.time keep consistent with standard implementation
2 parents d70801a + 99010ef commit ee81675

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

_glua-tests/issues.lua

+67
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,70 @@ function test()
264264
m:f2()
265265
end
266266
test()
267+
268+
-- issue #292
269+
function test()
270+
t0 = {}
271+
t0.year = 2006
272+
t0.month = 1
273+
t0.day = 2
274+
t0.hour = 15
275+
t0.min = 4
276+
t0.sec = 5
277+
278+
t1 = {}
279+
t1.year = "2006"
280+
t1.month = "1"
281+
t1.day = "2"
282+
t1.hour = "15"
283+
t1.min = "4"
284+
t1.sec = "5"
285+
286+
assert(os.time(t0) == os.time(t1))
287+
288+
t2 = {}
289+
t2.year = " 2006"--prefix blank space
290+
t2.month = "1"
291+
t2.day = "2"
292+
t2.hour = "15"
293+
t2.min = "4"
294+
t2.sec = "5"
295+
assert(os.time(t0) == os.time(t2))
296+
297+
t3 = {}
298+
t3.year = " 0002006"--prefix blank space and 0
299+
t3.month = "1"
300+
t3.day = "2"
301+
t3.hour = "15"
302+
t3.min = "4"
303+
t3.sec = "5"
304+
assert(os.time(t1) == os.time(t3))
305+
306+
t4 = {}
307+
t4.year = "0002006"--prefix 0
308+
t4.month = "1"
309+
t4.day = "2"
310+
t4.hour = "15"
311+
t4.min = "4"
312+
t4.sec = "5"
313+
assert(os.time(t1) == os.time(t4))
314+
315+
t5 = {}
316+
t5.year = "0x7d6"--prefix 0x
317+
t5.month = "1"
318+
t5.day = "2"
319+
t5.hour = "15"
320+
t5.min = "4"
321+
t5.sec = "5"
322+
assert(os.time(t1) == os.time(t5))
323+
324+
t6 = {}
325+
t6.year = "0X7d6"--prefix 0X
326+
t6.month = "1"
327+
t6.day = "2"
328+
t6.hour = "15"
329+
t6.min = "4"
330+
t6.sec = "5"
331+
assert(os.time(t1) == os.time(t6))
332+
end
333+
test()

oslib.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,24 @@ func init() {
1515

1616
func getIntField(L *LState, tb *LTable, key string, v int) int {
1717
ret := tb.RawGetString(key)
18-
if ln, ok := ret.(LNumber); ok {
19-
return int(ln)
18+
19+
switch lv := ret.(type) {
20+
case LNumber:
21+
return int(lv)
22+
case LString:
23+
slv := string(lv)
24+
slv = strings.TrimLeft(slv, " ")
25+
if strings.HasPrefix(slv, "0") && !strings.HasPrefix(slv, "0x") && !strings.HasPrefix(slv, "0X") {
26+
//Standard lua interpreter only support decimal and hexadecimal
27+
slv = strings.TrimLeft(slv, "0")
28+
}
29+
if num, err := parseNumber(slv); err == nil {
30+
return int(num)
31+
}
32+
default:
33+
return v
2034
}
35+
2136
return v
2237
}
2338

0 commit comments

Comments
 (0)