WayneSystemTime.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace Wayne.Lib
  4. {
  5. /// <summary>
  6. /// System time structure for Win32.
  7. /// </summary>
  8. [StructLayout(LayoutKind.Sequential)]
  9. public struct SystemTime
  10. {
  11. [MarshalAs(UnmanagedType.U2)]
  12. public short Year;
  13. [MarshalAs(UnmanagedType.U2)]
  14. public short Month;
  15. [MarshalAs(UnmanagedType.U2)]
  16. public short DayOfWeek;
  17. [MarshalAs(UnmanagedType.U2)]
  18. public short Day;
  19. [MarshalAs(UnmanagedType.U2)]
  20. public short Hour;
  21. [MarshalAs(UnmanagedType.U2)]
  22. public short Minute;
  23. [MarshalAs(UnmanagedType.U2)]
  24. public short Second;
  25. [MarshalAs(UnmanagedType.U2)]
  26. public short Milliseconds;
  27. }
  28. /// <summary>
  29. /// System time proxy, should be used instead of DateTime.Now, and injected to the class,
  30. /// so the current time can be mocked in unit tests.
  31. ///
  32. /// Added routine to set system time, this seemed as good a place as any to put it. (JHC)
  33. /// </summary>
  34. public class WayneSystemTime : ISystemTime
  35. {
  36. /// <summary>
  37. /// The current Date and time.
  38. /// </summary>
  39. public DateTime Now
  40. {
  41. get { return DateTime.Now; }
  42. set { SetSystemTime(value); }
  43. }
  44. [DllImport("kernel32.dll")]
  45. public static extern bool SetSystemTime(ref SystemTime systemTime);
  46. [DllImport("kernel32.dll")]
  47. public static extern bool SetLocalTime(ref SystemTime systemTime);
  48. ///<summary>
  49. /// Sets the system time
  50. ///</summary>
  51. ///<param name="newTime"></param>
  52. ///<exception cref="Exception"></exception>
  53. public void SetSystemTime(DateTime newTime)
  54. {
  55. SystemTime st = ConvertToSystemTime(newTime);
  56. bool rc = SetSystemTime(ref st);
  57. if (!rc)
  58. {
  59. int wrc = Marshal.GetLastWin32Error();
  60. throw new Exception("Error " + wrc + " setting system time");
  61. }
  62. }
  63. /// <summary>
  64. /// Set local time
  65. /// </summary>
  66. /// <param name="newTime"></param>
  67. public void SetLocalTime(DateTime newTime)
  68. {
  69. // Microsoft.VisualBasic.DateAndTime.TimeOfDay = DateTime.SpecifyKind(newTime, DateTimeKind.Local);
  70. SystemTime st = ConvertToSystemTime(newTime);
  71. bool rc = SetLocalTime(ref st);
  72. if (!rc)
  73. {
  74. int wrc = Marshal.GetLastWin32Error();
  75. throw new Exception("Error " + wrc + " setting local time");
  76. }
  77. }
  78. private static SystemTime ConvertToSystemTime(DateTime t)
  79. {
  80. SystemTime st = new SystemTime();
  81. st.Year = (short) t.Year;
  82. st.Month = (short) t.Month;
  83. st.Day = (short) t.Day;
  84. st.Hour = (short) t.Hour;
  85. st.Minute = (short) t.Minute;
  86. st.Second = (short) t.Second;
  87. return st;
  88. }
  89. }
  90. }