Time Durations in Golang
Last Updated :
10 Sep, 2021
Operations related to time and date are a crucial part of software development (example log keeping). Go standard library provides a time package that has many functions and methods to deal with date and time. The operating system measures two types of time “Wall clock” time and “Monotonic” time. Wall clock time is used to tell time whereas monotonic clock time is used to measure time. Go time package provides functionalities to measure and manipulate both clocks. Golang has time.Time datatype to deal with wall clock time and time.Duration to deal with monotonic time.
The first basic method is time.Now() which returns the current date and time up to nanosecond precision. The value returned has datatype time.Time, which is a struct. According to Golang’s official documentation “A Time represents an instant in time with nanosecond precision.”
Go
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println(t)
}
|
Output:
2020-04-29 00:37:36.849599502 +0530 IST m=+0.000166550
In Go any variable which is declared but not initialized is by default set to its zero value. The zero value of type Time is January 1, year 1, 00:00:00.000000000 UTC, which is impossible in a real-life scenario. IsZero method can also be used to check whether a Time variable is initialized or not.
Go
package main
import (
"fmt"
"time"
)
func main() {
var t time.Time
fmt.Println(t)
fmt.Println(t.IsZero())
}
|
Output:
0001-01-01 00:00:00 +0000 UTC
true
time.Time datatype has a base type structure that contains time, date, location, timezone, and monotonic time. Go time package has methods to extract individual parts from time instant (like date, time, day, location, etc).
Go
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println(t)
fmt.Println(t.Clock())
fmt.Println(t.Hour())
fmt.Println(t.Minute())
fmt.Println(t.Second())
fmt.Println(t.Nanosecond())
fmt.Println( "---------------------------------" )
fmt.Println(t.Date())
fmt.Println(t.Day())
fmt.Println(t.Month())
fmt.Println(t.Year())
fmt.Println(t.YearDay())
fmt.Println(t.Weekday())
fmt.Println(t.ISOWeek())
fmt.Println( "---------------------------------" )
fmt.Println(t.String())
fmt.Println(t.Unix())
fmt.Println(t.Zone())
fmt.Println(t.UnixNano())
}
|
Output:
2020-04-29 17:54:25.643755713 +0530 IST m=+0.000064065
17 54 25
17
54
25
643755713
---------------------------------
2020 April 29
29
April
2020
120
Wednesday
2020 18
---------------------------------
2020-04-29 17:54:25.643755713 +0530 IST m=+0.000064065
1588163065
IST 19800
1588163065643755713
Golang == operator compares not only time instant but also the Location and the monotonic clock reading.
time.Duration has a base type int64. Duration represents the elapsed time between two instants as an int64 nanosecond count”. The maximum possible nanosecond representation is up to 290 years.
type Duration int64
Conversion of various duration instances:
const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)
Go
package main
import (
"fmt"
"time"
)
func main() {
var d time.Duration = 1000000000
fmt.Println(d.Hours())
fmt.Println(d.Minutes())
fmt.Println(d.Seconds())
fmt.Println(d.Milliseconds())
fmt.Println(d.Microseconds())
fmt.Println(d.String())
}
|
Output:
0.0002777777777777778
0.016666666666666666
1
1000
1000000
1s
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...