Skip to content

Instantly share code, notes, and snippets.

@m1lkweed
Created January 7, 2024 22:01
Show Gist options
  • Save m1lkweed/4f5ffd3ce2f4d2c0196f1bf2b65f07ac to your computer and use it in GitHub Desktop.
Save m1lkweed/4f5ffd3ce2f4d2c0196f1bf2b65f07ac to your computer and use it in GitHub Desktop.
Compile-time date parser, converts `__DATE__` to ints
#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'))
int main(void){
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("%.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