clendar.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * https://ru.wikibooks.org/wiki/%D0%A0%D0%B5%D0%B0%D0%BB%D0%B8%D0%B7%D0%B0%D1%86%D0%B8%D0%B8_%D0%B0%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC%D0%BE%D0%B2/%D0%92%D0%B5%D1%87%D0%BD%D1%8B%D0%B9_%D0%BA%D0%B0%D0%BB%D0%B5%D0%BD%D0%B4%D0%B0%D1%80%D1%8C#C,_C++
  3. * получения дня недели из даты
  4. * 0 - вск, 1-6 - пнд-сбт.
  5. */
  6. typedef unsigned short Year;
  7. typedef unsigned char Month;
  8. typedef unsigned char Day;
  9. typedef unsigned char Weekday;
  10. const char* weekdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  11. Weekday weekday(Year year, Month month, Day day) {
  12. if (month < 3u) {
  13. --year;
  14. month += 10u;
  15. } else
  16. month -= 2u;
  17. return (Weekday)((day + 31u * month / 12u + year + year / 4u - year / 100u + year / 400u) % 7u);
  18. }
  19. /**
  20. * https://blog.reverberate.org/2020/05/12/optimizing-date-algorithms.html
  21. * Optimizing UTC → Unix Time Conversion For Size And Speed
  22. */
  23. /* https://github.com/protocolbuffers/upb/blob/22182e6e/upb/json/parser.rl#L1697
  24. * epoch_days(1970, 1, 1) == 1970-01-01 == 0 */
  25. int epoch_days_table(int year, int month, int day) {
  26. static const uint16_t month_yday[12] = {0, 31, 59, 90, 120, 151,
  27. 181, 212, 243, 273, 304, 334};
  28. uint32_t year_adj = year + 4800; /* Ensure positive year, multiple of 400. */
  29. uint32_t febs = year_adj - (month <= 2 ? 1 : 0); /* Februaries since base. */
  30. uint32_t leap_days = 1 + (febs / 4) - (febs / 100) + (febs / 400);
  31. uint32_t days = 365 * year_adj + leap_days + month_yday[month - 1] + day - 1;
  32. return days - 2472692; /* Adjust to Unix epoch. */
  33. }
  34. /* https://github.com/protocolbuffers/upb/blob/22182e6e/upb/json_decode.c#L982
  35. * epoch_days_fast(1970, 1, 1) == 1970-01-01 == 0. */
  36. int epoch_days_fast(int y, int m, int d) {
  37. const uint32_t year_base = 4800; /* Before min year, multiple of 400. */
  38. const uint32_t m_adj = m - 3; /* March-based month. */
  39. const uint32_t carry = m_adj > m ? 1 : 0;
  40. const uint32_t adjust = carry ? 12 : 0;
  41. const uint32_t y_adj = y + year_base - carry;
  42. const uint32_t month_days = ((m_adj + adjust) * 62719 + 769) / 2048;
  43. const uint32_t leap_days = y_adj / 4 - y_adj / 100 + y_adj / 400;
  44. return y_adj * 365 + leap_days + month_days + (d - 1) - 2472632;
  45. }