C++ Boost解析日期/时间字符串并生成与.NET兼容的刻度值
我想使用C++/Boost来解析时间字符串,比如C++ Boost解析日期/时间字符串并生成与.NET兼容的刻度值,c++,.net,datetime,boost,C++,.net,Datetime,Boost,我想使用C++/Boost来解析时间字符串,比如1980.12.06 21:12:04.232,并获取一个ticks值,该值将对应于tick计数(用于初始化.NET的System.DateTime)。我怎么做 更新:我确实需要使用C++;我无法使用C++/CLI进行此操作。.Net的“滴答声”以100纳秒为间隔 ticksype:System.Int64以数字表示的日期和时间 自0001年1月1日起的100纳秒间隔 公历00:00:00.000 因此,您需要已知历元(例如Unix历元)的刻
1980.12.06 21:12:04.232
,并获取一个ticks
值,该值将对应于tick计数(用于初始化.NET的System.DateTime
)。我怎么做
更新:我确实需要使用C++;我无法使用C++/CLI进行此操作。
.Net的“滴答声”以100纳秒为间隔total_nanoseconds accessor
可能会有所帮助)。然后,您可以通过简单的加法和乘法,轻松计算.Net等效的滴答数
您可能仍然存在与可代表的日期范围有关的问题。- 在.Net中,日期时间从01.01.01 00:00:00开始
- 在boost中,ptime从1400.01.01 00.00.00开始
#include <boost/date_time/posix_time/posix_time.hpp>
int main(int argc, char* argv[])
{
using namespace boost::posix_time;
using namespace boost::gregorian;
//C# offset till 1400.01.01 00:00:00
uint64_t netEpochOffset = 441481536000000000LL;
ptime ptimeEpoch(date(1400,1,1), time_duration(0,0,0));
//note: using different format than yours, you'll need to parse the time in a different way
ptime time = from_iso_string("19801206T211204,232");
time_duration td = time - netEpoch;
uint64_t nano = td.total_microseconds() * 10LL;
std::cout <<"net ticks = " <<nano + netEpochOffset;
return 0;
}
刚刚意识到,您是在实际使用.NET,还是希望值与.NET提供的值相同而不实际使用.NET?
static void Main(string[] args)
{
DateTime date = new DateTime(1400,1,1);
Console.WriteLine(date.Ticks);
DateTime date2 = new DateTime(624805819242320000L); //C++ output
Console.WriteLine(date2);
/*output
* 441481536000000000
* 6/12/1980 21:12:04
* */
return;
}