summaryrefslogtreecommitdiff
path: root/devdocs/c/chrono%2Fdifftime.html
blob: d72402bb995680db2139804ea99721580459b020 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    <h1 id="firstHeading" class="firstHeading">difftime</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;time.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">double difftime( time_t time_end, time_t time_beg );</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr>  </table> <p>Computes difference between two calendar times as <code><a href="time_t" title="c/chrono/time t">time_t</a></code> objects (<code>time_end - time_beg</code>) in seconds. If <code>time_end</code> refers to time point before <code>time_beg</code> then the result is negative.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> time_beg, time_end </td> <td> - </td> <td> times to compare </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>Difference between two times in seconds.</p>
<h3 id="Notes"> Notes</h3> <p>On POSIX systems, <code><a href="time_t" title="c/chrono/time t">time_t</a></code> is measured in seconds, and <code>difftime</code> is equivalent to arithmetic subtraction, but C and C++ allow fractional units for <code><a href="time_t" title="c/chrono/time t">time_t</a></code>.</p>
<h3 id="Example"> Example</h3> <div class="t-example">
<p>The following program computes the number of seconds that have passed since the beginning of the month.</p>
<div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;time.h&gt;
 
int main(void)
{
    time_t now = time(0);
 
    struct tm beg = *localtime(&amp;now);
 
    // set beg to the beginning of the month
    beg.tm_hour = 0,
    beg.tm_min = 0,
    beg.tm_sec = 0,
    beg.tm_mday = 1;
 
    double seconds = difftime(now, mktime(&amp;beg));
 
    printf("%.f seconds have passed since the beginning of the month.\n", seconds);
 
    return 0;
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">1937968 seconds have passed since the beginning of the month.</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.27.2.2 The difftime function (p: 285) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.27.2.2 The difftime function (p: 390) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.23.2.2 The difftime function (p: 338) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 7.12.2.2 The difftime function (p: 171) </li></ul>
</ul>               <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/chrono/c/difftime" title="cpp/chrono/c/difftime">C++ documentation</a></span> for <code>difftime</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/chrono/difftime" class="_attribution-link">https://en.cppreference.com/w/c/chrono/difftime</a>
  </p>
</div>