-1

I try to use golang's time package to format some date with timezone from a SQL server, but found:

package main

import (
    "time"
    "fmt"
)

func main() {
    loc, _ := time.LoadLocation("Asia/Shanghai")
    endTime1 := time.Date(1, 1, 17, 23, 59, 59, 999*int(time.Millisecond), loc)
    fmt.Printf("format string 1 : %s\n", endTime1.UTC().Format("2006-01-02T15:04:05.000Z"))
    
    endTime2 := time.Date(2021, 1, 17, 23, 59, 59, 999*int(time.Millisecond), loc)
    fmt.Printf("format string 2 : %s\n", endTime2.UTC().Format("2006-01-02T15:04:05.000Z"))
}
# output
format string 1 : 0001-01-17T15:54:16.999Z
format string 2 : 2021-01-17T15:59:59.999Z

The output's "HH:mm:ss" part is different. I finally found the key time is 1900-12-31, but not found any clue about that day from source code or the Internet.

  • Does this answer your question? [Why Is Subtracting These Two Times (in 1927) Giving A Strange Result?](https://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result) – LeGEC May 17 '21 at 12:28

1 Answers1

3

Seems to be related to the Shanghai timezone.

From this page you can read

1 Jan 1901 - Time Zone Change (LMT → CST) When local standard time was about to reach Tuesday, 1 January 1901, 00:00:00 clocks were turned backward 0:05:43 hours to Monday, 31 December 1900, 23:54:17 local standard time instead.

Further we can find some more information in the go docs

A daylight savings time transition skips or repeats times. For example, in the United States, March 13, 2011 2:15am never occurred, while November 6, 2011 1:15am occurred twice. In such cases, the choice of time zone, and therefore the time, is not well-defined. Date returns a time that is correct in one of the two zones involved in the transition, but it does not guarantee which.

super
  • 12,335
  • 2
  • 19
  • 29