日期/时间转换:时间的字符串表示形式 “MM-DD-YY HH:MM:SS”转换为C或C++中的时间> < /代码>值?恐怕标准C/C++中没有任何一个。POSIX函数strtime可以转换为struct tm,然后可以使用mktime将其转换为time
如果您的目标是实现跨平台兼容性,最好使用具有复杂功能的日期/时间转换:时间的字符串表示形式 “MM-DD-YY HH:MM:SS”转换为C或C++中的时间> < /代码>值?恐怕标准C/C++中没有任何一个。POSIX函数strtime可以转换为struct tm,然后可以使用mktime将其转换为time,c++,c,datetime,C++,C,Datetime,如果您的目标是实现跨平台兼容性,最好使用具有复杂功能的boost::date\u time。使用strtime()将时间解析为struct tm,然后使用mktime()转换为time\u tboost的日期时间库应该会有所帮助;特别是,您可能需要查看在没有strtime的情况下,您可以使用sscanf将数据解析为struct tm,然后调用mktime。虽然不是最优雅的解决方案,但它会起作用。请注意,公认答案中提到的strtime是不可移植的。下面是我用来将字符串转换为std::time\t的
boost::date\u time
。使用strtime()
将时间解析为struct tm
,然后使用mktime()
转换为time\u tboost的日期时间库应该会有所帮助;特别是,您可能需要查看在没有strtime
的情况下,您可以使用sscanf
将数据解析为struct tm
,然后调用mktime
。虽然不是最优雅的解决方案,但它会起作用。请注意,公认答案中提到的strtime
是不可移植的。下面是我用来将字符串转换为std::time\t的方便的C++11代码:
static time_t MKTimestamp(int year, int month, int day, int hour, int min, int sec)
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = gmtime ( &rawtime );
timeinfo->tm_year = year-1900 ;
timeinfo->tm_mon = month-1;
timeinfo->tm_mday = day;
timeinfo->tm_hour = hour;
timeinfo->tm_min = min;
timeinfo->tm_sec = sec;
timeinfo->tm_isdst = 0; // disable daylight saving time
time_t ret = mktime ( timeinfo );
return ret;
}
static time_t GetDateTime(const std::string pstr)
{
try
{
// yyyy-mm-dd
int m, d, y, h, min;
std::istringstream istr (pstr);
istr >> y;
istr.ignore();
istr >> m;
istr.ignore();
istr >> d;
istr.ignore();
istr >> h;
istr.ignore();
istr >> min;
time_t t;
t=MKTimestamp(y,m,d,h-1,min,0);
return t;
}
catch(...)
{
}
}
static std::time_t to_time_t(const std::string& str, bool is_dst = false, const std::string& format = "%Y-%b-%d %H:%M:%S")
{
std::tm t = {0};
t.tm_isdst = is_dst ? 1 : 0;
std::istringstream ss(str);
ss >> std::get_time(&t, format.c_str());
return mktime(&t);
}
你可以这样称呼它:
std::time_t t = to_time_t("2018-February-12 23:12:34");
您可以找到字符串格式参数
将格式为“MM-DD-YY HH:MM:SS”的日期字符串转换为时间的最佳方式
将代码限制在标准C库函数中是在寻找与strftime()相反的。要扩展总体思路,请使用sscanf()
使用“%n”
检测已完成的扫描
time_t date_string_to_time(const char *date) {
struct tm tm = { 0 }; // Important, initialize all members
int n = 0;
sscanf(date, "%d-%d-%d %d:%d:%d %n", &tm.tm_mon, &tm.tm_mday, &tm.tm_year,
&tm.tm_hour, &tm.tm_min, &tm.tm_sec, &n);
// If scan did not completely succeed or extra junk
if (n == 0 || date[n]) {
return (time_t) -1;
}
tm.tm_isdst = -1; // Assume local daylight setting per date/time
tm.tm_mon--; // Months since January
// Assume 2 digit year if in the range 2000-2099, else assume year as given
if (tm.tm_year >= 0 && tm.tm_year < 100) {
tm.tm_year += 2000;
}
tm.tm_year -= 1900; // Years since 1900
time_t t = mktime(&tm);
return t;
}
time\t date\u string\u to\u time(const char*date){
struct tm={0};//重要信息,初始化所有成员
int n=0;
sscanf(日期,“%d-%d-%d%d:%d:%d%n”,&tm.tm\u mon,&tm.tm\u mday,&tm.tm\u year,
&tm.tm_小时和tm.tm_分钟和tm.tm_秒和n);
//如果扫描未完全成功或出现额外的垃圾
如果(n==0 | |日期[n]){
返回(时间t)-1;
}
tm.tm_isdst=-1;//假设每个日期/时间的本地日光设置
tm.tm_mon-->//从一月开始的几个月
//如果在2000-2099范围内,则假定为2位数的年份,否则假定为给定的年份
如果(tm.tm_年>=0和tm.tm_年<100){
tm.tm_年+=2000年;
}
tm.tm_year-=1900;//自1900年起的年份
时间t=mktime(&tm);
返回t;
}
附加代码可用于确保仅2位时间戳部分、正值、间距等
注意:这假设“MM-DD-YY HH:MM:SS”是本地时间。strtime()在Windows上似乎不可用。有好的替代方案吗?不知道在Windows上是否有好的替代方案,有几个开源实现可以使用。或者,如果您知道日期总是以您提供的格式显示,您可以将其解析为struct tm,可能使用sscanf,然后使用mktime获取时间。下面是一个示例@an̳̳̳drawstrtime()
通常在带有gcc编译器的Windows上可用。可用性是编译器的问题,而不是操作系统的问题。有趣的是,我从未真正找到%n
的用例,因为我总是使用*scanf
的返回值来确保扫描所有参数。但是,它似乎是%n
(您使用它的方式)这样做的,并确保预期字符串后面没有额外的gumpf。现在我很少了解C语言的新知识,所以很荣幸:-)@paxdiablo谢谢。“%n”
通常也有助于检测任何所需的尾随文本,返回值无法区分这些文本。例如,%d xyz%n“
.1)代码未设置tm_isdst
成员。不这样做可能导致不一致的结果。2) struct tm
允许使用其他成员进行初始化。也许std::tmt={0}代码>?3) OP的月份字符串是数字,而不是字母,请使用“%m”
,而不是“%b”
。4) OP使用的顺序与YMD hms不同。我添加了dst选项。使用0初始化是个好主意。其他事情可以根据个人需求轻松调整。