<p>If you want to truncate a unix timestamp to the hour value, you can do this using the <a href="https://golang.org/pkg/time/#Time.Truncate"><code>Truncate</code></a> function:</p> <div class="highlight"><pre><span></span><span class="kn">import</span> <span class="p">(</span> <span class="s">&quot;time&quot;</span> <span class="p">)</span> <span class="c1">// UnixTruncateToHour returns the timestamp truncated to the hour.</span> <span class="kd">func</span> <span class="nx">UnixTruncateToHour</span><span class="p">(</span><span class="nx">unixTime</span> <span class="kt">int64</span><span class="p">)</span> <span class="kt">int64</span> <span class="p">{</span> <span class="nx">t</span> <span class="o">:=</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Unix</span><span class="p">(</span><span class="nx">unixTime</span><span class="p">,</span> <span class="mi">0</span><span class="p">).</span><span class="nx">UTC</span><span class="p">()</span> <span class="k">return</span> <span class="nx">t</span><span class="p">.</span><span class="nx">Truncate</span><span class="p">(</span><span class="nx">time</span><span class="p">.</span><span class="nx">Hour</span><span class="p">).</span><span class="nx">UTC</span><span class="p">().</span><span class="nx">Unix</span><span class="p">()</span> <span class="p">}</span> </pre></div> <blockquote><p><code>Truncate</code> returns the result of rounding <code>t</code> down to a multiple of <code>d</code> (since the zero time). If <code>d &lt;= 0</code>, <code>Truncate</code> returns <code>t</code> stripped of any monotonic clock reading but otherwise unchanged.</p> <p><code>Truncate</code> operates on the time as an absolute duration since the zero time; it does not operate on the presentation form of the time. Thus, <code>Truncate(Hour)</code> may return a time with a non-zero minute, depending on the time's Location.</p> </blockquote> <p>Be aware that there is also a <a href="https://golang.org/pkg/time/#Time.Round"><code>Round</code></a> function:</p> <div class="highlight"><pre><span></span><span class="kn">import</span> <span class="p">(</span> <span class="s">&quot;time&quot;</span> <span class="p">)</span> <span class="c1">// UnixRoundToHour returns the timestamp rounded to the hour.</span> <span class="kd">func</span> <span class="nx">UnixRoundToHour</span><span class="p">(</span><span class="nx">unixTime</span> <span class="kt">int64</span><span class="p">)</span> <span class="kt">int64</span> <span class="p">{</span> <span class="nx">t</span> <span class="o">:=</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Unix</span><span class="p">(</span><span class="nx">unixTime</span><span class="p">,</span> <span class="mi">0</span><span class="p">).</span><span class="nx">UTC</span><span class="p">()</span> <span class="k">return</span> <span class="nx">t</span><span class="p">.</span><span class="nx">Round</span><span class="p">(</span><span class="nx">time</span><span class="p">.</span><span class="nx">Hour</span><span class="p">).</span><span class="nx">UTC</span><span class="p">().</span><span class="nx">Unix</span><span class="p">()</span> <span class="p">}</span> </pre></div> <blockquote><p><code>Round</code> returns the result of rounding <code>t</code> to the nearest multiple of <code>d</code> (since the zero time). The rounding behavior for halfway values is to round up. If <code>d &lt;= 0</code>, <code>Round</code> returns <code>t</code> stripped of any monotonic clock reading but otherwise unchanged.</p> <p><code>Round</code> operates on the time as an absolute duration since the zero time; it does not operate on the presentation form of the time. Thus, <code>Round(Hour)</code> may return a time with a non-zero minute, depending on the time's Location.</p> </blockquote>

Related Posts

  • Looking up a CNAME in Go
  • Embedding file with Go 1.16
  • Parsing a key pair from a PEM file in Go
  • Gotcha with defer in Go
  • Pretty-print JSON with Go