title: Example of C++ 11 std::chrono::duration
body: <p>I wrote one sample function to understand behavior of <code>std::chrono::duration</code> which recently introduced as part of C++11 standard.</p>
  
  <pre><code>void exampleForDuration()
  {
      seconds Sec(minutes(1));
  
      cout&lt;&lt;"1 second is "&lt;&lt;duration_cast&lt;nanoseconds&gt;(seconds(1)).count()&lt;&lt;" nanoseconds"&lt;&lt;endl;
      cout&lt;&lt;"1 minute is "&lt;&lt;Sec.count()&lt;&lt;" seconds"&lt;&lt;endl;
      cout&lt;&lt;"1 second is "&lt;&lt;duration_cast&lt;duration&lt;int,centi&gt;&gt;(seconds(1)).count()&lt;&lt;" centiseconds"&lt;&lt;endl;
      cout&lt;&lt;"100 second is "&lt;&lt;duration_cast&lt;minutes&gt;(seconds(100)).count()&lt;&lt;" minute."&lt;&lt;endl;
      cout&lt;&lt;"Waiting for 10 seconds...";
      auto start=system_clock::now();
      this_thread::sleep_for(seconds(10));
      cout&lt;&lt;"Done."&lt;&lt;endl;
      auto end=system_clock::now();
      auto waitedFor=end-start;
      cout&lt;&lt;"Waited for "&lt;&lt;duration_cast&lt;seconds&gt;(waitedFor).count()&lt;&lt;" seconds"&lt;&lt;endl;
      printCurrentDateTime();
  }
  </code></pre>
  
  <p>Output:</p>
  
  
  
  <blockquote>
  <pre class="lang-none prettyprint-override"><code>1 second is 1000000000 nanoseconds
  1 minute is 60 seconds
  1 second is 100 centiseconds
  100 second is 1 minute.                -------&gt; 1)
  Waiting for 10 seconds...Done.         -------&gt; 2)
  Waited for 10 seconds
  </code></pre>
  </blockquote>
  
  <p>When I ran the above function, the program surprisingly waited for 10 seconds after printing 1) rather than after printing 2). I was expecting the program to wait after printing "Waiting for 10 seconds..." then wait and then print "Done." but it printed "100 second is 1 minute." then waited for 10 seconds and then the rest of output.</p>