]> git.r.bdr.sh - rbdr/mobius/blame_incremental - hotline/time.go
Wrap file transfer errors with more context
[rbdr/mobius] / hotline / time.go
... / ...
CommitLineData
1package hotline
2
3import (
4 "encoding/binary"
5 "slices"
6 "time"
7)
8
9type Time [8]byte
10
11// NewTime converts a time.Time to the 8 byte Hotline time format:
12// Year (2 bytes), milliseconds (2 bytes) and seconds (4 bytes)
13func NewTime(t time.Time) (b Time) {
14 yearBytes := make([]byte, 2)
15 secondBytes := make([]byte, 4)
16
17 // Get a time.Time for January 1st 00:00 from t so we can calculate the difference in seconds from t
18 startOfYear := time.Date(t.Year(), time.January, 1, 0, 0, 0, 0, time.Local)
19
20 binary.BigEndian.PutUint16(yearBytes, uint16(t.Year()))
21 binary.BigEndian.PutUint32(secondBytes, uint32(t.Sub(startOfYear).Seconds()))
22
23 return [8]byte(slices.Concat(
24 yearBytes,
25 []byte{0, 0},
26 secondBytes,
27 ))
28}