Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my Azure Function App Timestamp an hour less

I have an Azure Function App that runs every five minutes. The document has a Timestamp field using

DateTime.Now

I live in the UK which is currently british summer time 26-09-18 which is 1 hour ahead of GMT. My Azure data centre is UK South. The timestamp is one hour wrong. E.G. 20:01 is stored in the documnt as 19:01. How can I correct this?

like image 600
Paul Stanley Avatar asked Jan 29 '26 13:01

Paul Stanley


2 Answers

Azure Functions by default are UTC.

  • For Windows add the setting WEBSITE_TIME_ZONE and set the value your TimeZone
    • Ex: WEBSITE_TIME_ZONE = GMT Standard Time
  • For Linux add the setting TZ and set the value to your Timezone
    • Ex: TZ = Europe/Lisbon

For Windows TimeZone values check here
For Linux TimeZone values check here

like image 165
hjgraca Avatar answered Jan 31 '26 02:01

hjgraca


You can handle this in code, for the UK, you can use the following which should auto adjust for daylight savings.

TimeZoneInfo ukTimeZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");

DateTime utcDate = new DateTime(2019, 1, 1, 0, 0, 0, DateTimeKind.Utc);
DateTime ukTime = TimeZoneInfo.ConvertTimeFromUtc(utcDate, ukTimeZone);
Console.WriteLine(utcDate == ukTime);

On the 1st of January, the value of ukTime should be equal to UTC, as the time zone used in the UK at that time of year is the GMT time zone which is equal to UTC.

GMT


On the 1st of June, the time in the UK should be an hour ahead of UTC, as the UK will have switched over the the BST time zone during that time of year.

utcDate = new DateTime(2019, 6, 1, 0, 0, 0, DateTimeKind.Utc);
ukTime = TimeZoneInfo.ConvertTimeFromUtc(utcDate, ukTimeZone);
Console.WriteLine(utcDate == ukTime.AddHours(-1));

BST

like image 36
Aydin Avatar answered Jan 31 '26 02:01

Aydin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!