Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Location() function in Go language is used to check the data of the time zone that is associated with “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) Location() *Location

Here, “t” is the stated time and *Location is the pointer to Location.

Return value: It returns the information of the time zone that is associated with “t”.

Example 1:




// Golang program to illustrate the usage of
// Time.Location() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Defining t for calling Location method
    t := time.Date(2019, 2, 11, 10, 03, 00, 00, time.UTC)
  
    // Calling Location method
    loc := t.Location()
  
    // Prints output
    fmt.Printf("%v\n", loc)
}


Output:

UTC

Example 2:




// Golang program to illustrate the usage of
// Time.Location() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Defining location using FixedZone method
    location := time.FixedZone("UTC-7", -6*56*34)
  
    // Defining t for calling Location method
    t := time.Date(2019, 2, 11, 10, 03, 00, 00, location)
  
    // Calling Location method
    loc := t.Location()
  
    // Prints output
    fmt.Printf("%v\n", loc)
}


Output:

UTC-7

Here, FixedZone() method is used in order to define the location parameter of Date() method so the data of the time zone in the output is returned according to that location.



Last Updated : 19 Apr, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads