2019-10-08 05:48:49 +02:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Time.Clock;
|
|
|
|
|
using Ryujinx.HLE.Utilities;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
using System.IO;
|
2019-09-19 02:45:11 +02:00
|
|
|
|
using static Ryujinx.HLE.HOS.Services.Time.TimeZone.TimeZoneRule;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
private TimeZoneRule _myRules;
|
|
|
|
|
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();
|
2019-07-04 17:20:40 +02:00
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
// Empty rules
|
2019-07-04 17:20:40 +02:00
|
|
|
|
_myRules = new TimeZoneRule
|
|
|
|
|
{
|
|
|
|
|
Ats = new long[TzMaxTimes],
|
|
|
|
|
Types = new byte[TzMaxTimes],
|
|
|
|
|
Ttis = new TimeTypeInfo[TzMaxTypes],
|
|
|
|
|
Chars = new char[TzCharsArraySize]
|
|
|
|
|
};
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
bool timeZoneConversionSuccess = TimeZone.ParseTimeZoneBinary(out TimeZoneRule rules, timeZoneBinaryStream);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
public ResultCode ParseTimeZoneRuleBinary(out 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)
|
|
|
|
|
{
|
|
|
|
|
bool timeZoneConversionSuccess = TimeZone.ParseTimeZoneBinary(out 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
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
result = ToCalendarTime(_myRules, time, out calendar);
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
public ResultCode ToCalendarTime(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
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
result = TimeZone.ToCalendarTime(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)
|
|
|
|
|
{
|
|
|
|
|
result = ToPosixTime(_myRules, calendarTime, out posixTime);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
posixTime = 0;
|
|
|
|
|
result = ResultCode.UninitializedClock;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2019-07-04 17:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
|
public ResultCode ToPosixTime(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
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
|
result = TimeZone.ToPosixTime(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
|
|
|
|
}
|