Documentation ¶
Index ¶
- Constants
- func FromTimestamp(ts *timestamp.Timestamp) time.Time
- func FromUnixMillis(ms uint64) time.Time
- func IsISO8601(s string) bool
- func MaxTimestamp() *timestamp.Timestamp
- func ParseISO8601(s string) (duration time.Duration, err error)
- func SafeUnix(sec int64) time.Time
- func ToTimestamp(t time.Time) *timestamp.Timestamp
- func ToUnixMillis(t time.Time) uint64
Constants ¶
const ( // MaxTimestampSeconds is the seconds field of the latest valid protobuf // timestamp. Note that the maximum possible timestamp also has // `MaxTimestampNanos` as the nanoseconds field. // This is time.Date(10000, 1, 1, 0, 0, 0, 0, time.UTC).Unix() - 1. // Reference: https://github.com/golang/protobuf/blob/master/ptypes/timestamp.go MaxTimestampSeconds = 253402300799 // MinTimestampSeconds is the seconds field of the oldest valid protobuf // timestamp. This is equal to the seconds value of the zero time. // Reference: https://github.com/golang/protobuf/blob/master/ptypes/timestamp.go MinTimestampSeconds = -62135596800 // MaxTimestampNanos is the largest number of nanoseconds allowed in a // protobuf timestamp. This is strictly less than a second. // Reference: https://github.com/golang/protobuf/blob/master/ptypes/timestamp.go#L73 MaxTimestampNanos int64 = 1e9 - 1 )
Variables ¶
This section is empty.
Functions ¶
func FromTimestamp ¶
FromTimestamp parses a `ptypes.Timestamp` into the bounds of a timestamp.
* A nil timestamp will be parsed to a zero `time.Time`. * A timestamp outside of the nano range will be corrected, then re-parsed. * A timestamp outside of either second bound will be parsed to the correct limit.
func FromUnixMillis ¶
FromUnixMillis parses the given uint64 as a timestamp in ms. We convert from uint64 to int64 within this, and as such there is a chance of subtle errors occurring.
* If the arg underflows, return the zero time. * If the arg overflows, return the max timestamp time * Otherwise, parse it normally.
func MaxTimestamp ¶
MaxTimestamp returns the maximum timestamp possible, which corresponds to having a seconds field of `253402300799`, and a nanoseconds field of 1e9-1.
func ParseISO8601 ¶
Parses an ISO-8601 formatted duration.
This attempts to mimic Java's Duration.parse. Note the leading plus/minus sign, and negative values for other units are not part of the ISO-8601 standard but Java includes them so we handle them here as well.
func SafeUnix ¶
SafeUnix is a safe way of calling a `time.Unix` without nsec added. This does so in a way where: * If the time can be safely parsed, parse it. * If the time can underflow, return the zero time. * If the time can overflow, return the max protobuf timestamp (Year 9999).
func ToTimestamp ¶
ToTimestamp parses a `time.Time` into the bounds of a timestamp, rounding values outside of the valid range to those inside the valid range.
func ToUnixMillis ¶
ToUnixMillis parses a given time into unsigned milliseconds. Since `UnixNano()` is used to convert, and it is not defined for years outside of certain bounds (see the constants for more info), we have to define the behaviour for this, which may limit the use of this function in other places.
For a `time.Time` with a year >= 2262, we return the MaxTimestampSeconds as millis. For a `time.Time` with a year < 1970, we return 0 For a `time.Time` with 1970 <= year < 2262, we return the parsed millis.
Types ¶
This section is empty.