Different format of __DATE__ macro

≡放荡痞女 提交于 2020-01-14 10:26:26

问题


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


回答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.




回答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;


来源:https://stackoverflow.com/questions/17739390/different-format-of-date-macro

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!