time.go 6.0 KB

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