-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
runtime_lua_oslib.go
129 lines (116 loc) · 3.54 KB
/
runtime_lua_oslib.go
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
// The MIT License (MIT)
//
// Copyright (c) 2015 Yusuke Inuzuka
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package server
import (
"strings"
"time"
"github.com/yuin/gopher-lua"
)
var startedAt time.Time
func init() {
startedAt = time.Now()
}
func getIntField(L *lua.LState, tb *lua.LTable, key string, v int) int {
ret := tb.RawGetString(key)
if ln, ok := ret.(lua.LNumber); ok {
return int(ln)
}
return v
}
func getBoolField(L *lua.LState, tb *lua.LTable, key string, v bool) bool {
ret := tb.RawGetString(key)
if lb, ok := ret.(lua.LBool); ok {
return bool(lb)
}
return v
}
func OpenOs(L *lua.LState) int {
osmod := L.RegisterModule(lua.OsLibName, osFuncs)
L.Push(osmod)
return 1
}
var osFuncs = map[string]lua.LGFunction{
"clock": osClock,
"difftime": osDiffTime,
"date": osDate,
"time": osTime,
}
func osClock(L *lua.LState) int {
L.Push(lua.LNumber(float64(time.Now().Sub(startedAt)) / float64(time.Second)))
return 1
}
func osDiffTime(L *lua.LState) int {
L.Push(lua.LNumber(L.CheckInt64(1) - L.CheckInt64(2)))
return 1
}
func osDate(L *lua.LState) int {
t := time.Now().UTC()
cfmt := "%c"
if L.GetTop() >= 1 {
cfmt = L.CheckString(1)
if strings.HasPrefix(cfmt, "!") {
t = time.Now().UTC()
cfmt = strings.TrimLeft(cfmt, "!")
}
if L.GetTop() >= 2 {
t = time.Unix(L.CheckInt64(2), 0)
}
if strings.HasPrefix(cfmt, "*t") {
ret := L.CreateTable(0, 9)
ret.RawSetString("year", lua.LNumber(t.Year()))
ret.RawSetString("month", lua.LNumber(t.Month()))
ret.RawSetString("day", lua.LNumber(t.Day()))
ret.RawSetString("hour", lua.LNumber(t.Hour()))
ret.RawSetString("min", lua.LNumber(t.Minute()))
ret.RawSetString("sec", lua.LNumber(t.Second()))
ret.RawSetString("wday", lua.LNumber(t.Weekday()))
ret.RawSetString("yday", lua.LNumber(t.YearDay()))
// TODO dst
ret.RawSetString("isdst", lua.LFalse)
L.Push(ret)
return 1
}
}
L.Push(lua.LString(strftime(t, cfmt)))
return 1
}
func osTime(L *lua.LState) int {
if L.GetTop() == 0 {
L.Push(lua.LNumber(time.Now().UTC().Unix()))
} else {
tbl := L.CheckTable(1)
sec := getIntField(L, tbl, "sec", 0)
min := getIntField(L, tbl, "min", 0)
hour := getIntField(L, tbl, "hour", 12)
day := getIntField(L, tbl, "day", -1)
month := getIntField(L, tbl, "month", -1)
year := getIntField(L, tbl, "year", -1)
isdst := getBoolField(L, tbl, "isdst", false)
t := time.Date(year, time.Month(month), day, hour, min, sec, 0, time.UTC)
// TODO dst
if false {
print(isdst)
}
L.Push(lua.LNumber(t.UTC().Unix()))
}
return 1
}