<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jingshao Chen</title>
    <description>The latest articles on DEV Community by Jingshao Chen (@jingshao_chen_02a2352f476).</description>
    <link>https://dev.to/jingshao_chen_02a2352f476</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1311537%2F3ebc7070-c6a6-4c41-a40a-cae2f7e653e9.png</url>
      <title>DEV Community: Jingshao Chen</title>
      <link>https://dev.to/jingshao_chen_02a2352f476</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jingshao_chen_02a2352f476"/>
    <language>en</language>
    <item>
      <title>Go time and its two clocks</title>
      <dc:creator>Jingshao Chen</dc:creator>
      <pubDate>Sat, 21 Sep 2024 16:36:48 +0000</pubDate>
      <link>https://dev.to/jingshao_chen_02a2352f476/go-time-and-its-two-clocks-3p0p</link>
      <guid>https://dev.to/jingshao_chen_02a2352f476/go-time-and-its-two-clocks-3p0p</guid>
      <description>&lt;p&gt;To calculate the time lapse in Go, you can use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c"&gt;// long time consuming task&lt;/span&gt;
&lt;span class="n"&gt;duration&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Since&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But do you know that the time package in Go actually has two times in it, and &lt;code&gt;time.Since()&lt;/code&gt; actually measures only the processing time (monotonic clock), not the real time (wall clock)? &lt;/p&gt;

&lt;p&gt;For example if the task is pretty long, overnight long, and your computer went to sleep. Then in the morning, you may come and see the duration last only 4 hours while in total 10 hours had been passed.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://pkg.go.dev/time#" rel="noopener noreferrer"&gt;&lt;code&gt;time&lt;/code&gt; package in Go&lt;/a&gt; states that &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Operating systems provide both a “wall clock,” which is subject to changes for clock synchronization, and a “monotonic clock,” which is not. The general rule is that the wall clock is for telling time and the monotonic clock is for measuring time. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What it really tells you in our above example is that your program actively ran for 4 hours, but the computer sleeps 6 hours, so in total you waited 10 hours. If the computer had not slept, it would be done in 4 hours.&lt;/p&gt;

&lt;p&gt;So what to do if you want to measure the wall time? You can use the &lt;code&gt;Round(0)&lt;/code&gt; function. &lt;code&gt;t = t.Round(0)&lt;/code&gt; will remove the monotonic clock in &lt;code&gt;t&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>go</category>
      <category>time</category>
      <category>programming</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
