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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。