0

I recently changed my website hosting provider and the new server's time zone is set to UTC.

I have written the following code but it doesn't work as expected. Am I doing something wrong?

When I call DateTime.Now the time returned is indeed UTC time, but to my understanding DateTime.Now (passed to the method) should be of 'Kind' Utc. But the method is returning the same UTC time, as opposed to converting to local time (desired result).

DEFAULT_TIMEZONE is string constant: AUS Eastern Standard Time

public static DateTime UTCToLocalTime(DateTime dt, string destinationTimeZone)
{
    DateTime dt_local = dt;
    try
    {
        if (dt.Kind == DateTimeKind.Local)
            return dt_local;
        if (string.IsNullOrEmpty(destinationTimeZone))
            destinationTimeZone = DEFAULT_TIMEZONE;
        TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(destinationTimeZone);
        dt_local = TimeZoneInfo.ConvertTimeFromUtc(dt, tzi);
        if (tzi.IsDaylightSavingTime(dt_local))
            dt_local = dt_local.AddHours(-1);
    }
    catch (TimeZoneNotFoundException)
    {
        AddLogEntry("Registry does not define time zone: '" + destinationTimeZone + "'.");
    }
    catch (InvalidTimeZoneException)
    {
        AddLogEntry("Registry data for time zone: '" + destinationTimeZone + "' is not valid.");
    }
    catch (Exception ex)
    {
        ProcessError(ex);
    }
    return dt_local;
}

Calling the method:

DateTime dt = gFunc.UTCToLocalTime(DateTime.Now, string.Empty);

UPDATE: CODE CHANGES

I misunderstood how 'Kind' works. I thought if the OS timezone was set to UTC then a call to DateTime.Now would be of 'Kind' Utc. The following code now works for me as expected.

public static DateTime UTCNowToLocal(string destinationTimeZone)
{
    DateTime dt_now = DateTime.UtcNow;
    try
    {
        if (string.IsNullOrEmpty(destinationTimeZone))
            destinationTimeZone = DEFAULT_TIMEZONE;
        TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(destinationTimeZone);
        dt_now = TimeZoneInfo.ConvertTimeFromUtc(dt_now, tzi);
        if (tzi.IsDaylightSavingTime(dt_now))
            dt_now = dt_now.AddHours(-1);
    }
    catch (TimeZoneNotFoundException)
    {
        AddLogEntry("Registry does not define time zone: '" + destinationTimeZone + "'.");
    }
    catch (InvalidTimeZoneException)
    {
        AddLogEntry("Registry data for time zone: '" + destinationTimeZone + "' is not valid.");
    }
    catch (Exception ex)
    {
        ProcessError(ex);
    }
    return dt_now;
}
Ross Kelly
  • 477
  • 1
  • 6
  • 23
  • Rather than setting the server to UTC, why not just use `DateTime.UtcNow`? – ProgrammingLlama Jun 19 '21 at 17:08
  • 1
    If the server's time zone is set to UTC, the value of `DateTime.Now` is going to be in UTC, and the `Kind` set to `Local` (since `DateTime.Now` always sets the `Kind` to `Local`). That your hosting provider decided the local time zone is UTC is irrelevant to `Kind`; its value is determined by where the value is pulled from, not by the value. – Heretic Monkey Jun 19 '21 at 17:16
  • If you want this to work, you need to compare the value given to `DateTime.UtcNow` (perhaps `var diff = DateTime.UtcNow.Subtract(dt);`) and decide the minimum difference allowable. – Heretic Monkey Jun 19 '21 at 17:18
  • See duplicates for how to convert from one time zone to another. Since the server's time zone is UTC, it has no idea what "local" time is. It's up to you to explicitly provide that. Note that `DateTimeOffset` generally provides better time zone handling than `DateTime`, so you may prefer to use that type instead. – Peter Duniho Jun 19 '21 at 17:31
  • thanks all for feedback. I misunderstood how 'Kind' works. I thought if the OS timezone was set to UTC then a call to DateTime.Now would be of 'Kind' Utc. I've updated my post with the changed code. – Ross Kelly Jun 20 '21 at 09:39

0 Answers0