Switched to expression-based timestamps
This commit is contained in:
parent
7bf586757a
commit
5c89c44ff7
105
cmds/timestamps/ast.go
Normal file
105
cmds/timestamps/ast.go
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
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) RequiresRefreshing() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
type DateWithTimeComponents struct {
|
||||
Date *string `parser:"@Date"`
|
||||
MaybeTime *TimeComponent `parser:" @@?"`
|
||||
}
|
||||
|
||||
func (d DateWithTimeComponents) Time() (time.Time, error) {
|
||||
t, err := time.Parse("2006-01-02", *d.Date)
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
if d.MaybeTime != nil {
|
||||
tt, err := d.MaybeTime.Time()
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
t = t.Add(tt.Sub(tt.Truncate(24 * time.Hour)))
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
type Timestamp struct {
|
||||
Full *string `parser:"@Timestamp"`
|
||||
DT *DateWithTimeComponents `parser:"| @@"`
|
||||
TimeToday *TimeComponent `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:
|
||||
tt, err := t.TimeToday.Time()
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
today := time.Now().In(tt.Location())
|
||||
return time.Date(today.Year(), today.Month(), today.Day(), tt.Hour(), tt.Minute(), tt.Second(), tt.Nanosecond(), tt.Location()), nil
|
||||
}
|
||||
return time.Unix(int64(*t.Int), 0), nil
|
||||
}
|
||||
|
||||
func (t Timestamp) RequiresRefreshing() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
type Atom struct {
|
||||
Timestamp *Timestamp `parser:"@@"`
|
||||
Identifier *string `parser:" | @Identifier"`
|
||||
}
|
||||
|
||||
func (a Atom) Time() (time.Time, error) {
|
||||
switch {
|
||||
case a.Timestamp != nil:
|
||||
return a.Timestamp.Time()
|
||||
case a.Identifier != nil:
|
||||
if *a.Identifier == "now" {
|
||||
return time.Now(), nil
|
||||
}
|
||||
|
||||
return time.Time{}, errors.New("unknown identifier")
|
||||
}
|
||||
return time.Time{}, errors.New("unhandled case")
|
||||
}
|
||||
|
||||
func (a Atom) RequiresRefreshing() bool {
|
||||
switch {
|
||||
case a.Timestamp != nil:
|
||||
return a.Timestamp.RequiresRefreshing()
|
||||
case a.Identifier != nil:
|
||||
if *a.Identifier == "now" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
35
cmds/timestamps/input.go
Normal file
35
cmds/timestamps/input.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/alecthomas/participle/v2"
|
||||
"github.com/alecthomas/participle/v2/lexer"
|
||||
)
|
||||
|
||||
var basicLexer = lexer.MustSimple([]lexer.SimpleRule{
|
||||
{"Identifier", `[a-zA-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?`},
|
||||
{"Int", `\d+`},
|
||||
{"Whitespace", `[ ]+`},
|
||||
})
|
||||
|
||||
var parser = participle.MustBuild[Atom](
|
||||
participle.Lexer(basicLexer),
|
||||
participle.Elide("Whitespace"),
|
||||
)
|
||||
|
||||
func Parse(expr string) (TimeResult, error) {
|
||||
res, err := parser.ParseString("expr", expr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
type TimeResult interface {
|
||||
Time() (time.Time, error)
|
||||
RequiresRefreshing() bool
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"fmt"
|
||||
"strings"
|
||||
"syscall/js"
|
||||
"time"
|
||||
|
|
@ -59,9 +59,32 @@ 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":
|
||||
return fmt.Sprintf("%d", t.Unix())
|
||||
case "unix_micro":
|
||||
return fmt.Sprintf("%d", t.UnixNano()/1000)
|
||||
}
|
||||
|
||||
/*
|
||||
switch operation {
|
||||
case "unix":
|
||||
sec, err := strconv.ParseInt(trimmedLine, 10, 64)
|
||||
|
|
@ -96,6 +119,7 @@ func processLine(line, operation, timezone string) string {
|
|||
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),
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -1,3 +1,5 @@
|
|||
module github.com/lmika/webtools
|
||||
|
||||
go 1.24.3
|
||||
|
||||
require github.com/alecthomas/participle/v2 v2.1.4 // indirect
|
||||
|
|
|
|||
2
go.sum
Normal file
2
go.sum
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
github.com/alecthomas/participle/v2 v2.1.4 h1:W/H79S8Sat/krZ3el6sQMvMaahJ+XcM9WSI2naI7w2U=
|
||||
github.com/alecthomas/participle/v2 v2.1.4/go.mod h1:8tqVbpTX20Ru4NfYQgZf4mP18eXPTBViyMWiArNEgGI=
|
||||
|
|
@ -13,10 +13,9 @@
|
|||
<h1>Timestamp Converter</h1>
|
||||
<div class="controls">
|
||||
<select id="operation">
|
||||
<option value="unix">Convert from Unix</option>
|
||||
<option value="unix_micro">Convert from Unix Micro</option>
|
||||
<option value="to_utc">To UTC</option>
|
||||
<option value="from_utc">From UTC</option>
|
||||
<option value="iso_8601">ISO-8601</option>
|
||||
<option value="unix">Unix</option>
|
||||
<option value="unix_micro">Unix Micro</option>
|
||||
</select>
|
||||
<div class="timezone-selector">
|
||||
<label>
|
||||
|
|
|
|||
|
|
@ -1,103 +0,0 @@
|
|||
|
||||
function processLine(line, operation, timezone) {
|
||||
if (!line.trim() || line.trim().startsWith('#')) {
|
||||
return line;
|
||||
}
|
||||
|
||||
try {
|
||||
let date;
|
||||
const trimmedLine = line.trim();
|
||||
|
||||
switch (operation) {
|
||||
case 'unix':
|
||||
// Input is seconds
|
||||
date = new Date(Number(trimmedLine) * 1000);
|
||||
break;
|
||||
case 'unix_micro':
|
||||
// Input is milliseconds
|
||||
date = new Date(Number(trimmedLine));
|
||||
break;
|
||||
case 'to_utc':
|
||||
case 'from_utc':
|
||||
// Input is ISO 8601
|
||||
date = new Date(trimmedLine);
|
||||
break;
|
||||
}
|
||||
|
||||
if (isNaN(date.getTime())) {
|
||||
return 'Invalid Date';
|
||||
}
|
||||
|
||||
if (operation === 'to_utc') {
|
||||
let dateToConvert = date;
|
||||
if (timezone === 'utc' && !trimmedLine.toUpperCase().endsWith('Z') && !trimmedLine.match(/[+-]\d{2}:?\d{2}$/)) {
|
||||
dateToConvert = new Date(trimmedLine + 'Z');
|
||||
}
|
||||
return dateToConvert.toISOString();
|
||||
}
|
||||
|
||||
if (operation === 'from_utc') {
|
||||
let dateFromUtc;
|
||||
if (!trimmedLine.toUpperCase().endsWith('Z') && !trimmedLine.match(/[+-]\d{2}:?\d{2}$/)) {
|
||||
dateFromUtc = new Date(trimmedLine + 'Z');
|
||||
} else {
|
||||
dateFromUtc = new Date(trimmedLine);
|
||||
}
|
||||
|
||||
if (timezone === 'utc') {
|
||||
return dateFromUtc.toISOString();
|
||||
} else {
|
||||
// Local
|
||||
const offset = dateFromUtc.getTimezoneOffset();
|
||||
const localDate = new Date(dateFromUtc.getTime() - (offset * 60 * 1000));
|
||||
return localDate.toISOString().slice(0, -1);
|
||||
}
|
||||
}
|
||||
|
||||
// For Unix conversions:
|
||||
if (timezone === 'utc') {
|
||||
return date.toISOString();
|
||||
} else {
|
||||
// Local
|
||||
const offset = date.getTimezoneOffset();
|
||||
const localDate = new Date(date.getTime() - (offset * 60 * 1000));
|
||||
return localDate.toISOString().slice(0, -1);
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
return 'Error';
|
||||
}
|
||||
}
|
||||
|
||||
// Tests
|
||||
console.log("Running tests...");
|
||||
|
||||
// 1. Unix to UTC
|
||||
const t1 = processLine('1672531200', 'unix', 'utc');
|
||||
console.log(`1. Unix to UTC: ${t1} (Expected: 2023-01-01T00:00:00.000Z)`);
|
||||
if (t1 !== '2023-01-01T00:00:00.000Z') console.error("FAIL");
|
||||
|
||||
// 2. Unix Micro to UTC
|
||||
const t2 = processLine('1672531200000', 'unix_micro', 'utc');
|
||||
console.log(`2. Unix Micro to UTC: ${t2} (Expected: 2023-01-01T00:00:00.000Z)`);
|
||||
if (t2 !== '2023-01-01T00:00:00.000Z') console.error("FAIL");
|
||||
|
||||
// 3. To UTC (from Local input)
|
||||
// Note: Node.js server might be in UTC or Local.
|
||||
// If I run this on a machine, "Local" depends on system time.
|
||||
// However, the logic for 'to_utc' with 'utc' timezone forces Z.
|
||||
const t3 = processLine('2023-01-01T00:00:00', 'to_utc', 'utc');
|
||||
console.log(`3. To UTC (input no offset, selected UTC): ${t3} (Expected: 2023-01-01T00:00:00.000Z)`);
|
||||
if (t3 !== '2023-01-01T00:00:00.000Z') console.error("FAIL");
|
||||
|
||||
// 4. From UTC (to UTC)
|
||||
const t4 = processLine('2023-01-01T00:00:00', 'from_utc', 'utc');
|
||||
console.log(`4. From UTC (input no offset, selected UTC): ${t4} (Expected: 2023-01-01T00:00:00.000Z)`);
|
||||
if (t4 !== '2023-01-01T00:00:00.000Z') console.error("FAIL");
|
||||
|
||||
// 5. Comment
|
||||
const t5 = processLine('# comment', 'unix', 'utc');
|
||||
console.log(`5. Comment: ${t5} (Expected: # comment)`);
|
||||
if (t5 !== '# comment') console.error("FAIL");
|
||||
|
||||
console.log("Tests finished.");
|
||||
Loading…
Reference in a new issue