升压解析日期/时间字符串和产量.NET兼容蜱值字符串、产量、日期、时间

2023-09-04 09:03:57 作者:Get Lost 给我消失

我想用C ++ /升压解析时间字符串,如 1980年12月6日21:12:04.232 并获得值,该值将对应于时钟计数(用来初始化.NET的的System.DateTime )。我该怎么办呢?

I'd like to use C++/Boost to parse time strings such as 1980.12.06 21:12:04.232 and acquire a ticks value that would correspond to the tick count( used to initialize .NET's System.DateTime). How can I do it?

更新:的我的不的需要使用C ++;我不能使用C ++ / CLI这一点。

Update: I do need to use C++; I cannot use C++/CLI for this.

推荐答案

在净日期时间为01.01.01 00:00:00 启动 在升压分组时间从1400年1月1日00.00.00 启动

// C ++ code

//c++ code

#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;
}

//输出624805819242320000

// outputs 624805819242320000

在C#中测试

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;
}