ParseInLocation方法对于某些年份产生了错误的结果。

huangapple go评论72阅读模式
英文:

The ParseInLocation method produces incorrect results for certain years

问题

func main() {
    timeStr := "1974-05-10 10:30:00"
    timeStr2 := "1975-11-10 10:30:00"

    timeZone := "Asia/Ho_Chi_Minh"

    location, err := time.LoadLocation(timeZone)
    if err != nil {
        fmt.Println("加载时区错误:", err)
        return
    }

    parsedTime1, err := time.ParseInLocation("2006-01-02 15:04:05", timeStr, location)
    if err != nil {
        fmt.Println("格式化错误:", err)
        return
    }

    parsedTime2, err2 := time.ParseInLocation("2006-01-02 15:04:05", timeStr2, location)
    if err2 != nil {
        fmt.Println("格式化错误:", err)
        return
    }

    fmt.Println("解析后的时间1:", parsedTime1)
    fmt.Println("解析后的时间2:", parsedTime2)
}

输出:

解析后的时间1: 1974-05-10 10:30:00 +0800 +08
解析后的时间2: 1975-11-10 10:30:00 +0700 +07

在1975年之后的执行中,时区是正确的。

你可以查看上面的截图。

英文:
func main() {
	timeStr := "1974-05-10 10:30:00"
	timeStr2 := "1975-11-10 10:30:00"

	timeZone := "Asia/Ho_Chi_Minh"

	location, err := time.LoadLocation(timeZone)
	if err != nil {
		fmt.Println("Load Location error :", err)
		return
	}

	parsedTime1, err := time.ParseInLocation("2006-01-02 15:04:05", timeStr, location)
	if err != nil {
		fmt.Println("Format error:", err)
		return
	}

	parsedTime2, err2 := time.ParseInLocation("2006-01-02 15:04:05", timeStr2, location)
	if err2 != nil {
		fmt.Println("Format error:", err)
		return
	}

	fmt.Println("parsed time 1:", parsedTime1)
	fmt.Println("parsed time 2:", parsedTime2)

}

Playground link: https://go.dev/play/p/RV7hXchRcNx

Output:

parsed time 1: 1974-05-10 10:30:00 +0800 +08
parsed time 2: 1975-11-10 10:30:00 +0700 +07

Output in Goand: enter image description here

The time zone is correct for executions after the year 1975.

You can view the screenshot above.

答案1

得分: 5

胡志明市位于越南南部。引用自维基百科:越南时间

历史:

  • ...
  • 1959年12月31日23:00,南越时间区从UTC+08:00更改为UTC+07:00,时差减少60分钟。
  • ...
  • 1975年4月至5月,随着西贡的陷落,统一的越南采用UTC+07:00,西贡(以及其他南部地区)在1975年6月13日延迟60分钟。

因此,在1959年12月31日至1975年5月期间,南越的时差为+08:00,自1975年6月13日起为+07:00。你的输出是正确的,它考虑了历史变化。

英文:

Ho Chi Minh city is in South Vietnam. Quoting from Wikipedia: Time in Vietnam:

> ## History
> - ...
> - South Vietnam time zone was changed to UTC+08:00 from 23:00, 31 December 1959, passing 60 minutes.
> - ...
> - Following the Fall of Saigon in April–May 1975, reunified Vietnam then observes UTC+07:00 with Saigon (and other southern parts) delaying 60 minutes on 13 June 1975.

So between 31 December, 1959 and May 1975 there was +08:00 offset in South Vietnam, and since 13 June, 1975 there is +07:00. Your output is correct, it takes historical changes into account.

huangapple
  • 本文由 发表于 2023年5月10日 18:15:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定