数据存储正在对时间字段进行四舍五入处理。

huangapple go评论83阅读模式
英文:

Time field being rounded by datastore

问题

当将时间值保存到数据存储中时,似乎会丢失一些精度。但是,保存到内存缓存中的时间值不会出现这种情况。

以下代码通常返回以下结果:

  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

为什么会发生这种情况?我能否防止这种情况发生?或者我是否存在一些基本误解?

谢谢。

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.

huangapple
  • 本文由 发表于 2014年9月30日 04:49:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/26108626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定