-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontWasm.go
More file actions
215 lines (189 loc) · 5.54 KB
/
Copy pathfrontWasm.go
File metadata and controls
215 lines (189 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//go:build wasm
package time
import (
"syscall/js"
. "github.com/tinywasm/fmt"
)
func init() {
provider = &timeClient{
dateCtor: js.Global().Get("Date"),
}
}
// timeClient implements timeProvider for WASM/JS environments using the JavaScript Date API.
type timeClient struct {
dateCtor js.Value
}
func (tc *timeClient) UnixNano() int64 {
jsDate := tc.dateCtor.New()
msTimestamp := jsDate.Call("getTime").Float()
return int64(msTimestamp) * 1000000
}
func (tc *timeClient) applyOffset(nano int64) js.Value {
offsetMs := float64(getOffsetMinutes()) * 60000
return tc.dateCtor.New(float64(nano)/1e6 + offsetMs)
}
func (tc *timeClient) FormatDate(value any) string {
switch v := value.(type) {
case int64:
jsDate := tc.applyOffset(v)
return jsDate.Call("toISOString").String()[0:10]
case string:
if len(v) == 10 && v[4] == '-' && v[7] == '-' {
return v
}
}
return ""
}
func (tc *timeClient) FormatTime(value any) string {
switch v := value.(type) {
case int64: // UnixNano
jsDate := tc.applyOffset(v)
hours := jsDate.Call("getUTCHours").Int()
minutes := jsDate.Call("getUTCMinutes").Int()
seconds := jsDate.Call("getUTCSeconds").Int()
return Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
case int16: // Minutes since midnight
hours := v / 60
minutes := v % 60
return Sprintf("%02d:%02d", hours, minutes)
case string:
if nano, err := Convert(v).Int64(); err == nil {
jsDate := tc.applyOffset(nano)
hours := jsDate.Call("getUTCHours").Int()
minutes := jsDate.Call("getUTCMinutes").Int()
seconds := jsDate.Call("getUTCSeconds").Int()
return Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
}
if Count(v, ":") >= 1 {
return v
}
}
return ""
}
func (tc *timeClient) FormatDateTime(value any) string {
switch v := value.(type) {
case int64:
jsDate := tc.applyOffset(v)
iso := jsDate.Call("toISOString").String()
return iso[0:10] + " " + iso[11:19]
case string:
if len(v) == 19 && v[4] == '-' && v[7] == '-' && v[10] == ' ' && v[13] == ':' && v[16] == ':' {
return v
}
}
return ""
}
func (tc *timeClient) FormatDateTimeShort(value any) string {
switch v := value.(type) {
case int64:
jsDate := tc.applyOffset(v)
iso := jsDate.Call("toISOString").String()
return iso[0:10] + " " + iso[11:16]
case string:
if len(v) == 16 && v[4] == '-' && v[7] == '-' && v[10] == ' ' && v[13] == ':' {
return v
}
}
return ""
}
func (tc *timeClient) FormatISO8601(nano int64) string {
// We MUST NOT use applyOffset here. We need raw UTC.
jsDate := tc.dateCtor.New(float64(nano) / 1e6)
// 'toISOString' natively outputs 'YYYY-MM-DDTHH:mm:ss.sssZ'
iso := jsDate.Call("toISOString").String()
// We slice out the milliseconds to strictly match 'YYYY-MM-DDTHH:MM:SSZ'
return iso[0:19] + "Z"
}
func (tc *timeClient) FormatCompact(nano int64) string {
jsDate := tc.dateCtor.New(float64(nano) / 1e6)
iso := jsDate.Call("toISOString").String()
// iso = "YYYY-MM-DDTHH:mm:ss.sssZ"
// extraer: YYYY(0:4) MM(5:7) DD(8:10) HH(11:13) mm(14:16) ss(17:19)
return iso[0:4] + iso[5:7] + iso[8:10] + iso[11:13] + iso[14:16] + iso[17:19]
}
func (tc *timeClient) ParseDate(dateStr string) (int64, error) {
if len(dateStr) != 10 || dateStr[4] != '-' || dateStr[7] != '-' {
return 0, Errf("invalid date format: %s (expected YYYY-MM-DD)", dateStr)
}
jsDate := tc.dateCtor.New(dateStr + "T00:00:00Z")
if jsDate.Call("toString").String() == "Invalid Date" {
return 0, Errf("invalid date format: %s", dateStr)
}
year := jsDate.Call("getUTCFullYear").Int()
month := jsDate.Call("getUTCMonth").Int() + 1
day := jsDate.Call("getUTCDate").Int()
expected := Sprintf("%04d-%02d-%02d", year, month, day)
if expected != dateStr {
return 0, Errf("invalid date: %s (auto-corrected to %s)", dateStr, expected)
}
ms := jsDate.Call("getTime").Float()
return int64(ms) * 1000000, nil
}
func (tc *timeClient) ParseTime(timeStr string) (int16, error) {
return parseTime(timeStr)
}
func (tc *timeClient) ParseDateTime(dateStr, timeStr string) (int64, error) {
if len(timeStr) == 5 {
timeStr += ":00"
}
isoStr := dateStr + "T" + timeStr + "Z"
jsDate := tc.dateCtor.New(isoStr)
if jsDate.Call("toString").String() == "Invalid Date" {
return 0, Errf("invalid date/time format: %s %s", dateStr, timeStr)
}
ms := jsDate.Call("getTime").Float()
return int64(ms) * 1000000, nil
}
func (tc *timeClient) IsToday(nano int64) bool {
jsDateLocal := tc.applyOffset(nano)
nowLocal := tc.applyOffset(tc.UnixNano())
return jsDateLocal.Call("toDateString").String() == nowLocal.Call("toDateString").String()
}
func (tc *timeClient) IsPast(nano int64) bool {
return nano < tc.UnixNano()
}
func (tc *timeClient) IsFuture(nano int64) bool {
return nano > tc.UnixNano()
}
func (tc *timeClient) LocalMinutesToUnixUTC(dateSec int64, localMinutes int, tz string) int64 {
offsetSec := int64(getOffsetMinutes()) * 60
midnight := MidnightUTC(dateSec)
return midnight + int64(localMinutes)*60 - offsetSec
}
type WasmTimer struct {
id js.Value
active bool
jsFunc js.Func
f func()
}
func (wt *WasmTimer) Stop() bool {
if !wt.active {
return false
}
js.Global().Call("clearTimeout", wt.id)
wt.active = false
wt.jsFunc.Release()
return true
}
func (wt *WasmTimer) Fire() {
if !wt.active {
return
}
wt.active = false
wt.jsFunc.Release()
if wt.f != nil {
wt.f()
}
}
func (tc *timeClient) AfterFunc(milliseconds int, f func()) Timer {
wt := &WasmTimer{
active: true,
f: f,
}
wt.jsFunc = js.FuncOf(func(this js.Value, args []js.Value) any {
wt.Fire()
return nil
})
wt.id = js.Global().Call("setTimeout", wt.jsFunc, milliseconds)
return wt
}