dqz30992 2018-10-29 00:31
浏览 948
已采纳

为什么使用ParseInLocation打印不同的时区?

I'm trying to parse a string into time with a user-specific timezone location -

// error handling skipped for brevity
loc, _ := time.LoadLocation("Asia/Kolkata")
now, _ := time.ParseInLocation("15:04", "10:10", loc)
fmt.Println("Location : ", loc, " Time : ", now)

The output I get on my system is - Location : Asia/Kolkata Time : 0000-01-01 10:10:00 +0553 HMT

Where did this HMT time zone come from?

If instead of parsing the time I use now := time.Now().In(loc), the timezone printed is correct - IST. Am I doing something wrong with timezone parsng or is my system timezone database faulty?

  • 写回答

1条回答 默认 最新

  • dow72046 2018-10-29 01:35
    关注

    This may be a relic of the fact that your year for now is 0000, while time.Now() returns the current time. Timezones are weird, and certain locations haven't always used the same timezone. This is an excerpt from the IANA Time Zone Database:

    # Zone  NAME        GMTOFF  RULES   FORMAT  [UNTIL]
    Zone    Asia/Kolkata    5:53:28 -   LMT 1854 Jun 28 # Kolkata
            5:53:20 -   HMT 1870        # Howrah Mean Time?
            5:21:10 -   MMT 1906 Jan  1 # Madras local time
            5:30    -   IST 1941 Oct
            5:30    1:00    +0630   1942 May 15
            5:30    -   IST 1942 Sep
            5:30    1:00    +0630   1945 Oct 15
            5:30    -   IST
    

    If I am interpreting this correctly, it seems HMT was used from 1854 until 1870—I'm not exactly sure why this would cause it to be used for year 0000, which would seem to fall under LMT, but it's possible the Go database is slightly different (or it's possible that I'm misinterpreting the database). If you're concerned about the correct timezone being used for historical dates (like 0000) I'm not sure I can give a great answer, however for anything recent IST should be correctly used.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名