Help us shape the future of Go! Take the 2026 Go Developer Survey

compact_time

package module
v1.8.3 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2021 License: MIT Imports: 5 Imported by: 11

README

Compact Time

A go implementation of compact time.

Library Usage

func demonstrateEncode() {
	location, err := time.LoadLocation("Asia/Singapore")
	if err != nil {
		// TODO: Handle error
	}
	goDate := time.Date(2020, time.Month(8), 30, 15, 33, 14, 19577323, location)
	compactDate, err := AsCompactTime(goDate)
	if err != nil {
		// TODO: Handle error
	}
	buffer := &bytes.Buffer{}
	encodedCount, err := compactDate.Encode(buffer)
	if err != nil {
		// TODO: Handle error
	}
	fmt.Printf("Encoded [%v] into %v bytes: %v\n", goDate, encodedCount, buffer.Bytes())
	// Prints: Encoded [2020-08-30 15:33:14.019577323 +0800 +08] into 21 bytes: [95 207 85 9 156 240 121 68 1 22 83 47 83 105 110 103 97 112 111 114 101]
}

func demonstrateDecode() {
	buffer := []byte{0x28, 0x9a, 0x12, 0x78, 0x08}
	compactDate, decodedCount, err := DecodeTimestamp(bytes.NewBuffer(buffer))
	if err != nil {
		// TODO: Handle error
	}
	goDate, err := compactDate.AsGoTime()
	if err != nil {
		// TODO: Handle error
	}
	fmt.Printf("Decoded %v bytes of %v into [%v]\n", decodedCount, buffer, goDate)
	// Prints: Decoded 5 bytes of [40 154 18 120 8] into [1966-12-01 05:13:05 +0000 UTC]
}

License

MIT License:

Copyright 2019 Karl Stenerud

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package compact_time provides encoding and decoding mechanisms for the compact time format (https://github.com/kstenerud/compact-time), as well as basic conversion functions to/from the go time package.

Basic validation is performed when decoding data, enough to ensure that it isn't blatantly wrong (such as invalid area/location values, latitude 500, december 54th, etc). However, it does not do more nuanced checks such as on which years February 29th is valid, or when leap seconds are allowed. It also doesn't check for impossible timestamp values such as 2011-03-13/02:10:00/Los_Angeles.

Package compact_time provides encoding and decoding mechanisms for the compact time format (https://github.com/kstenerud/compact-time), as well as basic conversion functions to/from the go time package.

Basic validation is performed when decoding data, enough to ensure that it isn't blatantly wrong (such as invalid area/location values, latitude 500, december 54th, etc). However, it does not do more nuanced checks such as on which years February 29th is valid, or when leap seconds are allowed. It also doesn't check for impossible timestamp values such as 2011-03-13/02:10:00/Los_Angeles.

Package compact_time provides encoding and decoding mechanisms for the compact time format (https://github.com/kstenerud/compact-time), as well as basic conversion functions to/from the go time package.

Basic validation is performed when decoding data, enough to ensure that it isn't blatantly wrong (such as invalid area/location values, latitude 500, december 54th, etc). However, it does not do more nuanced checks such as on which years February 29th is valid, or when leap seconds are allowed. It also doesn't check for impossible timestamp values such as 2011-03-13/02:10:00/Los_Angeles.

Index

Constants

View Source
const (
	TimeTypeDate = TimeType(iota)
	TimeTypeTime
	TimeTypeTimestamp
)
View Source
const (
	TimezoneTypeUnset = TimezoneType(iota)
	TimezoneTypeUTC
	TimezoneTypeLocal
	TimezoneTypeAreaLocation
	TimezoneTypeLatitudeLongitude
	TimezoneTypeUTCOffset
)
View Source
const MaxEncodeLength = 50

Maximum byte length that this library will encode

View Source
const RequiredBufferSize = 127

Variables

View Source
var ErrorIncomplete = fmt.Errorf("Compact time value is incomplete")

Functions

func EncodeGoDate added in v1.5.0

func EncodeGoDate(time gotime.Time, writer io.Writer) (bytesEncoded int, err error)

func EncodeGoDateToBytes added in v1.7.0

func EncodeGoDateToBytes(time gotime.Time, buffer []byte) (bytesEncoded int)

func EncodeGoTime added in v1.5.0

func EncodeGoTime(time gotime.Time, writer io.Writer) (bytesEncoded int, err error)

func EncodeGoTimeToBytes added in v1.7.0

func EncodeGoTimeToBytes(time gotime.Time, buffer []byte) (bytesEncoded int)

func EncodeGoTimestamp added in v1.5.0

func EncodeGoTimestamp(time gotime.Time, writer io.Writer) (bytesEncoded int, err error)

func EncodeGoTimestampToBytes added in v1.7.0

func EncodeGoTimestampToBytes(time gotime.Time, buffer []byte) (bytesEncoded int)

func EncodedSizeGoDate added in v1.5.0

func EncodedSizeGoDate(time gotime.Time) int

func EncodedSizeGoTime added in v1.5.0

func EncodedSizeGoTime(time gotime.Time) int

func EncodedSizeGoTimestamp added in v1.5.0

func EncodedSizeGoTimestamp(time gotime.Time) int

Types

type Time added in v1.0.1

type Time struct {
	Timezone   Timezone
	Year       int
	Nanosecond uint32
	Second     uint8
	Minute     uint8
	Hour       uint8
	Day        uint8
	Month      uint8
	Type       TimeType
}

func AsCompactTime added in v1.0.1

func AsCompactTime(src gotime.Time) Time

Convert a golang time value to compact time

func DecodeDate

func DecodeDate(reader io.Reader) (time Time, bytesDecoded int, err error)

Decode a date.

func DecodeDateWithBuffer added in v1.7.0

func DecodeDateWithBuffer(reader io.Reader, buffer []byte) (time Time, bytesDecoded int, err error)

func DecodeTime

func DecodeTime(reader io.Reader) (time Time, bytesDecoded int, err error)

Decode a time value.

func DecodeTimeWithBuffer added in v1.7.0

func DecodeTimeWithBuffer(reader io.Reader, buffer []byte) (time Time, bytesDecoded int, err error)

func DecodeTimestamp

func DecodeTimestamp(reader io.Reader) (time Time, bytesDecoded int, err error)

Decode a timestamp.

func DecodeTimestampWithBuffer added in v1.7.0

func DecodeTimestampWithBuffer(reader io.Reader, buffer []byte) (time Time, bytesDecoded int, err error)

func NewDate added in v1.0.1

func NewDate(year, month, day int) Time

func NewTime added in v1.0.1

func NewTime(hour, minute, second, nanosecond int, timezone Timezone) Time

func NewTimestamp added in v1.0.1

func NewTimestamp(year, month, day, hour, minute, second, nanosecond int, timezone Timezone) Time

func ZeroDate added in v1.6.0

func ZeroDate() Time

Create a "zero" date, which will encode to all zeroes.

func ZeroTime added in v1.6.0

func ZeroTime() Time

Create a "zero" time, which will encode to all zeroes.

func ZeroTimestamp added in v1.6.0

func ZeroTimestamp() Time

Create a "zero" timestamp, which will encode to all zeroes.

func (*Time) AsGoTime added in v1.0.1

func (this *Time) AsGoTime() (result gotime.Time, err error)

Convert compact time into golang time. Note: Go time doesn't support latitude/longitude time zones. Attempting to

convert this type of time zone will result in an error.

Note: Converting to go time will validate area/location time zone (if any)

func (*Time) Encode added in v1.5.0

func (this *Time) Encode(writer io.Writer) (bytesEncoded int, err error)

Encode a time value (date, time, or timestamp).

func (*Time) EncodeToBytes added in v1.7.0

func (this *Time) EncodeToBytes(buffer []byte) (bytesEncoded int)

Encode a time value (date, time, or timestamp) to a byte array. Assumes that the buffer is big enough.

func (*Time) EncodedSize added in v1.5.0

func (this *Time) EncodedSize() int

Get the number of bytes that would be required to encode this time value.

func (*Time) InitDate added in v1.0.1

func (this *Time) InitDate(year, month, day int)

func (*Time) InitTime added in v1.0.1

func (this *Time) InitTime(hour, minute, second, nanosecond int, timezone Timezone)

func (*Time) InitTimestamp added in v1.0.1

func (this *Time) InitTimestamp(year, month, day, hour, minute, second, nanosecond int, tz Timezone)

func (*Time) IsEquivalentTo added in v1.7.1

func (this *Time) IsEquivalentTo(that Time) bool

Check if two times are equivalent. This handles cases where the time zones are technically equivalent (Z == UTC == Etc/UTC == Etc/GMT, etc)

func (*Time) IsZeroValue added in v1.6.0

func (this *Time) IsZeroValue() bool

func (Time) String added in v1.3.0

func (this Time) String() string

func (*Time) Validate added in v1.0.1

func (this *Time) Validate() error

type TimeType added in v1.0.1

type TimeType uint8

type Timezone added in v1.8.0

type Timezone struct {
	ShortAreaLocation    string
	LongAreaLocation     string
	LatitudeHundredths   int16
	LongitudeHundredths  int16
	MinutesOffsetFromUTC int16
	Type                 TimezoneType
}

func TZAtAreaLocation added in v1.8.0

func TZAtAreaLocation(areaLocation string) Timezone

func TZAtLatLong added in v1.8.0

func TZAtLatLong(latitudeHundredths, longitudeHundredths int) Timezone

func TZAtUTC added in v1.8.0

func TZAtUTC() Timezone

func TZLocal added in v1.8.0

func TZLocal() Timezone

func TZWithMiutesOffsetFromUTC added in v1.8.0

func TZWithMiutesOffsetFromUTC(minutesOffsetFromUTC int) Timezone

func (*Timezone) InitWithAreaLocation added in v1.8.0

func (this *Timezone) InitWithAreaLocation(areaLocation string)

func (*Timezone) InitWithLatLong added in v1.8.0

func (this *Timezone) InitWithLatLong(latitudeHundredths, longitudeHundredths int)

func (*Timezone) InitWithMinutesOffsetFromUTC added in v1.8.0

func (this *Timezone) InitWithMinutesOffsetFromUTC(minutesOffsetFromUTC int)

func (*Timezone) IsEquivalentTo added in v1.8.0

func (this *Timezone) IsEquivalentTo(that *Timezone) bool

func (*Timezone) String added in v1.8.0

func (this *Timezone) String() string

func (*Timezone) Validate added in v1.8.0

func (this *Timezone) Validate() error

type TimezoneType added in v1.0.1

type TimezoneType uint8

Jump to

Keyboard shortcuts

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