0

I have Iana time zone Id and need to know the time zone offset for some DateTime. As I understand, the following function returns the offset for DateTime.Now (or no daylight offset?).

TimeSpan GetTimeZoneOffset(string timeZoneId) =>
    new DateTimeZoneCache(TzdbDateTimeZoneSource.Default)
        .GetZoneOrNull(timeZoneId)
        .GetUtcOffset(SystemClock.Instance.GetCurrentInstant())
        .ToTimeSpan();

But I need

TimeSpan GetTimeZoneOffset(string timeZoneId, DateTime utcInstant)

How to implement it?

7
  • A date doesn't have an offset - a specific instant in time does. A single date could span multiple offsets, due to (for example) daylight saving time transitions. Please clarify the question.
    – Jon Skeet
    Dec 2, 2020 at 11:04
  • Okay, so "for some DateTime" is a little more reasonable - is that always a DateTime with a Kind of UTC? Your example answer assumes so, but it's really important. (What if it's a Kind of Unspecified, and that value is skipped or ambiguous?
    – Jon Skeet
    Dec 2, 2020 at 18:17
  • As a more meta note, Noda Time provides the most value when you use it throughout your code base - I'd recommend staying within the context of Noda Time (so Instant and Offset rather than DateTime and TimeSpan) for as much of the code as possible. Only convert to BCL types at the boundaries of your code, if you need to for compatibility.
    – Jon Skeet
    Dec 2, 2020 at 19:14
  • @JonSkeet, I am trying to find a solution to my problem with very small experience in Node Time. To do that I google using terminology that is already known to me and the knowledge that I already have. There is good to use correct terms in an article, but in SO question, it is better to keep the question as is (to simplify googling for people who will have the same problem). If something is wrong in my question it can be corrected in an answer and can be pointed to me that I don't understand something. Dec 3, 2020 at 14:54
  • Well, I really don't want to add an answer until the assumptions in the question are clarified - mostly what Kind of DateTime you need to be able to handle. Will it only ever be UTC?
    – Jon Skeet
    Dec 3, 2020 at 15:02

2 Answers 2

1

Fundamentally, you need DateTimeZone.GetUtcOffset, which accepts an Instant.

If the DateTime value always has a Kind of Utc, you can use Instant.FromDateTimeUtc. If it might have a different Kind, you'll need to work out more detailed requirements.

Next you need an IDateTimeZoneProvider to map the time zone ID into a DateTimeZone. That might be DateTimeZoneProviders.Tzdb, or it might be one you've injected somewhere for testability.

Once you've got the Instant and the DateTimeZone, you can call GetUtcOffset to get an Offset. You can convert that back to a TimeSpan, but I'd actually encourage you to avoid using DateTime and TimeSpan as far as possible in your app - if you can use the Noda Time types everywhere within your codebase and only convert between those and the BCL types at boundaries (e.g. database access) you'll find you need to do a lot less of this work.

But if you really need a TimeSpan, the method would look like this:

TimeSpan GetTimeZoneOffset(string timeZoneId, DateTime dateTimeUtc)
{
    Instant instant = Instant.FromDateTimeUtc(dateTimeUtc);
    DateTimeZone zone = DateTimeZoneProviders.Tzdb[timeZoneId];
    Offset offset = zone.GetUtcOffset(instant);
    return offset.ToTimeSpan();
}

Note that the IDateTimeZoneProvider[string] indexer will throw an exception if the time zone isn't found in that provider. If you want to handle this a different way, use IDateTimeZoneProvider.GetZoneOrNull() and check whether the result is null or not.

0
TimeSpan GetTimeZoneOffset(string timeZoneId, DateTime date) =>
    new DateTimeZoneCache(TzdbDateTimeZoneSource.Default)
        .GetZoneOrNull(timeZoneId)
        .GetUtcOffset(Instant.FromDateTimeUtc(utcDay))
        .ToTimeSpan();
2
  • I would recommend using DateTimeZoneProviders.Tzdb rather than creating a new DateTimeZoneCache each time - and this will throw a NullReferenceException if the zone ID is unknown. If you want to fail with an exception, just use the indexer - that will throw a much clearer exception.
    – Jon Skeet
    Dec 2, 2020 at 11:05
  • @JonSkeet In my real-world project, I have DateTimeZoneCache as a singleton injection. Also, I sure that timeZoneId is correct because it was validated in an earlier stage of data flow. Anyway indexer using is a good point. Dec 3, 2020 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.