2022-07-11 23:41:58 -04:00
|
|
|
package data
|
2016-02-04 00:07:09 -08:00
|
|
|
|
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 (
|
2022-05-09 20:43:24 -04:00
|
|
|
"errors"
|
2016-02-04 00:07:09 -08:00
|
|
|
"time"
|
2022-05-09 20:43:24 -04:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2016-02-04 00:07:09 -08:00
|
|
|
)
|
|
|
|
|
2022-05-09 20:43:24 -04:00
|
|
|
const DATE_SIZE = 8
|
|
|
|
|
2016-02-04 00:07:09 -08:00
|
|
|
type Date [8]byte
|
|
|
|
|
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) {
|
2022-05-09 20:43:24 -04:00
|
|
|
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-05-09 20:43:24 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDate(data []byte) (date *Date, remainder []byte, err error) {
|
|
|
|
objdate, remainder, err := ReadDate(data)
|
|
|
|
date = &objdate
|
|
|
|
return
|
|
|
|
}
|