ReadTimeBytes reads a time.Time extension object from 'b' and returns the remaining bytes. Both the official and the format in this package will be read. Possible errors: - [ErrShortBytes] (not enough bytes in 'b') - [TypeError] (object not a time extension 5 or -1) - [ExtensionTypeError] (object
(b []byte)
| 1083 | // - [TypeError] (object not a time extension 5 or -1) |
| 1084 | // - [ExtensionTypeError] (object an extension of the correct size, but not a time.Time) |
| 1085 | func ReadTimeBytes(b []byte) (t time.Time, o []byte, err error) { |
| 1086 | if len(b) < 6 { |
| 1087 | err = ErrShortBytes |
| 1088 | return |
| 1089 | } |
| 1090 | typ, o, b, err := readExt(b) |
| 1091 | if err != nil { |
| 1092 | return |
| 1093 | } |
| 1094 | switch typ { |
| 1095 | case TimeExtension: |
| 1096 | if len(b) != 12 { |
| 1097 | err = ErrShortBytes |
| 1098 | return |
| 1099 | } |
| 1100 | sec, nsec := getUnix(b) |
| 1101 | t = time.Unix(sec, int64(nsec)).Local() |
| 1102 | return |
| 1103 | case MsgTimeExtension: |
| 1104 | switch len(b) { |
| 1105 | case 4: |
| 1106 | t = time.Unix(int64(binary.BigEndian.Uint32(b)), 0).Local() |
| 1107 | return |
| 1108 | case 8: |
| 1109 | v := binary.BigEndian.Uint64(b) |
| 1110 | nanos := int64(v >> 34) |
| 1111 | if nanos > 999999999 { |
| 1112 | // In timestamp 64 and timestamp 96 formats, nanoseconds must not be larger than 999999999. |
| 1113 | err = InvalidTimestamp{Nanos: nanos} |
| 1114 | return |
| 1115 | } |
| 1116 | t = time.Unix(int64(v&(1<<34-1)), nanos).Local() |
| 1117 | return |
| 1118 | case 12: |
| 1119 | nanos := int64(binary.BigEndian.Uint32(b)) |
| 1120 | if nanos > 999999999 { |
| 1121 | // In timestamp 64 and timestamp 96 formats, nanoseconds must not be larger than 999999999. |
| 1122 | err = InvalidTimestamp{Nanos: nanos} |
| 1123 | return |
| 1124 | } |
| 1125 | ux := int64(binary.BigEndian.Uint64(b[4:])) |
| 1126 | t = time.Unix(ux, nanos).Local() |
| 1127 | return |
| 1128 | default: |
| 1129 | err = InvalidTimestamp{FieldLength: len(b)} |
| 1130 | return |
| 1131 | } |
| 1132 | default: |
| 1133 | err = errExt(typ, TimeExtension) |
| 1134 | return |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | // ReadMapStrIntfBytes reads a map[string]interface{} |
| 1139 | // out of 'b' and returns the map and remaining bytes. |
searching dependent graphs…