将字符串从__DATE__转换为time_t [英] Convert string from __DATE__ into a time_t
本文介绍了将字符串从__DATE__转换为time_t的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将从 __ DATE __
宏生成的字符串转换为 time_t
。我不需要一个完整的日期/时间解析器,只处理 __ DATE __
宏格式的格式是很棒的。
I'm trying to convert the string produced from the __DATE__
macro into a time_t
. I don't need a full-blown date/time parser, something that only handles the format of the __DATE__
macro would be great.
一个预处理器的方法会很漂亮,但一个函数也可以工作。如果相关,我使用MSVC。
A preprocessor method would be nifty, but a function would work just as well. If it's relevant, I'm using MSVC.
推荐答案
编辑:更正的函数应该是这样的:
the corrected function should look something like this:
time_t cvt_TIME(char const *time) {
char s_month[5];
int month, day, year;
struct tm t = {0};
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
sscanf(time, "%s %d %d", s_month, &day, &year);
month = (strstr(month_names, s_month)-month_names)/3;
t.tm_mon = month;
t.tm_mday = day;
t.tm_year = year - 1900;
t.tm_isdst = -1;
return mktime(&t);
}
这篇关于将字符串从__DATE__转换为time_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文