Created
February 21, 2024 16:24
-
-
Save Andersama/12c4c0ca89cf95aac71d602c1de41d40 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <iostream> | |
#include <cstdint> | |
// 1/1/1900 -> Monday | |
uint64_t days_in_year(uint64_t y) { | |
bool div4 = (y & 0x3) == 0; | |
bool div16 = (y & 0xf) == 0; | |
bool div100 = (y % 100) == 0; | |
bool leap_year = | |
(div4 && !div100) || (div16 && div100); | |
return 365 + leap_year; | |
} | |
enum months : uint8_t { | |
jan, | |
feb, | |
mar, | |
apr, | |
may, | |
jun, | |
jul, | |
aug, | |
sep, | |
oct, | |
nov, | |
dec | |
}; | |
enum days : uint8_t { | |
mon, | |
tue, | |
wed, | |
thu, | |
fri, | |
sat, | |
sun | |
}; | |
uint64_t days_in_month(uint64_t y, uint64_t m) { | |
uint64_t extra_years = m / 12; | |
uint64_t real_month = m % 12; | |
uint64_t days = days_in_year(y+extra_years); | |
const uint8_t mxb[12] = { 31,28 | 0x80,31,30,31,30,31,31,30,31,30,31 }; | |
uint8_t v = mxb[real_month]; | |
return (v & 0x7f) + ((v >> 7) * (days == 366)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment