timestamp

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2021 License: Apache-2.0, BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

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 ISO8601CompactInLocation

func ISO8601CompactInLocation(t time.Time, location *time.Location) string

ISO8601CompactInLocation timestamp with no sub seconds in location

"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 ISO8601CompactMsecInLocation

func ISO8601CompactMsecInLocation(t time.Time, location *time.Location) string

ISO8601CompactMsecInLocation timestamp with no seconds in location

"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 ISO8601CompactMsecUTC

func ISO8601CompactMsecUTC(t time.Time) string

ISO8601CompactMsecUTC 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 ISO8601CompactUTC

func ISO8601CompactUTC(t time.Time) string

ISO8601CompactUTC 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 ISO8601InLocation

func ISO8601InLocation(t time.Time, location *time.Location) string

ISO8601InLocation timestamp long format string result in location

"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 ISO8601MsecInLocation

func ISO8601MsecInLocation(t time.Time, location *time.Location) string

ISO8601MsecInLocation ISO-8601 longtimestamp with msec in location

"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 ISO8601MsecUTC

func ISO8601MsecUTC(t time.Time) string

ISO8601MsecUTC 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 ISO8601UTC

func ISO8601UTC(t time.Time) string

ISO8601UTC 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 LocationFromOffset

func LocationFromOffset(offsetSec int) *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) (d 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) (d 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) (time.Time, 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, 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 RunesToString

func RunesToString(runes ...rune) string

RunesToString convert runes list to string with no allocation

WriteRune is more complex than WriteByte so can't inline

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

func StartTimeIsBeforeEndTime

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

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

func StringToInt

func StringToInt(input string) (int, error)

StringToInt convert a string with no decimal spaces to int. It's slower by a tiny amount than Atoi and will be removed.

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 TwoDigitOffset

func TwoDigitOffset(in int, addPrefix bool) (string, 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 ZoneFromHM

func ZoneFromHM(offsetH, offsetM int) *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.

Jump to

Keyboard shortcuts

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