iikkoo
iikkoo

Reputation: 2856

Time zones and day light savings time in Azure functions

I am writing an Azure Function i .NET Core 3.1 that will parse a log containing strings and make a DateTime object from a subset of the string. The string with date and time is on the form yymmddhhmmss and represents a timestamp of an event. It is implicit that the date and time is given in either CET or CEST. The input is an old legacy format that I can't change, thus we use the function to transform it to a more flexible format for further processing.

The challenge that I face is to read this string and convert it to UTC. And that would be no problem if it weren't for daylight savings time. The input is given in CET or CEST, that is UTC+1 or UTC+2. So, depending on the time of year I have to read the string and create a DateTime object, using either CET or CEST as local time zone, and then convert it to UTC.

In Azure Functions I can set configuration to make the function run using a specific time zone. But, depending on whether it is winter or summer, I would have to use different time zones when creating the DateTime object and have to do this at runtime.

Example:

The string 191027010203 should be parsed using CEST as local time and converted to UTC. 2019-10-27 01:02:03 CEST -> 2019-10-27 23:02:03 UTC

The string 191027030203 should be parsed using CET as local time and converted to UTC. 2019-10-27 03:02:03 CET -> 2019-10-27 02:02:03 UTC

The string 190331015050 should be parsed using CET as local time and converted to UTC. 2019-03-31 01:50:50 CET -> 2019-03-31 00:59:59 UTC

The string 190331030101 should be parsed using CEST as local time and converted to UTC. 2019-03-31 03:01:01 CEST -> 2019-03-31 01:01:01 UTC

Is there a way to achieve this?

Upvotes: 1

Views: 922

Answers (1)

rickvdbosch
rickvdbosch

Reputation: 15621

The method DateTime.Parse(String, IFormatProvider) should be smart enough to see what the time of year is, and convert accordingly.

All overloads of the Parse method are culture-sensitive unless the string to be parsed conforms to the ISO 8601 pattern.

More info here.

Extra reference: DateTime.IsDaylightSavingTime Method

Upvotes: 2

Related Questions