time.Time.Zone() Function in Golang with Examples
Last Updated :
21 Apr, 2020
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Zone() function in Go language is used to determine the time zone that is in work at time “t”. 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) Zone() (name string, offset int)
Here, “t” is the stated time, “name” returned is of type string, and “offset” returned is of type int.
Return value: It returns the shortened zone name and its offset which is in seconds east of UTC.
Example 1:
package main
import "fmt"
import "time"
func main() {
loc := time .FixedZone( "UTC-7" , 1*13*16)
t := time .Date(2014, 6, 5, 11, 56, 45, 05, loc)
zone_name, offset := t.Zone()
fmt.Printf( "The zone name is: %s\n" , zone_name)
fmt.Printf( "The offset returned is: %d\n" , offset)
}
|
Output:
The zone name is: UTC-7
The offset returned is: 208
Here, we have used FixedZone() method in order to specify zone name and offset.
Example 2:
package main
import "fmt"
import "time"
func main() {
loc := time .FixedZone( "UTC-6" , -4*23*16)
t := time .Date(2014, 32, 35, 64, 76, 98, 3432, loc)
zone_name, offset := t.Zone()
fmt.Printf( "The zone name is: %s\n" , zone_name)
fmt.Printf( "The offset returned is: %d\n" , offset)
}
|
Output:
The zone name is: UTC-6
The offset returned is: -1472
Here, the “t” stated above has values that are out of usual range but they are normalized while conversion.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...