Debian Codegraph @

vendor/github.com/ugorji/go/codec/time.go

raw
1 // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
2 // Use of this source code is governed by a MIT license found in the LICENSE file.
3
4 package codec
5
6 import (
7 "fmt"
8 "time"
9 )
10
11 var timeDigits = [...]byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
12
13 type timeExt struct{}
14
15 func (x timeExt) WriteExt(v interface{}) (bs []byte) {
16 switch v2 := v.(type) {
17 case time.Time:
18 bs = encodeTime(v2)
19 case *time.Time:
20 bs = encodeTime(*v2)
21 default:
22 panic(fmt.Errorf("unsupported format for time conversion: expecting time.Time; got %T", v2))
23 }
24 return
25 }
26 func (x timeExt) ReadExt(v interface{}, bs []byte) {
27 tt, err := decodeTime(bs)
28 if err != nil {
29 panic(err)
30 }
31 *(v.(*time.Time)) = tt
32 }
33
34 func (x timeExt) ConvertExt(v interface{}) interface{} {
35 return x.WriteExt(v)
36 }
37 func (x timeExt) UpdateExt(v interface{}, src interface{}) {
38 x.ReadExt(v, src.([]byte))
39 }
40
41 // EncodeTime encodes a time.Time as a []byte, including
42 // information on the instant in time and UTC offset.
43 //
44 // Format Description
45 //
46 // A timestamp is composed of 3 components:
47 //
48 // - secs: signed integer representing seconds since unix epoch
49 // - nsces: unsigned integer representing fractional seconds as a
50 // nanosecond offset within secs, in the range 0 <= nsecs < 1e9
51 // - tz: signed integer representing timezone offset in minutes east of UTC,
52 // and a dst (daylight savings time) flag
53 //
54 // When encoding a timestamp, the first byte is the descriptor, which
55 // defines which components are encoded and how many bytes are used to
56 // encode secs and nsecs components. *If secs/nsecs is 0 or tz is UTC, it
57 // is not encoded in the byte array explicitly*.
58 //
59 // Descriptor 8 bits are of the form `A B C DDD EE`:
60 // A: Is secs component encoded? 1 = true
61 // B: Is nsecs component encoded? 1 = true
62 // C: Is tz component encoded? 1 = true
63 // DDD: Number of extra bytes for secs (range 0-7).
64 // If A = 1, secs encoded in DDD+1 bytes.
65 // If A = 0, secs is not encoded, and is assumed to be 0.
66 // If A = 1, then we need at least 1 byte to encode secs.
67 // DDD says the number of extra bytes beyond that 1.
68 // E.g. if DDD=0, then secs is represented in 1 byte.
69 // if DDD=2, then secs is represented in 3 bytes.
70 // EE: Number of extra bytes for nsecs (range 0-3).
71 // If B = 1, nsecs encoded in EE+1 bytes (similar to secs/DDD above)
72 //
73 // Following the descriptor bytes, subsequent bytes are:
74 //
75 // secs component encoded in `DDD + 1` bytes (if A == 1)
76 // nsecs component encoded in `EE + 1` bytes (if B == 1)
77 // tz component encoded in 2 bytes (if C == 1)
78 //
79 // secs and nsecs components are integers encoded in a BigEndian
80 // 2-complement encoding format.
81 //
82 // tz component is encoded as 2 bytes (16 bits). Most significant bit 15 to
83 // Least significant bit 0 are described below:
84 //
85 // Timezone offset has a range of -12:00 to +14:00 (ie -720 to +840 minutes).
86 // Bit 15 = have\_dst: set to 1 if we set the dst flag.
87 // Bit 14 = dst\_on: set to 1 if dst is in effect at the time, or 0 if not.
88 // Bits 13..0 = timezone offset in minutes. It is a signed integer in Big Endian format.
89 //
90 func encodeTime(t time.Time) []byte {
91 //t := rv.Interface().(time.Time)
92 tsecs, tnsecs := t.Unix(), t.Nanosecond()
93 var (
94 bd byte
95 btmp [8]byte
96 bs [16]byte
97 i int = 1
98 )
99 l := t.Location()
100 if l == time.UTC {
101 l = nil
102 }
103 if tsecs != 0 {
104 bd = bd | 0x80
105 bigen.PutUint64(btmp[:], uint64(tsecs))
106 f := pruneSignExt(btmp[:], tsecs >= 0)
107 bd = bd | (byte(7-f) << 2)
108 copy(bs[i:], btmp[f:])
109 i = i + (8 - f)
110 }
111 if tnsecs != 0 {
112 bd = bd | 0x40
113 bigen.PutUint32(btmp[:4], uint32(tnsecs))
114 f := pruneSignExt(btmp[:4], true)
115 bd = bd | byte(3-f)
116 copy(bs[i:], btmp[f:4])
117 i = i + (4 - f)
118 }
119 if l != nil {
120 bd = bd | 0x20
121 // Note that Go Libs do not give access to dst flag.
122 _, zoneOffset := t.Zone()
123 //zoneName, zoneOffset := t.Zone()
124 zoneOffset /= 60
125 z := uint16(zoneOffset)
126 bigen.PutUint16(btmp[:2], z)
127 // clear dst flags
128 bs[i] = btmp[0] & 0x3f
129 bs[i+1] = btmp[1]
130 i = i + 2
131 }
132 bs[0] = bd
133 return bs[0:i]
134 }
135
136 // DecodeTime decodes a []byte into a time.Time.
137 func decodeTime(bs []byte) (tt time.Time, err error) {
138 bd := bs[0]
139 var (
140 tsec int64
141 tnsec uint32
142 tz uint16
143 i byte = 1
144 i2 byte
145 n byte
146 )
147 if bd&(1<<7) != 0 {
148 var btmp [8]byte
149 n = ((bd >> 2) & 0x7) + 1
150 i2 = i + n
151 copy(btmp[8-n:], bs[i:i2])
152 //if first bit of bs[i] is set, then fill btmp[0..8-n] with 0xff (ie sign extend it)
153 if bs[i]&(1<<7) != 0 {
154 copy(btmp[0:8-n], bsAll0xff)
155 //for j,k := byte(0), 8-n; j < k; j++ { btmp[j] = 0xff }
156 }
157 i = i2
158 tsec = int64(bigen.Uint64(btmp[:]))
159 }
160 if bd&(1<<6) != 0 {
161 var btmp [4]byte
162 n = (bd & 0x3) + 1
163 i2 = i + n
164 copy(btmp[4-n:], bs[i:i2])
165 i = i2
166 tnsec = bigen.Uint32(btmp[:])
167 }
168 if bd&(1<<5) == 0 {
169 tt = time.Unix(tsec, int64(tnsec)).UTC()
170 return
171 }
172 // In stdlib time.Parse, when a date is parsed without a zone name, it uses "" as zone name.
173 // However, we need name here, so it can be shown when time is printed.
174 // Zone name is in form: UTC-08:00.
175 // Note that Go Libs do not give access to dst flag, so we ignore dst bits
176
177 i2 = i + 2
178 tz = bigen.Uint16(bs[i:i2])
179 i = i2
180 // sign extend sign bit into top 2 MSB (which were dst bits):
181 if tz&(1<<13) == 0 { // positive
182 tz = tz & 0x3fff //clear 2 MSBs: dst bits
183 } else { // negative
184 tz = tz | 0xc000 //set 2 MSBs: dst bits
185 //tzname[3] = '-' (TODO: verify. this works here)
186 }
187 tzint := int16(tz)
188 if tzint == 0 {
189 tt = time.Unix(tsec, int64(tnsec)).UTC()
190 } else {
191 // For Go Time, do not use a descriptive timezone.
192 // It's unnecessary, and makes it harder to do a reflect.DeepEqual.
193 // The Offset already tells what the offset should be, if not on UTC and unknown zone name.
194 // var zoneName = timeLocUTCName(tzint)
195 tt = time.Unix(tsec, int64(tnsec)).In(time.FixedZone("", int(tzint)*60))
196 }
197 return
198 }
199
200 // func timeLocUTCName(tzint int16) string {
201 // if tzint == 0 {
202 // return "UTC"
203 // }
204 // var tzname = []byte("UTC+00:00")
205 // //tzname := fmt.Sprintf("UTC%s%02d:%02d", tzsign, tz/60, tz%60) //perf issue using Sprintf. inline below.
206 // //tzhr, tzmin := tz/60, tz%60 //faster if u convert to int first
207 // var tzhr, tzmin int16
208 // if tzint < 0 {
209 // tzname[3] = '-' // (TODO: verify. this works here)
210 // tzhr, tzmin = -tzint/60, (-tzint)%60
211 // } else {
212 // tzhr, tzmin = tzint/60, tzint%60
213 // }
214 // tzname[4] = timeDigits[tzhr/10]
215 // tzname[5] = timeDigits[tzhr%10]
216 // tzname[7] = timeDigits[tzmin/10]
217 // tzname[8] = timeDigits[tzmin%10]
218 // return string(tzname)
219 // //return time.FixedZone(string(tzname), int(tzint)*60)
220 // }