Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

SYSTEMTIME (Structures)
 
.
Summary
Represents a date and time using individual members for the month, day, year, weekday, hour, minute, second, and millisecond.

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.ToLocalTime();
      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 static bool operator ==(SystemTime s1, SystemTime s2)
   {
      return (s1.Year == s2.Year && s1.Month == s2.Month && s1.Day == s2.Day && s1.Hour == s2.Hour && s1.Minute == s2.Minute && s1.Second == s2.Second && s1.Milliseconds == s2.Milliseconds);
   }

   public static bool operator !=(SystemTime s1, SystemTime s2)
   {
      return !(s1 == s2);
   }

   public static readonly SystemTime MinValue, MaxValue;

   static SystemTime()
   {
      MinValue = new SystemTime(1601, 1, 1);
      MaxValue = new SystemTime(30827, 12, 31, 23, 59, 59, 999);
   }

   public override bool Equals(object obj)
   {
      if (obj is SystemTime)
     return ((SystemTime)obj) == this;
      return base.Equals(obj);
   }

   public override int GetHashCode()
   {
      return base.GetHashCode();
   }
}

Documentation
SYSTEMTIME on MSDN

Please edit this page!

Do you have...

  • helpful tips?
  • corrections to the existing content?
  • alternate definitions?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing any supporting types needed.

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions