| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- using Ryujinx.Common.Memory;
- using Ryujinx.HLE.HOS.Services.Time.Clock;
- using System;
- using System.IO;
- namespace Ryujinx.HLE.HOS.Services.Time.TimeZone
- {
- class TimeZoneManager
- {
- private bool _isInitialized;
- private Box<TimeZoneRule> _myRules;
- private string _deviceLocationName;
- private UInt128 _timeZoneRuleVersion;
- private uint _totalLocationNameCount;
- private SteadyClockTimePoint _timeZoneUpdateTimePoint;
- private object _lock;
- public TimeZoneManager()
- {
- _isInitialized = false;
- _deviceLocationName = "UTC";
- _timeZoneRuleVersion = new UInt128();
- _lock = new object();
- _myRules = new Box<TimeZoneRule>();
- _timeZoneUpdateTimePoint = SteadyClockTimePoint.GetRandom();
- }
- public bool IsInitialized()
- {
- bool res;
- lock (_lock)
- {
- res = _isInitialized;
- }
- return res;
- }
- public void MarkInitialized()
- {
- lock (_lock)
- {
- _isInitialized = true;
- }
- }
- public ResultCode GetDeviceLocationName(out string deviceLocationName)
- {
- ResultCode result = ResultCode.UninitializedClock;
- deviceLocationName = null;
- lock (_lock)
- {
- if (_isInitialized)
- {
- deviceLocationName = _deviceLocationName;
- result = ResultCode.Success;
- }
- }
- return result;
- }
- public ResultCode SetDeviceLocationNameWithTimeZoneRule(string locationName, Stream timeZoneBinaryStream)
- {
- ResultCode result = ResultCode.TimeZoneConversionFailed;
- lock (_lock)
- {
- Box<TimeZoneRule> rules = new Box<TimeZoneRule>();
- bool timeZoneConversionSuccess = TimeZone.ParseTimeZoneBinary(ref rules.Data, timeZoneBinaryStream);
- if (timeZoneConversionSuccess)
- {
- _deviceLocationName = locationName;
- _myRules = rules;
- result = ResultCode.Success;
- }
- }
- return result;
- }
- public void SetTotalLocationNameCount(uint totalLocationNameCount)
- {
- lock (_lock)
- {
- _totalLocationNameCount = totalLocationNameCount;
- }
- }
- public ResultCode GetTotalLocationNameCount(out uint totalLocationNameCount)
- {
- ResultCode result = ResultCode.UninitializedClock;
- totalLocationNameCount = 0;
- lock (_lock)
- {
- if (_isInitialized)
- {
- totalLocationNameCount = _totalLocationNameCount;
- result = ResultCode.Success;
- }
- }
- return result;
- }
- public ResultCode SetUpdatedTime(SteadyClockTimePoint timeZoneUpdatedTimePoint, bool bypassUninitialized = false)
- {
- ResultCode result = ResultCode.UninitializedClock;
- lock (_lock)
- {
- if (_isInitialized || bypassUninitialized)
- {
- _timeZoneUpdateTimePoint = timeZoneUpdatedTimePoint;
- result = ResultCode.Success;
- }
- }
- return result;
- }
- public ResultCode GetUpdatedTime(out SteadyClockTimePoint timeZoneUpdatedTimePoint)
- {
- ResultCode result;
- lock (_lock)
- {
- if (_isInitialized)
- {
- timeZoneUpdatedTimePoint = _timeZoneUpdateTimePoint;
- result = ResultCode.Success;
- }
- else
- {
- timeZoneUpdatedTimePoint = SteadyClockTimePoint.GetRandom();
- result = ResultCode.UninitializedClock;
- }
- }
- return result;
- }
- public ResultCode ParseTimeZoneRuleBinary(ref TimeZoneRule outRules, Stream timeZoneBinaryStream)
- {
- ResultCode result = ResultCode.Success;
- lock (_lock)
- {
- bool timeZoneConversionSuccess = TimeZone.ParseTimeZoneBinary(ref outRules, timeZoneBinaryStream);
- if (!timeZoneConversionSuccess)
- {
- result = ResultCode.TimeZoneConversionFailed;
- }
- }
- return result;
- }
- public void SetTimeZoneRuleVersion(UInt128 timeZoneRuleVersion)
- {
- lock (_lock)
- {
- _timeZoneRuleVersion = timeZoneRuleVersion;
- }
- }
- public ResultCode GetTimeZoneRuleVersion(out UInt128 timeZoneRuleVersion)
- {
- ResultCode result;
- lock (_lock)
- {
- if (_isInitialized)
- {
- timeZoneRuleVersion = _timeZoneRuleVersion;
- result = ResultCode.Success;
- }
- else
- {
- timeZoneRuleVersion = new UInt128();
- result = ResultCode.UninitializedClock;
- }
- }
- return result;
- }
- public ResultCode ToCalendarTimeWithMyRules(long time, out CalendarInfo calendar)
- {
- ResultCode result;
- lock (_lock)
- {
- if (_isInitialized)
- {
- result = ToCalendarTime(in _myRules.Data, time, out calendar);
- }
- else
- {
- calendar = new CalendarInfo();
- result = ResultCode.UninitializedClock;
- }
- }
- return result;
- }
- public ResultCode ToCalendarTime(in TimeZoneRule rules, long time, out CalendarInfo calendar)
- {
- ResultCode result;
- lock (_lock)
- {
- result = TimeZone.ToCalendarTime(in rules, time, out calendar);
- }
- return result;
- }
- public ResultCode ToPosixTimeWithMyRules(CalendarTime calendarTime, out long posixTime)
- {
- ResultCode result;
- lock (_lock)
- {
- if (_isInitialized)
- {
- result = ToPosixTime(in _myRules.Data, calendarTime, out posixTime);
- }
- else
- {
- posixTime = 0;
- result = ResultCode.UninitializedClock;
- }
- }
- return result;
- }
- public ResultCode ToPosixTime(in TimeZoneRule rules, CalendarTime calendarTime, out long posixTime)
- {
- ResultCode result;
- lock (_lock)
- {
- result = TimeZone.ToPosixTime(in rules, calendarTime, out posixTime);
- }
- return result;
- }
- }
- }
|