title: Convert unix timestamp to date without system libs
body: <p>I am building a embedded project which displays the time retrieved from a GPS module on a display, but I would also like to display the current date. I currently have the time as a unix time stamp and the progject is written in C.</p>
  
  <p>I am looking for a way to calculate the current UTC date from the timestamp, taking leap years into account? Remember, this is for an embedded project where there is no FPU, so floating point math is emulated, avoiding it as much as possible for performance is required.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>After looking at @R...'s code, I decided to have a go a writing this myself and came up with the following.</p>
  
  <pre><code>void calcDate(struct tm *tm)
  {
    uint32_t seconds, minutes, hours, days, year, month;
    uint32_t dayOfWeek;
    seconds = gpsGetEpoch();
  
    /* calculate minutes */
    minutes  = seconds / 60;
    seconds -= minutes * 60;
    /* calculate hours */
    hours    = minutes / 60;
    minutes -= hours   * 60;
    /* calculate days */
    days     = hours   / 24;
    hours   -= days    * 24;
  
    /* Unix time starts in 1970 on a Thursday */
    year      = 1970;
    dayOfWeek = 4;
  
    while(1)
    {
      bool     leapYear   = (year % 4 == 0 &amp;&amp; (year % 100 != 0 || year % 400 == 0));
      uint16_t daysInYear = leapYear ? 366 : 365;
      if (days &gt;= daysInYear)
      {
        dayOfWeek += leapYear ? 2 : 1;
        days      -= daysInYear;
        if (dayOfWeek &gt;= 7)
          dayOfWeek -= 7;
        ++year;
      }
      else
      {
        tm-&gt;tm_yday = days;
        dayOfWeek  += days;
        dayOfWeek  %= 7;
  
        /* calculate the month and day */
        static const uint8_t daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        for(month = 0; month &lt; 12; ++month)
        {
          uint8_t dim = daysInMonth[month];
  
          /* add a day to feburary if this is a leap year */
          if (month == 1 &amp;&amp; leapYear)
            ++dim;
  
          if (days &gt;= dim)
            days -= dim;
          else
            break;
        }
        break;
      }
    }
  
    tm-&gt;tm_sec  = seconds;
    tm-&gt;tm_min  = minutes;
    tm-&gt;tm_hour = hours;
    tm-&gt;tm_mday = days + 1;
    tm-&gt;tm_mon  = month;
    tm-&gt;tm_year = year;
    tm-&gt;tm_wday = dayOfWeek;
  }
  </code></pre>