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() {
// Returns current time.
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)
// We can get individual values
// of hour, minute, seconds,
// nanoseconds from time.Time
// datatype
// Returns wall clock time
// from time.Time datatype
fmt.Println(t.Clock())
fmt.Println(t.Hour())
fmt.Println(t.Minute())
fmt.Println(t.Second())
fmt.Println(t.Nanosecond())
fmt.Println("---------------------------------")
// We can get individual values
// of day, month, year, yearday,
// and weekday from time.Time
// datatype
// Returns date from
// time.Time datatype
fmt.Println(t.Date())
fmt.Println(t.Day())
fmt.Println(t.Month())
fmt.Println(t.Year())
fmt.Println(t.YearDay())
fmt.Println(t.Weekday())
// week number
fmt.Println(t.ISOWeek())
fmt.Println("---------------------------------")
// current time in string formats
fmt.Println(t.String())
// nanoseconds passed
// from 1 january 1970
fmt.Println(t.Unix())
// prints abbreviated timezone and
// its offset in seconds from
// east of UTC
fmt.Println(t.Zone())
// prints nanoseconds
// elapsed from 1 january 1970
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() {
// random duration in nanoseconds
var d time.Duration = 1000000000
// converts d in hour
fmt.Println(d.Hours())
// converts d in minutes
fmt.Println(d.Minutes())
// converts d in seconds
fmt.Println(d.Seconds())
// converts d in milliseconds
fmt.Println(d.Milliseconds())
// converts d in microseconds
fmt.Println(d.Microseconds())
// string representation go d
fmt.Println(d.String())
}
Output:
0.0002777777777777778
0.016666666666666666
1
1000
1000000
1s
Similar Reads
Time Formatting in Golang
Golang supports time formatting and parsing via pattern-based layouts. To format time, we use the Format() method which formats a time.Time object. Syntax: func (t Time) Format(layout string) string We can either provide custom format or predefined date and timestamp format constants are also availa
2 min read
main and init function in Golang
The Go language reserve two functions for special purpose and the functions are main() and init() function.main() functionIn Go language, the main package is a special package which is used with the programs that are executable and this package contains main() function. The main() function is a spec
2 min read
Atomic Variable in Golang
In Go language, Atomic Variables are utilized in order to control state. Here, the "sync/atomic" package must be used to use these variables. Moreover, it also prevents race conditions which allows two or more Goroutines to access identical sources. The Atomic counters are available for several goro
3 min read
time.Date() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Date() function in Go language is used to find Date the Time which is equivalent to yyyy-mm-dd hh:mm:ss + nsec nanoseconds in the proper time zone in the stated location. Moreover, this function is defi
2 min read
How to Get Current time in Golang?
With the help of time.Now() function, we can get the current time in Golang by importing time module. Syntax: time.Now() Return: Return current date and time. Example #1: In this example, we can see that by using time.Now() function, we are able to get the current date and time. C // Golang program
1 min read
time.After() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The After() function in Go language is used to wait for the duration of time to pass and after that it delivers the actual time on the returned channel. Moreover, this function is defined under the time pac
3 min read
time.Now() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Now() function in Go language is used to find the current local time. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these functio
2 min read
Parsing Time in Golang
Parsing time is to convert our time to Golang time object so that we can extract information such as date, month, etc from it easily. We can parse any time by using time.Parse function which takes our time string and format in which our string is written as input and if there is no error in our form
2 min read
time.Unix() Function in Golang with Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Unix() function in Go language is used to yield the local time which is related to the stated Unix time from January 1, 1970, in UTC. Moreover, this function is defined under the time package. Here, you
2 min read
time.Tick() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Tick() function in Go language is a utility wrapper for NewTicker function. It only allows access to the ticking channel. In addition, Tick is beneficial for clients who don't require to shut down the T
3 min read