CLI file timestamps reject offsets and discard milliseconds (#20491)

Fixes #20486

## Summary

Try RFC3339/RFC3339Nano and preserve the millisecond remainder when
converting epoch milliseconds.
This commit is contained in:
Taranum Wasu
2026-10-10 10:47:29 +08:00
committed by GitHub
parent 4c9edbfdf7
commit f2e618aed6
2 changed files with 36 additions and 2 deletions
+5 -2
View File
@@ -760,6 +760,8 @@ func parseTime(v interface{}) time.Time {
formats := []string{
"2006-01-02 15:04:05",
"2006-01-02T15:04:05",
time.RFC3339Nano,
time.RFC3339,
"2006-01-02T15:04:05Z",
"2006-01-02",
}
@@ -774,9 +776,10 @@ func parseTime(v interface{}) time.Time {
return time.Time{}
}
// Convert milliseconds to seconds if timestamp is in milliseconds (13 digits)
// Convert milliseconds to seconds if timestamp is in milliseconds (13 digits),
// preserving the sub-second remainder.
if ts > 1e12 {
ts = ts / 1000
return time.Unix(ts/1000, (ts%1000)*int64(time.Millisecond))
}
return time.Unix(ts, 0)
@@ -0,0 +1,31 @@
package filesystem
import (
"encoding/json"
"testing"
"time"
)
func TestParseTimeRFC3339WithOffset(t *testing.T) {
got := parseTime("2026-09-30T15:04:05+05:30")
if got.IsZero() {
t.Fatal("RFC3339 timestamp with zone offset must parse, got zero time")
}
want, _ := time.Parse(time.RFC3339, "2026-09-30T15:04:05+05:30")
if !got.Equal(want) {
t.Fatalf("got %v, want %v", got, want)
}
}
func TestParseTimeMillisecondsRemainder(t *testing.T) {
var record map[string]interface{}
if err := json.Unmarshal([]byte(`{"created_at":1727700000123}`), &record); err != nil {
t.Fatal(err)
}
for _, input := range []interface{}{int64(1727700000123), record["created_at"]} {
got := parseTime(input)
if got.UnixMilli() != 1727700000123 {
t.Fatalf("millisecond remainder lost for %T: got %d", input, got.UnixMilli())
}
}
}