Reputation: 11
I have a couple of Azure WebJobs which should fetch some entities from DB, when a given timestamp on the entity is passed. The problem is my timezone (GMT+1 - Denmark) where there is a 2 hour difference as of now (DST).
So the WebJobs are currently fetching 2 hours too late.
In the services I use DateTime.Now
as a marker for current time.
The timestamp in DB looks fine.
I need to figure out how to make them in sync, and preferably without having to rewrite the entire service to DateTime.UtcNow
instead.
I have added the WEBSITE_TIME_ZONE
application setting in Azure and set it to Romance Standard Time
, which should be correct and should fix the problem according to other posts, but this does not help.
C# code snippet from service:
var now = DateTime.Now;
var orderBatch = await db.OrderEntities
.Where(o => o.OrderStatus == (int)Status.Accepted)
.Where(o => o.ExecutionTime.HasValue && o.ExecutionTime < now)
.Where(o => (o.NextSendTry == null || o.NextSendTry < now))
.Select(o => new
{
OrderId = o.Id
})
.Take(100)
.ToListAsync();
Log snippet from WebJobs logs where the current time was 04:38 and not 02:38. There is one order with execution time to 03:00, so it should have been fetched, but was not until 05:00:
[06/29/2019 02:38:21 > 98c279: INFO] 2019-06-29 04:38:21.618 +02:00
[INF] 0 orders fetched from db @ 6/29/2019 4:38:21 AM
The web app should have the Romance Standard Time
, and the WebJobs running in this app service should have the same timezone.
Therefore DateTime.Now
should be the servers (Azure web app) current time.
----- UPDATE -----
According to Matt's comments, we tried using UTC times in the service by converting our DB times on run-time with the .toUniversalTime()
extension and then checking it againt DateTime.UtcNow
.
It is working fine, and the times in DB are still local times as we want them to be.
Upvotes: 1
Views: 644
Reputation: 3208
I'd suggest to use DateTimeOffset(.NET)/datetimeoffset(SQL) data type instead of storing timestamp as DateTime(.NET)/datetime2(SQL) in your database.
Then you can set in OrderEntity property ExecutionTime to DateTimeOffset.Now or new DateTimeOffset(DateTime specificTime) and do the same thing for your webjob code.
The comparison will take into account timeshift between time zones.
Hope it helps. Cheers!
Upvotes: 0