SystemClock.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System.ComponentModel;
  3. using System.Threading;
  4. namespace System.Reactive.PlatformServices
  5. {
  6. /// <summary>
  7. /// (Infrastructure) Provides access to local system clock services.
  8. /// </summary>
  9. /// <remarks>
  10. /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
  11. /// No guarantees are made about forward compatibility of the type's functionality and its usage.
  12. /// </remarks>
  13. [EditorBrowsable(EditorBrowsableState.Never)]
  14. public static class SystemClock
  15. {
  16. private static Lazy<ISystemClock> s_serviceSystemClock = new Lazy<ISystemClock>(InitializeSystemClock);
  17. private static Lazy<INotifySystemClockChanged> s_serviceSystemClockChanged = new Lazy<INotifySystemClockChanged>(InitializeSystemClockChanged);
  18. private static int _refCount;
  19. /// <summary>
  20. /// Gets the local system clock time.
  21. /// </summary>
  22. public static DateTimeOffset UtcNow
  23. {
  24. get { return s_serviceSystemClock.Value.UtcNow; }
  25. }
  26. /// <summary>
  27. /// Event that gets raised when a system clock change is detected, if there's any interest as indicated by AddRef calls.
  28. /// </summary>
  29. public static event EventHandler<SystemClockChangedEventArgs> SystemClockChanged;
  30. /// <summary>
  31. /// Adds a reference to the system clock monitor, causing it to be sending notifications.
  32. /// </summary>
  33. /// <exception cref="NotSupportedException">Thrown when the system doesn't support sending clock change notifications.</exception>
  34. public static void AddRef()
  35. {
  36. if (Interlocked.Increment(ref _refCount) == 1)
  37. {
  38. s_serviceSystemClockChanged.Value.SystemClockChanged += OnSystemClockChanged;
  39. }
  40. }
  41. /// <summary>
  42. /// Removes a reference to the system clock monitor, causing it to stop sending notifications
  43. /// if the removed reference was the last one.
  44. /// </summary>
  45. public static void Release()
  46. {
  47. if (Interlocked.Decrement(ref _refCount) == 0)
  48. {
  49. s_serviceSystemClockChanged.Value.SystemClockChanged -= OnSystemClockChanged;
  50. }
  51. }
  52. private static void OnSystemClockChanged(object sender, SystemClockChangedEventArgs e)
  53. {
  54. var scc = SystemClockChanged;
  55. if (scc != null)
  56. scc(sender, e);
  57. }
  58. private static ISystemClock InitializeSystemClock()
  59. {
  60. return PlatformEnlightenmentProvider.Current.GetService<ISystemClock>() ?? new DefaultSystemClock();
  61. }
  62. private static INotifySystemClockChanged InitializeSystemClockChanged()
  63. {
  64. return PlatformEnlightenmentProvider.Current.GetService<INotifySystemClockChanged>() ?? new DefaultSystemClockMonitor();
  65. }
  66. }
  67. /// <summary>
  68. /// (Infrastructure) Provides access to the local system clock.
  69. /// </summary>
  70. /// <remarks>
  71. /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
  72. /// No guarantees are made about forward compatibility of the type's functionality and its usage.
  73. /// </remarks>
  74. [EditorBrowsable(EditorBrowsableState.Never)]
  75. public interface ISystemClock
  76. {
  77. /// <summary>
  78. /// Gets the current time.
  79. /// </summary>
  80. DateTimeOffset UtcNow { get; }
  81. }
  82. /// <summary>
  83. /// (Infrastructure) Provides a mechanism to notify local schedulers about system clock changes.
  84. /// </summary>
  85. /// <remarks>
  86. /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
  87. /// No guarantees are made about forward compatibility of the type's functionality and its usage.
  88. /// </remarks>
  89. [EditorBrowsable(EditorBrowsableState.Never)]
  90. public interface INotifySystemClockChanged
  91. {
  92. /// <summary>
  93. /// Event that gets raised when a system clock change is detected.
  94. /// </summary>
  95. event EventHandler<SystemClockChangedEventArgs> SystemClockChanged;
  96. }
  97. /// <summary>
  98. /// (Infrastructure) Event arguments for system clock change notifications.
  99. /// </summary>
  100. /// <remarks>
  101. /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
  102. /// No guarantees are made about forward compatibility of the type's functionality and its usage.
  103. /// </remarks>
  104. [EditorBrowsable(EditorBrowsableState.Never)]
  105. public class SystemClockChangedEventArgs : EventArgs
  106. {
  107. /// <summary>
  108. /// Creates a new system clock notification object with unknown old and new times.
  109. /// </summary>
  110. public SystemClockChangedEventArgs()
  111. : this(DateTimeOffset.MinValue, DateTimeOffset.MaxValue)
  112. {
  113. }
  114. /// <summary>
  115. /// Creates a new system clock notification object with the specified old and new times.
  116. /// </summary>
  117. /// <param name="oldTime">Time before the system clock changed, or DateTimeOffset.MinValue if not known.</param>
  118. /// <param name="newTime">Time after the system clock changed, or DateTimeOffset.MaxValue if not known.</param>
  119. public SystemClockChangedEventArgs(DateTimeOffset oldTime, DateTimeOffset newTime)
  120. {
  121. OldTime = oldTime;
  122. NewTime = newTime;
  123. }
  124. /// <summary>
  125. /// Gets the time before the system clock changed, or DateTimeOffset.MinValue if not known.
  126. /// </summary>
  127. public DateTimeOffset OldTime { get; private set; }
  128. /// <summary>
  129. /// Gets the time after the system clock changed, or DateTimeOffset.MaxValue if not known.
  130. /// </summary>
  131. public DateTimeOffset NewTime { get; private set; }
  132. }
  133. }