time.Time.In() Function in Golang with Examples
Last Updated :
28 Apr, 2020
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:
package main
import "fmt"
import "time"
func main() {
t := time .Date(2019, 12, 13, 3, 23, 43, 02, time .UTC)
loc := time .FixedZone( "UTC" , 6*54*44)
res := t.In(loc)
fmt.Printf( "%v\n" , res)
}
|
Output:
2019-12-13 07:21:19.000000002 +0357 UTC
Example 2:
package main
import "fmt"
import "time"
func main() {
t := time .Date(2022, 23, 45, 36, 67, 88, 667, time .UTC)
loc := time .FixedZone( "UTC-6" , -3*66*77)
res := t.In(loc)
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...