C ++干净地将Datetime字符串转换为Epoch

时间:2022-01-27 15:45:43

Is there a C/C++/STL/Boost clean method to convert a date time string to epoch time (in seconds)?

是否有一个C / C ++ / STL / Boost清理方法将日期时间字符串转换为纪元时间(以秒为单位)?

yyyy:mm:dd hh:mm:ss

3 个解决方案

#1


See: Date/time conversion: string representation to time_t

请参阅:日期/时间转换:字符串表示形式为time_t

And: [Boost-users] [date_time] So how come there isn't a to_time_t helper func?

并且:[Boost-users] [date_time]那么为什么没有to_time_t辅助函数?

So, apparently something like this should work:

所以,显然这样的事情应该有效:

#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::posix_time;

std::string ts("2002-01-20 23:59:59");
ptime t(time_from_string(ts));
ptime start(gregorian::date(1970,1,1)); 
time_duration dur = t - start; 
time_t epoch = dur.total_seconds();    

But I don't think it's much cleaner than Rob's suggestion: use sscanf to parse the data into a struct tm and then call mktime.

但我不认为它比Rob的建议更清晰:使用sscanf将数据解析为struct tm然后调用mktime。

#2


On Windows platform you can do something like this if don't want to use Boost:

在Windows平台上,如果不想使用Boost,您可以执行以下操作:

// parsing string
SYSTEMTIME stime = { 0 };
sscanf(timeString, "%04d:%02d:%02d %02d:%02d:%02d",
       &stime.wYear, &stime.wMonth,  &stime.wDay,
       &stime.wHour, &stime.wMinute, &stime.wSecond);

// converting to utc file time
FILETIME lftime, ftime;
SystemTimeToFileTime(&stime, &lftime);
LocalFileTimeToFileTime(&lftime, &ftime);

// calculating seconds elapsed since 01/01/1601
// you can write similiar code to get time elapsed from other date
ULONGLONG elapsed = *(ULONGLONG*)&ftime / 10000000ull;

If you prefer standard library, you can use struct tm and mktime() to do the same job.

如果您更喜欢标准库,则可以使用struct tm和mktime()来完成相同的工作。

#3


http://www.boost.org/doc/libs/1_39_0/doc/html/date_time.html Should do the trick.

http://www.boost.org/doc/libs/1_39_0/doc/html/date_time.html应该做的伎俩。

#1


See: Date/time conversion: string representation to time_t

请参阅:日期/时间转换:字符串表示形式为time_t

And: [Boost-users] [date_time] So how come there isn't a to_time_t helper func?

并且:[Boost-users] [date_time]那么为什么没有to_time_t辅助函数?

So, apparently something like this should work:

所以,显然这样的事情应该有效:

#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::posix_time;

std::string ts("2002-01-20 23:59:59");
ptime t(time_from_string(ts));
ptime start(gregorian::date(1970,1,1)); 
time_duration dur = t - start; 
time_t epoch = dur.total_seconds();    

But I don't think it's much cleaner than Rob's suggestion: use sscanf to parse the data into a struct tm and then call mktime.

但我不认为它比Rob的建议更清晰:使用sscanf将数据解析为struct tm然后调用mktime。

#2


On Windows platform you can do something like this if don't want to use Boost:

在Windows平台上,如果不想使用Boost,您可以执行以下操作:

// parsing string
SYSTEMTIME stime = { 0 };
sscanf(timeString, "%04d:%02d:%02d %02d:%02d:%02d",
       &stime.wYear, &stime.wMonth,  &stime.wDay,
       &stime.wHour, &stime.wMinute, &stime.wSecond);

// converting to utc file time
FILETIME lftime, ftime;
SystemTimeToFileTime(&stime, &lftime);
LocalFileTimeToFileTime(&lftime, &ftime);

// calculating seconds elapsed since 01/01/1601
// you can write similiar code to get time elapsed from other date
ULONGLONG elapsed = *(ULONGLONG*)&ftime / 10000000ull;

If you prefer standard library, you can use struct tm and mktime() to do the same job.

如果您更喜欢标准库,则可以使用struct tm和mktime()来完成相同的工作。

#3


http://www.boost.org/doc/libs/1_39_0/doc/html/date_time.html Should do the trick.

http://www.boost.org/doc/libs/1_39_0/doc/html/date_time.html应该做的伎俩。