Files
Go_I2p/lib/common/data/date.go

72 lines
1.7 KiB
Go
Raw Normal View History

2022-09-12 08:31:02 +00:00
// Package data implements common data structures used in higher level structures.
package data
2016-02-04 00:07:09 -08:00
import (
"errors"
2016-02-04 00:07:09 -08:00
"time"
log "github.com/sirupsen/logrus"
2016-02-04 00:07:09 -08:00
)
2022-09-12 08:31:02 +00:00
// DATE_SIZE is the length in bytes of an I2P Date.
const DATE_SIZE = 8
2022-09-12 08:31:02 +00:00
/*
[I2P Date]
Accurate for version 0.9.49
2016-02-04 00:07:09 -08:00
2022-09-12 08:31:02 +00:00
Description
The number of milliseconds since midnight on Januyar 1, 1970 in the GMT timezone.
If the number is 0, the date is undefined or null.
Contents
8 byte Integer
*/
// Date is the represenation of an I2P Date.
//
2022-09-12 08:31:02 +00:00
// https://geti2p.net/spec/common-structures#date
type Date [8]byte
// Bytes returns the raw []byte content of a Date.
func (i Date) Bytes() []byte {
return i[:]
}
2022-09-12 08:31:02 +00:00
// Int returns the Date as a Go integer.
func (i Date) Int() int {
return intFromBytes(i.Bytes())
}
2016-02-14 22:40:29 -08:00
// Time takes the value stored in date as an 8 byte big-endian integer representing the
// number of milliseconds since the beginning of unix time and converts it to a Go time.Time
2016-02-06 01:42:47 -08:00
// struct.
2016-02-14 22:40:29 -08:00
func (date Date) Time() (date_time time.Time) {
seconds := Integer(date[:])
2022-04-27 10:48:59 -04:00
date_time = time.Unix(0, int64(seconds.Int()*1000000))
2016-02-14 22:40:29 -08:00
return
2016-02-04 00:07:09 -08:00
}
2022-09-12 08:31:02 +00:00
// ReadDate creates a Date from []byte using the first DATE_SIZE bytes.
// Any data after DATE_SIZE is returned as a remainder.
func ReadDate(data []byte) (date Date, remainder []byte, err error) {
if len(data) < 8 {
log.WithFields(log.Fields{
"data": data,
}).Error("ReadDate: data is too short")
err = errors.New("ReadDate: data is too short")
return
}
copy(date[:], data[:8])
remainder = data[8:]
return
}
2022-09-12 08:31:02 +00:00
// NewDate creates a new Date from []byte using ReadDate.
// Returns a pointer to Date unlike ReadDate.
func NewDate(data []byte) (date *Date, remainder []byte, err error) {
objdate, remainder, err := ReadDate(data)
date = &objdate
return
}