Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This should be accurate (fills out a cut-down imitation of a <code>struct tm</code>, my <code>year</code> uses Common Era instead of a 1900 CE epoch):</p> <pre><code>struct xtm { unsigned int year, mon, day, hour, min, sec; }; #define YEAR_TO_DAYS(y) ((y)*365 + (y)/4 - (y)/100 + (y)/400) void untime(unsigned long unixtime, struct xtm *tm) { /* First take out the hour/minutes/seconds - this part is easy. */ tm-&gt;sec = unixtime % 60; unixtime /= 60; tm-&gt;min = unixtime % 60; unixtime /= 60; tm-&gt;hour = unixtime % 24; unixtime /= 24; /* unixtime is now days since 01/01/1970 UTC * Rebaseline to the Common Era */ unixtime += 719499; /* Roll forward looking for the year. This could be done more efficiently * but this will do. We have to start at 1969 because the year we calculate here * runs from March - so January and February 1970 will come out as 1969 here. */ for (tm-&gt;year = 1969; unixtime &gt; YEAR_TO_DAYS(tm-&gt;year + 1) + 30; tm-&gt;year++) ; /* OK we have our "year", so subtract off the days accounted for by full years. */ unixtime -= YEAR_TO_DAYS(tm-&gt;year); /* unixtime is now number of days we are into the year (remembering that March 1 * is the first day of the "year" still). */ /* Roll forward looking for the month. 1 = March through to 12 = February. */ for (tm-&gt;mon = 1; tm-&gt;mon &lt; 12 &amp;&amp; unixtime &gt; 367*(tm-&gt;mon+1)/12; tm-&gt;mon++) ; /* Subtract off the days accounted for by full months */ unixtime -= 367*tm-&gt;mon/12; /* unixtime is now number of days we are into the month */ /* Adjust the month/year so that 1 = January, and years start where we * usually expect them to. */ tm-&gt;mon += 2; if (tm-&gt;mon &gt; 12) { tm-&gt;mon -= 12; tm-&gt;year++; } tm-&gt;day = unixtime; } </code></pre> <p>My apologies for all the magic numbers. 367*month/12 is a neat trick to generate the 30/31 day sequence of the calendar. The calculation works with years that start in March until the fixup at the end, which makes things easy because then the leap day falls at the end of a "year".</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload