time

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2024 License: BSD-3-Clause Imports: 2 Imported by: 0

README

time GoDoc

Usage

local time = require("time")

-- time.unix(), time.sleep()
local begin = time.unix()
time.sleep(1.2)
local stop = time.unix()
local result = stop - begin
result = math.floor(result * 10^2 + 0.5) / 10^2
if not(result == 1) then error("time.sleep()") end

-- time.parse(value, layout)
local result, err = time.parse("Dec  2 03:33:05 2018", "Jan  2 15:04:05 2006")
if err then error(err) end
if not(result == 1543721585) then error("time.parse()") end

-- time.format(value, layout, location)
local result, err = time.format(1543721585, "Jan  2 15:04:05 2006", "Europe/Moscow")
if err then error(err) end
if not(result == "Dec  2 06:33:05 2018") then error("time.format()") end

Documentation

Overview

Package time implements golang package time functionality for lua.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(L *lua.LState) int

Format lua time.format(unixts, ...layout, ...location) returns (string, err)

Example

time.format(value, layout, timezone)

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

func Loader

func Loader(L *lua.LState) int

Loader is the module loader function.

func Parse

func Parse(L *lua.LState) int

Parse lua time.parse(value, layout, ...location) returns (number, error)

Example

time.parse(value, layout)

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

func Preload

func Preload(L *lua.LState)

Preload adds time to the given Lua state's package.preload table. After it has been preloaded, it can be loaded using require:

local time = require("time")

func Sleep

func Sleep(L *lua.LState) int

Sleep lua time.sleep(number) port of go time.Sleep(int64)

Example

time.sleep(number)

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.2

func Unix

func Unix(L *lua.LState) int

Unix lua time.unix() returns unix timestamp in seconds (float)

func UnixNano

func UnixNano(L *lua.LState) int

UnixNano lua time.unix_nano() returns unix timestamp in nano seconds

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL