123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- using System;
- using System.Runtime.InteropServices;
- namespace Wayne.Lib
- {
-
-
-
- [StructLayout(LayoutKind.Sequential)]
- internal 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 class WayneSystemTime : ISystemTime
- {
-
-
-
- public DateTime Now
- {
- get { return DateTime.Now; }
- set { SetSystemTime(value); }
- }
- [DllImport("kernel32.dll")]
- static extern bool SetSystemTime(
- ref SystemTime systemTime);
- [DllImport("kernel32.dll")]
- static extern bool SetLocalTime(
- ref SystemTime systemTime);
-
-
-
-
-
- public void SetSystemTime(DateTime newTime)
- {
- SystemTime st = ConvertToSystemTime(newTime.ToUniversalTime());
- bool rc = SetSystemTime(ref st);
- if (!rc)
- {
- int wrc = Marshal.GetLastWin32Error();
- throw new Exception("Error " + wrc + " setting system time");
- }
- }
-
-
-
-
- public void SetLocalTime(DateTime newTime)
- {
-
- SystemTime st = ConvertToSystemTime(newTime);
- bool rc = SetLocalTime(ref st);
- if (!rc)
- {
- int wrc = Marshal.GetLastWin32Error();
- throw new Exception("Error " + wrc + " setting local time");
- }
- }
- internal static SystemTime ConvertToSystemTime(DateTime t)
- {
- SystemTime st = new SystemTime();
- st.Year = (short)t.Year;
- st.Month = (short)t.Month;
- st.Day = (short)t.Day;
- st.Hour = (short)t.Hour;
- st.Minute = (short)t.Minute;
- st.Second = (short)t.Second;
- return st;
- }
- }
- }
|