<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Time on 100 Exercises to Learn Go</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/10_time/</link><description>Recent content in Time on 100 Exercises to Learn Go</description><generator>Hugo</generator><language>en</language><atom:link href="https://hamzahraihan.github.io/100-exercises-to-learn-go/10_time/index.xml" rel="self" type="application/rss+xml"/><item><title>Formatting Time</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/10_time/format/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/10_time/format/</guid><description>&lt;h1 id="formatting-time"&gt;Formatting Time&lt;a class="anchor" href="#formatting-time"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Every language formats dates with codes: &lt;code&gt;%Y&lt;/code&gt; for year, &lt;code&gt;%m&lt;/code&gt; for month, a
table memorized once and forgotten monthly. Go looked at that table and
replaced it with a single moment in history. No codes. Just an example.&lt;/p&gt;
&lt;h2 id="the-reference-moment"&gt;The reference moment&lt;a class="anchor" href="#the-reference-moment"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;t&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Format&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;2006-01-02&amp;#34;&lt;/span&gt;) &lt;span style="color:#75715e"&gt;// e.g. &amp;#34;2026-09-21&amp;#34;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The layout &lt;code&gt;&amp;quot;2006-01-02&amp;quot;&lt;/code&gt; isn&amp;rsquo;t a pattern of symbols — it&amp;rsquo;s a &lt;em&gt;date&lt;/em&gt;: what
you write is how January 2nd, 2006 renders in the shape you want, and Go
maps each component onto the real value. Year where &lt;code&gt;2006&lt;/code&gt; sits, month
where &lt;code&gt;01&lt;/code&gt; sits, day where &lt;code&gt;02&lt;/code&gt; sits. Want slashes? Write &lt;code&gt;&amp;quot;2006/01/02&amp;quot;&lt;/code&gt;.
Want the month&amp;rsquo;s name? The reference month is January, so &lt;code&gt;&amp;quot;Jan&amp;quot;&lt;/code&gt; gives
&lt;code&gt;&amp;quot;Sep&amp;quot;&lt;/code&gt; and &lt;code&gt;&amp;quot;January&amp;quot;&lt;/code&gt; gives &lt;code&gt;&amp;quot;September&amp;quot;&lt;/code&gt;.&lt;/p&gt;</description></item><item><title>Parsing Time</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/10_time/parse/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/10_time/parse/</guid><description>&lt;h1 id="parsing-time"&gt;Parsing Time&lt;a class="anchor" href="#parsing-time"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Formatting turns a moment into text. Parsing turns text back into a
moment — same layout language, opposite direction, and a strictness that
surprises everyone exactly once.&lt;/p&gt;
&lt;h2 id="the-layout-must-match-exactly"&gt;The layout must match, exactly&lt;a class="anchor" href="#the-layout-must-match-exactly"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;got&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;err&lt;/span&gt; &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;time&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Parse&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;2006-01-02&amp;#34;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#34;2026-09-21&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// got == 2026-09-21 00:00:00 UTC, err == nil&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;time.Parse&lt;/code&gt; reads the input &lt;em&gt;through&lt;/em&gt; the layout: where the layout shows
&lt;code&gt;2006&lt;/code&gt; it expects a year, where &lt;code&gt;01&lt;/code&gt; a month, where &lt;code&gt;02&lt;/code&gt; a day. Feed it
&lt;code&gt;&amp;quot;21/09/2026&amp;quot;&lt;/code&gt; against &lt;code&gt;&amp;quot;2006-01-02&amp;quot;&lt;/code&gt; and it returns an error instead of a
time — no guessing, no partial credit, no &amp;ldquo;close enough.&amp;rdquo; That strictness
is the feature: a mismatched layout fails loudly at the border rather than
smuggling a misread date into the database.&lt;/p&gt;</description></item><item><title>Durations</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/10_time/duration/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/10_time/duration/</guid><description>&lt;h1 id="durations"&gt;Durations&lt;a class="anchor" href="#durations"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;A &lt;code&gt;time.Time&lt;/code&gt; is a moment. The gap between two moments is something else:
a &lt;code&gt;time.Duration&lt;/code&gt; — and Go models it as what it is, a number of
nanoseconds wearing a good API. No separate interval type, no calendar
math surprises at this scale.&lt;/p&gt;
&lt;h2 id="subtraction-returns-a-quantity"&gt;Subtraction returns a quantity&lt;a class="anchor" href="#subtraction-returns-a-quantity"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;func&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;HoursBetween&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;a&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;b&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;time&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Time&lt;/span&gt;) &lt;span style="color:#66d9ef"&gt;float64&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;b&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Sub&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;a&lt;/span&gt;).&lt;span style="color:#a6e22e"&gt;Hours&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;b.Sub(a)&lt;/code&gt; is &amp;ldquo;b minus a&amp;rdquo; as a &lt;code&gt;Duration&lt;/code&gt;: positive when &lt;code&gt;b&lt;/code&gt; is later,
negative when earlier, zero for identical moments. The test&amp;rsquo;s three shapes
cover the space — 90 minutes apart yields &lt;code&gt;1.5&lt;/code&gt;, same instant yields &lt;code&gt;0&lt;/code&gt; —
and the signature&amp;rsquo;s direction (&lt;code&gt;b&lt;/code&gt; minus &lt;code&gt;a&lt;/code&gt;, not &lt;code&gt;a&lt;/code&gt; minus &lt;code&gt;b&lt;/code&gt;) decides
the sign. Name the order in the function name (&lt;code&gt;HoursBetween(a, b)&lt;/code&gt; reads
&amp;ldquo;from a to b&amp;rdquo;) so callers never guess.&lt;/p&gt;</description></item><item><title>Testable Time</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/10_time/compare/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/10_time/compare/</guid><description>&lt;h1 id="testable-time"&gt;Testable Time&lt;a class="anchor" href="#testable-time"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;IsExpired&lt;/code&gt; looks innocent: compare now against the expiry. But &amp;ldquo;now&amp;rdquo; is
the enemy of testing — every run reads a different clock, so no assertion
can pin the answer. Code that calls &lt;code&gt;time.Now&lt;/code&gt; directly is untestable by
construction. This exercise cuts the wire between the logic and the clock.&lt;/p&gt;
&lt;h2 id="the-seam-a-variable-holding-a-function"&gt;The seam: a variable holding a function&lt;a class="anchor" href="#the-seam-a-variable-holding-a-function"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Now is the clock. Tests replace it with a fixed time; production&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// leaves it as time.Now.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Now&lt;/span&gt; = &lt;span style="color:#a6e22e"&gt;time&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Now&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Functions are values in Go, so a package-level variable can &lt;em&gt;be&lt;/em&gt; the
clock. Production never touches it — &lt;code&gt;Now&lt;/code&gt; starts as &lt;code&gt;time.Now&lt;/code&gt; and stays
that way. Tests reassign it to a fixture:&lt;/p&gt;</description></item></channel></rss>