Switched to expression-based timestamps
This commit is contained in:
parent
7bf586757a
commit
5c89c44ff7
7 changed files with 215 additions and 151 deletions
|
|
@ -3,7 +3,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"fmt"
|
||||
"strings"
|
||||
"syscall/js"
|
||||
"time"
|
||||
|
|
@ -59,43 +59,67 @@ func processLine(line, operation, timezone string) string {
|
|||
return line
|
||||
}
|
||||
|
||||
var t time.Time
|
||||
var err error
|
||||
res, err := Parse(trimmedLine)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
t, err := res.Time()
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
if timezone == "utc" {
|
||||
t = t.In(time.UTC)
|
||||
} else {
|
||||
t = t.In(time.Local)
|
||||
}
|
||||
|
||||
switch operation {
|
||||
case "iso_8601":
|
||||
return t.Format(time.RFC3339)
|
||||
case "unix":
|
||||
sec, err := strconv.ParseInt(trimmedLine, 10, 64)
|
||||
if err != nil {
|
||||
return "Invalid Date"
|
||||
}
|
||||
t = time.Unix(sec, 0)
|
||||
return fmt.Sprintf("%d", t.Unix())
|
||||
case "unix_micro":
|
||||
ms, err := strconv.ParseInt(trimmedLine, 10, 64)
|
||||
if err != nil {
|
||||
return "Invalid Date"
|
||||
}
|
||||
t = time.Unix(0, ms*1000000)
|
||||
case "to_utc", "from_utc":
|
||||
// Go's time.Parse is strict. We need to handle ISO 8601 flexible formats.
|
||||
// Try standard layouts.
|
||||
layouts := []string{
|
||||
time.RFC3339,
|
||||
time.RFC3339Nano,
|
||||
"2006-01-02T15:04:05",
|
||||
"2006-01-02T15:04:05.999",
|
||||
}
|
||||
parsed := false
|
||||
for _, layout := range layouts {
|
||||
t, err = time.Parse(layout, trimmedLine)
|
||||
if err == nil {
|
||||
parsed = true
|
||||
break
|
||||
return fmt.Sprintf("%d", t.UnixNano()/1000)
|
||||
}
|
||||
|
||||
/*
|
||||
switch operation {
|
||||
case "unix":
|
||||
sec, err := strconv.ParseInt(trimmedLine, 10, 64)
|
||||
if err != nil {
|
||||
return "Invalid Date"
|
||||
}
|
||||
t = time.Unix(sec, 0)
|
||||
case "unix_micro":
|
||||
ms, err := strconv.ParseInt(trimmedLine, 10, 64)
|
||||
if err != nil {
|
||||
return "Invalid Date"
|
||||
}
|
||||
t = time.Unix(0, ms*1000000)
|
||||
case "to_utc", "from_utc":
|
||||
// Go's time.Parse is strict. We need to handle ISO 8601 flexible formats.
|
||||
// Try standard layouts.
|
||||
layouts := []string{
|
||||
time.RFC3339,
|
||||
time.RFC3339Nano,
|
||||
"2006-01-02T15:04:05",
|
||||
"2006-01-02T15:04:05.999",
|
||||
}
|
||||
parsed := false
|
||||
for _, layout := range layouts {
|
||||
t, err = time.Parse(layout, trimmedLine)
|
||||
if err == nil {
|
||||
parsed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !parsed {
|
||||
return "Invalid Date"
|
||||
}
|
||||
}
|
||||
if !parsed {
|
||||
return "Invalid Date"
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
if operation == "to_utc" {
|
||||
// If input has no timezone (t.Location() is UTC but offset is 0 and it might have been local),
|
||||
|
|
@ -103,12 +127,12 @@ func processLine(line, operation, timezone string) string {
|
|||
// If we want to treat it as "Local" (browser local), we don't know what browser local is easily in Go WASM without JS help.
|
||||
// However, the JS implementation relied on `new Date()` which uses browser local.
|
||||
// In Go WASM, `time.Local` is usually UTC unless configured.
|
||||
|
||||
|
||||
// Let's use JS Date for parsing to be consistent with previous behavior and browser's local time?
|
||||
// Or we can try to implement it purely in Go.
|
||||
// If we want to support "Local" interpretation of a string like "2023-01-01T10:00:00", we need the local offset.
|
||||
// We can get the local timezone offset from JS.
|
||||
|
||||
|
||||
if timezone == "utc" {
|
||||
// If user selected UTC, we treat the input as UTC (which time.Parse does by default for no-offset strings)
|
||||
// and output UTC.
|
||||
|
|
@ -117,35 +141,35 @@ func processLine(line, operation, timezone string) string {
|
|||
// If user selected Local, we treat input as Local?
|
||||
// "To UTC": converts the input in ISO 8601 in the selected timezone into UTC.
|
||||
// If I select "Local" and input "10:00", I mean "10:00 Local". I want "XX:XX UTC".
|
||||
|
||||
|
||||
// If the input string HAS an offset, we respect it.
|
||||
// If it doesn't, we assume it's in `timezone`.
|
||||
|
||||
|
||||
// Check if input has offset.
|
||||
// time.Parse parses into UTC if no offset found.
|
||||
// If we want to interpret "10:00" as Local, we need to adjust.
|
||||
|
||||
|
||||
// Let's stick to a simpler approach: Use JS to parse if possible, or just use Go.
|
||||
// Using JS from Go is easy.
|
||||
|
||||
|
||||
jsDate := js.Global().Get("Date").New(trimmedLine)
|
||||
if jsDate.Call("toString").String() == "Invalid Date" {
|
||||
return "Invalid Date"
|
||||
}
|
||||
|
||||
|
||||
// If "to_utc", we just want to show the UTC string.
|
||||
// But wait, `new Date("2023-01-01T10:00:00")` is Local in browser.
|
||||
// `new Date("2023-01-01T10:00:00Z")` is UTC.
|
||||
|
||||
|
||||
if timezone == "utc" && !strings.HasSuffix(strings.ToUpper(trimmedLine), "Z") && !strings.ContainsAny(trimmedLine, "+-") {
|
||||
// Force UTC interpretation
|
||||
jsDate = js.Global().Get("Date").New(trimmedLine + "Z")
|
||||
}
|
||||
|
||||
|
||||
return jsDate.Call("toISOString").String()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if operation == "from_utc" {
|
||||
// Input assumed UTC.
|
||||
// "2023-01-01T10:00:00" -> Treat as UTC.
|
||||
|
|
@ -153,7 +177,7 @@ func processLine(line, operation, timezone string) string {
|
|||
if !strings.HasSuffix(strings.ToUpper(trimmedLine), "Z") && !strings.ContainsAny(trimmedLine, "+-") {
|
||||
jsDate = js.Global().Get("Date").New(trimmedLine + "Z")
|
||||
}
|
||||
|
||||
|
||||
if jsDate.Call("toString").String() == "Invalid Date" {
|
||||
return "Invalid Date"
|
||||
}
|
||||
|
|
@ -173,7 +197,7 @@ func processLine(line, operation, timezone string) string {
|
|||
// So SG (UTC+8) is -480.
|
||||
// To get local time in UTC components: add (-offset).
|
||||
// timestamp - (offset * 60 * 1000)
|
||||
|
||||
|
||||
newTime := jsDate.Call("getTime").Float() - (float64(offset) * 60 * 1000)
|
||||
localDate := js.Global().Get("Date").New(newTime)
|
||||
iso := localDate.Call("toISOString").String()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue