Documentation
¶
Index ¶
- Variables
- func BytesToString(bytes ...byte) string
- func DurationOverflows(durations ...time.Duration) (sum int64, ok bool)
- func ISO8601(t time.Time) string
- func ISO8601Compact(t time.Time) string
- func ISO8601CompactMsec(t time.Time) string
- func ISO8601Msec(t time.Time) string
- func Int64Overflows(int64s ...int64) (sum int64, ok bool)
- func LocationFromOffset(offsetSec int) (location *time.Location)
- func LocationOffsetString(d time.Duration) (string, error)
- func LocationOffsetStringDelimited(d time.Duration) (string, error)
- func OffsetForLocation(year int, month time.Month, day int, locationName string) (duration time.Duration, err error)
- func OffsetForTime(t time.Time) (duration time.Duration)
- func OffsetHM(d time.Duration) (offsetH, offsetM int)
- func ParseISOInLocation(timeStr string, location *time.Location) (time.Time, error)
- func ParseISOInUTC(timeStr string) (time.Time, error)
- func ParseISOTimestamp(timeStr string, location *time.Location) (t time.Time, err error)
- func ParseInLocation(timeStr string, location *time.Location) (time.Time, error)
- func ParseInUTC(timeStr string) (time.Time, error)
- func ParseUnixTS(timeStr string) (time.Time, error)
- func RFC7232(t time.Time) string
- func RangeOverTimes(start, end time.Time) func() (time time.Time, err error)
- func StartTimeIsBeforeEndTime(t1 time.Time, t2 time.Time) bool
- func TimeDateOnly(t time.Time) time.Time
- func TimeIsOutOfBounds(t time.Time) bool
- func TwoDigitOffset(in int, addPrefix bool) (digits string, err error)
- func YearDiffOverflows(startYear int64, endYear int64) bool
- func YearIsBeyondMax(year int64) bool
- func YearIsBeyondMin(year int64) bool
- func YearIsOutOfBounds(year int64) bool
- func ZoneFromHM(offsetH, offsetM int) (location *time.Location)
Constants ¶
This section is empty.
Variables ¶
var MaxTimestamp = time.Unix(1<<63-62135596801, 999999999)
MaxTimestamp max time value
var MinTimestamp = time.Time{}
MinTimestamp the minimum timestamp
Functions ¶
func BytesToString ¶
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 ¶
DurationOverflows does a list of durations overflow int64?
func ISO8601 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Int64Overflows does a list of int64s overflow int64?
func LocationFromOffset ¶
LocationFromOffset get a location based on the offset seconds from UTC. Uses a cache of locations based on offset.
func LocationOffsetString ¶
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 ¶
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 ¶
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 ¶
OffsetHM get hours and minutes for location offset from UTC Avoiding math.Abs and casting allows inlining in
func ParseISOInLocation ¶
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 ¶
ParseISOInUTC parse limited to ISO timestamp formats and return UTC zoned time
func ParseISOTimestamp ¶
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 ¶
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 ¶
ParseInUTC parse for all timestamps, defaulting to UTC, and return UTC zoned time
func ParseUnixTS ¶
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 ¶
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 ¶
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 ¶
StartTimeIsBeforeEndTime if time 1 is before time 2 return true, else false
func TimeDateOnly ¶
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 ¶
TimeIsOutOfBounds is time less than min or greater than max
func TwoDigitOffset ¶
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 ¶
YearDiffOverflows do to year values summed exceed the maximum year value Subtractions both ways are tried
func YearIsBeyondMax ¶
YearIsBeyondMax incoming year is greater than the max year
func YearIsBeyondMin ¶
YearIsBeyondMin incoming year is less than the min year
func YearIsOutOfBounds ¶
YearIsOutOfBounds is year greater than max year or less than min year
func ZoneFromHM ¶
ZoneFromHM get fixed zone from hour and minute offset A negative offsetH will result in a negative zone offset
Types ¶
This section is empty.