2022-06-24 19:04:57 +02:00
|
|
|
|
using Ryujinx.Common.Memory;
|
|
|
|
|
using Ryujinx.HLE.HOS.Services.Time.Clock;
|
2022-11-09 20:22:43 +01:00
|
|
|
|
using System;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.Time.TimeZone
|
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
class TimeZoneManager
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
private bool _isInitialized;
|
2022-06-24 19:04:57 +02:00
|
|
|
|
private Box<TimeZoneRule> _myRules;
|
2019-10-08 05:48:49 +02:00
|
|
|
|
private string _deviceLocationName;
|
|
|
|
|
private UInt128 _timeZoneRuleVersion;
|
|
|
|
|
private uint _totalLocationNameCount;
|
|
|
|
|
private SteadyClockTimePoint _timeZoneUpdateTimePoint;
|
|
|
|
|
private object _lock;
|
|
|
|
|
|
|
|
|
|
public TimeZoneManager()
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
_isInitialized = false;
|
|
|
|
|
_deviceLocationName = "UTC";
|
|
|
|
|
_timeZoneRuleVersion = new UInt128();
|
|
|
|
|
_lock = new object();
|
2022-06-24 19:04:57 +02:00
|
|
|
|
_myRules = new Box<TimeZoneRule>();
|
2019-07-04 17:20:40 +02:00
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
_timeZoneUpdateTimePoint = SteadyClockTimePoint.GetRandom();
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
public bool IsInitialized()
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
bool res;
|
|
|
|
|
|
|
|
|
|
lock (_lock)
|
|
|
|
|
{
|
|
|
|
|
res = _isInitialized;
|
|
|
|
|
}
|
2019-07-04 17:20:40 +02:00
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
return res;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
public void MarkInitialized()
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
lock (_lock)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
_isInitialized = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-04 17:20:40 +02:00
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
public ResultCode GetDeviceLocationName(out string deviceLocationName)
|
|
|
|
|
{
|
|
|
|
|
ResultCode result = ResultCode.UninitializedClock;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
deviceLocationName = null;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
lock (_lock)
|
|
|
|
|
{
|
|
|
|
|
if (_isInitialized)
|
|
|
|
|
{
|
|
|
|
|
deviceLocationName = _deviceLocationName;
|
|
|
|
|
result = ResultCode.Success;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2019-07-04 17:20:40 +02:00
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
public ResultCode SetDeviceLocationNameWithTimeZoneRule(string locationName, Stream timeZoneBinaryStream)
|
|
|
|
|
{
|
|
|
|
|
ResultCode result = ResultCode.TimeZoneConversionFailed;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
lock (_lock)
|
|
|
|
|
{
|
2022-06-24 19:04:57 +02:00
|
|
|
|
Box<TimeZoneRule> rules = new Box<TimeZoneRule>();
|
|
|
|
|
|
|
|
|
|
bool timeZoneConversionSuccess = TimeZone.ParseTimeZoneBinary(ref rules.Data, timeZoneBinaryStream);
|
2019-10-08 05:48:49 +02:00
|
|
|
|
|
|
|
|
|
if (timeZoneConversionSuccess)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
_deviceLocationName = locationName;
|
|
|
|
|
_myRules = rules;
|
|
|
|
|
result = ResultCode.Success;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
2019-10-08 05:48:49 +02:00
|
|
|
|
}
|
2019-07-04 17:20:40 +02:00
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetTotalLocationNameCount(uint totalLocationNameCount)
|
|
|
|
|
{
|
|
|
|
|
lock (_lock)
|
|
|
|
|
{
|
|
|
|
|
_totalLocationNameCount = totalLocationNameCount;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
public ResultCode GetTotalLocationNameCount(out uint totalLocationNameCount)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
ResultCode result = ResultCode.UninitializedClock;
|
|
|
|
|
|
|
|
|
|
totalLocationNameCount = 0;
|
|
|
|
|
|
|
|
|
|
lock (_lock)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
if (_isInitialized)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
totalLocationNameCount = _totalLocationNameCount;
|
|
|
|
|
result = ResultCode.Success;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
return result;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
public ResultCode SetUpdatedTime(SteadyClockTimePoint timeZoneUpdatedTimePoint, bool bypassUninitialized = false)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
ResultCode result = ResultCode.UninitializedClock;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
lock (_lock)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
if (_isInitialized || bypassUninitialized)
|
|
|
|
|
{
|
|
|
|
|
_timeZoneUpdateTimePoint = timeZoneUpdatedTimePoint;
|
|
|
|
|
result = ResultCode.Success;
|
|
|
|
|
}
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
return result;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
public ResultCode GetUpdatedTime(out SteadyClockTimePoint timeZoneUpdatedTimePoint)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
ResultCode result;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
lock (_lock)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
if (_isInitialized)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
timeZoneUpdatedTimePoint = _timeZoneUpdateTimePoint;
|
|
|
|
|
result = ResultCode.Success;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
2019-10-08 05:48:49 +02:00
|
|
|
|
else
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
timeZoneUpdatedTimePoint = SteadyClockTimePoint.GetRandom();
|
|
|
|
|
result = ResultCode.UninitializedClock;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
return result;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 19:04:57 +02:00
|
|
|
|
public ResultCode ParseTimeZoneRuleBinary(ref TimeZoneRule outRules, Stream timeZoneBinaryStream)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
ResultCode result = ResultCode.Success;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
lock (_lock)
|
|
|
|
|
{
|
2022-06-24 19:04:57 +02:00
|
|
|
|
bool timeZoneConversionSuccess = TimeZone.ParseTimeZoneBinary(ref outRules, timeZoneBinaryStream);
|
2019-07-04 17:20:40 +02:00
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
if (!timeZoneConversionSuccess)
|
|
|
|
|
{
|
|
|
|
|
result = ResultCode.TimeZoneConversionFailed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
public void SetTimeZoneRuleVersion(UInt128 timeZoneRuleVersion)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
lock (_lock)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
_timeZoneRuleVersion = timeZoneRuleVersion;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
2019-10-08 05:48:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ResultCode GetTimeZoneRuleVersion(out UInt128 timeZoneRuleVersion)
|
|
|
|
|
{
|
|
|
|
|
ResultCode result;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
lock (_lock)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
if (_isInitialized)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
timeZoneRuleVersion = _timeZoneRuleVersion;
|
|
|
|
|
result = ResultCode.Success;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
2019-10-08 05:48:49 +02:00
|
|
|
|
else
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
timeZoneRuleVersion = new UInt128();
|
|
|
|
|
result = ResultCode.UninitializedClock;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-08 05:48:49 +02:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ResultCode ToCalendarTimeWithMyRules(long time, out CalendarInfo calendar)
|
|
|
|
|
{
|
|
|
|
|
ResultCode result;
|
|
|
|
|
|
|
|
|
|
lock (_lock)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
if (_isInitialized)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2022-06-24 19:04:57 +02:00
|
|
|
|
result = ToCalendarTime(in _myRules.Data, time, out calendar);
|
2019-10-08 05:48:49 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
calendar = new CalendarInfo();
|
|
|
|
|
result = ResultCode.UninitializedClock;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
return result;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 19:04:57 +02:00
|
|
|
|
public ResultCode ToCalendarTime(in TimeZoneRule rules, long time, out CalendarInfo calendar)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
ResultCode result;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
lock (_lock)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2022-06-24 19:04:57 +02:00
|
|
|
|
result = TimeZone.ToCalendarTime(in rules, time, out calendar);
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
return result;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
public ResultCode ToPosixTimeWithMyRules(CalendarTime calendarTime, out long posixTime)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
ResultCode result;
|
|
|
|
|
|
|
|
|
|
lock (_lock)
|
|
|
|
|
{
|
|
|
|
|
if (_isInitialized)
|
|
|
|
|
{
|
2022-06-24 19:04:57 +02:00
|
|
|
|
result = ToPosixTime(in _myRules.Data, calendarTime, out posixTime);
|
2019-10-08 05:48:49 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
posixTime = 0;
|
|
|
|
|
result = ResultCode.UninitializedClock;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 19:04:57 +02:00
|
|
|
|
public ResultCode ToPosixTime(in TimeZoneRule rules, CalendarTime calendarTime, out long posixTime)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
ResultCode result;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
lock (_lock)
|
2019-07-04 17:20:40 +02:00
|
|
|
|
{
|
2022-06-24 19:04:57 +02:00
|
|
|
|
result = TimeZone.ToPosixTime(in rules, calendarTime, out posixTime);
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
return result;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-08 05:48:49 +02:00
|
|
|
|
}
|