Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #518 from cocoa-mhlw/hot-fix-517
Browse files Browse the repository at this point in the history
Hot fix 517
  • Loading branch information
cocoa-dev004 authored Nov 25, 2021
2 parents 3252946 + 0b3f7ed commit fa055dd
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions Covid19Radar/Covid19Radar/Services/Migration/Migrator_1_3_0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal class Migrator_1_3_0
private const string TERMS_OF_SERVICE_LAST_UPDATE_DATETIME = "TermsOfServiceLastUpdateDateTime";
private const string PRIVACY_POLICY_LAST_UPDATE_DATETIME = "PrivacyPolicyLastUpdateDateTime";

private readonly DateTime FALLBACK_DATETIME = new DateTime(2020, 6, 19);
private readonly TimeSpan TIME_DIFFERENCIAL_JST_UTC = TimeSpan.FromHours(+9);

private readonly IPreferencesService _preferencesService;
Expand All @@ -33,15 +34,16 @@ public Task ExecuteAsync()
{
if (_preferencesService.ContainsKey(START_DATETIME))
{
MigrateDateTimeToEpoch(START_DATETIME, PreferenceKey.StartDateTimeEpoch, TimeSpan.Zero);
MigrateDateTimeToEpoch(START_DATETIME, PreferenceKey.StartDateTimeEpoch, TimeSpan.Zero, DateTime.UtcNow);
}

if (_preferencesService.ContainsKey(TERMS_OF_SERVICE_LAST_UPDATE_DATETIME))
{
MigrateDateTimeToEpoch(
TERMS_OF_SERVICE_LAST_UPDATE_DATETIME,
PreferenceKey.TermsOfServiceLastUpdateDateTimeEpoch,
-TIME_DIFFERENCIAL_JST_UTC
-TIME_DIFFERENCIAL_JST_UTC,
FALLBACK_DATETIME
);
}

Expand All @@ -50,27 +52,38 @@ public Task ExecuteAsync()
MigrateDateTimeToEpoch(
PRIVACY_POLICY_LAST_UPDATE_DATETIME,
PreferenceKey.PrivacyPolicyLastUpdateDateTimeEpoch,
-TIME_DIFFERENCIAL_JST_UTC
-TIME_DIFFERENCIAL_JST_UTC,
FALLBACK_DATETIME
);
}

return Task.CompletedTask;
}

private void MigrateDateTimeToEpoch(string dateTimeKey, string epochKey, TimeSpan differential)
private void MigrateDateTimeToEpoch(string dateTimeKey, string epochKey, TimeSpan differential, DateTime fallbackDateTime)
{
string dateTimeStr = _preferencesService.GetValue(dateTimeKey, DateTime.UtcNow.ToString());
string dateTimeStr = _preferencesService.GetValue(dateTimeKey, fallbackDateTime.ToString());

DateTime dateTime;
try
{
dateTime = DateTime.SpecifyKind(DateTime.Parse(dateTimeStr) + differential, DateTimeKind.Utc);
dateTime = DateTime.SpecifyKind(DateTime.Parse(dateTimeStr), DateTimeKind.Utc);
}
catch (FormatException exception)
{
_loggerService.Exception($"Parse dateTime FormatException occurred. {dateTimeStr}", exception);
dateTime = DateTime.UtcNow;
dateTime = fallbackDateTime;
}

try
{
dateTime += differential;
}
catch (ArgumentOutOfRangeException exception)
{
_loggerService.Exception($"{dateTimeStr} {differential} The added or subtracted value results in an un-representable DateTime.", exception);
}

_preferencesService.SetValue(epochKey, dateTime.ToUnixEpoch());
_preferencesService.RemoveValue(dateTimeKey);
}
Expand Down

0 comments on commit fa055dd

Please sign in to comment.