Files
dotnet-screenlogic/HLTime.cs
Parnic e524e394c6 C#-ified
Converted as much ugly code as I could find into appropriate C#-friendly versions. For example, all the manual ByteHelper stuff, buffer index tracking, etc. are now BinaryWriters/BinaryReaders. Also cleaned up a bunch of getter and setter methods to use C# properties.

There's still more to be done here, but this greatly simplifies the code for reading and comprehension.
2018-03-28 15:30:05 -05:00

67 lines
2.0 KiB
C#

using System;
using System.IO;
namespace ScreenLogicConnect
{
public class HLTime
{
public const int size = 16;
public short day = 1;
public short dayOfWeek = 1;
public short hour = 1;
public short millisecond = 1;
public short minute = 1;
public short month = 1;
public short second = 1;
public short year = 2000;
public HLTime(byte[] data, int startIndex)
{
if (data.Length - startIndex >= size)
{
using (var ms = new MemoryStream(data))
{
using (var br = new BinaryReader(ms))
{
year = br.ReadInt16();
month = br.ReadInt16();
dayOfWeek = br.ReadInt16();
day = br.ReadInt16();
hour = br.ReadInt16();
minute = br.ReadInt16();
second = br.ReadInt16();
millisecond = br.ReadInt16();
}
}
}
}
public HLTime(short year, short month, short dayOfWeek, short day, short hour, short minute, short second, short millisecond)
{
this.year = year;
this.month = month;
this.dayOfWeek = dayOfWeek;
this.day = day;
this.hour = hour;
this.minute = minute;
this.second = second;
this.millisecond = millisecond;
}
public String toString()
{
return "" + this.month + "/" + this.day + "/" + this.year;
}
public DateTime toDate()
{
return new DateTime(year: (this.year - 2000) + 100, month: month - 1, day: day, hour: hour, minute: minute, second: second);
}
public long toMilliseconds()
{
return (long)new TimeSpan(toDate().Ticks).TotalMilliseconds;
}
}
}