Time Durations in Golang

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.”

filter_none

edit
close

play_arrow

link
brightness_4
code

package main
  
import (
    "fmt"
    "time"
)
  
func main() {
  
    // Returns current time.
    t := time.Now()
    fmt.Println(t)
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

package main
  
import (
    "fmt"
    "time"
)
  
func main() {
    var t time.Time
    fmt.Println(t)
    fmt.Println(t.IsZero())
}

chevron_right


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).

filter_none

edit
close

play_arrow

link
brightness_4
code

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())
}

chevron_right


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
)
filter_none

edit
close

play_arrow

link
brightness_4
code

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 miliseconds
    fmt.Println(d.Milliseconds())
  
    // converts d in microseconds
    fmt.Println(d.Microseconds())
  
    // string representation go d
    fmt.Println(d.String())
}

chevron_right


Output:

0.0002777777777777778
0.016666666666666666
1
1000
1000000
1s



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.