@@ -1138,16 +1138,31 @@ func (t Time) UnixNano() int64 {
1138
1138
return (t .unixSec ())* 1e9 + int64 (t .nsec ())
1139
1139
}
1140
1140
1141
- const timeBinaryVersion byte = 1
1141
+ const (
1142
+ timeBinaryVersionV1 byte = iota + 1 // For general situation
1143
+ timeBinaryVersionV2 // For LMT only
1144
+ )
1145
+
1146
+ var timeBinaryVersion byte
1142
1147
1143
1148
// MarshalBinary implements the encoding.BinaryMarshaler interface.
1144
1149
func (t Time ) MarshalBinary () ([]byte , error ) {
1145
1150
var offsetMin int16 // minutes east of UTC. -1 is UTC.
1151
+ var offsetSec int16
1146
1152
1147
1153
if t .Location () == UTC {
1148
1154
offsetMin = - 1
1155
+ } else if name , offset := t .Zone (); name == "LMT" {
1156
+ timeBinaryVersion = timeBinaryVersionV2
1157
+ offsetSec = int16 (offset % 60 )
1158
+
1159
+ offset /= 60
1160
+ if offset < - 32768 || offset == - 1 || offset > 32767 {
1161
+ return nil , errors .New ("Time.MarshalBinary: unexpected zone offset" )
1162
+ }
1163
+ offsetMin = int16 (offset )
1149
1164
} else {
1150
- _ , offset := t . Zone ()
1165
+ timeBinaryVersion = timeBinaryVersionV1
1151
1166
if offset % 60 != 0 {
1152
1167
return nil , errors .New ("Time.MarshalBinary: zone offset has fractional minute" )
1153
1168
}
@@ -1160,22 +1175,47 @@ func (t Time) MarshalBinary() ([]byte, error) {
1160
1175
1161
1176
sec := t .sec ()
1162
1177
nsec := t .nsec ()
1163
- enc := []byte {
1164
- timeBinaryVersion , // byte 0 : version
1165
- byte (sec >> 56 ), // bytes 1-8: seconds
1166
- byte (sec >> 48 ),
1167
- byte (sec >> 40 ),
1168
- byte (sec >> 32 ),
1169
- byte (sec >> 24 ),
1170
- byte (sec >> 16 ),
1171
- byte (sec >> 8 ),
1172
- byte (sec ),
1173
- byte (nsec >> 24 ), // bytes 9-12: nanoseconds
1174
- byte (nsec >> 16 ),
1175
- byte (nsec >> 8 ),
1176
- byte (nsec ),
1177
- byte (offsetMin >> 8 ), // bytes 13-14: zone offset in minutes
1178
- byte (offsetMin ),
1178
+
1179
+ var enc []byte
1180
+ if timeBinaryVersion == timeBinaryVersionV1 {
1181
+ enc = []byte {
1182
+ timeBinaryVersion , // byte 0 : version
1183
+ byte (sec >> 56 ), // bytes 1-8: seconds
1184
+ byte (sec >> 48 ),
1185
+ byte (sec >> 40 ),
1186
+ byte (sec >> 32 ),
1187
+ byte (sec >> 24 ),
1188
+ byte (sec >> 16 ),
1189
+ byte (sec >> 8 ),
1190
+ byte (sec ),
1191
+ byte (nsec >> 24 ), // bytes 9-12: nanoseconds
1192
+ byte (nsec >> 16 ),
1193
+ byte (nsec >> 8 ),
1194
+ byte (nsec ),
1195
+ byte (offsetMin >> 8 ), // bytes 13-14: zone offset in minutes
1196
+ byte (offsetMin ),
1197
+ }
1198
+ } else {
1199
+ // For timeBinaryVersionV2
1200
+ enc = []byte {
1201
+ timeBinaryVersion , // byte 0 : version
1202
+ byte (sec >> 56 ), // bytes 1-8: seconds
1203
+ byte (sec >> 48 ),
1204
+ byte (sec >> 40 ),
1205
+ byte (sec >> 32 ),
1206
+ byte (sec >> 24 ),
1207
+ byte (sec >> 16 ),
1208
+ byte (sec >> 8 ),
1209
+ byte (sec ),
1210
+ byte (nsec >> 24 ), // bytes 9-12: nanoseconds
1211
+ byte (nsec >> 16 ),
1212
+ byte (nsec >> 8 ),
1213
+ byte (nsec ),
1214
+ byte (offsetMin >> 8 ), // bytes 13-14: zone offset in minutes
1215
+ byte (offsetMin ),
1216
+ byte (offsetSec >> 8 ), // bytes 15-16: zone offset in seconds which sum with offsetMin can get the total offset time
1217
+ byte (offsetSec ),
1218
+ }
1179
1219
}
1180
1220
1181
1221
return enc , nil
@@ -1191,8 +1231,9 @@ func (t *Time) UnmarshalBinary(data []byte) error {
1191
1231
if buf [0 ] != timeBinaryVersion {
1192
1232
return errors .New ("Time.UnmarshalBinary: unsupported version" )
1193
1233
}
1234
+ timeVersion := buf [0 ]
1194
1235
1195
- if len (buf ) != /*version*/ 1 + /*sec*/ 8 + /*nsec*/ 4 + /*zone offset*/ 2 {
1236
+ if ( len (buf ) != /*version*/ 1 + /*sec*/ 8 + /*nsec*/ 4 + /*zone offset*/ 2 ) && ( len ( buf ) != /*version*/ 1 + /*sec*/ 8 + /*nsec*/ 4 + /*zone offset*/ 4 ) {
1196
1237
return errors .New ("Time.UnmarshalBinary: invalid length" )
1197
1238
}
1198
1239
@@ -1204,7 +1245,13 @@ func (t *Time) UnmarshalBinary(data []byte) error {
1204
1245
nsec := int32 (buf [3 ]) | int32 (buf [2 ])<< 8 | int32 (buf [1 ])<< 16 | int32 (buf [0 ])<< 24
1205
1246
1206
1247
buf = buf [4 :]
1207
- offset := int (int16 (buf [1 ])| int16 (buf [0 ])<< 8 ) * 60
1248
+ var offset int
1249
+ if timeVersion == timeBinaryVersionV2 {
1250
+ offset = int (int16 (buf [1 ])| int16 (buf [0 ])<< 8 )* 60 + int (int16 (buf [3 ])| int16 (buf [2 ])<< 8 )
1251
+ } else {
1252
+ // timeVersion is equal to timeBinaryVersionV1
1253
+ offset = int (int16 (buf [1 ])| int16 (buf [0 ])<< 8 ) * 60
1254
+ }
1208
1255
1209
1256
* t = Time {}
1210
1257
t .wall = uint64 (nsec )
0 commit comments