Skip to content

Commit

Permalink
Rewrited the time
Browse files Browse the repository at this point in the history
  • Loading branch information
ofunc committed Jul 1, 2019
1 parent 4dbcf11 commit 9e3ab11
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 28 deletions.
4 changes: 0 additions & 4 deletions lmodos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ This library is implemented through table `os`.

Returns an absolute representation of `path`.

### os.clock()

Returns an approximation of the amount in seconds of CPU time used by the program.

### os.date([format[, time]])

Returns a string or a table containing date and time, formatted according to the given string format.
Expand Down
2 changes: 1 addition & 1 deletion lmodos/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func metainfo(l *lua.State) int {
case "size":
l.Push(info.Size())
case "modtime":
l.Push(info.ModTime().Unix())
l.Push(info.ModTime())
default:
l.Push(nil)
}
Expand Down
25 changes: 10 additions & 15 deletions lmodos/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ func Open(l *lua.State) int {
l.Push(labs)
l.SetTableRaw(-3)

l.Push("clock")
l.Push(lclock)
l.SetTableRaw(-3)

l.Push("date")
l.Push(ldate)
l.SetTableRaw(-3)
Expand Down Expand Up @@ -148,14 +144,13 @@ func labs(l *lua.State) int {
}
}

func lclock(l *lua.State) int {
l.Push(float64(time.Now().Sub(start)) / float64(time.Second))
return 1
}

func ldate(l *lua.State) int {
f := l.OptString(1, "%c")
t := time.Unix(l.OptInteger(2, time.Now().Unix()), 0)
t := time.Now()
if !l.IsNil(2) {
t = l.GetRaw(2).(time.Time)
}

if strings.HasPrefix(f, "!") {
t = t.UTC()
f = f[1:]
Expand Down Expand Up @@ -202,9 +197,9 @@ func ldate(l *lua.State) int {
}

func ldifftime(l *lua.State) int {
x := l.ToInteger(1)
y := l.ToInteger(2)
l.Push(x - y)
x := l.GetRaw(1).(time.Time)
y := l.GetRaw(2).(time.Time)
l.Push(float64(x.Sub(y)) / float64(time.Second))
return 1
}

Expand Down Expand Up @@ -444,7 +439,7 @@ func lstat(l *lua.State) int {

func ltime(l *lua.State) int {
if l.IsNil(1) {
l.Push(time.Now().Unix())
l.Push(time.Now())
} else {
l.Push("year")
l.GetTable(1)
Expand All @@ -470,7 +465,7 @@ func ltime(l *lua.State) int {
l.GetTable(1)
sec := int(l.OptInteger(-1, 0))

l.Push(time.Date(year, month, day, hour, min, sec, 0, time.Local).Unix())
l.Push(time.Date(year, month, day, hour, min, sec, 0, time.Local))
}
return 1
}
Expand Down
8 changes: 0 additions & 8 deletions test/os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ function test.abs()
assert(a:match('.*[\\/](.+)$') == 'os.lua')
end

function test.clock()
local c1 = os.clock()
os.sleep(100)
local c2 = os.clock()
assert(type(c1) == 'number' and type(c2) == 'number')
assert(c1 >= 0 and c2-c1 > 0.09999)
end

function test.date()
assert(type(os.date()) == 'string')

Expand Down

0 comments on commit 9e3ab11

Please sign in to comment.