dongyi2159 2019-05-03 08:59
浏览 122
已采纳

我应该如何存储Go的time.Location在Postgres中?

In Postgres I store data given to me by a user with:

   Column   |           Type           | Collation | Nullable | Default
------------+--------------------------+-----------+----------+---------
 id         | uuid                     |           | not null |
 value      | numeric                  |           |          |
 date       | timestamp with time zone |           |          |

Now I'm presented with the requirement of maintaining the original timezone in which the data was produced. The timestamp with timezone is normalized to the database's timezone and the original timezone is lost, so I must manually restore date back from the normalized timezone before serving it back to the user.

Most solutions suggest adding an extra column to the table and storing the original timezone information together with the timestamp:

   Column   |           Type           | Collation | Nullable | Default
------------+--------------------------+-----------+----------+---------
 id         | uuid                     |           | not null |
 value      | numeric                  |           |          |
 date       | timestamp with time zone |           |          |
 tz         | text                     |           |          |

So given that I'm using Go, which information should I extract from time.Time to store in tz for the most precise and seamless restoration?

date.Location().String() doesn't seem right as it might return the value Local which is relative.

And how should I restore the information from tz back into to time.Time?

Is the result of time.LoadLocation(tz) good enough?

  • 写回答

2条回答 默认 最新

  • duanduo7400 2019-05-03 09:18
    关注

    Upon save, I would obtain the zone name and offset using Time.Zone():

    func (t Time) Zone() (name string, offset int)
    

    Then when querying such a timestamp from the database, you can construct a time.Location using time.FixedZone():

    func FixedZone(name string, offset int) *Location
    

    And switch to this location using Time.In().

    Word of caution! This will restore you a timestamp with "seemingly" in the same time zone, but if you need to apply operations on it (such as adding days to it), the results might not be the same. The reason for this is because time.FixedZone() returns a time zone with a fixed offset, which does not know anything about daylight savings for example, while the original timestamp you saved might have a time.Location which does know about these things.

    Here's an example of such a deviation. There is a daylight saving day in March, so we'll use a timestamp pointing to March 1, and add 1 month to it, which results in a timestamp being after the daylight saving.

    cet, err := time.LoadLocation("CET")
    if err != nil {
        panic(err)
    }
    
    t11 := time.Date(2019, time.March, 1, 12, 0, 0, 0, cet)
    t12 := t11.AddDate(0, 1, 0)
    fmt.Println(t11, t12)
    
    name, offset := t11.Zone()
    cet2 := time.FixedZone(name, offset)
    t21 := t11.UTC().In(cet2)
    t22 := t21.AddDate(0, 1, 0)
    fmt.Println(t21, t22)
    
    now := time.Date(2019, time.April, 2, 0, 0, 0, 0, time.UTC)
    fmt.Println("Time since t11:", now.Sub(t11))
    fmt.Println("Time since t21:", now.Sub(t21))
    fmt.Println("Time since t12:", now.Sub(t12))
    fmt.Println("Time since t22:", now.Sub(t22))
    

    This will output (try it on the Go Playground):

    2019-03-01 12:00:00 +0100 CET 2019-04-01 12:00:00 +0200 CEST
    2019-03-01 12:00:00 +0100 CET 2019-04-01 12:00:00 +0100 CET
    Time since t11: 757h0m0s
    Time since t21: 757h0m0s
    Time since t12: 14h0m0s
    Time since t22: 13h0m0s
    

    As you can see, the output time after the 1-month addition is the same, but the zone offset is different, so they designate a different time instant in time (which is proven by showing the time difference with an arbitrary time). The original has 2-hour offset, because it knows about the daylight saving that happened in the 1 month we skipped, while the "restored" timestamp's zone doesn't know about that, so the result has the same 1-hour offset. In the timestamp after addition, even the zone name changes in real life: from CET to CEST, and again, the restored timestamp's zone doesn't know about this either.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛