forked from vadv/gopher-lua-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
64 lines (59 loc) · 1.41 KB
/
example_test.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
package time
import (
"log"
lua "github.com/yuin/gopher-lua"
)
// time.sleep(number)
func ExampleSleep() {
state := lua.NewState()
Preload(state)
source := `
local time = require("time")
local begin = time.unix()
time.sleep(1.2)
local stop = time.unix()
local result = stop - begin
-- round
result = math.floor(result * 10^2 + 0.5) / 10^2
print(result)
`
if err := state.DoString(source); err != nil {
log.Fatal(err.Error())
}
// Output:
// 1
}
// time.parse(value, layout)
func ExampleParse() {
state := lua.NewState()
Preload(state)
source := `
local time = require("time")
local result, err = time.parse("Dec 2 03:33:05 2018", "Jan 2 15:04:05 2006")
if err then error(err) end
print(result)
`
if err := state.DoString(source); err != nil {
log.Fatal(err.Error())
}
// Output:
// 1543721585
}
// time.format(value, layout, timezone)
func ExampleFormat() {
state := lua.NewState()
Preload(state)
source := `
local time = require("time")
print( time.format(0, "Mon Jan 2 15:04:05 -0700 MST 2006", "UTC") )
print( time.format(0, "Mon Jan 2 15:04:05 -0700 MST 2006", "Europe/Moscow") )
print( time.format(1543721585, "Jan 2 15:04:05 2006", "Europe/Moscow") )
`
if err := state.DoString(source); err != nil {
log.Fatal(err.Error())
}
// Output:
// Thu Jan 1 00:00:00 +0000 UTC 1970
// Thu Jan 1 03:00:00 +0300 MSK 1970
// Dec 2 06:33:05 2018
}