Added tests and fixed timestamp handling
All checks were successful
/ publish (push) Successful in 47s

This commit is contained in:
Leon Mika 2025-11-19 22:33:10 +11:00
parent 5c89c44ff7
commit e74906e0c4
4 changed files with 164 additions and 34 deletions

View file

@ -2,7 +2,6 @@ package main
import (
"errors"
"strings"
"time"
)
@ -10,51 +9,66 @@ type TimeComponent struct {
TS *string `parser:"@Time"`
}
func (t TimeComponent) Time() (time.Time, error) {
if strings.HasSuffix(*t.TS, "Z") {
return time.ParseInLocation("15:04:05Z", *t.TS, time.UTC)
}
return time.ParseInLocation("15:04:05", *t.TS, time.Local)
func (t TimeComponent) Time(loc *time.Location) (time.Time, error) {
return time.ParseInLocation("15:04:05", *t.TS, loc)
}
func (t TimeComponent) RequiresRefreshing() bool {
return false
}
type TimeInZoneComponent struct {
TS *string `parser:"@Time"`
InUTC bool `parser:"@Z?"`
}
func (t TimeInZoneComponent) Time() (time.Time, error) {
loc := time.Local
if t.InUTC {
loc = time.UTC
}
return time.ParseInLocation("15:04:05", *t.TS, loc)
}
func (t TimeInZoneComponent) RequiresRefreshing() bool {
return false
}
type DateWithTimeComponents struct {
Date *string `parser:"@Date"`
MaybeTime *TimeComponent `parser:" @@?"`
MaybeTime *TimeComponent `parser:"(T? @@)?"`
InUTC bool `parser:"(@Z)?"`
}
func (d DateWithTimeComponents) Time() (time.Time, error) {
t, err := time.Parse("2006-01-02", *d.Date)
loc := time.Local
if d.InUTC {
loc = time.UTC
}
t, err := time.ParseInLocation("2006-01-02", *d.Date, loc)
if err != nil {
return time.Time{}, err
}
if d.MaybeTime != nil {
tt, err := d.MaybeTime.Time()
tt, err := d.MaybeTime.Time(loc)
if err != nil {
return time.Time{}, err
}
t = t.Add(tt.Sub(tt.Truncate(24 * time.Hour)))
t = time.Date(t.Year(), t.Month(), t.Day(), tt.Hour(), tt.Minute(), tt.Second(), 0, loc)
}
return t, nil
}
type Timestamp struct {
Full *string `parser:"@Timestamp"`
DT *DateWithTimeComponents `parser:"| @@"`
TimeToday *TimeComponent `parser:" | @@"`
DT *DateWithTimeComponents `parser:"@@"`
TimeToday *TimeInZoneComponent `parser:" | @@"`
Int *int `parser:" | @Int"`
}
func (t Timestamp) Time() (time.Time, error) {
switch {
case t.Full != nil:
if strings.HasSuffix(*t.Full, "Z") {
return time.ParseInLocation("2006-01-02T15:04:05Z", *t.Full, time.UTC)
}
return time.ParseInLocation("2006-01-02T15:04:05", *t.Full, time.Local)
case t.DT != nil:
return t.DT.Time()
case t.TimeToday != nil:

View file

@ -8,17 +8,19 @@ import (
)
var basicLexer = lexer.MustSimple([]lexer.SimpleRule{
{"Identifier", `[a-zA-Z_][a-zA-Z0-9_]*`},
{"Identifier", `[a-z][a-zA-Z0-9_]*`},
{"Date", `\d{4}-\d{2}-\d{2}`},
{"Timestamp", `\d{4}-\d{2}-\d{2}T?\d{2}:\d{2}:\d{2}Z?`},
{"Time", `\d{2}:\d{2}:\d{2}Z?`},
{"Time", `\d{2}:\d{2}:\d{2}`},
{"Int", `\d+`},
{"T", `T`},
{"Z", `Z`},
{"Whitespace", `[ ]+`},
})
var parser = participle.MustBuild[Atom](
participle.Lexer(basicLexer),
participle.Elide("Whitespace"),
participle.UseLookahead(2),
)
func Parse(expr string) (TimeResult, error) {

View file

@ -0,0 +1,113 @@
package main
import (
"testing"
"time"
)
func TestParse(t *testing.T) {
// Reference time: 2025-11-19T11:07:13Z
// Unix: 1763550433
refTime := time.Date(2025, 11, 19, 11, 7, 13, 0, time.UTC)
tests := []struct {
input string
expected func() time.Time
desc string
}{
{
input: "1763550433",
expected: func() time.Time {
return refTime
},
desc: "Unix timestamp",
},
{
input: "2025-11-19T11:07:13Z",
expected: func() time.Time {
return refTime
},
desc: "ISO 8601 UTC",
},
{
input: "2025-11-19T11:07:13",
expected: func() time.Time {
return time.Date(2025, 11, 19, 11, 7, 13, 0, time.Local)
},
desc: "ISO 8601 Local",
},
{
input: "2025-11-19 11:07:13Z",
expected: func() time.Time {
return refTime
},
desc: "Date Space Time Z",
},
{
input: "2025-11-19",
expected: func() time.Time {
return time.Date(2025, 11, 19, 0, 0, 0, 0, time.Local)
},
desc: "Date only Local",
},
{
input: "2025-11-19Z",
expected: func() time.Time {
return time.Date(2025, 11, 19, 0, 0, 0, 0, time.UTC)
},
desc: "Date only UTC",
},
{
input: "11:07:13",
expected: func() time.Time {
now := time.Now()
return time.Date(now.Year(), now.Month(), now.Day(), 11, 7, 13, 0, time.Local)
},
desc: "Time only Local",
},
{
input: "11:07:13Z",
expected: func() time.Time {
now := time.Now().In(time.UTC)
return time.Date(now.Year(), now.Month(), now.Day(), 11, 7, 13, 0, time.UTC)
},
desc: "Time only UTC",
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
res, err := Parse(tc.input)
if err != nil {
t.Fatalf("Parse(%q) error: %v", tc.input, err)
}
got, err := res.Time()
if err != nil {
t.Fatalf("res.Time() error: %v", err)
}
want := tc.expected()
// For "Time only" tests, there might be a slight race condition if the day changes between test execution and expected value generation.
// But practically it's fine.
if !got.Truncate(time.Second).Equal(want) {
t.Errorf("Parse(%q) = %v, want %v", tc.input, got, want)
}
})
}
t.Run("now", func(t *testing.T) {
res, err := Parse("now")
if err != nil {
t.Fatalf("Parse('now') error: %v", err)
}
got, err := res.Time()
if err != nil {
t.Fatalf("res.Time() error: %v", err)
}
if time.Since(got) > time.Second {
t.Errorf("Parse('now') = %v, want close to now", got)
}
})
}

View file

@ -1,15 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="light dark">
<title>Tools</title>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"
>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css">
</head>
<body class="container">
<header>
<hgroup>
@ -24,7 +23,9 @@
<li><a href="/gotemplate/">Go Template Playground</a></li>
<li><a href="/gradient-bands/">Gradient Bands</a></li>
<li><a href="/2lcc/">Two-letter Country Codes</a></li>
<li><a href="/timestamps/">Timestamp Converter</a></li>
</ul>
</main>
</body>
</html>