How to find the time zone and return time using the DaylightSavingTime function?

Am I trying to find the time zone and return the time when using DaylightSavingTime?

Currently I can:

  • find time zone
  • get utc offset
  • calculate local time based on this
  • determine if the timezone uses DaylightSavingTime
  • get rules for DaylightSavingTime
  • determine if current time is using DaylightSavingTime

However, I am having problems applying the rules, here is the code:

Fyi

System.DateTime.Now.ToUniversalTime().Add(timeDiffUtcClient) return = 2010/07/10 09:25:45 AM

 DateTime localDate = System.DateTime.Now.ToUniversalTime();
// Get the venue time zone info
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
TimeSpan timeDiffUtcClient = tz.BaseUtcOffset;
localDate = System.DateTime.Now.ToUniversalTime().Add(timeDiffUtcClient);


if (tz.SupportsDaylightSavingTime && tz.IsDaylightSavingTime(localDate))
{
    localDate = localDate.Subtract(tz.GetAdjustmentRules().Single(r => localDate >= r.DateStart && localDate <= r.DateEnd).DaylightDelta);
}
DateTimeOffset utcDate = localDate.ToUniversalTime();


return localDate;

The final value of localDate of is {2010/07/10 08:20:40 AM}

It should be {2010/07/10 09:20:40 AM}

For some reason, it's 1 hour.

+3
2

, , . :

public static DateTime GetLocalTime(string TimeZoneName)
{
  return TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById(TimeZoneName));
}
+3

ok, :

 public static DateTime GetLocalTime(string TimeZoneName)
    {
        DateTime localDate = System.DateTime.Now.ToUniversalTime();

        // Get the venue time zone info
        TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneName);
        TimeSpan timeDiffUtcClient = tz.BaseUtcOffset;
        localDate = System.DateTime.Now.ToUniversalTime().Add(timeDiffUtcClient);
        //DateTimeOffset localDate = new DateTimeOffset(venueTime, tz.BaseUtcOffset);

        if (tz.SupportsDaylightSavingTime && tz.IsDaylightSavingTime(localDate))
        {
            TimeZoneInfo.AdjustmentRule[] rules = tz.GetAdjustmentRules();
            foreach (var adjustmentRule in rules)
            {
                if (adjustmentRule.DateStart <= localDate && adjustmentRule.DateEnd >= localDate)
                {
                    localDate = localDate.Add(adjustmentRule.DaylightDelta);
                }
            }
            //localDate = localDate.Subtract(tz.GetAdjustmentRules().Single(r => localDate >= r.DateStart && localDate <= r.DateEnd).DaylightDelta);
        }
        DateTimeOffset utcDate = localDate.ToUniversalTime();


        return localDate;
    }

, :

Hashtable list = new Hashtable();
        foreach (TimeZoneInfo tzi in TimeZoneInfo.GetSystemTimeZones())
        {
            string name = tzi.DisplayName;
            DateTime localtime = TimeZoneHelper.GetLocalTime(tzi.Id);
            list.Add(name, localtime);
        }

"" worldtimeserver.com .

+2

Source: https://habr.com/ru/post/1754015/


All Articles