add timestamp、formatDate

This commit is contained in:
Star 2024-11-10 19:54:17 +08:00
parent 57314c7c02
commit 00f1acfdb4
3 changed files with 52 additions and 8 deletions

4
go.mod
View File

@ -5,7 +5,7 @@ go 1.18
require ( require (
apigo.cc/gojs v0.0.4 apigo.cc/gojs v0.0.4
github.com/ZZMarquis/gm v1.3.2 github.com/ZZMarquis/gm v1.3.2
github.com/emmansun/gmsm v0.29.1 github.com/emmansun/gmsm v0.29.2
github.com/obscuren/ecies v0.0.0-20150213224233-7c0f4a9b18d9 github.com/obscuren/ecies v0.0.0-20150213224233-7c0f4a9b18d9
github.com/ssgo/u v1.7.11 github.com/ssgo/u v1.7.11
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
@ -20,7 +20,7 @@ require (
github.com/ssgo/log v1.7.7 // indirect github.com/ssgo/log v1.7.7 // indirect
github.com/ssgo/standard v1.7.7 // indirect github.com/ssgo/standard v1.7.7 // indirect
github.com/ssgo/tool v0.4.27 // indirect github.com/ssgo/tool v0.4.27 // indirect
golang.org/x/crypto v0.28.0 // indirect golang.org/x/crypto v0.29.0 // indirect
golang.org/x/sys v0.27.0 // indirect golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect golang.org/x/text v0.20.0 // indirect
) )

46
util.go
View File

@ -763,9 +763,38 @@ func init() {
panic(vm.NewGoError(err)) panic(vm.NewGoError(err))
} }
}, },
"toDatetime": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { "formatDate": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1) args := gojs.MakeArgs(&argsIn, vm).Check(1)
return vm.ToValue(time.UnixMilli(args.Int64(0)).Format("2006-01-02 15:04:05")) format := args.Str(0)
var tm time.Time
if timestamp := args.Int64(1); timestamp > 0 {
if timestamp < 10000000000 {
timestamp *= 1000
}
tm = time.UnixMilli(timestamp)
} else {
tm = time.Now()
}
return vm.ToValue(tm.Format(format))
},
"timestampMS": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
return vm.ToValue(time.Now().UnixMilli())
},
"timestamp": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
return vm.ToValue(time.Now().Unix())
},
"toDatetime": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
var tm time.Time
if timestamp := args.Int64(0); timestamp > 0 {
if timestamp < 10000000000 {
timestamp *= 1000
}
tm = time.UnixMilli(timestamp)
} else {
tm = time.Now()
}
return vm.ToValue(tm.Format("2006-01-02 15:04:05"))
}, },
"fromDatetime": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { "fromDatetime": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1) args := gojs.MakeArgs(&argsIn, vm).Check(1)
@ -783,8 +812,17 @@ func init() {
} }
}, },
"toDate": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { "toDate": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1) args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(time.UnixMilli(args.Int64(0)).Format("2006-01-02")) var tm time.Time
if timestamp := args.Int64(0); timestamp > 0 {
if timestamp < 10000000000 {
timestamp *= 1000
}
tm = time.UnixMilli(timestamp)
} else {
tm = time.Now()
}
return vm.ToValue(tm.Format("2006-01-02"))
}, },
"fromDate": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { "fromDate": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1) args := gojs.MakeArgs(&argsIn, vm).Check(1)

10
util.ts
View File

@ -61,6 +61,9 @@ export default {
sleep, sleep,
setTimeout, setTimeout,
shell, shell,
formatDate,
timestampMS,
timestamp,
toDatetime, toDatetime,
fromDatetime, fromDatetime,
toDate, toDate,
@ -141,9 +144,12 @@ function tpl(text: string, data: any, functions?: Object): string { return '' }
function sleep(ms: number): void { } function sleep(ms: number): void { }
function setTimeout(callback: () => void, ms?: number, ...args: any): void { } function setTimeout(callback: () => void, ms?: number, ...args: any): void { }
function shell(cmd: string, ...args: string[]): string[] { return [] } function shell(cmd: string, ...args: string[]): string[] { return [] }
function toDatetime(timestamp: number): string { return '' } function formatDate(format: string, timestamp?: number): string { return '' }
function timestampMS(): number { return 0 }
function timestamp(): number { return 0 }
function toDatetime(timestamp?: number): string { return '' }
function fromDatetime(datetimeStr: string): number { return 0 } function fromDatetime(datetimeStr: string): number { return 0 }
function toDate(timestamp: number): string { return '' } function toDate(timestamp?: number): string { return '' }
function fromDate(dateStr: string): number { return 0 } function fromDate(dateStr: string): number { return 0 }
function os(): string { return '' } function os(): string { return '' }
function arch(): string { return '' } function arch(): string { return '' }