|
@@ -0,0 +1,50 @@
|
|
|
+
|
|
|
+ * https:
|
|
|
+ * получения дня недели из даты
|
|
|
+ * 0 - вск, 1-6 - пнд-сбт.
|
|
|
+ */
|
|
|
+
|
|
|
+typedef unsigned short Year;
|
|
|
+typedef unsigned char Month;
|
|
|
+typedef unsigned char Day;
|
|
|
+typedef unsigned char Weekday;
|
|
|
+const char* weekdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
|
|
|
+
|
|
|
+Weekday weekday(Year year, Month month, Day day) {
|
|
|
+ if (month < 3u) {
|
|
|
+ --year;
|
|
|
+ month += 10u;
|
|
|
+ } else
|
|
|
+ month -= 2u;
|
|
|
+ return (Weekday)((day + 31u * month / 12u + year + year / 4u - year / 100u + year / 400u) % 7u);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * https:
|
|
|
+ * Optimizing UTC → Unix Time Conversion For Size And Speed
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+ * epoch_days(1970, 1, 1) == 1970-01-01 == 0 */
|
|
|
+int epoch_days_table(int year, int month, int day) {
|
|
|
+ static const uint16_t month_yday[12] = {0, 31, 59, 90, 120, 151,
|
|
|
+ 181, 212, 243, 273, 304, 334};
|
|
|
+ uint32_t year_adj = year + 4800;
|
|
|
+ uint32_t febs = year_adj - (month <= 2 ? 1 : 0);
|
|
|
+ uint32_t leap_days = 1 + (febs / 4) - (febs / 100) + (febs / 400);
|
|
|
+ uint32_t days = 365 * year_adj + leap_days + month_yday[month - 1] + day - 1;
|
|
|
+ return days - 2472692;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * epoch_days_fast(1970, 1, 1) == 1970-01-01 == 0. */
|
|
|
+int epoch_days_fast(int y, int m, int d) {
|
|
|
+ const uint32_t year_base = 4800;
|
|
|
+ const uint32_t m_adj = m - 3;
|
|
|
+ const uint32_t carry = m_adj > m ? 1 : 0;
|
|
|
+ const uint32_t adjust = carry ? 12 : 0;
|
|
|
+ const uint32_t y_adj = y + year_base - carry;
|
|
|
+ const uint32_t month_days = ((m_adj + adjust) * 62719 + 769) / 2048;
|
|
|
+ const uint32_t leap_days = y_adj / 4 - y_adj / 100 + y_adj / 400;
|
|
|
+ return y_adj * 365 + leap_days + month_days + (d - 1) - 2472632;
|
|
|
+}
|