Golang program to get current time zone


Golang has internal functions like Now() Time, and other Time functions that can be used for getting the current time zone. Current time zone in Go is a time zone where people reside in and see the same standard time. In a country like India IST is observed throughout the nation. In this program we will discuss how to obtain current time zone using two examples.

Syntax

func Now() Time

The Now() function is defined in time package. this function generates the current local time. To use this function, we have to first import the time package in our program.

time.LoadLocation()

This function belongs to the time package. It is used to return the location based on the time zone. It takes one parameter i.e. the time zone.

Using time.LoadLocation Function

In this method, we will use LoadLocation method from the time package to retrieve the current timezone which will be printed as an Output of the program but if an error is obtained while retrieving the information of time zone the error will be printed on the console.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) and time package in the program where main produces executable codes and fmt helps in formatting input and Output.

  • Step 2 − Create a main function and in this function obtain the local time zone information using the function LoadLocation from time package.

  • Step 3 − This function takes a string input which indicates the information of the time zone we desire. Here, it is local i.e. of the current system one is using.

  • Step 4 − If an error is obtained while loading the time zone information i.e. the error is not 0, print the error on the console and return.

  • Step 5 − If all the information is obtained successfully, print the location’s current time zone on the console using fmt.Println() function from the fmt package where ln means new line.

Example

The following example demonstrates Golang program to get current time zone using time.LoadLocation function

package main

import (
   "fmt"
   "time"
)

func main() {
   location_currentzone, err := time.LoadLocation("Local") 
   if err != nil {
      fmt.Println("Error:", err) 
      return
   }
   fmt.Println("The Current time zone dtermined here is:", location_currentzone) 
}

Output

The Current time zone dtermined here is: UTC

Using Now and Zone From Time Package

In this method, we will use Now and Zone function from the similar package to obtain the current time zone and offset value which will be printed on the terminal.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) and time package in the program where main produces executable codes and fmt helps in formatting input and Output.

  • Step 2 − In the main function, obtain the current time using Now function from the time package and store it inside the current_time variable.

  • Step 3 − Then, use Zone method to get the current time zone and time zone offset, the value obtained will be stored in both the variables.

  • Step 4 − Here, print the currentTimeZone and offset using fmt.Println() function from fmt package where ln means new line.

Example

Following example demonstrates Golang program to get current time zone using Now and Zone from time package

package main

import (
   "fmt"
   "time"       
)

func main() {
   current_time := time.Now()    
   currentTimeZone, offset := current_time.Zone()
   fmt.Println("The Current time zone is:", currentTimeZone)
   fmt.Println("Time zone offset:", offset) 
}

Output

The Current time zone is: UTC
Time zone offset: 0

Conclusion

We executed the program of getting the current time zone using two examples. In the first example we used LoadLocation function from time package and in the second example we used Now and Zone function from the similar package. Both the examples provided the desired Output.

Updated on: 03-Apr-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements