提升解析日期/时间字符串并生成与.NET兼容的Ticks值
Boost parse date/time string and yield .NET-compatible Ticks value
我想使用C++/Boost来解析时间字符串,如1980.12.06 21:12:04.232
,并获取一个与刻度计数相对应的ticks
值(用于初始化.NET的System.DateTime
)。我该怎么做?
更新:Ido需要使用C++;对此,我无法使用C++/CLI。
- in.Net日期时间从01.01.01 00:00:00开始
- 升压时间从1400.01.01 00.00.00开始
//c++代码
#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
在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的"Ticks"以100纳秒为间隔。
- http://msdn.microsoft.com/en-us/library/z2xf7zzk.aspx
ticksType:System.Int64日期和时间,用自0001年1月1日起在公历00:00:00.000
因此,您需要已知历元(例如Unix历元)的刻度计数、历元与所需日期/时间之间的天数以及一天中的时间(the total_nanoseconds accessor
可能会有所帮助)。然后,您可以通过简单的加法和乘法,轻松地计算.Net等效的蜱虫计数。
您可能仍有与可表示日期范围有关的问题。
相关文章:
- 在 C++/CLI 中将 .NET 事件从一个 DLL 引发到另一个 DLL
- 如何在 64 位 vb.net Windows 应用程序中引用 32 位 dll
- 加载与引用 .NET DLL 位于同一文件夹中的引用的 .NET DLL 时"Not found"异常
- 是否可以在Linux上使用.Net Core 3.1创建C++/CLI代码的C#DLL
- Poco::Net::FTPClientSession 在 open() 方法上挂起 129 秒,如果 ftp 主机不存
- C++ DLL(不是 CLI)是否可以调用 .NET Core 3.0 委托?
- System.AccessViolationException:shared_ptr C# .NET 和 C++ 应用程
- C++/CLI targetting .NET Core 3.1
- 是否有可能构建面向Linux和Windows的.Net Core C++ / CLI应用程序?
- WM_PAINT消息频率:C#(.Net Framework 4.7.2 WinForms)与C++
- 如何将 VB.Net 字符串作为字符*传递给C++dll?
- 从C++标头中导入常量而不是硬编码它们:扩展 .net 控件?
- VB.NET DLL中的C++DLL函数复制
- .NET核心应用程序无法加载C++DLL
- TEA在VB.NET中的实现
- 如何使用Winforms C#或C++.Net在不更改文件中其他内容的情况下修改jpg文件中的Orientation e
- 在.NET Core 3.1中运行托管C++/CLI程序集时,映像格式错误
- 如何使函数返回数组?用于制作在VB.NET中使用的DLL
- 生成.NET包装程序需要源代码吗
- 提升解析日期/时间字符串并生成与.NET兼容的Ticks值