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

compact_time

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2020 License: MIT Imports: 4 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
	}
	date := time.Date(2020, time.Month(8), 30, 15, 33, 14, 19577323, location)
	buffer := make([]byte, compact_time.EncodedSizeTimestamp(compact_time.AsCompactTime(date)))
	encodedCount, ok, err := compact_time.EncodeTimestamp(compact_time.AsCompactTime(date), buffer)
	if err != nil {
		// TODO: Handle error
	}
	if !ok {
		// TODO: Not enough room in buffer to encode
	}
	fmt.Printf("Encoded [%v] into %v bytes: %v\n", date, encodedCount, buffer)
	// Prints: Encoded [2020-08-30 15:33:14.019577323 +0800 +08] into 21 bytes: [59 225 243 184 158 171 18 0 80 22 83 47 83 105 110 103 97 112 111 114 101]
}

func demonstrateDecode() {
	buffer := []byte{0x14, 0x4d, 0x09, 0x1c, 0x07}
	rawDate, decodedCount, ok, err := compact_time.DecodeTimestamp(buffer)
	if err != nil {
		// TODO: Handle error
	}
	if !ok {
		// TODO: Not enough bytes in buffer to decode
	}
	date, err := compact_time.AsGoTime(rawDate)
	if err != nil {
		// TODO: Handle error
	}
	fmt.Printf("Decoded %v bytes of %v into [%v]\n", decodedCount, buffer, date)
	// Prints: Decoded 5 bytes of [20 77 9 28 7] into [1966-12-01 05:13:05 +0000 UTC]
}

Documentation

Index

Constants

View Source
const (
	TypeDate = TimeType(iota)
	TypeTime
	TypeTimestamp
)
View Source
const (
	TypeUTC = TimezoneType(iota)
	TypeAreaLocation
	TypeLatitudeLongitude
)

Variables

This section is empty.

Functions

func Encode added in v1.0.1

func Encode(time *Time, dst []byte) (bytesEncoded int, isComplete bool, err error)

Encode a time value (date, time, or timestamp). Returns the number of bytes encoded, or the number of bytes it attempted to encode. Returns isComplete=true if there was enough room in dst. Returns an error if something went wrong other than there not being enough room.

func EncodedSize added in v1.0.1

func EncodedSize(time *Time) int

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

Types

type Time added in v1.0.1

type Time struct {
	Year                int
	Nanosecond          int32
	LatitudeHundredths  int16
	LongitudeHundredths int16
	Month               int8
	Day                 int8
	Hour                int8
	Minute              int8
	Second              int8
	TimeIs              TimeType
	TimezoneIs          TimezoneType
	AreaLocation        string
}

func AsCompactTime added in v1.0.1

func AsCompactTime(src *gotime.Time) (result *Time)

func DecodeDate

func DecodeDate(src []byte) (time *Time, bytesDecoded int, isComplete bool, err error)

Decode a date. Returns the number of bytes decoded, or the number of bytes it attempted to decode. Returns isComplete=true if there was enough data in src. If isComplete == false, the resulting date is invalid.

func DecodeTime

func DecodeTime(src []byte) (time *Time, bytesDecoded int, isComplete bool, err error)

Decode a time value. Returns the number of bytes decoded, or the number of bytes it attempted to decode. Returns isComplete=true if there was enough data in src. If isComplete == false or err != nil, the resulting time value is invalid.

func DecodeTimestamp

func DecodeTimestamp(src []byte) (time *Time, bytesDecoded int, isComplete bool, err error)

Decode a timestamp. Returns the number of bytes decoded, or the number of bytes it attempted to decode. Returns isComplete=true if there was enough data in src. If isComplete == false or err != nil, the resulting timestamp is invalid.

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, areaLocation string) *Time

Create a new time. If areaLocation is empty, UTC is assumed.

func NewTimeLatLong added in v1.0.1

func NewTimeLatLong(hour, minute, second, nanosecond, latitudeHundredths, longitudeHundredths int) *Time

func NewTimestamp added in v1.0.1

func NewTimestamp(year, month, day, hour, minute, second, nanosecond int, areaLocation string) *Time

Create a new timestamp. If areaLocation is empty, UTC is assumed.

func NewTimestampLatLong added in v1.0.1

func NewTimestampLatLong(year, month, day, hour, minute, second, nanosecond, latitudeHundredths, longitudeHundredths int) *Time

func (*Time) AsGoTime added in v1.0.1

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

Convert this time into a standard go time. Note: Go time doesn't support latitude/longitude time zones. Attempting to

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

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, areaLocation string)

Init a time. If areaLocation is empty, UTC is assumed.

func (*Time) InitTimeLatLong added in v1.0.1

func (this *Time) InitTimeLatLong(hour, minute, second, nanosecond, latitudeHundredths, longitudeHundredths int)

func (*Time) InitTimestamp added in v1.0.1

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

Init a timestamp. If areaLocation is empty, UTC is assumed.

func (*Time) InitTimestampLatLong added in v1.0.1

func (this *Time) InitTimestampLatLong(year, month, day, hour, minute, second, nanosecond, latitudeHundredths, longitudeHundredths int)

func (*Time) Validate added in v1.0.1

func (this *Time) Validate() (err error)

Validate this time. Only basic validation is performed, enough to ensure that it isn't blatantly wrong (such as invalid area/location values, latitude 500, december 54th, etc). 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.

Note: The field AreaLocation is not validated. You can validate this field using time.LoadLocation() or time.LoadLocationFromTZData().

type TimeType added in v1.0.1

type TimeType int8

type TimezoneType added in v1.0.1

type TimezoneType int8

Jump to

Keyboard shortcuts

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