Time in GoLang

Learn via video courses
Topics Covered

Overview

Any operations related to date and time are very crucial when it comes to software development. The standard library of the Go language provides a time package that has many different methods and functions that is related to golang time and date.

Introduction

In the Golang Time, the time library resides in the standard Go package which generally deals with the time and date. The operating system mainly measures two types of time, monotonic time and wall clock time. The monotonic clock is used for measuring the time whereas the wall clock is used for displaying or telling the time. The time package of the Go language provides a functionality that is used for measuring as well as manipulating both the clocks, the Go language has a datatype time. The time that is used for dealing with the wall clock time whereas the time. Duration is used for dealing with monotonic time.

Getting the Current System Time

The first basic method time.Now() is used for returning the current date and time of the system with a precision of up to nanoseconds. The data type of the value that is returned is time. Time which is a struct. A Time represents an instant in time with nanosecond precision, this is according to the official documentation of golang.

To understand golang time better, let’s take an example.

Output:

Explanation: In this example, the current date and time are displayed in the output. This is possible due to the package time of golang. The precision of the output is up to nanoseconds.

Formatting Time to a String

In order to format the golang time to string, the Time.String() is used. This function yields the time which is then formatted into a string with the help of the format string function which is defined under the time package. Hence in order to use this function, the time package needs to be imported.

The syntax for formatting time to a string is as shown below:

Here t is a time variable.

The return value is the time formatted into the string.

Here is a code to understand golang time concept better Write a Golang program to illustrate the usage of the Time.String() function

Output:

Explanation: In this example, the basic package like fmt and time are imported. The time is defined for the string method and then the method time.String() is used on the time in order to format that time into a string. The formatted string is then printed at the output.

Parsing Time Strings

The parsing of time in golang means that the time in golang is converted to an object so that useful information from the time such as date and time can be extracted and displayed in a simple-to-understand manner. Any string can be parsed by using time. Parse function of golang. It takes the time as a string and parses it into a string without any errors in the output.

To understand this better, let’s take an example.

Write a Golang program to show the parsing of the time

Output:

Explanation: In this example, you can see that the output has the date and time displayed properly. This is because the time is parsed and all the information is extracted from it so that it can be displayed in a simple manner. This is done by using time.Parse() method.

Duration - Adding and Subtracting Time

The time package in golang also has additional functionalities that can be used for determining as well as viewing the time. The function Time.Add() in golang is used for adding the time that is stated along with the duration. This function resides under the same time package and hence it needs to be imported before using the function.

The syntax for using this function is as mentioned below:

Here, t is the time that is stated whereas d is the duration of time that is to be added.

The value returned is the addition of the two times t and duration d.

Let's take an example to understand this better.

Output

In this example, the time has different components like seconds, minutes, and hours. These components are added by using Add method of time package. The respective time is added and converted to the proper format of time and is displayed in the output.

The second additional function that this time package provides is Time.Sub() function which is used for yielding the duration obtained by subtracting operation. Since this function resides in the time package, the package needs to be imported.

The syntax for using this function is as follows:

Here, t and u are the time that is stated.

The return value is the duration that is obtained by performing the t-u operation.

To understand this better, look at the example given below:

Write a Golang program to illustrate the usage of Time.Sub() function

Output:

Explanation: In this example, the Sub() function is used for subtracting the time stated. It takes as input the time u and displays the result at the output.

Time Comparison

If we want to compare two times or an instance of time, whether the first time is greater than or lesser than the other then the Before and After method of time package is used in golang. These methods return a boolean value which gives the output as true or false according to the expression.

Look at the example given below to understand this better

Output:

Explanation: These methods are more readable and concise for pure comparison rather than using add or sub-methods for checking the positives and negatives and comparing the time.

Getting Unix/Epoch Time

A Unix or Epoch time is the total number of seconds in elapsed since January 1, 1970, UTC. It is one of the best ways for representing a time zone that is independent of an instance of time since an integer value is returned which is stored in a database or any storage system efficiently.

The methods that can be used to get epoch time from an instance of a time are,Unix and UnixMilli. Look at the example given below:

Output:

Explanation: Here the method prints the Unix and UnixMilli time at the output.

Conclusion

  • The standard library of the Go language provides a time package that has many different methods and functions that is related to time and date.
  • In the Go language, the time library resides in the standard Go package which generally deals with the time and date. The operating system mainly measures two types of time, monotonic time and wall clock time.
  • The monotonic clock is used for measuring the time whereas the wall clock is used for displaying or telling the time. The time package of the Go language provides a functionality that is used for measuring as well as manipulating both the clocks, the Go language has a datatype time. The time that is used for dealing with the wall clock time whereas the time. Duration is used for dealing with monotonic time.