# Go's time.Duration Type Unravelled

- Published: June 11, 2013
- Author: William Kennedy
- URL: https://www.ardanlabs.com/blog/2013/06/gos-duration-type-unravelled.html

I have been struggling with using the Time package that comes in the Go standard library. My struggles have come from two pieces of functionality. First, trying to capture the number of milliseconds between two different time periods. Second, comparing that duration in milliseconds against a pre-defined time span. It sounds like a no&nbsp;brainier but like I said, I have been struggling.<br />
<br />

In the Time package there is a custom type called Duration and a set of helper constants:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
type Duration int64<br />
<br />
const (<br />
&nbsp; Nanosecond  Duration = 1<br />
&nbsp; Microsecond          = 1000 &#42; Nanosecond<br />
&nbsp; Millisecond          = 1000 &#42; Microsecond<br />
&nbsp; Second               = 1000 &#42; Millisecond<br />
&nbsp; Minute               = 60 &#42; Second<br />
&nbsp; Hour                 = 60 &#42; Minute<br />
)
</div>
<br />
I looked at this maybe 1000 times but it didn't mean anything to me. I just want to compare two time periods, get the duration back, compare the duration and do something if the right amount of time has&nbsp;elapsed. I could not for the life of me understand how this structure was going to help me&nbsp;unravel&nbsp;this mystery.<br />
<br />
I wrote this Test function but it didn't work.<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
func Test() {<br />
&nbsp; var waitFiveHundredMilliseconds int64 = 500<br />
<br />
&nbsp; startingTime := time.Now().UTC()<br />
&nbsp; time.Sleep(10 &#42; time.Millisecond)<br />
&nbsp; endingTime := time.Now().UTC()<br />
<br />
&nbsp; var duration time.Duration = endingTime.Sub(startingTime)<br />
&nbsp; var durationAsInt64 = int64(duration)<br />
<br />
&nbsp; if durationAsInt64 &gt;= waitFiveHundredMilliseconds {<br />
&nbsp; &nbsp; fmt.Printf(&quot;Time Elapsed : Wait[%d] Duration[%d]\n&quot;,<br />
&nbsp; &nbsp; &nbsp; &nbsp;waitFiveHundredMilliseconds, durationAsInt64)<br />
&nbsp; } else {<br />
&nbsp; &nbsp; fmt.Printf(&quot;Time DID NOT Elapsed : Wait[%d] Duration[%d]\n&quot;,<br />
&nbsp; &nbsp; &nbsp; &nbsp;waitFiveHundredMilliseconds, durationAsInt64)<br />
&nbsp; }<br />
}
</div>
<br />
When I ran the Test function and look at the output the code thinks that 500 milliseconds have elapsed.<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
Time Elapsed : Wait[500] Duration[10724798]
</div>
<br />
So what went wrong? I looked at the Duration type and constants again.<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
type Duration int64<br />
<br />
const (<br />
&nbsp; Nanosecond  Duration = 1<br />
&nbsp; Microsecond          = 1000 &#42; Nanosecond<br />
&nbsp; Millisecond          = 1000 &#42; Microsecond<br />
&nbsp; Second               = 1000 &#42; Millisecond<br />
&nbsp; Minute               = 60 &#42; Second<br />
&nbsp; Hour                 = 60 &#42; Minute<br />
)
</div>
<br />
The basic unit of time for the Duration type is a Nanosecond. Ah, that is why when casting a Duration type that contains 10 milliseconds to an int64 I get 10,000,000.<br />
<br />
So direct casting is not going to work. I need a different strategy and a better understanding of how to use and convert the Duration type.<br />
<br />
I know it would be best to use the Duration type&nbsp;natively. This will minimize problems when using the type. Based on the constant values, I can create a Duration type variable in the following ways:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
func Test() {<br />
&nbsp;&nbsp;<b>var</b> duration_Milliseconds <b>time.Duration</b> = 500 &#42; time.Millisecond<br />
&nbsp; <b>var</b> duration_Seconds <b>time.Duration</b> = (1250 &#42; 10) &#42; time.Millisecond<br />
&nbsp; <b>var</b> duration_Minute <b>time.Duration</b> = 2 &#42; time.Minute<br />
<br />
&nbsp; fmt.Printf(&quot;Milli [%v]\nSeconds [%v]\nMinute [%v]\n&quot;,<br />
&nbsp; &nbsp; duration_Milliseconds,<br />
&nbsp; &nbsp; duration_Seconds,<br />
&nbsp; &nbsp; duration_Minute)<br />
}</div>
<br />
I created 3 variables of type Duration. Then by using the time constants, I am able to create the correct duration time span values. When I use the standard library Printf function and the %v operator I get the following output:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
Milli [500ms]<br />
Seconds [12.5s]<br />
Minute [2m0s]
</div>
<br />
This is very cool. The Printf function knows how to natively display a Duration type. Based on the value in each Duration variable, the Printf function prints the value in the proper time period. I am also getting the values I expected.<br />
<br />
The Duration type does have member functions that will convert the value of the Duration variable to a native Go type, either in int64 or float64 respectively:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
func Test() {<br />
<b>&nbsp;&nbsp;</b>var&nbsp;duration_Seconds&nbsp;time.Duration&nbsp;= (1250 &#42; 10) &#42; time.Millisecond<br />
&nbsp;&nbsp;var&nbsp;duration_Minute&nbsp;time.Duration&nbsp;= 2 &#42; time.Minute<br />
<b><br /></b><b>&nbsp; var</b> float64_Seconds <b>float64</b> = duration_Seconds.<b>Seconds()</b><br />
<b>&nbsp; var</b> float64_Minutes <b>float64</b> = duration_Minute.<b>Minutes()</b>
<br />
<br />
&nbsp; fmt.Printf(&quot;Seconds [%.3f]\nMinutes [%.2f]\n&quot;,<br />
&nbsp; &nbsp; float64_Seconds,<br />
&nbsp; &nbsp; float64_Minutes)
<br />
}</div>
<br />
I noticed pretty quickly that there is no Milliseconds function. There is a function for every other time unit but Milliseconds. When I display the Seconds and Minutes natively I get the following output as expected:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
Seconds [12.500]<br />
Minutes [2.00]
</div>
<br />
However I need the Milliseconds, So why is the Milliseconds function missing?<br />
<br />
The designers of Go didn't want to lock me into a single native type for the Milliseconds. They wanted me to have options.<br />
<br />
The following code converts the value of the Duration variable to Milliseconds as both an int64 and float64:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
func Test() {<br />
&nbsp; var&nbsp;duration_Milliseconds&nbsp;time.Duration&nbsp;= 500 &#42; time.Millisecond<br />
<br />
&nbsp; var castToInt64 <b>int64</b> = duration_Milliseconds.Nanoseconds() <b>/</b> <b>1e6</b><br />
&nbsp; var castToFloat64 <b>float64</b> = duration_Milliseconds.Seconds() <b>&#42;</b> <b>1e3</b><br />
<br />
&nbsp; fmt.Printf(&quot;Duration [%v]\ncastToInt64 [%d]\ncastToFloat64 [%.0f]\n&quot;,<br />
&nbsp; &nbsp; duration_Milliseconds,<br />
&nbsp; &nbsp; castToInt64,<br />
&nbsp; &nbsp; castToFloat64)<br />
}</div>
<br />
If I divide the Nanoseconds by 1e6 I get the Milliseconds as a int64. If I multiply the Seconds by 1e3 I get the Milliseconds as a float64.<br />
<br />
Here is the output:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
Duration [500ms]<br />
castToInt64 [500]<br />
castToFloat64 [500]
</div>
<br />
If you are wondering what 1e6 or 1e3 represents you are not alone:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
1e3 = 10<span style="vertical-align: super;">3</span> = One Thousand = 1 plus 3 0's<br />
<br />
1e6 = 10<span style="vertical-align: super;">6</span> = One Million &nbsp;= 1 plus 6 0's</div>
<br />
Now that I understand what a Duration type is and how best to use and manipulate it, I have my final test example using Milliseconds:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
func Test() {<br />
&nbsp; var waitFiveHundredMilliseconds time.Duration = 500 &#42; time.Millisecond<br />
<br />
&nbsp; startingTime := time.Now().UTC()<br />
&nbsp; time.Sleep(600 &#42; time.Millisecond)<br />
&nbsp; endingTime := time.Now().UTC()<br />
<br />
&nbsp; var duration time.Duration = endingTime.Sub(startingTime)<br />
<br />
&nbsp; if duration &gt;= waitFiveHundredMilliseconds {<br />
&nbsp; &nbsp; fmt.Printf(&quot;Wait %v\nNative [%v]\nMilliseconds [%d]\nSeconds [%.3f]\n&quot;,<br />
&nbsp; &nbsp; &nbsp; &nbsp;waitFiveHundredMilliseconds,<br />
&nbsp; &nbsp; &nbsp; &nbsp;duration,<br />
&nbsp; &nbsp; &nbsp; &nbsp;duration.Nanoseconds()/1e6,<br />
&nbsp; &nbsp; &nbsp; &nbsp;duration.Seconds())<br />
&nbsp; }<br />
}</div>
<br />
I get the following output:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
Wait 500ms<br />
Native [601.091066ms]<br />
Milliseconds [601]<br />
Seconds [0.601]
</div>
<br />
Instead of comparing&nbsp;native&nbsp;types to determine if the time has elapsed I am comparing two Duration types. This is much cleaner. When displaying the values I am using the Duration type custom formatting and converting the value of the Duration variable to Milliseconds as both an int64 and float64.<br />
<br />
It took a while but eventually using the Duration type started to make sense. As always I hope this helps someone else navigate using the Duration type in their Go applications.
