How to Read Time Data from CI Data Files in C#

Introduction
The ATFX API consists of two DLL files that can be integrated with custom software to directly read and extract data from an ATFX file. This post will show a simple C# code example in extracting and creating a DateTimeNano object that has accuracy in nanoseconds.

The DateTimeNano class is a subclass of the DateTime class from Microsoft with additional properties and functions to extend the accuracy of DateTime to nanoseconds.

For a simple way to open and read a ATFX file, please refer to the ATFX API Reading a ATFX File in C# article.

For more detailed information on how to implement the following code sections and properties in a class, please refer to the ATFX API manual and the provided C# Demo code. The package can be downloaded from our Programming Corner.

Extracting and Creating Time Data with Accuracy in Nanoseconds
To create a DateTimeNano object from the ATFX file, the recording property CreateTime and a class, NVHMeasurement, to access NanoSecondElapsed are needed to put into the constructor.

The below code sample shows the creation process and several of the new properties in the DateTimeNano class, such as ms_us_ns which shows Milisecond, Microsecond and Nanosecond in 000/000/000 format.

 

RecordingManager.Manager.OpenRecording(recordingPath, out IRecording rec);

// Create several ATFX API classes to extract specific data to be used in creating the DateTimeNano
ODSNVHATFXMLRecording nvhRec = rec as ODSNVHATFXMLRecording;
NVHMeasurement nvhMeasurement = nvhRec.Measurement as NVHMeasurement;
NVHEnvironment nvhEnvironment = nvhRec.Environment as NVHEnvironment;

// Create a DateTimeNano object with UTC Time Format
createTimeUTC = new DateTimeNano(nvhRec.Environment.GetUTCTime(nvhRec.RecordingProperty.CreateTime), nvhMeasurement.NanoSecondElapsed);

// DateTimeNano object properties
string
isNanoTime = string.Format ("IsNanoTime: ", createTimeUTC.IsNanoTime);
string nanoseconds = string.Format ("NanoSeconds: ", createTimeUTC.ms_us_ns);
string totalnanosecond = string.Format ("TotalNanosec: ", createTimeUTC.DayNanoSeconds);
string nanoString = string.Format ("ToNanoString(): ", createTimeUTC.ToNanoString());

// Math in extracting exact Millisecond, Microsecond and Nanosecond data
int ms = (int) (createTimeUTC.ms_us_ns / 1e6);
int us = (int) (createTimeUTC.ms_us_ns / 1e3 % 1e3);
int ns = (int) (createTimeUTC.ms_us_ns % 1e3);
string customFormat = string.Format("Custom Format - yyyy/mm/dd/hh/mm/ss/ms/us/ns: ", createTimeUTC.Year, createTimeUTC.Month, createTimeUTC.Day, createTimeUTC.Hour, createTimeUTC.Minute, createTimeUTC.Second, ms, us, ns);

 

The following screenshot is an example of the displayed ATFX file’s DateTimeNano in UTC format:

displayed ATFX file’s DateTimeNano in UTC format