TimeZoneManager.cs 7.3 KB

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