Type is a signed int64 type, in nanoseconds, used to represent the duration between two time points. The type definition is as follows:
type Duration int64
The methods you have are as follows:
- func (d Duration) Hours() float64, returns the duration in hours.
- func (d Duration) Minutes() float64, returns the duration in minutes.
- func (d Duration) Seconds() float64, returns the duration in seconds.
- func (d Duration) Milliseconds() int64, returns the duration in milliseconds.
- func (d Duration) Microseconds() int64, returns the duration in microseconds.
- func (d Duration) Nanoseconds() int64, returns the duration in nanoseconds.
- func (d Duration) String() string, returns the duration of a string type, in the format "72h3m0.5s". Examples are as follows:
package main import ( "fmt" "time" ) func main() { a := (3) * (()) // 3 (()) // 180 (()) //10800 (()) //10800000 (()) //10800000000 (()) //10800000000000 (()) //3h0m0s }
- func (d Duration) Round(m Duration) Duration, returns the result of rounding d to the closest multiple of m. Let's understand it through examples:
package main import ( "fmt" "time" ) func main() { a := (10) * // The result is 10, and 10 is exactly an integer multiple of 1 (((1) * )) // The result is 9, 3 times 9, 4 times 3 is 12, 9 and 10 are closer (((3) * )) // The result is 12, 2 times the 4 is 8, 3 times the 4 is 12, the distance is as close as the upper reaches (((4) * )) // The result is 10, and 10 is exactly the integer times of 5 (((5) * )) // The result is 12, 6 is 1 times the time 6, 6 is 2 times the time 6 is 12, 10 is closer to 12 (((6) * )) }
- func (d Duration) Truncate(m Duration) Duration, used to truncate d to the nearest integer multiple of m. Let's understand it through examples:
package main import ( "fmt" "time" ) func main() { a := (10) * // The result is 10, and 10 is exactly an integer multiple of 1 (((1) * )) // The result is 9, 3 times the 3 is 9, the closest to 10 (((3) * )) // The result is 8, 2 times the 4 is 8, the closest to 10 (((4) * )) // The result is 10, and 10 is exactly the integer times of 5 (((5) * )) // The result is 6, 1 times the 6 is 6, the closest to 10 (((6) * )) // The result is 0, 1 times the 11 is 11, which is greater than 10 (((11) * )) }
Addition and subtraction operations
Because represents a time period, you can add and subtract the value of type, for example:
package main import ( "fmt" "time" ) func main() { a := (10) * b := (10) * (a + b) (a - b) }
This is all about this article about explaining the types in the Golang time package. For more related Golang content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!