Skip to content

Instantly share code, notes, and snippets.

@asi-msk
Created August 11, 2022 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asi-msk/46a20928c46ffeab1ad7a0760009e3bc to your computer and use it in GitHub Desktop.
Save asi-msk/46a20928c46ffeab1ad7a0760009e3bc to your computer and use it in GitHub Desktop.
8bitマイコンで比較的楽に曜日を何とかする方法(0-9999のみの対応)
#include <stdint.h>
uint8_t calc_day_of_week(uint8_t cent, uint8_t year, uint8_t month, uint8_t day) {
const uint8_t non_leap[] = {6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
const uint8_t leap[] = {6, 2, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5, 1};
uint8_t c = 7 - (cent * 2 - cent / 4) % 7;
uint8_t is_leap = 0;
if ((year == 0) && ((cent & 0b11) == 0)) {
is_leap = 1;
} else if (year == 0) {
is_leap = 0;
} else if ((year & 0b11) == 0) {
is_leap = 1;
}
if (is_leap) {
return (year + year / 4 + leap[month - 1] + day + c) % 7;
} else {
return (year + year / 4 + non_leap[month - 1] + day + c) % 7;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment