Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,11 @@ func mdbInterp(s string) (string, error) {
var epsgName = regexp.MustCompile(`"name":\s*"EPSG:(\d+)"`)
var epsgURN = regexp.MustCompile(`EPSG:+(\d+)`)

// hourOnlyOffset matches PostgreSQL's hour-only timezone offset on an ISO
// timestamp ("…18:57:52+00"); RFC 3339 — and strict parsers such as JS Date —
// require the ":00" minutes.
var hourOnlyOffset = regexp.MustCompile(`(\d\d:\d\d:\d\d(?:\.\d+)?)([+-]\d\d)(["\s]|$)`)

// rfc3339Tz completes hour-only timezone offsets so datetimes are RFC 3339.
func rfc3339Tz(s string) string { return hourOnlyOffset.ReplaceAllString(s, `$1$2:00$3`) }

// ogcify turns what MobilityDB writes into what the standard names. The
// interpolation needs nothing: MobilityDB's "Step" is already the token both
// `motionCurve` and `temporalPrimitiveValue` enumerate.
func ogcify(s string) string {
s = rfc3339Tz(s)
s = rfc3339In(s)
return epsgName.ReplaceAllString(s, `"name":"urn:ogc:def:crs:EPSG::$1"`)
}

Expand Down Expand Up @@ -1505,7 +1497,7 @@ func listTProperties(w http.ResponseWriter, r *http.Request) {
}
writeJSON(w, 200, map[string]any{
"temporalProperties": list, "numberReturned": len(list), "numberMatched": len(list),
"timeStamp": time.Now().UTC().Format(time.RFC3339),
"timeStamp": time.Now().UTC().Format(rfc3339Layout),
"links": []map[string]string{{"rel": "self", "href": base}},
})
}
Expand Down
4 changes: 2 additions & 2 deletions samples/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"temporal": {
"interval": [
[
"2026-01-01T08:00:00Z",
"2026-01-01T09:30:00Z"
"2026-01-01T08:00:00+00:00",
"2026-01-01T09:30:00+00:00"
]
],
"trs": "http://www.opengis.net/def/uom/ISO-8601/0/Gregorian"
Expand Down
8 changes: 4 additions & 4 deletions samples/moving-feature-collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
6222000
],
"time": [
"2026-01-01T08:00:00Z",
"2026-01-01T08:45:00Z"
"2026-01-01T08:00:00+00:00",
"2026-01-01T08:45:00+00:00"
],
"temporalGeometry": {
"type": "MovingPoint",
Expand Down Expand Up @@ -101,8 +101,8 @@
6220100
],
"time": [
"2026-01-01T09:00:00Z",
"2026-01-01T09:30:00Z"
"2026-01-01T09:00:00+00:00",
"2026-01-01T09:30:00+00:00"
],
"temporalGeometry": {
"type": "MovingPoint",
Expand Down
4 changes: 2 additions & 2 deletions samples/moving-feature.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
6222000
],
"time": [
"2026-01-01T08:00:00Z",
"2026-01-01T08:45:00Z"
"2026-01-01T08:00:00+00:00",
"2026-01-01T08:45:00+00:00"
],
"temporalGeometry": {
"type": "MovingPoint",
Expand Down
2 changes: 1 addition & 1 deletion samples/temporal-properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@
"type": "TText"
}
],
"timeStamp": "2026-08-29T21:58:37Z"
"timeStamp": "2026-08-29T21:58:37+00:00"
}

2 changes: 1 addition & 1 deletion stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ func streamQuery(w http.ResponseWriter, r *http.Request) {
return
}
b, _ := json.Marshal(ev)
fmt.Fprintf(w, "event: instant\ndata: %s\n\n", rfc3339Tz(string(b)))
fmt.Fprintf(w, "event: instant\ndata: %s\n\n", rfc3339In(string(b)))
fl.Flush()
}
}
Expand Down
4 changes: 2 additions & 2 deletions stream_engine_meos.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (h *meosHandle) runAggregate(ctx context.Context, spec QuerySpec, source <-
} else { // COUNT
win = append(win, in)
if len(win) >= spec.Window.Size {
if !h.emitWindow(ctx, spec, win, win[0].T, win[len(win)-1].T) {
if !h.emitWindow(ctx, spec, win, rfc3339(win[0].T), rfc3339(win[len(win)-1].T)) {
return
}
win = win[:0]
Expand Down Expand Up @@ -233,7 +233,7 @@ func (h *meosHandle) runHopping(ctx context.Context, spec QuerySpec, source <-ch
}

// unixRFC3339 writes a window bound, in Unix seconds, as an RFC 3339 UTC time.
func unixRFC3339(sec int64) string { return time.Unix(sec, 0).UTC().Format(time.RFC3339) }
func unixRFC3339(sec int64) string { return time.Unix(sec, 0).UTC().Format(rfc3339Layout) }

// hopSeconds converts a HOPPING window hop+unit into seconds.
func hopSeconds(w Window) int64 {
Expand Down
20 changes: 0 additions & 20 deletions stream_geometry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,3 @@ func TestParsePositionsMFJSON(t *testing.T) {
t.Error("expected an error for a document with no positions")
}
}

// rfc3339Tz completes PostgreSQL's hour-only timezone offset so datetimes are
// RFC 3339 (and parse in strict clients such as JS Date), without disturbing
// already-complete offsets or non-timestamp digits.
func TestRfc3339Tz(t *testing.T) {
cases := map[string]string{
`"2026-02-26T18:57:52+00"`: `"2026-02-26T18:57:52+00:00"`,
`"2026-02-26T18:57:52-05"`: `"2026-02-26T18:57:52-05:00"`,
`"2026-02-26T18:57:52.5+00"`: `"2026-02-26T18:57:52.5+00:00"`,
`"a":"2026-02-26T18:57:52+00","b":"x"`: `"a":"2026-02-26T18:57:52+00:00","b":"x"`,
`"2026-02-26T18:57:52+00:00"`: `"2026-02-26T18:57:52+00:00"`,
`"2026-02-26T18:57:52Z"`: `"2026-02-26T18:57:52Z"`,
`{"bbox":[152625.05,5883457.97,12.50]}`: `{"bbox":[152625.05,5883457.97,12.50]}`,
}
for in, want := range cases {
if got := rfc3339Tz(in); got != want {
t.Errorf("rfc3339Tz(%q) = %q, want %q", in, got, want)
}
}
}
10 changes: 5 additions & 5 deletions stream_meos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,12 @@ func TestMeosWindowBounds(t *testing.T) {
}{
// Records at 1 s and 2 s of the tumbling window [0 s, 3 s), closed by the record at 4 s.
{"TUMBLING", Window{Type: "TUMBLING", Size: 3, Unit: "SECONDS"},
[][2]float64{{1, 10}, {2, 20}, {4, 40}}, "2026-01-01T00:00:00Z", "2026-01-01T00:00:03Z"},
[][2]float64{{1, 10}, {2, 20}, {4, 40}}, "2026-01-01T00:00:00+00:00", "2026-01-01T00:00:03+00:00"},
// The first hop boundary after a record at 0 s is 1 s: the window [-2 s, 1 s).
{"HOPPING", Window{Type: "HOPPING", Size: 3, Unit: "SECONDS", Hop: 1, HopUnit: "SECONDS"},
[][2]float64{{0, 10}, {1, 20}}, "2025-12-31T23:59:58Z", "2026-01-01T00:00:01Z"},
[][2]float64{{0, 10}, {1, 20}}, "2025-12-31T23:59:58+00:00", "2026-01-01T00:00:01+00:00"},
{"COUNT", Window{Type: "COUNT", Size: 2},
[][2]float64{{1, 10}, {5, 20}}, "2026-01-01T00:00:01Z", "2026-01-01T00:00:05Z"},
[][2]float64{{1, 10}, {5, 20}}, "2026-01-01T00:00:01+00:00", "2026-01-01T00:00:05+00:00"},
} {
t.Run(c.what, func(t *testing.T) {
ev := windowResults(t, c.w, c.recs, 1)[0]
Expand All @@ -292,8 +292,8 @@ func TestMeosTumblingWindow(t *testing.T) {
start, end string
avg float64
}{
{"2026-01-01T00:00:00Z", "2026-01-01T00:00:02Z", 15},
{"2026-01-01T00:00:02Z", "2026-01-01T00:00:04Z", 35},
{"2026-01-01T00:00:00+00:00", "2026-01-01T00:00:02+00:00", 15},
{"2026-01-01T00:00:02+00:00", "2026-01-01T00:00:04+00:00", 35},
} {
ev := evs[i]
if ev["windowStart"] != want.start || ev["windowEnd"] != want.end {
Expand Down
2 changes: 1 addition & 1 deletion timeseries.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func parseFloatSeries(b []byte) []struct {
}
for i := 0; i < n; i++ {
ts, _ := dts[i].(string)
tm, err := time.Parse(time.RFC3339, rfc3339Tz(ts))
tm, err := time.Parse(time.RFC3339, rfc3339(ts))
if err != nil {
continue
}
Expand Down
26 changes: 24 additions & 2 deletions timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
// one place.
package main

import "time"
import (
"regexp"
"time"
)

// pgTimestampLayouts are the shapes a backend returns a timestamptz in: a space or a
// 'T' separator, an offset of hours or of hours and minutes, and optional fractional
Expand All @@ -27,6 +30,11 @@ var pgTimestampLayouts = []string{
"2006-01-02T15:04:05.999999Z07:00",
}

// rfc3339Layout writes an instant with the 'T' separator, its fractional seconds
// when it has any, and its offset in hours and minutes as the backend stated it:
// a UTC instant reads +00:00, as MobilityDB's MF-JSON writes it.
const rfc3339Layout = "2006-01-02T15:04:05.999999999-07:00"

// rfc3339 renders a timestamp a backend returned as text in the form the schemas
// require. A value it cannot parse is returned unchanged: the tier publishes what the
// database holds rather than substituting a guess, and a document carrying an
Expand All @@ -38,8 +46,22 @@ func rfc3339(s string) string {
}
for _, layout := range pgTimestampLayouts {
if t, err := time.Parse(layout, s); err == nil {
return t.Format(time.RFC3339Nano)
return t.Format(rfc3339Layout)
}
}
return s
}

// timestampInJSON matches a JSON string holding a timestamp in any shape
// #pgTimestampLayouts reads.
var timestampInJSON = regexp.MustCompile(
`"(\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}(?::\d{2})?))"`)

// rfc3339In renders every timestamp a JSON document holds through #rfc3339, so a
// document MobilityDB writes (MF-JSON) and a value the tier reads from a column
// leave in one form.
func rfc3339In(doc string) string {
return timestampInJSON.ReplaceAllStringFunc(doc, func(q string) string {
return `"` + rfc3339(q[1:len(q)-1]) + `"`
})
}
25 changes: 24 additions & 1 deletion timestamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ func TestATSTimestampIsRFC3339(t *testing.T) {
for _, c := range []struct{ in, want string }{
// What PostgreSQL returns: a space, and an offset abbreviated to the hour.
{"2026-01-01 09:00:00+01", "2026-01-01T09:00:00+01:00"},
{"2026-01-01 08:00:00+00", "2026-01-01T08:00:00Z"},
{"2026-01-01 08:00:00+00", "2026-01-01T08:00:00+00:00"},
// A UTC instant written with Z leaves with the offset, as the rest do.
{"2026-01-01T08:00:00Z", "2026-01-01T08:00:00+00:00"},
{"2000-01-01 00:00:00-08", "2000-01-01T00:00:00-08:00"},
// Sub-second precision survives; a whole second carries no fraction.
{"2026-01-01 09:00:00.123456+01", "2026-01-01T09:00:00.123456+01:00"},
Expand Down Expand Up @@ -76,3 +78,24 @@ func TestATSCollectionExtentIntervalIsRFC3339(t *testing.T) {
}
})
}

// Every timestamp a JSON document holds leaves through #rfc3339, MobilityDB's
// MF-JSON among them, and nothing else in the document moves.
func TestATSTimestampsInADocument(t *testing.T) {
cases := map[string]string{
`"2026-02-26T18:57:52+00"`: `"2026-02-26T18:57:52+00:00"`,
`"2026-02-26T18:57:52-05"`: `"2026-02-26T18:57:52-05:00"`,
`"2026-02-26T18:57:52.5+00"`: `"2026-02-26T18:57:52.5+00:00"`,
`"2026-02-26 18:57:52+01"`: `"2026-02-26T18:57:52+01:00"`,
`"a":"2026-02-26T18:57:52+00","b":"x"`: `"a":"2026-02-26T18:57:52+00:00","b":"x"`,
`"2026-02-26T18:57:52+00:00"`: `"2026-02-26T18:57:52+00:00"`,
`"2026-02-26T18:57:52Z"`: `"2026-02-26T18:57:52+00:00"`,
`{"bbox":[152625.05,5883457.97,12.50]}`: `{"bbox":[152625.05,5883457.97,12.50]}`,
`"2026-02-26"`: `"2026-02-26"`,
}
for in, want := range cases {
if got := rfc3339In(in); got != want {
t.Errorf("rfc3339In(%q) = %q, want %q", in, got, want)
}
}
}
2 changes: 1 addition & 1 deletion trajectories.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func parseTripSegments(b []byte) []tripSeg {
seg := tripSeg{}
for i := 0; i < n; i++ {
ts, _ := dts[i].(string)
t, err := time.Parse(time.RFC3339, rfc3339Tz(ts))
t, err := time.Parse(time.RFC3339, rfc3339(ts))
if err != nil {
continue
}
Expand Down