升压解析日期/时间字符串和产量.NET兼容蜱值 [英] Boost parse date/time string and yield .NET-compatible Ticks value

查看:135
本文介绍了升压解析日期/时间字符串和产量.NET兼容蜱值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用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;
}

这篇关于升压解析日期/时间字符串和产量.NET兼容蜱值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆