time.go 6.0 KB

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