Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than Structures, prefix the name with the module name and a period.
SYSTEMTIME (Structures)
.
C# Definition:
[StructLayout(LayoutKind.Sequential)]
private struct SYSTEMTIME {
[MarshalAs(UnmanagedType.U2)] public short Year;
[MarshalAs(UnmanagedType.U2)] public short Month;
[MarshalAs(UnmanagedType.U2)] public short DayOfWeek;
[MarshalAs(UnmanagedType.U2)] public short Day;
[MarshalAs(UnmanagedType.U2)] public short Hour;
[MarshalAs(UnmanagedType.U2)] public short Minute;
[MarshalAs(UnmanagedType.U2)] public short Second;
[MarshalAs(UnmanagedType.U2)] public short Milliseconds;
public SYSTEMTIME( DateTime dt )
{
dt = dt.ToUniversalTime(); // SetSystemTime expects the SYSTEMTIME in UTC
Year = (short)dt.Year;
Month = (short)dt.Month;
DayOfWeek = (short)dt.DayOfWeek;
Day = (short)dt.Day;
Hour = (short)dt.Hour;
Minute = (short)dt.Minute;
Second = (short)dt.Second;
Milliseconds = (short)dt.Millisecond;
}
}
VB Definition:
<StructLayout(LayoutKind.Sequential)> _
Private Structure SYSTEMTIME
<MarshalAs(UnmanagedType.U2)> Public Year As Short
<MarshalAs(UnmanagedType.U2)> Public Month As Short
<MarshalAs(UnmanagedType.U2)> Public DayOfWeek As Short
<MarshalAs(UnmanagedType.U2)> Public Day As Short
<MarshalAs(UnmanagedType.U2)> Public Hour As Short
<MarshalAs(UnmanagedType.U2)> Public Minute As Short
<MarshalAs(UnmanagedType.U2)> Public Second As Short
<MarshalAs(UnmanagedType.U2)> Public Milliseconds As Short
End Structure
Notes:
Sometimes, when trying to transform this struct into a CLR DateTime, I noticed that the year is 1 year behind. I don't know why.
Tips & Tricks:
Please add some!
C# Sample Class:
[StructLayout(LayoutKind.Sequential, Pack=2)]
internal struct SystemTime
{
public ushort Year;
public ushort Month;
public ushort DayOfWeek;
public ushort Day;
public ushort Hour;
public ushort Minute;
public ushort Second;
public ushort Milliseconds;
public SystemTime(DateTime dt)
{
dt = dt.ToUniversalTime();
Year = Convert.ToUInt16(dt.Year);
Month = Convert.ToUInt16(dt.Month);
DayOfWeek = Convert.ToUInt16(dt.DayOfWeek);
Day = Convert.ToUInt16(dt.Day);
Hour = Convert.ToUInt16(dt.Hour);
Minute = Convert.ToUInt16(dt.Minute);
Second = Convert.ToUInt16(dt.Second);
Milliseconds = Convert.ToUInt16(dt.Millisecond);
}
public SystemTime(ushort year, ushort month, ushort day, ushort hour = 0, ushort minute = 0, ushort second = 0, ushort millisecond = 0)
{
Year = year;
Month = month;
Day = day;
Hour = hour;
Minute = minute;
Second = second;
Milliseconds = millisecond;
DayOfWeek = 0;
}
public static implicit operator DateTime(SystemTime st)
{
if (st.Year == 0 || st == MinValue)
return DateTime.MinValue;
if (st == MaxValue)
return DateTime.MaxValue;
return new DateTime(st.Year, st.Month, st.Day, st.Hour, st.Minute, st.Second, st.Milliseconds, DateTimeKind.Local);
}
public ushort Year
{
get => wYear;
set
{
wYear = value > 1600 && value < 30828
? value
: throw new ArgumentOutOfRangeException(nameof(Year), value, null);
int daysInMonth = GetDaysInMonth(wMonth, wYear);
if (wDay > daysInMonth) wDay = (ushort) daysInMonth;
wDayOfWeek = (ushort)GetDayOfWeek(wDay, wMonth, wYear);
}
}
public ushort Month
{
get => wMonth;
set
{
wMonth = value > 0 && value < 13
? value
: throw new ArgumentOutOfRangeException(nameof(Month), value, null);
int daysInMonth = GetDaysInMonth(wMonth, wYear);
if (wDay > daysInMonth) wDay = (ushort) daysInMonth;
wDayOfWeek = (ushort)GetDayOfWeek(wDay, wMonth, wYear);
}
}
public ushort Day
{
get => wDay;
set
{
wDay = value > 0 && value <= GetDaysInMonth(wMonth, wYear)
? value
: throw new ArgumentOutOfRangeException(nameof(Day), value, null);
wDayOfWeek = (ushort)GetDayOfWeek(wDay, wMonth, wYear);
}
}
public DayOfWeek DayOfWeek
{
get => (DayOfWeek)wDayOfWeek;
}
public ushort Hour
{
get => wHour;
set => wHour = value < 24 ? value
: throw new ArgumentOutOfRangeException(nameof(Hour), value, null);
}
public ushort Minute
{
get => wMinute;
set => wMinute = value < 60 ? value
: throw new ArgumentOutOfRangeException(nameof(Minute), value, null);
}
public ushort Second
{
get => wSecond;
set => wSecond = value < 60 ? value
: throw new ArgumentOutOfRangeException(nameof(Second), value, null);
}
public ushort Milliseconds
{
get => wMilliseconds;
set => wMilliseconds = value < 1000 ? value
: throw new ArgumentOutOfRangeException(nameof(Milliseconds), value, null);
}
private static bool CheckIsLeapYear(int year)
{
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
private int GetDaysInMonth(int month, int year)
{
return month == 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 29 : 28 + ((62648012 >> (month * 2)) & 3);
switch (month)
{
case 2:
return CheckIsLeapYear(year) ? 29 : 28;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
default:
return 30;
}
}
private static int GetDayOfWeek(int day, int month, int year)
{
int a = (14 - month) / 12;
int y = year - a;
int m = month + 12 * a - 2;
return ((7000 + (day + y + y / 4 - y / 100 + y / 400 + (31 * m) / 12)) % 7);
}
public long ToFileTime()
{
return SystemTimeToFileTimeNative(this, out long fileTime) ? fileTime : 0L;
}
public DateTime ToDateTime()
{
return SystemTimeToFileTimeNative(this, out long fileTime)
? DateTime.FromFileTimeUtc(fileTime)
: DateTime.MinValue;
}
public double ToUnixTime()
{
SystemTimeToFileTimeNative(this, out long fileTime);
long seconds = fileTime / 10000000;
double unixTime = seconds - 11644473600;
return Math.Round(unixTime, 2);
}
public static SystemTime FromFileTime(long fileTime)
{
SystemTime systemTime = new SystemTime();
FileTimeToSystemTimeNative(fileTime, systemTime);
return systemTime;
}
public static SystemTime FromDateTime(DateTime dateTime)
{
long fileTime = dateTime.ToFileTimeUtc();
SystemTime systemTime = new SystemTime();
FileTimeToSystemTimeNative(fileTime, systemTime);
return systemTime;
}
public static SystemTime FromUnixTime(double unixTime)
{
SystemTime systemTime = new SystemTime();
long seconds = Convert.ToInt64(Math.Floor(unixTime + 11644473600));
long fileTime = seconds * 10000000;
FileTimeToSystemTimeNative(fileTime, systemTime);
return systemTime;
}