From 00f1acfdb450f58deb41ba1341637059f48f8aa2 Mon Sep 17 00:00:00 2001 From: Star Date: Sun, 10 Nov 2024 19:54:17 +0800 Subject: [PATCH] =?UTF-8?q?add=20timestamp=E3=80=81formatDate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 4 ++-- util.go | 46 ++++++++++++++++++++++++++++++++++++++++++---- util.ts | 10 ++++++++-- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 5ae3fdb..eed65bf 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.18 require ( apigo.cc/gojs v0.0.4 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/ssgo/u v1.7.11 gopkg.in/yaml.v3 v3.0.1 @@ -20,7 +20,7 @@ require ( github.com/ssgo/log v1.7.7 // indirect github.com/ssgo/standard v1.7.7 // 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/text v0.20.0 // indirect ) diff --git a/util.go b/util.go index a69a5e1..a5ec94c 100644 --- a/util.go +++ b/util.go @@ -763,9 +763,38 @@ func init() { 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) - 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 { args := gojs.MakeArgs(&argsIn, vm).Check(1) @@ -783,8 +812,17 @@ func init() { } }, "toDate": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { - args := gojs.MakeArgs(&argsIn, vm).Check(1) - return vm.ToValue(time.UnixMilli(args.Int64(0)).Format("2006-01-02")) + 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")) }, "fromDate": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { args := gojs.MakeArgs(&argsIn, vm).Check(1) diff --git a/util.ts b/util.ts index a92086e..d68f4ec 100644 --- a/util.ts +++ b/util.ts @@ -61,6 +61,9 @@ export default { sleep, setTimeout, shell, + formatDate, + timestampMS, + timestamp, toDatetime, fromDatetime, toDate, @@ -141,9 +144,12 @@ function tpl(text: string, data: any, functions?: Object): string { return '' } function sleep(ms: number): void { } function setTimeout(callback: () => void, ms?: number, ...args: any): void { } 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 toDate(timestamp: number): string { return '' } +function toDate(timestamp?: number): string { return '' } function fromDate(dateStr: string): number { return 0 } function os(): string { return '' } function arch(): string { return '' }