Files
KinginfoGateway/framework/Foundation/ThingsGateway.Foundation.Adapter.OPCDA/COM/Comn/Convert.cs

84 lines
2.6 KiB
C#
Raw Normal View History

2023-05-23 23:54:28 +08:00
#region copyright
//------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权除特别声明外的代码归作者本人Diego所有
// 源代码使用协议遵循本仓库的开源协议及附加协议
2023-07-16 17:48:22 +08:00
// Gitee源代码仓库https://gitee.com/diego2098/ThingsGateway
2023-05-23 23:54:28 +08:00
// Github源代码仓库https://github.com/kimdiego2098/ThingsGateway
2023-07-16 17:48:22 +08:00
// 使用文档https://diego2098.gitee.io/thingsgateway-docs/
2023-05-23 23:54:28 +08:00
// QQ群605534569
//------------------------------------------------------------------------------
#endregion
namespace ThingsGateway.Foundation.Adapter.OPCDA.Comn;
internal static class Convert
2023-03-09 11:19:48 +08:00
{
/// <summary>
/// windows的filetime是从1601-1-1 00:00:00开始的datetime是从1-1-1 00:00:00开始的
/// datetime和filetime的滴答单位都是100ns100纳秒千万分之一秒所以转换时只需要考虑开始时间即可
/// </summary>
private static readonly DateTime FILETIME_BaseTime = new DateTime(1601, 1, 1);
private static bool m_preserveUTC = false;
internal static object Clone(object source)
2023-03-09 11:19:48 +08:00
{
if (source == null)
{
return null;
}
if (source.GetType().IsValueType)
{
return source;
}
if (source.GetType().IsArray || source.GetType() == typeof(Array))
{
Array array = (Array)((Array)source).Clone();
for (int i = 0; i < array.Length; i++)
{
array.SetValue(Clone(array.GetValue(i)), i);
}
return array;
}
try
2023-03-09 11:19:48 +08:00
{
return ((ICloneable)source).Clone();
}
catch
{
throw new NotSupportedException("Object cannot be cloned.");
}
}
internal static DateTime FileTimeToDateTime(System.Runtime.InteropServices.ComTypes.FILETIME filetime)
{
long num = filetime.dwHighDateTime;
if (num < 0)
{
num += 4294967296L;
}
long num2 = num << 32;
num = filetime.dwLowDateTime;
if (num < 0)
{
num += 4294967296L;
}
num2 += num;
if (num2 == 0)
{
return DateTime.MinValue;
}
if (m_preserveUTC)
{
DateTime fILETIME_BaseTime = FILETIME_BaseTime;
return fILETIME_BaseTime.Add(new TimeSpan(num2));
2023-03-09 11:19:48 +08:00
}
DateTime fILETIME_BaseTime2 = FILETIME_BaseTime;
return fILETIME_BaseTime2.Add(new TimeSpan(num2)).ToLocalTime();
2023-03-09 11:19:48 +08:00
}
}