Golang program to demonstrate the time arithmetic


We are going to calculate the current time using the Now function from time package in Go programming language for demonstrating the time arithmetic. The time arithmetic is calculated using mathematical functions. In both examples, we will use mathematical calculations to find the past, future and the duration using Sub function. The output is printed using Println function from the fmt package.

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.

func sub()

This function is part of the time package. It is used to calculate the duration between the two time.Time values.

time.Hour()

This method is a part of the time package. It is used to get the current hour as time.Hour value.

Algorithm

  • Import the required packages in the program

  • Create a main function

  • In the main find the current time using Now function from the time package

  • Calculate the future, past and the duration using inbuilt-in functions and mathematical logic

  • Print the Current time, future, past and duration on the console

  • The print statement is executed using Println function from the fmt package

Example 1

In this example, the current time will be calculated using Now function from the time package. Then the future will be calculated by adding the duration of 3 hours to current time, past will be calculated by subtracting the duration of 2 days from the future and difference between the past and current time is calculated using Sub function. All these values are printed on the console using Println function.

//Golang program to demonstrate the time arithmetic
package main
import (
   "fmt"
   "time"
)

//Main function to execute the program
func main() {
	
   current_time := time.Now() //find the current time using Now function

   // Add a duration of 3 hours to now
   future := current_time.Add(3 * time.Hour)

   // Subtract a duration of 2 days from future
   past := future.Add(-2 * 24 * time.Hour)

   // Calculate the difference between past and current_time
   duration := current_time.Sub(past)

   // Print the values calculated above
   fmt.Println("Now:", current_time)
   fmt.Println("Future:", future)
   fmt.Println("Past:", past)
   fmt.Println("Duration between past and now:", duration)
}

Output

Now: 2023-02-28 09:31:56.827693083 +0000 UTC m=+0.000016893
Future: 2023-02-28 12:31:56.827693083 +0000 UTC m=+10800.000016893
Past: 2023-02-26 12:31:56.827693083 +0000 UTC m=-161999.999983107
Duration between past and now: 45h0m0s

Example 2

In this example, we will similarly calculate the current time using Now function as we did in last example. The future will be calculated by adding 10 minutes to the current time. Then, subtract 1 hour from the future time to get the past and the duration will be calculated using Sub function with the past and the current time. All the values calculated will be printed on the console. Let’s have a look at the code.

package main
import (
   "fmt"
   "time"
)

func main() {

   current_time := time.Now() //calculate the current time using the Now function

   // Add 10 minutes to the current time
   future := current_time.Add(10 * time.Minute)

   // Subtract 1 hour from the future time
   past := future.Add(-1 * time.Hour)

   // Calculate the duration between past and now
   duration := current_time.Sub(past)

   // Print the values calculated above
   fmt.Println("Now:", current_time)
   fmt.Println("Future:", future)
   fmt.Println("Past:", past)
   fmt.Println("Duration between past and now:", duration)
}

Output

Now: 2023-02-28 09:33:53.548874654 +0000 UTC m=+0.000018376
Future: 2023-02-28 09:43:53.548874654 +0000 UTC m=+600.000018376
Past: 2023-02-28 08:43:53.548874654 +0000 UTC m=-2999.999981624
Duration between past and now: 50m0s

Conclusion

We executed and compiled the program of demonstrating the time arithmetic using two examples. In both the example, we calculated past, future, currentTime and duration using different mathematical logics and Sub function which is helpful in finding duration.

Updated on: 01-Mar-2023

361 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements