1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-03-19 22:19:23 +02:00

added JSVM new Timezone binding

This commit is contained in:
Gani Georgiev 2025-01-03 17:35:49 +02:00
parent dadbca5248
commit ff3d51ce30
5 changed files with 4375 additions and 4278 deletions

View File

@ -1,3 +1,8 @@
## v0.25.0 (WIP)
- Added JSVM `new Timezone(name)` binding for constructing `time.Location` value ([#6219](https://github.com/pocketbase/pocketbase/discussions/6219)).
## v0.24.1 (WIP)
- Added missing time macros in the UI autocomplete.

View File

@ -538,6 +538,21 @@ func baseBinds(vm *goja.Runtime) {
return instanceValue
})
// note: named Timezone to avoid conflicts with the JS Location interface.
vm.Set("Timezone", func(call goja.ConstructorCall) *goja.Object {
name, _ := call.Argument(0).Export().(string)
instance, err := time.LoadLocation(name)
if err != nil {
instance = time.UTC
}
instanceValue := vm.ToValue(instance).(*goja.Object)
instanceValue.SetPrototype(call.This.Prototype())
return instanceValue
})
vm.Set("DateTime", func(call goja.ConstructorCall) *goja.Object {
instance := types.NowDateTime()

View File

@ -44,7 +44,7 @@ func TestBaseBindsCount(t *testing.T) {
vm := goja.New()
baseBinds(vm)
testBindsCount(vm, "this", 32, t)
testBindsCount(vm, "this", 33, t)
}
func TestBaseBindsSleep(t *testing.T) {
@ -538,6 +538,31 @@ func TestBaseBindsMiddleware(t *testing.T) {
}
}
func TestBaseBindsTimezone(t *testing.T) {
vm := goja.New()
baseBinds(vm)
_, err := vm.RunString(`
const v0 = (new Timezone()).string();
if (v0 != "UTC") {
throw new Error("(v0) Expected UTC got " + v0)
}
const v1 = (new Timezone("invalid")).string();
if (v1 != "UTC") {
throw new Error("(v1) Expected UTC got " + v1)
}
const v2 = (new Timezone("EET")).string();
if (v2 != "EET") {
throw new Error("(v2) Expected EET got " + v2)
}
`)
if err != nil {
t.Fatal(err)
}
}
func TestBaseBindsDateTime(t *testing.T) {
vm := goja.New()
baseBinds(vm)

File diff suppressed because it is too large Load Diff

View File

@ -579,6 +579,32 @@ declare class Middleware {
)
}
interface Timezone extends time.Location{} // merge
/**
* Timezone returns the timezone location with the given name.
*
* The name is expected to be a location name corresponding to a file
* in the IANA Time Zone database, such as "America/New_York".
*
* If the name is "Local", LoadLocation returns Local.
*
* If the name is "", invalid or "UTC", returns UTC.
*
* The constructor is equivalent to calling the Go ` + "`" + `time.LoadLocation(name)` + "`" + ` method.
*
* Example:
*
* ` + "```" + `js
* const zone = new Timezone("America/New_York")
* $app.cron().setTimezone(zone)
* ` + "```" + `
*
* @group PocketBase
*/
declare class Timezone implements time.Location {
constructor(name?: string)
}
interface DateTime extends types.DateTime{} // merge
/**
* DateTime defines a single DateTime type instance.