Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

time.LoadLocation() 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 LoadLocation() function in Go language is used to find a location with the name stated. So, if the stated name is “UTC” then it returns UTC and if the stated name is “Local” then it returns Local. Else, the name to be used is assumed to be a location which is equivalent to a file in the database of IANA Time Zone. Where this database is present only on Unix systems. 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 LoadLocation(name string) (*Location, error)

Here, “name” is the name of the location to be used, and *Location is the pointer to the Location. Where “Location” forms the set of time offsets in use. And “error” is a panic error.

Return Value: It returns the location with the stated name.

Example 1:






// Golang program to illustrate the usage of
// LoadLocation() function
  
// Including main package
package main
  
// Importing fmt and time
import (
    "fmt"
    "time"
)
  
// Calling main
func main() {
  
    // Calling LoadLocation
    // method with its parameter
    locat, error := time.LoadLocation("Asia/Kolkata")
  
    // If error not equal to nil then
    // return panic error
    if error != nil {
        panic(error)
    }
  
    // Prints location
    fmt.Println(locat)
}

Output:

Asia/Kolkata

Here, IANA time zone of India is returned as there is no error.

Example 2:




// Golang program to illustrate the usage of
// LoadLocation() function
  
// Including main package
package main
  
// Importing fmt and time
import (
    "fmt"
    "time"
)
  
// Calling main
func main() {
  
    // Calling LoadLocation 
    // method with its parameter
    locat, error := time.LoadLocation("Asia/Kolkata")
  
    // If error not 
    // equal to nil then
    // return panic error
    if error != nil {
        panic(error)
    }
  
    // Calling Date() method 
    // with its parameter
    tm := time.Date(2020, 4, 7, 16,
                 7, 0, 0, time.UTC)
  
    // Prints the time and date
    // of the stated location
    fmt.Println(tm.In(locat))
}

Output:

2020-04-07 21:37:00 +0530 IST

Here, at first LoadLocation() method is called then the Date() method is called with its parameters i.e, date and time then the date and time of the stated location are returned.




My Personal Notes arrow_drop_up
Recommended Articles
Page :