timestamp: clarify doc for ParseTimestamp

Signed-off-by: Samuel Karp <me@samuelkarp.com>
This commit is contained in:
Samuel Karp 2022-07-08 21:40:11 -07:00
parent 0910306bf9
commit 9cd67df5b2
No known key found for this signature in database
GPG Key ID: AAA3FE8A831FC087
2 changed files with 12 additions and 10 deletions

View File

@ -95,19 +95,19 @@ func GetTimestamp(value string, reference time.Time) (string, error) {
return fmt.Sprintf("%d.%09d", t.Unix(), int64(t.Nanosecond())), nil return fmt.Sprintf("%d.%09d", t.Unix(), int64(t.Nanosecond())), nil
} }
// ParseTimestamps returns seconds and nanoseconds from a timestamp that has the // ParseTimestamps returns seconds and nanoseconds from a timestamp that has
// format "%d.%09d", time.Unix(), int64(time.Nanosecond())) // the format ("%d.%09d", time.Unix(), int64(time.Nanosecond())).
// if the incoming nanosecond portion is longer or shorter than 9 digits it is // If the incoming nanosecond portion is longer than 9 digits it is truncated.
// converted to nanoseconds. The expectation is that the seconds and // The expectation is that the seconds and nanoseconds will be used to create a
// seconds will be used to create a time variable. For example: // time variable. For example:
// //
// seconds, nanoseconds, err := ParseTimestamp("1136073600.000000001",0) // seconds, nanoseconds, _ := ParseTimestamp("1136073600.000000001",0)
// if err == nil since := time.Unix(seconds, nanoseconds) // since := time.Unix(seconds, nanoseconds)
// //
// returns seconds as def(aultSeconds) if value == "" // returns seconds as defaultSeconds if value == ""
func ParseTimestamps(value string, def int64) (int64, int64, error) { func ParseTimestamps(value string, defaultSeconds int64) (int64, int64, error) {
if value == "" { if value == "" {
return def, 0, nil return defaultSeconds, 0, nil
} }
return parseTimestamp(value) return parseTimestamp(value)
} }

View File

@ -74,6 +74,8 @@ func TestParseTimestamps(t *testing.T) {
{"1136073600", 0, 1136073600, 0, false}, {"1136073600", 0, 1136073600, 0, false},
{"1136073600.000000001", 0, 1136073600, 1, false}, {"1136073600.000000001", 0, 1136073600, 1, false},
{"1136073600.0000000010", 0, 1136073600, 1, false}, {"1136073600.0000000010", 0, 1136073600, 1, false},
{"1136073600.0000000001", 0, 1136073600, 0, false},
{"1136073600.0000000009", 0, 1136073600, 0, false},
{"1136073600.00000001", 0, 1136073600, 10, false}, {"1136073600.00000001", 0, 1136073600, 10, false},
{"foo.bar", 0, 0, 0, true}, {"foo.bar", 0, 0, 0, true},
{"1136073600.bar", 0, 1136073600, 0, true}, {"1136073600.bar", 0, 1136073600, 0, true},