Go Package time: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(40 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
=External=
* https://pkg.go.dev/strconv


* https://golang.org/pkg/time/
=Internal=
* [[Go_Language_Modularization#time|Standard library]]
* [[Time, Date, Timestamp in Go]]


=Internal=
=Overview=


* [[Go Concepts - Standard Library#Packages|Standard Library]]
* <code>[https://golang.org/pkg/time/#After time.After]</code> returns a channel that after the given duration, will send current time on it. This can be used to implement a timeout with <code>[[Go Channels#select|select]]</code>.


=Overview=
=Creating Time Instances=


* <tt>[https://golang.org/pkg/time/#After time.After]</tt> returns a channel that after the given duration, will send current time on it. This can be used to implement a timeout with <tt>[[Go Channels#select|select]]</tt>.
<font color=darkkhaki>
* Understand representation
* time.Now()
* time.Parse(time.RFC822, "01 Jan 00 00:00 UTC")
* time.Unix()
* time.UnixMicro()
* time.UnixMilli()
</font>


==<tt>time.Sleep()</tt>==
==<tt>time.Sleep()</tt>==
{{External|https://golang.org/pkg/time/#Sleep}}
Sleep pauses the current [[Go_Language_Goroutines#Overview|goroutine]] for at least the duration <code>d</code>. A negative or zero duration causes <code>Sleep</code> to return immediately:
<syntaxhighlight lang='go'>
import "time"
...
secs := 10
time.Sleep(time.Duration(secs) * time.Second)
</syntaxhighlight>
Other available time units: <code>time.Milliseconds</code>.
=<tt>time.Now()</tt>=
Return the current local time as an instance of a <code>[[#Time|Time]]</code> struct.
<syntaxhighlight lang='go'>
t0 := time.Now()
</syntaxhighlight>
=<tt>time.Since()</tt>=
=<tt>Time</tt>=
A time structure that represents an instant in time with nanosecond precision.
==<tt>sec()</tt>==
Return seconds since Jan 1 year 1.
==<tt>UnixNano()</tt>==
Return the number of nanoseconds elapsed since January 1, 1970 UTC, represented on 8 bytes (<code>[[Go_Integers#Overview|int64]]</code>). The result does not depend on the location associated with the time instance the method was called on.
==<tt>After(Duration)</tt>==
The <code>After()</code> returns a channel that will provide a Time instance after the given duration elapses. Equivalent with <code>NewTimer(d).C</code>.
The function can be used with a <code>select</code> statement to [[Go_Channels#Timing_Out_select|time out the <code>select</code> statement]] if none of the participating channels ever unblock. It does that by returning a [[Go_Channels#Overview|channel]] what will send the current time after the provided duration.
=<tt>Duration</tt>=
The duration can be specified as "ns", "us" (or "µs"), "ms", "s", "m", "h".


* https://golang.org/pkg/time/#Sleep
Compute elapsed time (duration):
<syntaxhighlight lang='go'>
t0 := time.Now()
duration := time.Now().Sub(t0) // in milliseconds
</syntaxhighlight>


Sleep pauses the current goroutine for at least the duration d. A negative or zero duration causes Sleep to return immediately:
=<tt>Ticker</tt>=
A <code>Ticker</code> holds a channel that delivers "ticks" of a clock at intervals.
==<tt>NewTicker()</tt>==
<code>NewTicker()</code> returns a new <code>[[#Ticker|Ticker]]</code> containing a channel that will send the current time on the channel after each tick. The period of the ticks is specified by the duration argument. The ticker will adjust the time interval or drop ticks to make up for slow receivers. Stop the ticker to release associated resources.
==<tt>Tick</tt>==
<font color=darkkhaki>
<code>Tick</code> is a convenience wrapper for [[#NewTicker()|NewTicker]] providing access to the ticking channel only. While <code>Tick</code> is useful for clients that have no need to shut down the Ticker, be aware that without a way to shut it down the underlying Ticker cannot be recovered by the garbage collector; it "leaks". Unlike NewTicker, Tick will return nil if d <= 0.
</font>
=Functions=
==<tt>AfterFunc()</tt>==


<pre>
=Formatting=
import "time"
<font color=darkkhaki>
TODO:
</font>
<syntaxhighlight lang='go'>
time.Now().Format("03:04 AM")
</syntaxhighlight>
 
 
<syntaxhighlight lang='go'>
time.Now().Format(time.RFC822)
</syntaxhighlight>
 
Predefined layouts:
 
* <code>time.ANSIC</code>
* <code>time.UnixDate</code>
* <code>time.RFC822</code>
* <code>time.RFC822Z</code>
* <code>time.RFC850</code>
* <code>time.RFC1123</code>
* <code>time.RFC1123Z</code>
* <code>time.RFC3339</code>
* <code>time.RFC3339Nano</code>
* <code>time.Kitchen</code>
* <code>time.Stamp</code>
* <code>time.StampMilli</code>
* <code>time.StampMicro</code>
* <code>time.StampNano</code>
* <code>time.DateTime</code> "2006-01-02 15:04:05"
* <code>time.DateOnly</code> "2006-01-02"
* <code>time.TimeOnly</code> "15:04:05"
 
=Idioms=


...
==A Clock that Does Something Every Second==
time.Sleep(100 * time.Millisecond)
<syntaxhighlight lang='go'>
</pre>
for range time.Tick(1 * time.Second) {
  fmt.Printf(".\n")
}
</syntaxhighlight>
==Timestamp==
<syntaxhighlight lang='go'>
timestamp := time.Now().Unix()
</syntaxhighlight>

Latest revision as of 00:14, 9 August 2024

External

Internal

Overview

  • time.After returns a channel that after the given duration, will send current time on it. This can be used to implement a timeout with select.

Creating Time Instances

  • Understand representation
  • time.Now()
  • time.Parse(time.RFC822, "01 Jan 00 00:00 UTC")
  • time.Unix()
  • time.UnixMicro()
  • time.UnixMilli()

time.Sleep()

https://golang.org/pkg/time/#Sleep

Sleep pauses the current goroutine for at least the duration d. A negative or zero duration causes Sleep to return immediately:

import "time"
...
secs := 10
time.Sleep(time.Duration(secs) * time.Second)

Other available time units: time.Milliseconds.

time.Now()

Return the current local time as an instance of a Time struct.

t0 := time.Now()

time.Since()

Time

A time structure that represents an instant in time with nanosecond precision.

sec()

Return seconds since Jan 1 year 1.

UnixNano()

Return the number of nanoseconds elapsed since January 1, 1970 UTC, represented on 8 bytes (int64). The result does not depend on the location associated with the time instance the method was called on.

After(Duration)

The After() returns a channel that will provide a Time instance after the given duration elapses. Equivalent with NewTimer(d).C.

The function can be used with a select statement to time out the select statement if none of the participating channels ever unblock. It does that by returning a channel what will send the current time after the provided duration.

Duration

The duration can be specified as "ns", "us" (or "µs"), "ms", "s", "m", "h".

Compute elapsed time (duration):

t0 := time.Now()
duration := time.Now().Sub(t0) // in milliseconds

Ticker

A Ticker holds a channel that delivers "ticks" of a clock at intervals.

NewTicker()

NewTicker() returns a new Ticker containing a channel that will send the current time on the channel after each tick. The period of the ticks is specified by the duration argument. The ticker will adjust the time interval or drop ticks to make up for slow receivers. Stop the ticker to release associated resources.

Tick

Tick is a convenience wrapper for NewTicker providing access to the ticking channel only. While Tick is useful for clients that have no need to shut down the Ticker, be aware that without a way to shut it down the underlying Ticker cannot be recovered by the garbage collector; it "leaks". Unlike NewTicker, Tick will return nil if d <= 0.

Functions

AfterFunc()

Formatting

TODO:

time.Now().Format("03:04 AM")


time.Now().Format(time.RFC822)

Predefined layouts:

  • time.ANSIC
  • time.UnixDate
  • time.RFC822
  • time.RFC822Z
  • time.RFC850
  • time.RFC1123
  • time.RFC1123Z
  • time.RFC3339
  • time.RFC3339Nano
  • time.Kitchen
  • time.Stamp
  • time.StampMilli
  • time.StampMicro
  • time.StampNano
  • time.DateTime "2006-01-02 15:04:05"
  • time.DateOnly "2006-01-02"
  • time.TimeOnly "15:04:05"

Idioms

A Clock that Does Something Every Second

for range time.Tick(1 * time.Second) {
  fmt.Printf(".\n")
}

Timestamp

timestamp := time.Now().Unix()