TimeZoneManager.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using Ryujinx.HLE.HOS.Services.Time.Clock;
  2. using Ryujinx.HLE.Utilities;
  3. using System.IO;
  4. using static Ryujinx.HLE.HOS.Services.Time.TimeZone.TimeZoneRule;
  5. namespace Ryujinx.HLE.HOS.Services.Time.TimeZone
  6. {
  7. class TimeZoneManager
  8. {
  9. private bool _isInitialized;
  10. private TimeZoneRule _myRules;
  11. private string _deviceLocationName;
  12. private UInt128 _timeZoneRuleVersion;
  13. private uint _totalLocationNameCount;
  14. private SteadyClockTimePoint _timeZoneUpdateTimePoint;
  15. private object _lock;
  16. public TimeZoneManager()
  17. {
  18. _isInitialized = false;
  19. _deviceLocationName = "UTC";
  20. _timeZoneRuleVersion = new UInt128();
  21. _lock = new object();
  22. // Empty rules
  23. _myRules = new TimeZoneRule
  24. {
  25. Ats = new long[TzMaxTimes],
  26. Types = new byte[TzMaxTimes],
  27. Ttis = new TimeTypeInfo[TzMaxTypes],
  28. Chars = new char[TzCharsArraySize]
  29. };
  30. _timeZoneUpdateTimePoint = SteadyClockTimePoint.GetRandom();
  31. }
  32. public bool IsInitialized()
  33. {
  34. bool res;
  35. lock (_lock)
  36. {
  37. res = _isInitialized;
  38. }
  39. return res;
  40. }
  41. public void MarkInitialized()
  42. {
  43. lock (_lock)
  44. {
  45. _isInitialized = true;
  46. }
  47. }
  48. public ResultCode GetDeviceLocationName(out string deviceLocationName)
  49. {
  50. ResultCode result = ResultCode.UninitializedClock;
  51. deviceLocationName = null;
  52. lock (_lock)
  53. {
  54. if (_isInitialized)
  55. {
  56. deviceLocationName = _deviceLocationName;
  57. result = ResultCode.Success;
  58. }
  59. }
  60. return result;
  61. }
  62. public ResultCode SetDeviceLocationNameWithTimeZoneRule(string locationName, Stream timeZoneBinaryStream)
  63. {
  64. ResultCode result = ResultCode.TimeZoneConversionFailed;
  65. lock (_lock)
  66. {
  67. bool timeZoneConversionSuccess = TimeZone.ParseTimeZoneBinary(out TimeZoneRule rules, timeZoneBinaryStream);
  68. if (timeZoneConversionSuccess)
  69. {
  70. _deviceLocationName = locationName;
  71. _myRules = rules;
  72. result = ResultCode.Success;
  73. }
  74. }
  75. return result;
  76. }
  77. public void SetTotalLocationNameCount(uint totalLocationNameCount)
  78. {
  79. lock (_lock)
  80. {
  81. _totalLocationNameCount = totalLocationNameCount;
  82. }
  83. }
  84. public ResultCode GetTotalLocationNameCount(out uint totalLocationNameCount)
  85. {
  86. ResultCode result = ResultCode.UninitializedClock;
  87. totalLocationNameCount = 0;
  88. lock (_lock)
  89. {
  90. if (_isInitialized)
  91. {
  92. totalLocationNameCount = _totalLocationNameCount;
  93. result = ResultCode.Success;
  94. }
  95. }
  96. return result;
  97. }
  98. public ResultCode SetUpdatedTime(SteadyClockTimePoint timeZoneUpdatedTimePoint, bool bypassUninitialized = false)
  99. {
  100. ResultCode result = ResultCode.UninitializedClock;
  101. lock (_lock)
  102. {
  103. if (_isInitialized || bypassUninitialized)
  104. {
  105. _timeZoneUpdateTimePoint = timeZoneUpdatedTimePoint;
  106. result = ResultCode.Success;
  107. }
  108. }
  109. return result;
  110. }
  111. public ResultCode GetUpdatedTime(out SteadyClockTimePoint timeZoneUpdatedTimePoint)
  112. {
  113. ResultCode result;
  114. lock (_lock)
  115. {
  116. if (_isInitialized)
  117. {
  118. timeZoneUpdatedTimePoint = _timeZoneUpdateTimePoint;
  119. result = ResultCode.Success;
  120. }
  121. else
  122. {
  123. timeZoneUpdatedTimePoint = SteadyClockTimePoint.GetRandom();
  124. result = ResultCode.UninitializedClock;
  125. }
  126. }
  127. return result;
  128. }
  129. public ResultCode ParseTimeZoneRuleBinary(out TimeZoneRule outRules, Stream timeZoneBinaryStream)
  130. {
  131. ResultCode result = ResultCode.Success;
  132. lock (_lock)
  133. {
  134. bool timeZoneConversionSuccess = TimeZone.ParseTimeZoneBinary(out outRules, timeZoneBinaryStream);
  135. if (!timeZoneConversionSuccess)
  136. {
  137. result = ResultCode.TimeZoneConversionFailed;
  138. }
  139. }
  140. return result;
  141. }
  142. public void SetTimeZoneRuleVersion(UInt128 timeZoneRuleVersion)
  143. {
  144. lock (_lock)
  145. {
  146. _timeZoneRuleVersion = timeZoneRuleVersion;
  147. }
  148. }
  149. public ResultCode GetTimeZoneRuleVersion(out UInt128 timeZoneRuleVersion)
  150. {
  151. ResultCode result;
  152. lock (_lock)
  153. {
  154. if (_isInitialized)
  155. {
  156. timeZoneRuleVersion = _timeZoneRuleVersion;
  157. result = ResultCode.Success;
  158. }
  159. else
  160. {
  161. timeZoneRuleVersion = new UInt128();
  162. result = ResultCode.UninitializedClock;
  163. }
  164. }
  165. return result;
  166. }
  167. public ResultCode ToCalendarTimeWithMyRules(long time, out CalendarInfo calendar)
  168. {
  169. ResultCode result;
  170. lock (_lock)
  171. {
  172. if (_isInitialized)
  173. {
  174. result = ToCalendarTime(_myRules, time, out calendar);
  175. }
  176. else
  177. {
  178. calendar = new CalendarInfo();
  179. result = ResultCode.UninitializedClock;
  180. }
  181. }
  182. return result;
  183. }
  184. public ResultCode ToCalendarTime(TimeZoneRule rules, long time, out CalendarInfo calendar)
  185. {
  186. ResultCode result;
  187. lock (_lock)
  188. {
  189. result = TimeZone.ToCalendarTime(rules, time, out calendar);
  190. }
  191. return result;
  192. }
  193. public ResultCode ToPosixTimeWithMyRules(CalendarTime calendarTime, out long posixTime)
  194. {
  195. ResultCode result;
  196. lock (_lock)
  197. {
  198. if (_isInitialized)
  199. {
  200. result = ToPosixTime(_myRules, calendarTime, out posixTime);
  201. }
  202. else
  203. {
  204. posixTime = 0;
  205. result = ResultCode.UninitializedClock;
  206. }
  207. }
  208. return result;
  209. }
  210. public ResultCode ToPosixTime(TimeZoneRule rules, CalendarTime calendarTime, out long posixTime)
  211. {
  212. ResultCode result;
  213. lock (_lock)
  214. {
  215. result = TimeZone.ToPosixTime(rules, calendarTime, out posixTime);
  216. }
  217. return result;
  218. }
  219. }
  220. }