How to tell if it's British Summer Time

前端 未结 4 479
猫巷女王i
猫巷女王i 2021-01-19 02:21

I have the following code, which should return an offset of 60 (to show that in the UK at present, we are in British Summer Time - ie. 60 minutes ahead of GMT):

4条回答
  •  失恋的感觉
    2021-01-19 02:50

    To master conversion from one timezone to anther you need to see what's supported (how?) and what's not.

    foreach (var tz in TimeZoneInfo.GetSystemTimeZones())
    {
        Console.WriteLine("TimeZone Offset: {0} Name : {1}, Supports DLS: {2}", tz.BaseUtcOffset,tz.StandardName,tz.SupportsDaylightSavingTime);
    }
    

    This should give you the list all timezones including info about DayLightSaving. Notice that:

    TimeZone Offset: 00:00:00 Name : Greenwich Standard Time, Supports DLS: False

    TimeZone Offset: 00:00:00 Name : GMT Standard Time, Supports DLS: True

    So you need to use "GMT Standard Time" because it supports daylight saving already. No work needs to be done.

    Here is sample code:

    private static string GetBSTTimeStamp(string timestamp)
    {
        DateTime dt = DateTime.Parse(timestamp);
        //TimeZoneInfo bst = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
        //Console.WriteLine("Time zone supports dls? {0}", bst.SupportsDaylightSavingTime);
        //Console.WriteLine("Time zone offset? {0}", bst.BaseUtcOffset);
        DateTime dateTimeInUtc = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dt, "Eastern Standard Time", "GMT Standard Time");
        return dateTimeInUtc.ToString();
    }
    

提交回复
热议问题