How do I convert a timestamp to a date/time object in C#?|Classic OPC: DA, A&E, HDA, XML-DA, etc.|Forum|OPC Foundation

Avatar
Search
Forum Scope


Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
Lost password?
sp_Feed sp_PrintTopic sp_TopicIcon
How do I convert a timestamp to a date/time object in C#?
Avatar
Rob Richardson
Member
Members
Forum Posts: 3
Member Since:
07/28/2015
sp_UserOfflineSmall Offline
1
12/03/2020 - 12:08
sp_Permalink sp_Print

I am using a library based on twenty-year-old but still-working sample code to read OPC data.  My data point has a timestamp of 132514957825255872.  When I use "DateTime theTime = new DateTime(timestamp)", I get a date/time of 12/3/0420 7:03:02 PM.  That is exactly 1600 years and five hours away from the correct time.  In C#, an integer being converted to a timestamp is assumed to be the number of 100-nanosecond ticks since 1/1/0001 00:00:00.  It appears that the number I am getting from my OPC server (KEPWare's KEPServerEX, if it matters) is the number of 100-nanosecond ticks since 1/1/1600 00:00:00 in UTC time.  Is that correct?  

Avatar
Randy Armstrong
Admin
Forum Posts: 1450
Member Since:
05/30/2017
sp_UserOfflineSmall Offline
2
12/03/2020 - 20:23
sp_Permalink sp_Print sp_EditHistory

The 0 time for the .NET DateTime is January 1, 0001 at 00:00:00.000 in the Gregorian calendar.

The 0 for COM DA and OPC UA is 12:00 A.M. January 1, 1601 Coordinated Universal Time (UTC)

The OPC UA .NETStandard codebase has a constant Utils.TimeBase that can be used for adjustment.

Avatar
Lun raznik
New Member
Members
Forum Posts: 1
Member Since:
02/22/2021
sp_UserOfflineSmall Offline
3
02/23/2021 - 01:18
sp_Permalink sp_Print

You probably are encountering an issue with FILETIME structure was incorrectly written in C#, see this link

https://github.com/dotnet/runt.....sues/28700

 

I have modified my version of OPC Net API so that it uses uint vs int, like:

namespace OpcRcw.Da
{
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, Pack=4)]
public struct FILETIME
{
public uint dwLowDateTime;
public uint dwHighDateTime;
}
}

Of course you have to make changes to other locations for proper conversion.

Forum Timezone: America/Phoenix
Most Users Ever Online: 510
Currently Online: Andrey Tretyakov
Guest(s) 15
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Forum Stats:
Groups: 2
Forums: 10
Topics: 1348
Posts: 4575