From 26f061ceebb7a3fd7e783f9127c6babdea12ced2 Mon Sep 17 00:00:00 2001 From: Esteban Zimanyi Date: Thu, 19 Nov 2026 16:00:32 +0100 Subject: [PATCH] Write every timestamp through one converter Two converters wrote timestamps: rfc3339 in timestamp.go parsed a value the backend returned and wrote it with Go's RFC 3339 layout, a UTC instant as Z; rfc3339Tz in main.go completed the hour-only offsets inside the MF-JSON MobilityDB writes, a UTC instant as +00:00. The response time of a TemporalProperties document and the bounds of a Part 4 window were written by a third and a fourth route, as Z, the bounds of a count window being the records' times as received. rfc3339 is the one converter, writing an instant with its offset in hours and minutes as the backend stated it, rfc3339Layout. rfc3339In applies it to every timestamp a JSON document holds, which ogcify and the Server-Sent Events stream call; the time series and trajectory readers, the response time and the window bounds use rfc3339 or rfc3339Layout. rfc3339Tz and its regular expression are gone, and its test cases are rfc3339In's. Every timestamp of the samples is written +00:00. --- main.go | 12 ++---------- samples/collection.json | 4 ++-- samples/moving-feature-collection.json | 8 ++++---- samples/moving-feature.json | 4 ++-- samples/temporal-properties.json | 2 +- stream.go | 2 +- stream_engine_meos.go | 4 ++-- stream_geometry_test.go | 20 -------------------- stream_meos_test.go | 10 +++++----- timeseries.go | 2 +- timestamp.go | 26 ++++++++++++++++++++++++-- timestamp_test.go | 25 ++++++++++++++++++++++++- trajectories.go | 2 +- 13 files changed, 69 insertions(+), 52 deletions(-) diff --git a/main.go b/main.go index 732d594..ab2c0b9 100644 --- a/main.go +++ b/main.go @@ -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"`) } @@ -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}}, }) } diff --git a/samples/collection.json b/samples/collection.json index 9ad9dfb..f8730bb 100644 --- a/samples/collection.json +++ b/samples/collection.json @@ -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" diff --git a/samples/moving-feature-collection.json b/samples/moving-feature-collection.json index b953432..47537dc 100644 --- a/samples/moving-feature-collection.json +++ b/samples/moving-feature-collection.json @@ -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", @@ -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", diff --git a/samples/moving-feature.json b/samples/moving-feature.json index 95cc71d..136a10b 100644 --- a/samples/moving-feature.json +++ b/samples/moving-feature.json @@ -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", diff --git a/samples/temporal-properties.json b/samples/temporal-properties.json index 824c9e5..cbaa15e 100644 --- a/samples/temporal-properties.json +++ b/samples/temporal-properties.json @@ -55,6 +55,6 @@ "type": "TText" } ], - "timeStamp": "2026-08-29T21:58:37Z" + "timeStamp": "2026-08-29T21:58:37+00:00" } diff --git a/stream.go b/stream.go index 12d7e89..78abae0 100644 --- a/stream.go +++ b/stream.go @@ -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() } } diff --git a/stream_engine_meos.go b/stream_engine_meos.go index 36121b3..1dd35c8 100644 --- a/stream_engine_meos.go +++ b/stream_engine_meos.go @@ -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] @@ -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 { diff --git a/stream_geometry_test.go b/stream_geometry_test.go index fe62d7e..f573ffd 100644 --- a/stream_geometry_test.go +++ b/stream_geometry_test.go @@ -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) - } - } -} diff --git a/stream_meos_test.go b/stream_meos_test.go index 215cfe0..f27b99a 100644 --- a/stream_meos_test.go +++ b/stream_meos_test.go @@ -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] @@ -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 { diff --git a/timeseries.go b/timeseries.go index 634d6cd..c65e09a 100644 --- a/timeseries.go +++ b/timeseries.go @@ -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 } diff --git a/timestamp.go b/timestamp.go index 0e7c495..bbaf9e1 100644 --- a/timestamp.go +++ b/timestamp.go @@ -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 @@ -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 @@ -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]) + `"` + }) +} diff --git a/timestamp_test.go b/timestamp_test.go index c1e8dd0..186cf32 100644 --- a/timestamp_test.go +++ b/timestamp_test.go @@ -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"}, @@ -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) + } + } +} diff --git a/trajectories.go b/trajectories.go index 6ac2448..3470bde 100644 --- a/trajectories.go +++ b/trajectories.go @@ -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 }