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 memory | 5 package memory |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "encoding/binary" | 9 "encoding/binary" |
10 "fmt" | 10 "fmt" |
11 "math" | 11 "math" |
| 12 "time" |
| 13 |
| 14 "appengine" |
12 | 15 |
13 "github.com/luci/luci-go/common/funnybase" | 16 "github.com/luci/luci-go/common/funnybase" |
14 ) | 17 ) |
15 | 18 |
16 func writeString(buf *bytes.Buffer, s string) { | 19 func writeString(buf *bytes.Buffer, s string) { |
17 funnybase.WriteUint(buf, uint64(len(s))) | 20 funnybase.WriteUint(buf, uint64(len(s))) |
18 buf.WriteString(s) | 21 buf.WriteString(s) |
19 } | 22 } |
20 | 23 |
21 func readString(buf *bytes.Buffer) (string, error) { | 24 func readString(buf *bytes.Buffer) (string, error) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 func readFloat64(buf *bytes.Buffer) (float64, error) { | 62 func readFloat64(buf *bytes.Buffer) (float64, error) { |
60 // byte-ordered floats http://stereopsis.com/radix.html | 63 // byte-ordered floats http://stereopsis.com/radix.html |
61 data := make([]byte, 8) | 64 data := make([]byte, 8) |
62 _, err := buf.Read(data) | 65 _, err := buf.Read(data) |
63 if err != nil { | 66 if err != nil { |
64 return 0, err | 67 return 0, err |
65 } | 68 } |
66 bits := binary.BigEndian.Uint64(data) | 69 bits := binary.BigEndian.Uint64(data) |
67 return math.Float64frombits(bits ^ (((bits >> 63) - 1) | (1 << 63))), ni
l | 70 return math.Float64frombits(bits ^ (((bits >> 63) - 1) | (1 << 63))), ni
l |
68 } | 71 } |
| 72 |
| 73 // We truncate this to microseconds and drop the timezone, because that's the |
| 74 // way that the appengine SDK does it. Awesome, right? Also: its not documented. |
| 75 func writeTime(buf *bytes.Buffer, t time.Time) { |
| 76 funnybase.WriteUint(buf, uint64(t.Unix())*1e6+uint64(t.Nanosecond()/1e3)
) |
| 77 } |
| 78 |
| 79 func readTime(buf *bytes.Buffer) (time.Time, error) { |
| 80 v, err := funnybase.ReadUint(buf) |
| 81 if err != nil { |
| 82 return time.Time{}, err |
| 83 } |
| 84 return time.Unix(int64(v/1e6), int64((v%1e6)*1e3)), nil |
| 85 } |
| 86 |
| 87 func writeGeoPoint(buf *bytes.Buffer, gp appengine.GeoPoint) { |
| 88 writeFloat64(buf, gp.Lat) |
| 89 writeFloat64(buf, gp.Lng) |
| 90 } |
| 91 |
| 92 func readGeoPoint(buf *bytes.Buffer) (pt appengine.GeoPoint, err error) { |
| 93 if pt.Lat, err = readFloat64(buf); err != nil { |
| 94 return |
| 95 } |
| 96 pt.Lng, err = readFloat64(buf) |
| 97 return |
| 98 } |
OLD | NEW |