aboutsummaryrefslogtreecommitdiff
path: root/hotline/time.go
blob: edc700dd29f8b47a2cf3a3954b21a1839336cf45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package hotline

import (
	"encoding/binary"
	"slices"
	"time"
)

// toHotlineTime converts a time.Time to the 8 byte Hotline time format:
// Year (2 bytes), milliseconds (2 bytes) and seconds (4 bytes)
func toHotlineTime(t time.Time) (b [8]byte) {
	yearBytes := make([]byte, 2)
	secondBytes := make([]byte, 4)

	// Get a time.Time for January 1st 00:00 from t so we can calculate the difference in seconds from t
	startOfYear := time.Date(t.Year(), time.January, 1, 0, 0, 0, 0, time.Local)

	binary.BigEndian.PutUint16(yearBytes, uint16(t.Year()))
	binary.BigEndian.PutUint32(secondBytes, uint32(t.Sub(startOfYear).Seconds()))

	return [8]byte(slices.Concat(
		yearBytes,
		[]byte{0, 0},
		secondBytes,
	))
}