Different format of __DATE__ macro
Solution 1
In C you could have a macro that generates a compound literal on the fly that has the order that you like, something like
#define FDATE (char const[]){ __DATE__[7], __DATE__[8], ..., ' ', ... , '\0' }
in all places where it matters your optimizer should be able to handle this efficiently.
Solution 2
Here's a true hack:
union {
const char DOUBLE_DATE[18];
const char PAD[19];
} DATE_HELPER = { __DATE__ " " __DATE__ };
const char *MY_DATE = DATE_HELPER.DOUBLE_DATE + 7;
Solution 3
I wanted the date of compilation in ISO format in a C++ program, so I came up with this:
constexpr unsigned int compileYear = (__DATE__[7] - '0') * 1000 + (__DATE__[8] - '0') * 100 + (__DATE__[9] - '0') * 10 + (__DATE__[10] - '0');
constexpr unsigned int compileMonth = (__DATE__[0] == 'J') ? ((__DATE__[1] == 'a') ? 1 : ((__DATE__[2] == 'n') ? 6 : 7)) // Jan, Jun or Jul
: (__DATE__[0] == 'F') ? 2 // Feb
: (__DATE__[0] == 'M') ? ((__DATE__[2] == 'r') ? 3 : 5) // Mar or May
: (__DATE__[0] == 'A') ? ((__DATE__[2] == 'p') ? 4 : 8) // Apr or Aug
: (__DATE__[0] == 'S') ? 9 // Sep
: (__DATE__[0] == 'O') ? 10 // Oct
: (__DATE__[0] == 'N') ? 11 // Nov
: (__DATE__[0] == 'D') ? 12 // Dec
: 0;
constexpr unsigned int compileDay = (__DATE__[4] == ' ') ? (__DATE__[5] - '0') : (__DATE__[4] - '0') * 10 + (__DATE__[5] - '0');
constexpr char IsoDate[] =
{ compileYear/1000 + '0', (compileYear % 1000)/100 + '0', (compileYear % 100)/10 + '0', compileYear % 10 + '0',
'-', compileMonth/10 + '0', compileMonth%10 + '0',
'-', compileDay/10 + '0', compileDay%10 + '0',
0
};
// Test that it gets it right today
#include <cstring>
static_assert(strcmp(IsoDate, "2020-11-06") == 0);
Solution 4
In case anyone is looking for a way to convert Mmm DD YYYY
into a string YYMMDD
with ASCII digits only (like "220103"
), then here's the code for that. It should work in C and C++ both:
#include <stdio.h>
const char version[6+1] =
{
// YY year
__DATE__[9], __DATE__[10],
// First month letter, Oct Nov Dec = '1' otherwise '0'
(__DATE__[0] == 'O' || __DATE__[0] == 'N' || __DATE__[0] == 'D') ? '1' : '0',
// Second month letter
(__DATE__[0] == 'J') ? ( (__DATE__[1] == 'a') ? '1' : // Jan, Jun or Jul
((__DATE__[2] == 'n') ? '6' : '7') ) :
(__DATE__[0] == 'F') ? '2' : // Feb
(__DATE__[0] == 'M') ? (__DATE__[2] == 'r') ? '3' : '5' : // Mar or May
(__DATE__[0] == 'A') ? (__DATE__[1] == 'p') ? '4' : '8' : // Apr or Aug
(__DATE__[0] == 'S') ? '9' : // Sep
(__DATE__[0] == 'O') ? '0' : // Oct
(__DATE__[0] == 'N') ? '1' : // Nov
(__DATE__[0] == 'D') ? '2' : // Dec
0,
// First day letter, replace space with digit
__DATE__[4]==' ' ? '0' : __DATE__[4],
// Second day letter
__DATE__[5],
'\0'
};
int main(void)
{
puts(__DATE__);
puts(version);
}
Output:
Jan 3 2022
220103
Disassembly (gcc x86_64):
version:
.string "220103"
boleto
Updated on July 28, 2022Comments
-
boleto almost 2 years
C has a predefined macro
__DATE__
, that shows the date of the compiled source file .
The date is displayed in the format "Mmm dd yyyy" .Is there any way to be formatted this date, using macros ?
In this format "yyyy Mmm dd".Instead of being :
Jul 19 2013
Should be :
2013 Jul 19