热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

时区问题在Asp中。NetMVC-C#-TimeZoneIssueinAsp.NetMVC

Iamfacinganissueintimezone.RightnowIamsavingtimezonefromclientsideandstoredallDa

I am facing an issue in timezone. Right now I am saving time zone from client side and stored all DateTime in UTC. It's working properly but when I am trying to convert DateTime for UTC behind time zones like CST, EST, EDT it showing wrong data.

我正面临着一个时区问题。现在我从客户端保存时区,并将所有DateTime存储在UTC中。它工作正常,但是当我试图在CST、EST和EDT等时区之后转换UTC的DateTime时,它会显示错误的数据。

Issue - Let's assume if I did any task at 10 PM EDT and it would be saved in DB as 2 AM(as per UTC) but when I am trying to fetch data for a day and passing current UTC date.

让我们假设如果我在10 PM EDT上执行任何任务,它将在DB中被保存为2 AM(与UTC一样),但是当我试图获取一天的数据并通过当前UTC日期时。

My question is If am trying to fetch data for a day like 11 midnight( from EST) to the current time, but my conversion from UTC to EST is wrong due to UTC 12midnight is yesterday's 8 PM(as EDT 4hr behind from UTC). (From Date[UTC convert to EDT] - 06/07/2017 08:00pm) and To Date - 06/07/2017 11:00 pm) Due to this conversion I am getting data from 8 pm to 11 pm only <- I am expecting from date is 06/07/2017 04:00 AM as per UTC.

我的问题是,我是否试图获取一天的数据,比如从东部时间午夜11点(从东部时间)到现在的时间,但是我从东部时间12点转换到东部时间是错误的,因为东部时间是昨天晚上8点(东部时间4小时)。(从日期[UTC转换为EDT] - 06/07/2017 08:00pm)到日期- 06/07/2017 11:00 pm)由于此转换,我只收到从晚上8点到晚上11点的数据<-我预计从日期开始的日期是:06/07/2017 04:00 am。

Code - Below is the code for conversion. In from date I have taken utcnow.date only and from a date

代码-下面是转换的代码。从现在开始,我一直在使用utc。只有日期和日期

Javascript code -

Javascript代码,

function setTimezoneCOOKIE() {
            try {
                var timezone_COOKIE = "timezoneoffset";
                var timeZOneName= "timezonename"

                var tz = jstz.determine();
                var aa = tz.name();
                // if the timezone COOKIE not exists create one.

                if (!$.COOKIE(timezone_COOKIE)) {
                    // create a new COOKIE
                    $.COOKIE(timezone_COOKIE, new Date().getTimezoneOffset());
                    $.COOKIE(timeZoneName, aa);
                }
                else {
                    var storedOffset = parseInt($.COOKIE(timezone_COOKIE));
                    var currentOffset = new Date().getTimezoneOffset();
                    if (storedOffset !== currentOffset) {
                        $.COOKIE(timezone_COOKIE, new Date().getTimezoneOffset());
                        $.COOKIE(timeZoneName, aa);
                        location.reload();
                    }
                    else {
                        $.COOKIE(timeZoneName, aa);
                    }
                }
            }

c# code -

c#代码,

 fromDate =Convert.ToDateTime(fromDate).ToClientTimeZoneinDateTime().ToString();  
                toDate = Convert.ToDateTime(toDate).ToClientTimeZoneinDateTime().ToString();

                ObjectParameter totalRecords = new ObjectParameter("TotalRecords", typeof(int));
                var DetailsList = objDetailsList.GetDetails(loginUserId,locationId, userId, taskType, pageIndex, numberOfRows, sortColumnName, sortOrderBy, textSearch, totalRecords, fromDate, toDate);
                if (DetailsList.Count() > 0)
                {
                    string output = BuildJQGridResults(DetailsList, numberOfRows, pageIndex, Convert.ToInt32(totalRecords.Value));
                    response.Write(output);
                }
                else
                {
                    JQGridResults result = new JQGridResults();
                    List rows = new List();
                    result.rows = rows.ToArray();
                    result.page = 0;
                    result.total = 0;
                    result.records = 0;
                    response.Write(new JavascriptSerializer().Serialize(result));
                }

Below is the method of converting UTC time to client timezone

下面是将UTC时间转换为客户端时区的方法

public static DateTime ToClientTimeZoneinDateTime(this DateTime dt)
{
    try {               
        if (System.Web.HttpContext.Current.Request.COOKIEs["timezoneoffset"] != null || System.Web.HttpContext.Current.Request.COOKIEs["timezonename"] != null)
        {                  
            var timezOnename= System.Web.HttpContext.Current.Request.COOKIEs["timezonename"].Value;
            timezOnename= timezonename.Replace("%2F", "/");
            var timezoneLocal1 = FindTimezoneName(timezonename);
            TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(timezoneLocal1);
            bool isCurrentlyDaylightSavings = tzi.IsDaylightSavingTime(dt);
            if (isCurrentlyDaylightSavings == true)
                dt.AddHours(1);

            var timeOffSet = System.Web.HttpContext.Current.Request.COOKIEs["timezoneoffset"].Value;

            var offset = int.Parse(timeOffSet.ToString());
            dt = dt.AddMinutes(-1 * offset);                  
            return dt;
        }             
        return dt.ToLocalTime();
    }
    catch (Exception)
    {
        return DateTime.UtcNow;
    }
}

No doubt as Timezone handled properly but facing an issue for behind timezone from UTC if end user trying to fetch data after 8 PM EDT. I have attached screenshot as well.

毫无疑问,时区处理得很好,但是如果最终用户试图在东部时间晚上8点以后获取数据,那么就会面临来自UTC的时区后面的问题。我也附上了截图。

Below img of before conversion - enter image description here

低于img之前的转换-

Above img of after conversion - enter image description here

以上img的后转换-

How do I need to handle this situation?

我该如何处理这种情况?

1 个解决方案

#1


1  

The main problem is that you're converting the wrong direction. You are converting from UTC to the user's time zone, but your input is in the user's time zone, so you need to convert the other direction - from the user's time zone to UTC. Then your query will show better results.

主要的问题是你转换了错误的方向。您正在从UTC转换到用户的时区,但是您的输入位于用户的时区,所以您需要转换到另一个方向——从用户的时区转换到UTC。然后您的查询将显示更好的结果。

A few other things:

其他一些事情:

  • Don't convert time zones by trying to add/subtract minutes or hours manually. Use the conversion functions offered on TimeZoneInfo, such as ConvertTimeFromUtc, ConvertTimeToUtc, etc. There's no need to test for DST.

    不要通过手动添加/减去分钟或小时来转换时区。使用TimeZoneInfo提供的转换函数,如ConvertTimeFromUtc、ConvertTimeToUtc等。不需要对DST进行测试。

  • The try/catch shouldn't be in your code at all. Throw an exception if you can't perform the operation. Don't mask important errors by swallowing exceptions.

    try/catch不应该出现在代码中。如果无法执行操作,则抛出异常。不要通过吞咽异常来掩盖重要的错误。

  • dt.ToLocalTime() shouldn't be in your code either. Never rely on the server's local time zone.

    tolocaltime()也不应该出现在您的代码中。不要依赖服务器的本地时区。

  • The offset returned by new Date().getTimezoneOffset() is the user's current offset. You cannot assume that it's the correct offset for the dates chosen. You don't that anyway, as you're already getting the time zone name. (You don't need the timezoneoffset COOKIE at all.)

    new Date(). gettimezoneoffset()返回的偏移量是用户当前的偏移量。您不能假定它是所选日期的正确偏移量。不管怎样,你已经得到了时区的名称。(你根本不需要timezoneoffset COOKIE。)

  • The time zone name returned by jstz.determine() on the client-side is going to be an IANA tzdb identifier, such as America/Los_Angeles. These aren't going to work on the server-side with TimeZoneInfo.FindSystemTimeZoneById (unless you are running .NET Core on Linux or Mac). Conversion to a Windows time zone is required. I see you have a FindTimeZoneName function, which I assume is performing the conversion. You didn't show the details in your code, but I highly recommend you use my TimeZoneConverter library to implement that, as it's maintained with changes to time zones.

    客户端的jstz. decide()返回的时区名称将是一个IANA tzdb标识符,例如America/Los_Angeles。这些不会在服务器端使用TimeZoneInfo。FindSystemTimeZoneById(除非您在Linux或Mac上运行。net内核)。需要转换到Windows时区。我看到您有一个FindTimeZoneName函数,我假设它正在执行转换。您没有在代码中显示细节,但是我强烈建议您使用我的TimeZoneConverter库来实现它,因为它是通过对时区的更改来维护的。

  • Reading COOKIEs and time zone conversion are separate concerns. Don't bundle them together.

    读取COOKIE和时区转换是不同的关注点。不要捆绑在一起。

Ultimately, you should have something like this:

最终,你应该拥有这样的东西:

public static DateTime FromTimeZoneToUtc(this DateTime dt, string timeZone)
{
    var windowsId = TimeZoneConverter.TZConvert.IanaToWindows(timeZone);
    var tzi = TimeZoneInfo.FindSystemTimeZoneById(windowsId);
    return TimeZoneInfo.ConvertTimeFromUtc(dt, tzi);
}

Or, even better, if you use Noda Time, then you don't need to convert time zones at all.

或者更好的是,如果你使用野田时间,那么你根本不需要转换时区。

public static DateTime FromTimeZoneToUtc(this DateTime dt, string timeZone)
{
    var tz = DateTimeZoneProviders.Tzdb[timeZone];
    var local = LocalDateTime.FromDateTime(dt);
    return local.InZoneLeniently(tz).ToDateTimeUtc();
}

推荐阅读
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了RPC框架Thrift的安装环境变量配置与第一个实例,讲解了RPC的概念以及如何解决跨语言、c++客户端、web服务端、远程调用等需求。Thrift开发方便上手快,性能和稳定性也不错,适合初学者学习和使用。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 闭包一直是Java社区中争论不断的话题,很多语言都支持闭包这个语言特性,闭包定义了一个依赖于外部环境的自由变量的函数,这个函数能够访问外部环境的变量。本文以JavaScript的一个闭包为例,介绍了闭包的定义和特性。 ... [详细]
  • 摘要: 在测试数据中,生成中文姓名是一个常见的需求。本文介绍了使用C#编写的随机生成中文姓名的方法,并分享了相关代码。作者欢迎读者提出意见和建议。 ... [详细]
  • SpringMVC接收请求参数的方式总结
    本文总结了在SpringMVC开发中处理控制器参数的各种方式,包括处理使用@RequestParam注解的参数、MultipartFile类型参数和Simple类型参数的RequestParamMethodArgumentResolver,处理@RequestBody注解的参数的RequestResponseBodyMethodProcessor,以及PathVariableMapMethodArgumentResol等子类。 ... [详细]
  • 本文讨论了编写可保护的代码的重要性,包括提高代码的可读性、可调试性和直观性。同时介绍了优化代码的方法,如代码格式化、解释函数和提炼函数等。还提到了一些常见的坏代码味道,如不规范的命名、重复代码、过长的函数和参数列表等。最后,介绍了如何处理数据泥团和进行函数重构,以提高代码质量和可维护性。 ... [详细]
  • 本文讨论了在VMWARE5.1的虚拟服务器Windows Server 2008R2上安装oracle 10g客户端时出现的问题,并提供了解决方法。错误日志显示了异常访问违例,通过分析日志中的问题帧,找到了解决问题的线索。文章详细介绍了解决方法,帮助读者顺利安装oracle 10g客户端。 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了为什么要使用多进程处理TCP服务端,多进程的好处包括可靠性高和处理大量数据时速度快。然而,多进程不能共享进程空间,因此有一些变量不能共享。文章还提供了使用多进程实现TCP服务端的代码,并对代码进行了详细注释。 ... [详细]
  • 树莓派语音控制的配置方法和步骤
    本文介绍了在树莓派上实现语音控制的配置方法和步骤。首先感谢博主Eoman的帮助,文章参考了他的内容。树莓派的配置需要通过sudo raspi-config进行,然后使用Eoman的控制方法,即安装wiringPi库并编写控制引脚的脚本。具体的安装步骤和脚本编写方法在文章中详细介绍。 ... [详细]
  • 小程序wxs中的时间格式化以及格式化时间和date时间互转
    本文介绍了在小程序wxs中进行时间格式化操作的问题,并提供了解决方法。同时还介绍了格式化时间和date时间的互相转换的方法。 ... [详细]
author-avatar
晰mine
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有