Last active
August 30, 2024 23:13
-
-
Save m1lkweed/4f5ffd3ce2f4d2c0196f1bf2b65f07ac to your computer and use it in GitHub Desktop.
Compile-time date parser, converts `__DATE__` to ints
This file contains hidden or 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
#include <stdio.h> | |
#define pp_current_build_year (((__DATE__[7] - '0') * 1000) + ((__DATE__[8] - '0') * 100) + ((__DATE__[9] - '0') * 10) + (__DATE__[10] - '0')) | |
#define pp_current_build_day ((((__DATE__[4] - ' ') % 16) * 10) + (__DATE__[5] - '0')) | |
#define MONTH_CMP(b) ((__DATE__[0] == (b)[0]) && (__DATE__[1] == (b)[1]) && (__DATE__[2] == (b)[2])) | |
#define pp_current_build_month ( \ | |
(MONTH_CMP("Jan") * ( 1)) + \ | |
(MONTH_CMP("Feb") * ( 2)) + \ | |
(MONTH_CMP("Mar") * ( 3)) + \ | |
(MONTH_CMP("Apr") * ( 4)) + \ | |
(MONTH_CMP("May") * ( 5)) + \ | |
(MONTH_CMP("Jun") * ( 6)) + \ | |
(MONTH_CMP("Jul") * ( 7)) + \ | |
(MONTH_CMP("Aug") * ( 8)) + \ | |
(MONTH_CMP("Sep") * ( 9)) + \ | |
(MONTH_CMP("Oct") * (10)) + \ | |
(MONTH_CMP("Nov") * (11)) + \ | |
(MONTH_CMP("Dec") * (12)) \ | |
) | |
#define pp_current_build_hour (((__TIME__[0] - '0') * 10) + (__TIME__[1] - '0')) | |
#define pp_current_build_min (((__TIME__[3] - '0') * 10) + (__TIME__[4] - '0')) | |
#define pp_current_build_sec (((__TIME__[6] - '0') * 10) + (__TIME__[7] - '0')) | |
#define pp_current_build_year_is_leap_year_p ((!(pp_current_build_year % 4)) | ((!(pp_current_build_year % 400)) + (!(pp_current_build_year % 100)) == 2)) | |
#define pp_current_build_day_of_year ((pp_current_build_day) + (pp_current_build_year_is_leap_year_p?((int[]){0, 31, 60, 90, 121, 151, 182, 213, 244, 274, 304, 335}[pp_current_build_month - 1]):((int[]){0, 31, 59, 89, 120, 150, 181, 212, 243, 273, 303, 334}[pp_current_build_month - 1]))) | |
#define pp_current_build_zodiac (pp_current_build_day <= ((const int[12]){ \ | |
20, 18, 20, 19, 20, 21, 22, 22, 22, 23, 22, 21 \ | |
})[pp_current_build_month - 1] ? (pp_current_build_month \ | |
- 1) % 12 : pp_current_build_month % 12) | |
#define pp_current_build_moon_phase (((((((pp_current_build_day_of_year - (int[19]){6, 24, 13, 2, 21, 10, 29, 18, 8, 26, 15, 4, 23, 11, 1, 20, 9, 27, 16}[pp_current_build_year%19])*100) % 2953)+50)/100) / (2953/800)) % 8) | |
int main(void){ | |
const char *signs[12] = { | |
"Capricorn", "Aquarius", "Pisces", | |
"Aries", "Taurus", "Gemini", | |
"Cancer", "Leo", "Virgo", | |
"Libra", "Scorpio", "Sagittarius" | |
}; | |
const char *moon_phases[8] = { | |
"New", "Waxing crescent", | |
"First quarter", "Waxing gibbous", | |
"Full", "Waning gibbous", | |
"Last quarter", "Waning crescent" | |
}; | |
printf("%d\n", pp_current_build_year); | |
printf("%d\n", pp_current_build_month); | |
printf("%d\n", pp_current_build_day); | |
printf("%d\n", pp_current_build_hour); | |
printf("%d\n", pp_current_build_min); | |
printf("%d\n", pp_current_build_sec); | |
printf("This is %sa leap year\n", pp_current_build_year_is_leap_year_p?"":"not "); | |
printf("Day of year: %d\n", pp_current_build_day_of_year); | |
puts(signs[pp_current_build_zodiac]); | |
puts(moon_phases[pp_current_build_moon_phase]); | |
printf("%.2d-%.2d-%.2d %.2d:%.2d:%.2d UTC\n", pp_current_build_year, pp_current_build_month, pp_current_build_day, pp_current_build_hour, pp_current_build_min, pp_current_build_sec); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment