public DbSyncDateTimeService(ICuriosityDataContextFactory dataContextFactor) { if (dataContextFactor == null) { throw new ArgumentNullException(nameof(dataContextFactor)); } _dataContextFactory = dataContextFactor ?? throw new ArgumentNullException(nameof(dataContextFactor)); _timeCorrectionParams = TimeCorrectionParams.ForEqual(); }
/// <summary> /// Inits service on demand. /// </summary> /// <returns></returns> public async Task InitAsync(CancellationToken cancellationToken = default) { // don't consider request time to DB // we don't need high accuracy using (var context = _dataContextFactory.CreateContext()) { var serverTime = await context.GetImmediateServerTimeUtcAsync(cancellationToken); var localTime = System.DateTime.UtcNow; if (serverTime > localTime) { _timeCorrectionParams = TimeCorrectionParams.ForBigger(serverTime - localTime); } else if (serverTime < localTime) { _timeCorrectionParams = TimeCorrectionParams.ForSmaller(localTime - serverTime); } else { _timeCorrectionParams = TimeCorrectionParams.ForEqual(); } } }