如何将日期字符串(格式为"MM-DD-YY HH:MM:SS"
)转换为C或C++中的time_t
值?日期/时间转换:字符串表示形式到time_t
13
A
回答
18
使用strptime()
解析时间为struct tm
,然后使用mktime()
转换为time_t
。
2
恐怕没有任何标准的C/C++。 POSIX功能strptime
可以转换为struct tm
,然后可以使用mktime
将其转换为time_t
。
如果您的目标是跨平台兼容性,请更好地使用boost::date_time
,它具有复杂的功能。
3
Boost的日期时间库应该有所帮助;尤其是你可能想看看http://www.boost.org/doc/libs/1_37_0/doc/html/date_time/date_time_io.html
4
在没有strptime
你可以使用sscanf
分析数据为struct tm
,然后调用mktime
。不是最优雅的解决方案,但它会工作。
-1
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(...)
{
}
}
1
请注意,在接受的答案中提到的strptime
是不便携的。下面是我使用字符串转换为std :: time_t的方便的C++代码11:
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");
你可以找到字符串格式参数here。
1
的日期字符串,格式为 “MM-DD-YY HH:MM:SS” 转换效果最好的方式,以一个time_t的
限制代码标准C库函数是寻找反strftime()
。要扩大@Rob总体思路,请使用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;
}
附加代码可以用于确保仅2位的时间戳部分,正值,间距等
注意:此假设“MM-DD -YY HH:MM:SS“是本地时间。
相关问题
- 1. time_t的字符串表示形式?
- 2. time_t的C字符串表示形式
- 3. 转换字符串到日期时间
- 4. 转换日期/时间字符串值到.NET日期时间
- 5. 转换日期字符串到日期时间兼容到PostgreSQL
- 6. 转换日期字符串日期时间格式vb.net
- 7. 转换日期字符串以日期开始到日期时间格式vb.net
- 8. C#字符串日期时间转换
- 9. 转换日期时间字符串
- 10. 字符串日期时间转换javascript
- 11. 转换GMT日期时间字符串
- 12. 用精神解析日期时间字符串到time_t值
- 13. 字符串转换为time_t
- 14. 将日期时间转换为代表日期的字符串
- 15. javascript日期/时间字符串转换为SQL日期时间
- 16. 将字符串日期时间转换为Ruby日期时间
- 17. 将日期时间字符串转换为日期时间
- 18. 将熊猫日期时间月转换为字符串表示
- 19. 转换ISO 8601日期字符串到.NET日期时间
- 20. 转换日期时间字符串到日期
- 21. 转换日期字符串到日期时间
- 22. 转换日期字符串到日期时间
- 23. TSQL - 转换日期字符串到日期时间
- 24. Python的转换日期字符串到日期时间
- 25. 转换的日期字符串表示
- 26. 日期时间到字符串转换显示错误
- 27. 转换从字符转换日期/时间失败字符串
- 28. 使用PHP转换字符串日期时间到时间戳
- 29. 转换YYMMDDHHMM日期/时间字符串到PHP时间戳
- 30. 转换PHP的时间字符串到MySQL日期时间
strptime()在Windows上不可用。有很好的选择吗? – 2008-11-26 19:17:15
不知道在Windows上是不是一个好的选择,有几个可以使用的开源实现。或者,如果您知道日期将始终采用您提供的格式,则可以使用sscanf将其解析为struct tm,然后使用mktime获取time_t。 – 2008-11-26 19:23:46
这是一个示例http://stackoverflow.com/a/11213640/1115059 – Jaydeep 2014-09-19 01:46:29