time.LoadLocation() Function in Golang With Examples

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
}

chevron_right


Output:

Asia/Kolkata

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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))
}

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.