首页 > 解决方案 > 检索时区和日期时间的夏令时

问题描述

我想要 SQL Server 中通过日期和时区的 Daylight。

例如,我正在传递 TimeZone = 'PST' AND Date = '2020-05-20 09:41:55.130'。

我需要日光将当前日期申请到 pst 时区输出“是”或“否”。

注意:时区和日期是动态的。

我有 C# 代码,它工作正常。

     public static bool IsDaylightSavingTime(string timeZone)
        {
            try
            {
                //if isDaylight is true in 'PST' then offset is -7 else -8

                bool isDaylight = false;
                DateTime localTime = DateTime.Now;
                DateTime UTCTime = DateTime.UtcNow;

                string TimeZoneFullName = string.Empty;
                if (timeZone.ToUpper() == "PST" || timeZone.ToUpper() == "PT")
                {
                    TimeZoneFullName = "Pacific Standard Time"; //UTC−08 DST −07:00
                }
                else if (timeZone.ToUpper() == "MST" || timeZone.ToUpper() == "MT")
                {
                    TimeZoneFullName = "Mountain Standard Time"; //UTC−07  DST −06:00
                }
                else if (timeZone.ToUpper() == "CST" || timeZone.ToUpper() == "CT")
                {
                    TimeZoneFullName = "Central Standard Time"; //UTC−06 DST −05:00
                }
                else if (timeZone.ToUpper() == "EST" || timeZone.ToUpper() == "ET")
                {
                    TimeZoneFullName = "Eastern Standard Time"; //UTC−05  DST −04:00
                }


                if (TimeZoneFullName != string.Empty)
                {
                    TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneFullName);
                    isDaylight = tzi.IsDaylightSavingTime(localTime);
                }

                //WriteLog(string.Format("{0} isDaylight : {1} ", timeZone, isDaylight));
                return isDaylight;
            }
            catch (Exception ex)
            {
                WriteLog(string.Format("Error while check Day light Saving Time. (Utility - IsDaylightSavingTime) \n TimeZone {0} \n Error : {1}", timeZone, ex.ToString()));
                return false;
            }
        }

标签: sqlsql-serverfunctiondatetime

解决方案


推荐阅读