timestamp

package module
v0.0.0-...-e360d49 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2022 License: Apache-2.0 Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

timestamp

ISO-8601 timestamp parsing

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MaxTimestamp = time.Unix(1<<63-62135596801, 999999999)

MaxTimestamp max time value

View Source
var MinTimestamp = time.Time{}

MinTimestamp the minimum timestamp

Functions

func BytesToString

func BytesToString(bytes ...byte) string

BytesToString convert byte list to string with no allocation

can inline - strings.Builder WriteByte is less complex than WriteRune

A small cost a few ns in testing is incurred for using a string builder. There are no heap allocations using strings.Builder.

func DurationOverflows

func DurationOverflows(durations ...time.Duration) (sum int64, ok bool)

DurationOverflows does a list of durations overflow int64?

func ISO8601

func ISO8601(t time.Time) string

ISO8601 ISO-8601 timestamp long format string result

"2006-01-02T15:04:05-07:00"

Result will be in whatever the location the incoming time is set to. If UTC is desired set location to time.UTC first

func ISO8601Compact

func ISO8601Compact(t time.Time) string

ISO8601Compact ISO-8601 timestamp with no sub seconds

"20060102T150405-0700"

Result will be in whatever the location the incoming time is set to. If UTC is desired set location to time.UTC first

func ISO8601CompactMsec

func ISO8601CompactMsec(t time.Time) string

ISO8601CompactMsec ISO-8601 timestamp with no seconds

"20060102T150405.000-0700"

Result will be in whatever the location the incoming time is set to. If UTC is desired set location to time.UTC first

func ISO8601Msec

func ISO8601Msec(t time.Time) string

ISO8601Msec ISO-8601 longtimestamp with msec

"2006-01-02T15:04:05.000-07:00"

Result will be in whatever the location the incoming time is set to. If UTC is desired set location to time.UTC first

func Int64Overflows

func Int64Overflows(int64s ...int64) (sum int64, ok bool)

Int64Overflows does a list of int64s overflow int64?

func LocationFromOffset

func LocationFromOffset(offsetSec int) (location *time.Location)

LocationFromOffset get a location based on the offset seconds from UTC. Uses a cache of locations based on offset.

func LocationOffsetString

func LocationOffsetString(d time.Duration) (string, error)

LocationOffsetString get an offset in HHMM format based on hours and minutes offset from UTC.

For 5 hours and 30 minutes

0530

For -5 hours and 30 minutes

-0500

func LocationOffsetStringDelimited

func LocationOffsetStringDelimited(d time.Duration) (string, error)

LocationOffsetStringDelimited get an offset in HHMM format based on hours and minutes offset from UTC.

For 5 hours and 30 minutes

05:30

For -5 hours and 30 minutes

-05:00

func OffsetForLocation

func OffsetForLocation(year int, month time.Month, day int, locationName string) (duration time.Duration, err error)

OffsetForLocation get offset data for a named zone such a America/Tornto or EST or MST. Based on date the offset for a zone can differ, with, for example, an offset of -0500 for EST in the summer and -0400 for EST in the winter. This assumes that a year, month, and day is available and have been used to create the date to be analyzed. Based on this the offset for the supplied zone name is obtained. This has to be tested more, in particular the calculations to get the minutes.

Get integer value of hours offset

hours = int(d.Hours())

For 5.5 hours of offset or 0530

60 × 5.5 = 330 minutes total offset
330 % 60 = 30 minutes

For an offset of 4.25 hours or 0415

60 × 4.25 = 255 minutes total offset
255 % 60 = 15 minutes

If the zone is not recognized in Go's tzdata database an error will be returned.

func OffsetForTime

func OffsetForTime(t time.Time) (duration time.Duration)

OffsetForTime the duration of the offset from UTC. Mostly the same as doing the same thing inline but this reliably gets a duration.

func OffsetHM

func OffsetHM(d time.Duration) (offsetH, offsetM int)

OffsetHM get hours and minutes for location offset from UTC Avoiding math.Abs and casting allows inlining in

func ParseISOInLocation

func ParseISOInLocation(timeStr string, location *time.Location) (time.Time, error)

ParseISOInLocation parse limited to ISO timestamp formats, defaulting to location if there is no zone in the incoming timezone. Return time adjusted to UTC.

func ParseISOInUTC

func ParseISOInUTC(timeStr string) (time.Time, error)

ParseISOInUTC parse limited to ISO timestamp formats and return UTC zoned time

func ParseISOTimestamp

func ParseISOTimestamp(timeStr string, location *time.Location) (t time.Time, err error)

ParseISOTimestamp parse an ISO timetamp iteratively. The reult will be in the zone for the timestamp or if there is no zone offset in the incoming timestamp the incoming location will bue used. It is the responsibility of further steps to standardize to a specific zone offset.

func ParseInLocation

func ParseInLocation(timeStr string, location *time.Location) (time.Time, error)

ParseInLocation parse for all timestamp formats and default to location if there is no zone in the incoming timestamp. Return time adjusted to UTC.

func ParseInUTC

func ParseInUTC(timeStr string) (time.Time, error)

ParseInUTC parse for all timestamps, defaulting to UTC, and return UTC zoned time

func ParseUnixTS

func ParseUnixTS(timeStr string) (time.Time, error)

ParseUnixTS parse a timestamp directly, assuming input is some sort of UNIX timestamp. If the input is known to be a timestamp this will be faster than first trying to parse as other forms of timestamp. This function will handle timestamps in the form of seconds and nanoseconds delimited by a period.

e.g. 113621424536300000 becomes 1136214245.36300000

Can't inline

func RFC7232

func RFC7232(t time.Time) string

RFC7232 get format used for http headers

"Mon, 02 Jan 2006 15:04:05 GMT"

TimeFormat is the time format to use when generating times in HTTP headers. It is like time.RFC1123 but hard-codes GMT as the time zone. The time being formatted must be in UTC for Format to generate the correct format. This is done in the function before the call to format.

Can't inline as the format must be in GMT

func RangeOverTimes

func RangeOverTimes(start, end time.Time) func() (time time.Time, err error)

RangeOverTimes returns a date range function over start date to end date inclusive. After the end of the range, the range function returns a zero date, date.IsZero() is true. If zones for start and end differ an error will be returned and needs to be checked for before time.IsZero().

Note that this function has been modified to NOT change the location for the start and end time to UTC. This is in keeping with the avoidance of change to time locations passed into function. It is the responsibility of the caller to set location in keeping with the intended use of the function. The location used could affect the day values.

Sample usage assuming building a map with empty string values:

t1 := time.Now()
t2 := t1.Add(30 * 24 * time.Hour)

m := make(map[string]string)

var err error
var newTime time.Time
for rt := timestamp.RangeOverTimes(t1, t2); ; {
	newTime, err = rt()
	if err != nil {
		// Handle when there was an error in the input times
		break
	}
	if newTime.IsZero() {
		// Handle when the day range is done
		break
	}
	v := fmt.Sprintf("%04d-%02d-%02d", newTime.Year(), newTime.Month(), newTime.Day())
	m[v] = ""
}

if err != nil {
	// handle error due to non-equal UTC offsets
}

a := make([]string, 0, len(m))
for v := range m {
	a = append(a, v)
}
sort.Strings(a)
fmt.Println("Days in range")

for _, v := range a {
	fmt.Println("Got", v)
}

func StartTimeIsBeforeEndTime

func StartTimeIsBeforeEndTime(t1 time.Time, t2 time.Time) bool

StartTimeIsBeforeEndTime if time 1 is before time 2 return true, else false

func TimeDateOnly

func TimeDateOnly(t time.Time) time.Time

TimeDateOnly get date with zero time values

Note that this function has been modified to NOT change the location for the start and end time to UTC. This is in keeping with the avoidance of change to time locations passed into function. It is the responsibility of the caller to set location in keeping with the intended use of the function.

Can inline

func TimeIsOutOfBounds

func TimeIsOutOfBounds(t time.Time) bool

TimeIsOutOfBounds is time less than min or greater than max

func TwoDigitOffset

func TwoDigitOffset(in int, addPrefix bool) (digits string, err error)

TwoDigitOffset get digit offset for hours and minutes. This is designed solely to help with calculating offset strings for timestamps without using fmt.Sprintf, which causes allocations. This function is about 1/3 faster than fmt.Sprintf.

func YearDiffOverflows

func YearDiffOverflows(startYear int64, endYear int64) bool

YearDiffOverflows do to year values summed exceed the maximum year value Subtractions both ways are tried

func YearIsBeyondMax

func YearIsBeyondMax(year int64) bool

YearIsBeyondMax incoming year is greater than the max year

func YearIsBeyondMin

func YearIsBeyondMin(year int64) bool

YearIsBeyondMin incoming year is less than the min year

func YearIsOutOfBounds

func YearIsOutOfBounds(year int64) bool

YearIsOutOfBounds is year greater than max year or less than min year

func ZoneFromHM

func ZoneFromHM(offsetH, offsetM int) (location *time.Location)

ZoneFromHM get fixed zone from hour and minute offset A negative offsetH will result in a negative zone offset

Types

This section is empty.

Directories

Path Synopsis
pkg
xfmt
Package xfmt provides addons to std fmt and strconv packages with focus on formatting text without allocations.
Package xfmt provides addons to std fmt and strconv packages with focus on formatting text without allocations.

Jump to

Keyboard shortcuts

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