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.

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

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同