Skip to content

Instantly share code, notes, and snippets.

@Andersama
Created February 21, 2024 16:24
Show Gist options
  • Save Andersama/12c4c0ca89cf95aac71d602c1de41d40 to your computer and use it in GitHub Desktop.
Save Andersama/12c4c0ca89cf95aac71d602c1de41d40 to your computer and use it in GitHub Desktop.
#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