I have an application that reads times from a file. These times can be in three different formats, based on flag bits elsewhere in the file, as shown below:
[StructLayout(LayoutKind.Sequential, Pack = 1]
public struct IntraPacketTime_RTC
{
public UInt64 RTC;
}
[StructLayout(LayoutKind.Sequential, Pack = 1]
public struct IntraPacketTime_Ch4
{
public UInt16 Unused;
public UInt16 TimeHigh;
public UInt16 TimeLow;
public UInt16 MicroSeconds;
}
[StructLayout(LayoutKind.Sequential, Pack = 1]
public struct IntraPacketTime_IEEE1588
{
public UInt32 NanoSeconds;
public UInt32 Seconds;
}
As you can see, all three time formats take up eight bytes in the source data file, but these eight bytes are cast differently, depending on the specified time format.
However, the output time is always the same format, regardless of the way it is stored in the source data file (either YYY HH:MM:DD SS.ssssss, where ssssss is fractions of a second, or SSSSS.ssssss as seconds and fractional seconds past midnight).
How do I read these times from the data file and use them in a general way, without having case
statements all over the place? Is there a software pattern that would simplify this, make it more generalized, and abstract away some of the complexity?