Jump to content
    

Интересный способ преобразования Unix TimeStamp -> Date/Time

 /**
  * @brief Convert Unix timestamp to date
  * @param[in] t Unix timestamp
  * @param[out] date Pointer to a structure representing the date and time
  **/
  
 void convertUnixTimeToDate(time_t t, DateTime *date)
 {
    uint32_t a;
    uint32_t b;
    uint32_t c;
    uint32_t d;
    uint32_t e;
    uint32_t f;
  
    //Negative Unix time values are not supported
    if(t < 1)
    {
       t = 0;
    }
  
    //Clear milliseconds
    date->milliseconds = 0;
  
    //Retrieve hours, minutes and seconds
    date->seconds = t % 60;
    t /= 60;
    date->minutes = t % 60;
    t /= 60;
    date->hours = t % 24;
    t /= 24;
  
    //Convert Unix time to date
    a = (uint32_t) ((4 * t + 102032) / 146097 + 15);
    b = (uint32_t) (t + 2442113 + a - (a / 4));
    c = (20 * b - 2442) / 7305;
    d = b - 365 * c - (c / 4);
    e = d * 1000 / 30601;
    f = d - e * 30 - e * 601 / 1000;
  
    //January and February are counted as months 13 and 14 of the previous year
    if(e <= 13)
    {
       c -= 4716;
       e -= 1;
    }
    else
    {
       c -= 4715;
       e -= 13;
    }
  
    //Retrieve year, month and day
    date->year = c;
    date->month = e;
    date->day = f;
  
    //Calculate day of week
    date->dayOfWeek = computeDayOfWeek(c, e, f);
 }


Вообще, конечно, интересна математика происходящего. Как я понимаю, авторы каким-то образом считают в "14-месяцовых годах". В чем тут "фокус"? Откуда именно такие магические числа в преобразованиях?🙂

Share this post


Link to post
Share on other sites

2 часа назад, Arlleex сказал:

Как я понимаю, авторы каким-то образом считают в "14-месяцовых годах". В чем тут "фокус"?

Марсианский календарь?  :wink:

Share this post


Link to post
Share on other sites

Кто их разберет)) Почитал на вики - тут что-то с преобразованием юлианского календаря в григорианский, скорее всего.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...