0
votes

I am using the following method to convert utc datetime to datetime corresponding to other timezones

public string ConvertUTCDateTimetoUserDateTime(string utcDate)
{
    DateTime userDateTime = new DateTime();
    String displaydate = String.Empty;
    try
    {
        destinationTimeZone = TimeZoneInfo.FindSystemTimeZoneById(TIMEZONE_ID);
        DateTime returndt = Convert.ToDateTime(utcDate);
        string strDate = String.Format("{0:" + DATE_FORMAT + " " + "HH:mm}", returndt);
        sourceTimeZone = TimeZoneInfo.Utc;
        string dateFormat = DATE_FORMAT + " " + "HH:mm";
        DateTime unspecified = DateTime.ParseExact(strDate, dateFormat, CultureInfo.InvariantCulture);
        userDateTime = TimeZoneInfo.ConvertTime(unspecified, sourceTimeZone, destinationTimeZone);
        displaydate = userDateTime.ToString(DATE_FORMAT + " HH:mm");
    }
    catch (Exception ex)
    {
    }
    return displaydate;
}

If I pass '03/19/2016 18:48' as UTC date and Timezone as "Atlantic Standard Time" (UTC-4:00) then it is converting it to '03/19/2016 15:48' which is Atlantic Daylight Time (UTC-3:00). Why is it considering DST for Atlantic Standard Time? Not all places in Atlantic time considers DST.For Eastern Standard Time (UTC-5:00) and Pacific Standard Time(UTC-8:00) the conversion is correct with DST.But why is this happening in case of Atlantic Time.For Atlantic Daylight Time there is no TimeZone ID either.I can add one hour by checking if it is AST but is there any better way of handling AST and ADT?

1

1 Answers

0
votes

From the timezone tag wiki:

Identifiers and names are confusing and assigned haphazardly. Naming conventions have varied over the years, and there is little consistency. In general, do not rely on a Windows time zone identifier to mean anything in particular. Instead, choose a time zone based on its display name.

A few examples:

  • "Eastern Standard Time" refers to both EST and EDT.

    ...

The same applies here for Atlantic time. The "Atlantic Standard Time" entry has the display name "(UTC-04:00) Atlantic Time (Canada)", and applies to both AST and ADT. It applies in Canadian locations such as Halifax, Glace Bay, Goose Bay, and Moncto. It also applies in Thule Air Base (Greenland), and in Bermuda.

Locations in Atlantic time that do not have DST should use the "SA Western Standard Time" entry instead. It has the display name "(UTC-04:00) Georgetown, La Paz, Manaus, San Juan", which is indeed confusing, as only San Juan actually calls this zone "Atlantic". The others have various local names and abbreviations.

For more details, see this page. If you had a specific location in mind, let me know and I'll update accordingly.

Also - your code is a bit of a mess. It's relying on string formatting and parsing way too much, is swallowing exceptions, and relies on external variables outside the function. I recommend you clean this up.