Open In App

time.Time.Zone() Function in Golang with Examples

Last Updated : 21 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:




// Golang program to illustrate the usage of
// Time.Zone() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Defining location using FixedZone method
    loc := time.FixedZone("UTC-7", 1*13*16)
  
    // Declaring t for Zone method
    t := time.Date(2014, 6, 5, 11, 56, 45, 05, loc)
  
    // Calling Zone() method
    zone_name, offset := t.Zone()
  
    // Prints zone name
    fmt.Printf("The zone name is: %s\n", zone_name)
  
    // Prints offset
    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:




// Golang program to illustrate the usage of
// Time.Zone() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Defining location using FixedZone method
    loc := time.FixedZone("UTC-6", -4*23*16)
  
    // Declaring t for Zone method
    t := time.Date(2014, 32, 35, 64, 76, 98, 3432, loc)
  
    // Calling Zone() method
    zone_name, offset := t.Zone()
  
    // Prints zone name
    fmt.Printf("The zone name is: %s\n", zone_name)
  
    // Prints offset
    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
Previous
Next
Share your thoughts in the comments

Similar Reads