简体   繁体   中英

Timezone handling in AWS with C#

Timezone usage A stored object in the database has an UTC timestamp. The user requests this element and should receive the element with a timestamp in a configured timezone.

How to keeping the timezone up to date
On-premise, we stored the timezone data (offset, DST, etc.) for the next 5 years in our database and updated these values manually, if these 5 years were over or there were some timezone changes. (legacy code...)

Because we are now migrating to AWS, we also wanted to automate the timezone update handling.

We looked into TimeZoneInfo so we don't have to worry about timezone changes and let the OS handle that. But that won't work for AWS Lambda because it only "knows" UTC.

Is there any AWS Service or extension, which provides a localized timestamp from an UTC timestamp and a timezone?
Or a completely different approach to handle timezones changes?

I'm really surprised that AWS Lambda doesn't actually have full complement of time zone information, but assuming that's actually the case, you could certainly use my Noda Time project. That comes with the most recent time zone data from IANA baked into it, and you can load an updated file (usually obtained from nodatime.org via polling for changes) if you need to stay really up to date.

I'd personally recommend trying to adopt Noda Time throughout the codebase as a clearer date/time API, but if you absolutely wanted to stick to DateTime etc everywhere else, you could have a method such as:

public static DateTime ConvertUtcToZone(DateTime utc, string zoneId)
{
    var zone = DateTimeZoneProviders.Tzdb[zoneId];
    var instant = Instant.FromDateTimeUtc(utc);
    var zoned = instant.InZone(zone);
    return zoned.ToDateTimeUnspecified();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM