Golang中的时间戳

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

Timestamps in Golang

问题

尝试让我的应用程序中的时间戳方法起作用:https://gist.github.com/bsphere/8369aca6dde3e7b4392c#file-timestamp-go

这是代码:

  1. package timestamp
  2. import (
  3. "fmt"
  4. "labix.org/v2/mgo/bson"
  5. "strconv"
  6. "time"
  7. )
  8. type Timestamp time.Time
  9. func (t *Timestamp) MarshalJSON() ([]byte, error) {
  10. ts := time.Time(*t).Unix()
  11. stamp := fmt.Sprint(ts)
  12. return []byte(stamp), nil
  13. }
  14. func (t *Timestamp) UnmarshalJSON(b []byte) error {
  15. ts, err := strconv.Atoi(string(b))
  16. if err != nil {
  17. return err
  18. }
  19. *t = Timestamp(time.Unix(int64(ts), 0))
  20. return nil
  21. }
  22. func (t Timestamp) GetBSON() (interface{}, error) {
  23. if time.Time(*t).IsZero() {
  24. return nil, nil
  25. }
  26. return time.Time(*t), nil
  27. }
  28. func (t *Timestamp) SetBSON(raw bson.Raw) error {
  29. var tm time.Time
  30. if err := raw.Unmarshal(&tm); err != nil {
  31. return err
  32. }
  33. *t = Timestamp(tm)
  34. return nil
  35. }
  36. func (t *Timestamp) String() string {
  37. return time.Time(*t).String()
  38. }

以及相关的文章:https://medium.com/coding-and-deploying-in-the-cloud/time-stamps-in-golang-abcaf581b72f

然而,我得到了以下错误:

  1. core/timestamp/timestamp.go:31: invalid indirect of t (type Timestamp)
  2. core/timestamp/timestamp.go:35: invalid indirect of t (type Timestamp)

我的相关代码如下:

  1. import (
  2. "github.com/path/to/timestamp"
  3. )
  4. type User struct {
  5. Name string
  6. Created_at *timestamp.Timestamp `bson:"created_at,omitempty" json:"created_at,omitempty"`
  7. }

有人能看出我做错了什么吗?

相关问题
我也不知道如何使用这个包。我是否需要创建一个新的User模型,类似于这样?

  1. u := User{Name: "Joe Bloggs", Created_at: timestamp.Timestamp(time.Now())}
英文:

Trying to get this approach to timestamps working in my application: https://gist.github.com/bsphere/8369aca6dde3e7b4392c#file-timestamp-go

Here it is:

  1. package timestamp
  2. import (
  3. "fmt"
  4. "labix.org/v2/mgo/bson"
  5. "strconv"
  6. "time"
  7. )
  8. type Timestamp time.Time
  9. func (t *Timestamp) MarshalJSON() ([]byte, error) {
  10. ts := time.Time(*t).Unix()
  11. stamp := fmt.Sprint(ts)
  12. return []byte(stamp), nil
  13. }
  14. func (t *Timestamp) UnmarshalJSON(b []byte) error {
  15. ts, err := strconv.Atoi(string(b))
  16. if err != nil {
  17. return err
  18. }
  19. *t = Timestamp(time.Unix(int64(ts), 0))
  20. return nil
  21. }
  22. func (t Timestamp) GetBSON() (interface{}, error) {
  23. if time.Time(*t).IsZero() {
  24. return nil, nil
  25. }
  26. return time.Time(*t), nil
  27. }
  28. func (t *Timestamp) SetBSON(raw bson.Raw) error {
  29. var tm time.Time
  30. if err := raw.Unmarshal(&tm); err != nil {
  31. return err
  32. }
  33. *t = Timestamp(tm)
  34. return nil
  35. }
  36. func (t *Timestamp) String() string {
  37. return time.Time(*t).String()
  38. }

and the article that goes with it: https://medium.com/coding-and-deploying-in-the-cloud/time-stamps-in-golang-abcaf581b72f

However, I'm getting the following error:

  1. core/timestamp/timestamp.go:31: invalid indirect of t (type Timestamp)
  2. core/timestamp/timestamp.go:35: invalid indirect of t (type Timestamp)

My relevant code looks like this:

  1. import (
  2. "github.com/path/to/timestamp"
  3. )
  4. type User struct {
  5. Name string
  6. Created_at *timestamp.Timestamp `bson:"created_at,omitempty" json:"created_at,omitempty"`
  7. }

Can anyone see what I'm doing wrong?

Related question
I can't see how to implement this package either. Do I create a new User model something like this?

  1. u := User{Name: "Joe Bloggs", Created_at: timestamp.Timestamp(time.Now())}

答案1

得分: 6

你的代码中有一个拼写错误。你不能对非指针进行解引用,所以你需要将GetBSON方法改为指针接收器(或者你可以删除对t的间接引用,因为方法不会改变t的值)。

  1. func (t *Timestamp) GetBSON() (interface{}, error) {
  2. // code here
  3. }

要内联设置*Timestamp值,你需要有一个*time.Time来进行转换。

  1. now := time.Now()
  2. u := User{
  3. Name: "Bob",
  4. CreatedAt: (*Timestamp)(&now),
  5. }

构造函数和辅助函数(如New()Now())也可能对此有帮助。

英文:

Your code has a typo. You can't dereference a non-pointer, so you need to make GetBSON a pointer receiver (or you could remove the indirects to t, since the value of t isn't changed by the method).

  1. func (t *Timestamp) GetBSON() (interface{}, error) {

To set a *Timestamp value inline, you need to have a *time.Time to convert.

  1. now := time.Now()
  2. u := User{
  3. Name: "Bob",
  4. CreatedAt: (*Timestamp)(&now),
  5. }

Constructor and a helper functions like New() and Now() may come in handy for this as well.

答案2

得分: 0

你不能引用一个不是指针变量的间接引用。

  1. var a int = 3 // a = 3
  2. var A *int = &a // A = 0x10436184
  3. fmt.Println(*A == a) // true,两者都等于3
  4. fmt.Println(*&a == a) // true,两者都等于3
  5. fmt.Println(*a) // a的间接引用无效(类型为int)

因此,你不能使用*a引用a的地址。

看一下错误发生的地方:

  1. func (t Timestamp) GetBSON() (interface{}, error) {
  2. // t是一个变量类型为Timestamp,而不是*Timestamp(指针类型)
  3. // 因此,除非t是一个指针变量,并且你试图取消引用它以获取Timestamp值,否则这是不可能的
  4. if time.Time(*t).IsZero() {
  5. return nil, nil
  6. }
  7. // 这也是如此
  8. return time.Time(*t), nil
  9. }
英文:

You cannot refer to an indirection of something that is not a pointer variable.

  1. var a int = 3 // a = 3
  2. var A *int = &a // A = 0x10436184
  3. fmt.Println(*A == a) // true, both equals 3
  4. fmt.Println(*&a == a) // true, both equals 3
  5. fmt.Println(*a) // invalid indirect of a (type int)

Thus, you can not reference the address of a with *a.

Looking at where the error happens:

  1. func (t Timestamp) GetBSON() (interface{}, error) {
  2. // t is a variable type Timestamp, not type *Timestamp (pointer)
  3. // so this is not possible at all, unless t is a pointer variable
  4. // and you're trying to dereference it to get the Timestamp value
  5. if time.Time(*t).IsZero() {
  6. return nil, nil
  7. }
  8. // so is this
  9. return time.Time(*t), nil
  10. }

huangapple
  • 本文由 发表于 2015年8月15日 01:11:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/32015364.html
匿名

发表评论

匿名网友

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

确定