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