2016-02-04 00:07:09 -08:00
|
|
|
package common
|
|
|
|
|
2016-02-14 23:10:37 -08:00
|
|
|
/*
|
|
|
|
I2P Date
|
2016-06-16 23:17:21 -07:00
|
|
|
https://geti2p.net/spec/common-structures#date
|
2016-02-14 23:10:37 -08:00
|
|
|
Accurate for version 0.9.24
|
|
|
|
*/
|
|
|
|
|
2016-02-04 00:07:09 -08:00
|
|
|
import (
|
2021-05-18 09:06:24 -04:00
|
|
|
"errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-05-18 09:06:54 -04:00
|
|
|
"time"
|
2016-02-04 00:07:09 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Date [8]byte
|
|
|
|
|
2021-05-18 09:06:24 -04:00
|
|
|
const DATE_SIZE = 8
|
|
|
|
|
2016-02-06 01:42:47 -08:00
|
|
|
//
|
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) {
|
2021-06-29 19:34:41 -04:00
|
|
|
seconds, _ := NewInteger(date[:])
|
|
|
|
date_time = time.Unix(0, int64(seconds.Value()*1000000))
|
2016-02-14 22:40:29 -08:00
|
|
|
return
|
2016-02-04 00:07:09 -08:00
|
|
|
}
|
2021-05-18 09:06:24 -04:00
|
|
|
|
|
|
|
func ReadDate(data []byte) (h Date, remainder []byte, err error) {
|
|
|
|
if len(data) < DATE_SIZE {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"at": "(Date) ReadDate",
|
|
|
|
"data_len": len(data),
|
|
|
|
"required_len": "8",
|
|
|
|
"reason": "date missing data",
|
|
|
|
}).Error("date error")
|
|
|
|
err = errors.New("error reading date, insufficient length")
|
|
|
|
copy(h[:], data[0:len(data)-1])
|
2021-05-18 09:06:54 -04:00
|
|
|
} else {
|
2021-05-18 09:06:24 -04:00
|
|
|
copy(h[:], data[0:DATE_SIZE-1])
|
|
|
|
copy(remainder, data[DATE_SIZE-1:])
|
|
|
|
}
|
|
|
|
return
|
2021-05-18 09:06:54 -04:00
|
|
|
}
|