- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Python Program to demonstrate the time arithmetic
- Golang Program to demonstrate the string concatenation
- Golang program to demonstrate the string interpolation
- Golang Program to demonstrate the escape sequence characters
- Golang program to demonstrate the argument passing to block
- Golang program to demonstrate a simple module
- Golang program to demonstrate BEGIN and END blocks
- Golang program to demonstrate passing of pointers to a function
- Golang Program to demonstrate the example to write double quotes in a string
- Golang program to parse time
- Golang program to convert the local time to GMT
- Golang program to get current time zone
- Python Program to demonstrate the string interpolation
- Golang program to display current date and time
- Golang Program to Calculate Difference Between Two Time Periods
