mirror of https://bitbucket.org/ausocean/av.git
44 lines
757 B
Go
44 lines
757 B
Go
|
package psi
|
||
|
|
||
|
import (
|
||
|
"encoding/binary"
|
||
|
"errors"
|
||
|
)
|
||
|
|
||
|
func TimeToBytes(time uint64) []byte {
|
||
|
s := make([]byte, 8)
|
||
|
binary.BigEndian.PutUint64(s, time)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func chkTime(d []byte) error {
|
||
|
if d[descTagIndx] != timeDescTag {
|
||
|
return errors.New("PSI does not contain a time descriptor, cannot update")
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// Updatetime
|
||
|
func UpdateTime(d []byte, t int) error {
|
||
|
err := chkTime(d)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
ts := TimeToBytes(uint64(t))
|
||
|
for i := range d[timeIndx : timeIndx+8] {
|
||
|
d[i+timeIndx] = ts[i]
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func TimeOf(d []byte) (t uint64, err error) {
|
||
|
err = chkTime(d)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
for i := range d[timeIndx : timeIndx+8] {
|
||
|
t |= uint64(d[i+timeIndx]) << uint(56-i*8)
|
||
|
}
|
||
|
return t, nil
|
||
|
}
|