In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Round() function in Go language is used to find the output of rounding the stated time “t” to the closest multiple of the given duration “d” from the zero time. And the behavior of rounding for mid-values is to round up. 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 (t Time) Round(d Duration) Time
Here, “t” is the stated time, and “d” is the given duration.
Note: The Round() method works on the time in the form of an absolute duration from zero time. However, it doesn’t work on the layout form of the time.
Return Value: It returns the output of rounding the given time “t” to the closest multiple of the stated duration “d”. Where, if d is less than or equal to zero then it returns “t” out of any monotonic clock reading but else unaltered.
Example 1:
// Golang program to illustrate the usage of // Time.Round() function // Including main package package main // Importing fmt and time import "fmt"
import "time"
// Calling main func main() { // Defining t for Round method
t := time .Date(2013, 7, 6, 11, 34, 13, 60, time .UTC)
// Defining duration
d := (60 * time .Second)
// Calling Round() method
res := t.Round(d)
// Prints output
fmt.Printf( "The time after rounding is: %v\n" , res)
} |
Output:
The time after rounding is: 2013-07-06 11:34:00 +0000 UTC
Example 2:
// Golang program to illustrate the usage of // Time.Round() function // Including main package package main // Importing fmt and time import "fmt"
import "time"
// Calling main func main() { // Defining t for Round method
t := time .Date(2033, 76, 96, 100, 89, 99, 6665, time .UTC)
// Defining duration
d := (4 * time .Minute)
// Calling Round() method
res := t.Round(d)
// Prints output
fmt.Printf( "The time after rounding is: %v\n" , res)
} |
Output:
The time after rounding is: 2039-07-09 05:32:00 +0000 UTC
Here, the “t” stated in the above code has values that are outside the usual range but they are normalized while conversion.