Unix Timestamp to Time.Time Conversion - Addressing Out of Range Error
When parsing Unix timestamps using time.Parse(), you may encounter an "out of range" error even with the correct date and time format. This is because time.Parse() is not intended for Unix timestamp parsing.
To resolve this, use the following steps:
Here's an example:
package main import ( "fmt" "time" "strconv" ) func main() { i, err := strconv.ParseInt("1405544146", 10, 64) if err != nil { panic(err) } tm := time.Unix(i, 0) fmt.Println(tm) }
Output:
2014-07-16 20:55:46 +0000 UTC
This method ensures that Unix timestamps are correctly parsed and converted to time.Time objects, avoiding the out of range error.
The above is the detailed content of How to Correctly Convert a Unix Timestamp to a Go time.Time Object?. For more information, please follow other related articles on the PHP Chinese website!