| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package serialize | 5 package serialize |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "errors" | 9 "errors" |
| 10 "fmt" | 10 "fmt" |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 | 191 |
| 192 // WriteTime writes a time.Time in a byte-sortable way. | 192 // WriteTime writes a time.Time in a byte-sortable way. |
| 193 // | 193 // |
| 194 // This method truncates the time to microseconds and drops the timezone, | 194 // This method truncates the time to microseconds and drops the timezone, |
| 195 // because that's the (undocumented) way that the appengine SDK does it. | 195 // because that's the (undocumented) way that the appengine SDK does it. |
| 196 func WriteTime(buf Buffer, t time.Time) error { | 196 func WriteTime(buf Buffer, t time.Time) error { |
| 197 name, off := t.Zone() | 197 name, off := t.Zone() |
| 198 if name != "UTC" || off != 0 { | 198 if name != "UTC" || off != 0 { |
| 199 panic(fmt.Errorf("helper: UTC OR DEATH: %s", t)) | 199 panic(fmt.Errorf("helper: UTC OR DEATH: %s", t)) |
| 200 } | 200 } |
| 201 » _, err := cmpbin.WriteUint(buf, uint64(t.Unix())*1e6+uint64(t.Nanosecond
()/1e3)) | 201 » _, err := cmpbin.WriteInt(buf, t.Unix()*1e6+int64(t.Nanosecond()/1e3)) |
| 202 return err | 202 return err |
| 203 } | 203 } |
| 204 | 204 |
| 205 // ReadTime reads a time.Time from the buffer. | 205 // ReadTime reads a time.Time from the buffer. |
| 206 func ReadTime(buf Buffer) (time.Time, error) { | 206 func ReadTime(buf Buffer) (time.Time, error) { |
| 207 » v, _, err := cmpbin.ReadUint(buf) | 207 » v, _, err := cmpbin.ReadInt(buf) |
| 208 if err != nil { | 208 if err != nil { |
| 209 return time.Time{}, err | 209 return time.Time{}, err |
| 210 } | 210 } |
| 211 » return time.Unix(int64(v/1e6), int64((v%1e6)*1e3)).UTC(), nil | 211 » return time.Unix(v/1e6, (v%1e6)*1e3).UTC(), nil |
| 212 } | 212 } |
| 213 | 213 |
| 214 // WriteProperty writes a Property to the buffer. `context` behaves the same | 214 // WriteProperty writes a Property to the buffer. `context` behaves the same |
| 215 // way that it does for WriteKey, but only has an effect if `p` contains a | 215 // way that it does for WriteKey, but only has an effect if `p` contains a |
| 216 // Key as its Value. | 216 // Key as its Value. |
| 217 func WriteProperty(buf Buffer, context KeyContext, p ds.Property) (err error) { | 217 func WriteProperty(buf Buffer, context KeyContext, p ds.Property) (err error) { |
| 218 defer recoverTo(&err) | 218 defer recoverTo(&err) |
| 219 typb := byte(p.Type()) | 219 typb := byte(p.Type()) |
| 220 if p.IndexSetting() != ds.NoIndex { | 220 if p.IndexSetting() != ds.NoIndex { |
| 221 typb |= 0x80 | 221 typb |= 0x80 |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 } | 542 } |
| 543 } | 543 } |
| 544 | 544 |
| 545 func recoverTo(err *error) { | 545 func recoverTo(err *error) { |
| 546 if r := recover(); r != nil { | 546 if r := recover(); r != nil { |
| 547 if rerr := r.(parseError); rerr != nil { | 547 if rerr := r.(parseError); rerr != nil { |
| 548 *err = error(rerr) | 548 *err = error(rerr) |
| 549 } | 549 } |
| 550 } | 550 } |
| 551 } | 551 } |
| OLD | NEW |