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 func writeTime(buf *bytes.Buffer, t time.Time) { | |
74 funnybase.WriteUint(buf, uint64(t.Unix())*1e6+uint64(t.Nanosecond()/1e3) ) | |
M-A Ruel
2015/05/31 23:03:15
Why silently strip the resolution to µs?
iannucci
2015/05/31 23:31:33
Oh, you wanted nanosecond precision? Don't use a t
| |
75 } | |
76 | |
77 func readTime(buf *bytes.Buffer) (time.Time, error) { | |
78 v, err := funnybase.ReadUint(buf) | |
79 if err != nil { | |
80 return time.Time{}, err | |
81 } | |
82 return time.Unix(int64(v/1e6), int64((v%1e6)*1e3)), nil | |
83 } | |
84 | |
85 func writeGeoPoint(buf *bytes.Buffer, gp appengine.GeoPoint) { | |
86 writeFloat64(buf, gp.Lat) | |
87 writeFloat64(buf, gp.Lng) | |
88 } | |
89 | |
90 func readGeoPoint(buf *bytes.Buffer) (pt appengine.GeoPoint, err error) { | |
91 if pt.Lat, err = readFloat64(buf); err != nil { | |
92 return | |
93 } | |
94 pt.Lng, err = readFloat64(buf) | |
95 return | |
96 } | |
OLD | NEW |