Skip to content
Closed
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
19 changes: 18 additions & 1 deletion time.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"time"

Expand All @@ -30,7 +31,23 @@ func ToTimeInDefaultLocationE(i any, location *time.Location) (tim time.Time, er
case time.Time:
return v, nil
case string:
return StringToDateInDefaultLocation(v, location)
tm, err := StringToDateInDefaultLocation(v, location)
if err == nil {
return tm, nil
}
// Integer inputs are treated as Unix seconds. Accept the same for pure
// numeric strings (see #102). Require 10+ digits so short values like
// "2006" stay date-parse errors rather than becoming timestamps.
if n, perr := strconv.ParseInt(v, 10, 64); perr == nil {
digits := len(v)
if v[0] == '+' || v[0] == '-' {
digits--
}
if digits >= 10 {
return time.Unix(n, 0), nil
}
}
return tm, err
case json.Number:
// Originally this used ToInt64E, but adding string float conversion broke ToTime.
// the behavior of ToTime would have changed if we continued using it.
Expand Down
3 changes: 3 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func TestTime(t *testing.T) {
{uint64(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
{uint32(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
{json.Number("1234567890"), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
{"1234567890", time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false}, // Unix seconds as string (#102)
{"1600285405", time.Date(2020, 9, 16, 19, 43, 25, 0, time.UTC), false}, // Unix seconds as string (#102)
{"-62135596800", time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC), false}, // Unix seconds before epoch as string
{time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},

{ptr, time.Time{}, false},
Expand Down