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.
ExecutionType
andNextSendTry
? What about the corresponding types in the database? I assume this is Azure SQL DB? Or is it something else? Are the values in the database also in terms of Denmark local time? Or are they in UTC? Thanks. – Matt Johnson-Pint2019-10-27 02:00
, your code won't know if that's the first instance (UTC+2) or the second instance (UTC+1). I usually tell people to do their best to not rely onWEBSITE_TIME_ZONE
. It does work in most cases, but it is a crutch. Lean on it only when you absolutely must. – Matt Johnson-Pint