]> git.r.bdr.sh - rbdr/mobius/blob - hotline/time.go
Merge pull request #46 from jhalter/implement_access_no_agreement
[rbdr/mobius] / hotline / time.go
1 package hotline
2
3 import (
4 "encoding/binary"
5 "time"
6 )
7
8 // toHotlineTime converts a time.Time to the 8 byte Hotline time format:
9 // Year (2 bytes), milliseconds (2 bytes) and seconds (4 bytes)
10 func toHotlineTime(t time.Time) (b []byte) {
11 yearBytes := make([]byte, 2)
12 secondBytes := make([]byte, 4)
13
14 // Get a time.Time for January 1st 00:00 from t so we can calculate the difference in seconds from t
15 startOfYear := time.Date(t.Year(), time.January, 1, 0, 0, 0, 0, time.Local)
16
17 binary.BigEndian.PutUint16(yearBytes, uint16(t.Year()))
18 binary.BigEndian.PutUint32(secondBytes, uint32(t.Sub(startOfYear).Seconds()))
19
20 b = append(b, yearBytes...)
21 b = append(b, []byte{0, 0}...)
22 b = append(b, secondBytes...)
23
24 return b
25 }