timeutil

package
v0.85.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
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

func FromTimestamp(ts *timestamp.Timestamp) time.Time

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

func FromUnixMillis(ms uint64) time.Time

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 IsISO8601

func IsISO8601(s string) bool

func MaxTimestamp

func MaxTimestamp() *timestamp.Timestamp

MaxTimestamp returns the maximum timestamp possible, which corresponds to having a seconds field of `253402300799`, and a nanoseconds field of 1e9-1.

func ParseISO8601

func ParseISO8601(s string) (duration time.Duration, err error)

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

func SafeUnix(sec int64) time.Time

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

func ToTimestamp(t time.Time) *timestamp.Timestamp

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

func ToUnixMillis(t time.Time) uint64

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL