英文:
Time field being rounded by datastore
问题
当将时间值保存到数据存储中时,似乎会丢失一些精度。但是,保存到内存缓存中的时间值不会出现这种情况。
以下代码通常返回以下结果:
- 2014-09-29 21:38:45.5991137 +0100 BST
- 2014-09-29 21:38:45.599113 +0100 BST
- 2014-09-29 21:38:45.5991137 +0100 BST
为什么会发生这种情况?我能否防止这种情况发生?或者我是否存在一些基本误解?
谢谢。
import (
"appengine/aetest"
"appengine/datastore"
"appengine/memcache"
"log"
"os"
"testing"
"time"
)
func TestTimeDiff(t *testing.T) {
logger := log.New(os.Stderr, "", 0)
c, _ := aetest.NewContext(&aetest.Options{"", true})
defer c.Close()
type MyType struct {
Time time.Time
}
value1 := MyType{time.Now()} // create value1
k := datastore.NewKey(c, "MyType", "mykey", 0, nil)
datastore.Put(c, k, &value1) // persist value1 to datastore
var value2 MyType
datastore.Get(c, k, &value2) // create value2 as value1 retrieved from datastore
item := &memcache.Item{
Key: "MC_MYTYPE",
Object: value1,
}
memcache.Gob.Set(c, item) // persist value1 to memcache
var value3 MyType
memcache.Gob.Get(c, "MC_MYTYPE", &value3) // create value3 as value1 retrieved from memcache
logger.Println("1) " + value1.Time.String())
logger.Println("2) " + value2.Time.String())
logger.Println("3) " + value3.Time.String())
}
英文:
A Time value when saved to the datastore appears to lose some accuracy. The same does not happen for Time values saved to memcache.
The code below typically returns the following:
1) 2014-09-29 21:38:45.5991137 +0100 BST
2) 2014-09-29 21:38:45.599113 +0100 BST
3) 2014-09-29 21:38:45.5991137 +0100 BST
Why does this happen, and can I prevent it? Or do I have some fundamental misunderstanding?
Thanks.
import (
"appengine/aetest"
"appengine/datastore"
"appengine/memcache"
"log"
"os"
"testing"
"time"
)
func TestTimeDiff(t *testing.T) {
logger := log.New(os.Stderr, "", 0)
c, _ := aetest.NewContext(&aetest.Options{"", true})
defer c.Close()
type MyType struct {
Time time.Time
}
value1 := MyType{time.Now()} // create value1
k := datastore.NewKey(c, "MyType", "mykey", 0, nil)
datastore.Put(c, k, &value1) // persist value1 to datastore
var value2 MyType
datastore.Get(c, k, &value2) // create value2 as value1 retrieved from datastore
item := &memcache.Item{
Key: "MC_MYTYPE",
Object: value1,
}
memcache.Gob.Set(c, item) // persist value1 to memcache
var value3 MyType
memcache.Gob.Get(c, "MC_MYTYPE", &value3) // create value3 as value1 retrieved from memcache
logger.Println("1) " + value1.Time.String())
logger.Println("2) " + value2.Time.String())
logger.Println("3) " + value3.Time.String())
}
答案1
得分: 2
App Engine Datastore 以微秒精度存储时间。
Go的time.Time类型具有纳秒精度。存储到Memcache的gob使用了这个完整的精度。
英文:
The App Engine Datastore stores times with with microsecond precision.
The Go time.Time type has nanosecond precision. The gob stored to Memcache uses this full precision.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论