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

为什么使用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?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将字符串解析为具有用户特定时区位置的时间-

  //为简洁起见,错误处理已跳过
loc,_:= time.LoadLocation(“ Asia / Kolkata”)
now,_:= time.ParseInLocation(“ 15:04”,“ 10:  10“,loc)
fmt.Println(” Location:“,loc,” Time:“,现在)
   
 
 

我在系统上得到的输出是- 位置:亚洲/加尔各答时间:0000-01-01 10:10:00 +0553 HMT

HMT 时间在哪里

如果不是解析时间,而是使用 now:= time.Now()。In(loc),则显示的时区正确- <代码> IST 。 我对时区解析有问题吗?还是我的系统时区数据库有问题?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    已采纳该答案
    打赏 评论