Last active
February 17, 2026 20:44
-
-
Save kuro68k/f3bf26642a9bc854e2e18677f5c7e39e to your computer and use it in GitHub Desktop.
Format __DATE__ and __TIME__ to ISO 8601
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 MONTH0 ( __DATE__ [2] == 'n' ? (__DATE__ [1] == 'a' ? '0' : '0') \ | |
| : __DATE__ [2] == 'b' ? '0' \ | |
| : __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? '0' : '0') \ | |
| : __DATE__ [2] == 'y' ? '0' \ | |
| : __DATE__ [2] == 'l' ? '0' \ | |
| : __DATE__ [2] == 'g' ? '0' \ | |
| : __DATE__ [2] == 'p' ? '0' \ | |
| : __DATE__ [2] == 't' ? '1' \ | |
| : __DATE__ [2] == 'v' ? '1' \ | |
| : '1') | |
| #define MONTH1 ( __DATE__ [2] == 'n' ? (__DATE__ [1] == 'a' ? '1' : '6') \ | |
| : __DATE__ [2] == 'b' ? '2' \ | |
| : __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? '3' : '4') \ | |
| : __DATE__ [2] == 'y' ? '5' \ | |
| : __DATE__ [2] == 'l' ? '7' \ | |
| : __DATE__ [2] == 'g' ? '8' \ | |
| : __DATE__ [2] == 'p' ? '9' \ | |
| : __DATE__ [2] == 't' ? '0' \ | |
| : __DATE__ [2] == 'v' ? '1' \ | |
| : '2') | |
| // ISO 8601 YYYY-MM-DDThh:mm:ss | |
| #define ISO_8601_DATE (const char[]){ __DATE__[7], __DATE__[8], __DATE__[9], __DATE__[10], \ | |
| '-', MONTH0, MONTH1, \ | |
| '-', __DATE__[4], __DATE__[5], \ | |
| 'T', __TIME__[0], __TIME__[1], \ | |
| ':', __TIME__[3], __TIME__[4], \ | |
| ':', __TIME__[6], __TIME__[7], \ | |
| '\0' } | |
| void main(void) | |
| { | |
| printf("__DATE__: %s\n", __DATE__); | |
| printf("__TIME__: %s\n", __TIME__); | |
| printf("__TIMESTAMP__: %s\n", __TIMESTAMP__); | |
| printf("ISO_8601_DATE: %s\n", ISO_8601_DATE); | |
| } |
Author
Thanks, I fixed it. It was actually fixed in production years ago, I just forgot that I posted it as a gist. I'll add a comment to the production code so I remember next time.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stumbled across this and noticed two errors in line 4 and 15. The numbers need to be escaped to be the corresponding ASCII characters similar to the other lines. Also in line 4 it should be a 0 and not a 2. As it is it fails for Februaries and injects two control characters instead or the month code "02".
corrected line 4:
corrected line 15: