time.Time.In() Function in Golang with Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The In() function in Go language is used to find a copy of the stated “t” that represents the identical time instant, but along with the copy’s location data that is set to “loc” for display uses. And if “loc” is nil then panic is returned. Moreover, this function is defined under the time package. Here, you need to import the “time” package in order to use these functions.
Syntax:
func (t Time) In(loc *Location) Time
Here, “t” is the stated time, and “loc” is the stated location which is a pointer to the location.
Return value: It returns a copy of the stated “t” that represents the identical time instant, but along with the copy’s location data that is set to “loc” for display uses. And if “loc” is nil then panic is returned.
Example 1:
// Golang program to illustrate the usage of // Time.In() function // Including main package package main // Importing fmt and time import "fmt" import "time" // Calling main func main() { // Defining t for In method t := time .Date(2019, 12, 13, 3, 23, 43, 02, time .UTC) // Defining loc parameter of In method loc := time .FixedZone( "UTC" , 6*54*44) // Calling In() method res := t.In(loc) // Prints output fmt.Printf( "%v\n" , res) } |
Output:
2019-12-13 07:21:19.000000002 +0357 UTC
Example 2:
// Golang program to illustrate the usage of // Time.In() function // Including main package package main // Importing fmt and time import "fmt" import "time" // Calling main func main() { // Defining t for In method t := time .Date(2022, 23, 45, 36, 67, 88, 667, time .UTC) // Defining loc parameter of In method loc := time .FixedZone( "UTC-6" , -3*66*77) // Calling In() method res := t.In(loc) // Prints output fmt.Printf( "%v\n" , res) } |
Output:
2023-12-16 08:54:22.000000667 -0414 UTC-6
Here, the “t” stated in the above code has values that are outside usual range but they are normalized while conversion.