WayneSystemTime.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. internal 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. static extern bool SetSystemTime(
  46. ref SystemTime systemTime);
  47. [DllImport("kernel32.dll")]
  48. static extern bool SetLocalTime(
  49. ref SystemTime systemTime);
  50. ///<summary>
  51. /// Sets the system time
  52. ///</summary>
  53. ///<param name="newTime"></param>
  54. ///<exception cref="Exception"></exception>
  55. public void SetSystemTime(DateTime newTime)
  56. {
  57. SystemTime st = ConvertToSystemTime(newTime.ToUniversalTime());
  58. bool rc = SetSystemTime(ref st);
  59. if (!rc)
  60. {
  61. int wrc = Marshal.GetLastWin32Error();
  62. throw new Exception("Error " + wrc + " setting system time");
  63. }
  64. }
  65. /// <summary>
  66. /// Set local time
  67. /// </summary>
  68. /// <param name="newTime"></param>
  69. public void SetLocalTime(DateTime newTime)
  70. {
  71. // Microsoft.VisualBasic.DateAndTime.TimeOfDay = DateTime.SpecifyKind(newTime, DateTimeKind.Local);
  72. SystemTime st = ConvertToSystemTime(newTime);
  73. bool rc = SetLocalTime(ref st);
  74. if (!rc)
  75. {
  76. int wrc = Marshal.GetLastWin32Error();
  77. throw new Exception("Error " + wrc + " setting local time");
  78. }
  79. }
  80. internal static SystemTime ConvertToSystemTime(DateTime t)
  81. {
  82. SystemTime st = new SystemTime();
  83. st.Year = (short)t.Year;
  84. st.Month = (short)t.Month;
  85. st.Day = (short)t.Day;
  86. st.Hour = (short)t.Hour;
  87. st.Minute = (short)t.Minute;
  88. st.Second = (short)t.Second;
  89. return st;
  90. }
  91. }
  92. }