Lean  $LEAN_TAG$
TimeKeeper.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using System.Collections.Generic;
18 using System.Linq;
19 using NodaTime;
21 
22 namespace QuantConnect
23 {
24  /// <summary>
25  /// Provides a means of centralizing time for various time zones.
26  /// </summary>
27  public class TimeKeeper : ITimeKeeper
28  {
29  private DateTime _utcDateTime;
30 
31  private readonly Dictionary<string, LocalTimeKeeper> _localTimeKeepers;
32 
33  /// <summary>
34  /// Gets the current time in UTC
35  /// </summary>
36  public DateTime UtcTime
37  {
38  get { return _utcDateTime; }
39  }
40 
41  /// <summary>
42  /// Initializes a new instance of the <see cref="TimeKeeper"/> class at the specified
43  /// UTC time and for the specified time zones. Each time zone specified will cause the
44  /// creation of a <see cref="LocalTimeKeeper"/> to handle conversions for that time zone.
45  /// </summary>
46  /// <param name="utcDateTime">The initial time</param>
47  /// <param name="timeZones">The time zones used to instantiate <see cref="LocalTimeKeeper"/> instances.</param>
48  public TimeKeeper(DateTime utcDateTime, params DateTimeZone[] timeZones)
49  : this(utcDateTime, timeZones ?? Enumerable.Empty<DateTimeZone>())
50  {
51  }
52 
53  /// <summary>
54  /// Initializes a new instance of the <see cref="TimeKeeper"/> class at the specified
55  /// UTC time and for the specified time zones. Each time zone specified will cause the
56  /// creation of a <see cref="LocalTimeKeeper"/> to handle conversions for that time zone.
57  /// </summary>
58  /// <param name="utcDateTime">The initial time</param>
59  /// <param name="timeZones">The time zones used to instantiate <see cref="LocalTimeKeeper"/> instances.</param>
60  public TimeKeeper(DateTime utcDateTime, IEnumerable<DateTimeZone> timeZones)
61  {
62  _utcDateTime = utcDateTime;
63  _localTimeKeepers = timeZones.Distinct().Select(x => new LocalTimeKeeper(utcDateTime, x)).ToDictionary(x => x.TimeZone.Id);
64  }
65 
66  /// <summary>
67  /// Sets the current UTC time for this time keeper and the attached child <see cref="LocalTimeKeeper"/> instances.
68  /// </summary>
69  /// <param name="utcDateTime">The current time in UTC</param>
70  public virtual void SetUtcDateTime(DateTime utcDateTime)
71  {
72  _utcDateTime = utcDateTime;
73  foreach (var timeZone in _localTimeKeepers)
74  {
75  timeZone.Value.UpdateTime(utcDateTime);
76  }
77  }
78 
79  /// <summary>
80  /// Gets the local time in the specified time zone. If the specified <see cref="DateTimeZone"/>
81  /// has not already been added, this will throw a <see cref="KeyNotFoundException"/>.
82  /// </summary>
83  /// <param name="timeZone">The time zone to get local time for</param>
84  /// <returns>The local time in the specifed time zone</returns>
85  public DateTime GetTimeIn(DateTimeZone timeZone)
86  {
87  return GetLocalTimeKeeper(timeZone).LocalTime;
88  }
89 
90  /// <summary>
91  /// Gets the <see cref="LocalTimeKeeper"/> instance for the specified time zone
92  /// </summary>
93  /// <param name="timeZone">The time zone whose <see cref="LocalTimeKeeper"/> we seek</param>
94  /// <returns>The <see cref="LocalTimeKeeper"/> instance for the specified time zone</returns>
95  public LocalTimeKeeper GetLocalTimeKeeper(DateTimeZone timeZone)
96  {
97  LocalTimeKeeper localTimeKeeper;
98  if (!_localTimeKeepers.TryGetValue(timeZone.Id, out localTimeKeeper))
99  {
100  localTimeKeeper = new LocalTimeKeeper(UtcTime, timeZone);
101  _localTimeKeepers[timeZone.Id] = localTimeKeeper;
102  }
103  return localTimeKeeper;
104  }
105 
106  /// <summary>
107  /// Adds the specified time zone to this time keeper
108  /// </summary>
109  /// <param name="timeZone"></param>
110  public void AddTimeZone(DateTimeZone timeZone)
111  {
112  if (!_localTimeKeepers.ContainsKey(timeZone.Id))
113  {
114  _localTimeKeepers[timeZone.Id] = new LocalTimeKeeper(_utcDateTime, timeZone);
115  }
116  }
117  }
118 }