时区战略 [英] Timezone Strategy

查看:127
本文介绍了时区战略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个MVC 3应用程序,其中的用户可能无法在同一时区,所以我的目的是为了一切都存储在UTC和UTC转换为在提交的意见和本地时间为UTC本地时间。

I am building a MVC 3 application where the users may not be in the same time zone, so my intent was to store everything in UTC and convert from UTC to local time in the views and localtime to UTC on submissions.

做一些浏览,虽然似乎没有被很多很好的解决这个的。说实话,我有点期待的属性可用于自动转换UTC时间为至少本地时间,但似乎不存在。

Doing some browsing though there doesn't seem to be a lot of good solutions to this. To be honest, I sort of expected an attribute to be available to auto convert UTC time into local time at least, but it seems not to exist.

我觉得自己只是想勤于每个输入手动转换为UTC和每一个视图中手动转换为本地时间显示会很容易出错,导致难以察觉其中时间将不转换为或。错误

I feel like just trying to be diligent about manually converting every input to UTC and manually converting every view to local time display will be very error prone and lead to difficult to detect bugs where the time is not converted to or from.

如何处理这作为一个总体战略有什么建议?

Any suggestions on how to deal with this as a general strategy?

修改
每个人似乎都对我如何才能在客户端时区这块,这是我的意见人提非常卡住是不是我关心的。我很好,决定他们的时区用户设置,所以以为我已经知道客户端时区是什么...这并没有解决我的问题。

EDIT Everyone seems very stuck on the "how do I get the client timezone" piece, which as I mention in one of the comments is not my concern. I am fine with a user setting that determines their timezone, so assume I already know what the client time zone is...that doesn't address my problem.

现在,就当我呈现的日期每个视图,我需要调用一个方法以使其从UTC本地时区。我每次发送的日期,提交给服务器,我需要把它从本地时区转换为UTC。如果我忘了这样做会有问题...或者是提交的日期是错误的或者客户端的报告和过滤器将是错误的。

Right now, on each view when I render a date, I would need to call a method to render it in the local time zone from utc. Every time I send a date for submission to the server I need to convert it from the local timezone to UTC. If I forget to do this there will be problems...either a submitted date will be wrong or client side reports and filters will be wrong.

我希望存在是一个更加自动化的方法,特别是因为视图模型在MVC 3强类型我希望的总和魔术能够在一个时区至少自动呈现,如果不处理提交,就像日期格式或范围可以通过一个属性来控制。

What I was hoping existed was a more automated method, especially since the view model is strongly typed in MVC 3 I was hoping for sum magic to be able to at least automatically render in a time zone, if not handle the submission, just like the date format or range can be controlled by an attribute.

所以像

[DateRange]
Public DateTime MyDate

我能有这样的事情

I could have something like

[ConvertToUTC(offset)]
Public DateTime MyDate

总之,我想它看起来像我的唯一方法是编写自定义数据注释呈现在一个时区,并在MVC 3模型绑定一个覆盖这么进来的日期将被转换,除非我想换行有史以来日期在一个方法调用。所以,除非有人有进一步的意见或建议,这将是那些两个选项之一,我只是惊讶的东西不存在这样做。

Anyway, I guess it look like my only approach would be to write a custom data annotation to render it in a time zone, and a override on the MVC 3 model binder so incoming dates are converted unless I want to wrap ever date in a method call. So unless anyone has further comments or suggestions it will be one of those two options, I'm just surprised something doesn't exist already to do this.

如果我实现一个解决方案,我一定会张贴。

If I do implement a solution I will be sure to post it.

修改2
像这样的http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx对于MVC 3次和视图模型是我所期待的。

Edit 2 Something like This http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx for MVC 3 views and view models is what I am looking for.

最后编辑
标志着我epignosisx答案正确的,但也有一些意见的补充。
我发现了类似的东西在这里:
<一href=\"http://dalldorf.com/blog/2011/06/mvc3-timezones-1/\">http://dalldorf.com/blog/2011/06/mvc3-timezones-1/
通过将其放置在该cookie对于希望在第2部分(以下,因为在制品的第一部分的链接的链接部分2不工作)人获得来自客户端的时区的实施方案
<一href=\"http://dalldorf.com/blog/2011/09/mvc3-timezones-2/\">http://dalldorf.com/blog/2011/09/mvc3-timezones-2/

其重要的要注意这些方法,你必须在Editfor和Displayfor代替之类的东西TextForFor因为只有EditFor和DisplayFor利用用来告诉MVC如何显示该类型的模型属性的元数据提供商。如果直接在视图访问模型值(@ Model.MyDate)无转换会发生。

Its important to note with these approaches that you MUST you Editfor and Displayfor instead of things like TextForFor as only EditFor and DisplayFor make use of the metadata providers used to tell MVC how to display the property of that type on the model. If you access the model values directly in the view (@Model.MyDate) no conversion will take place.

推荐答案

您可以处理通过网站全DisplayTemplate的日期时间UTC转换为用户本地时间的​​问题。

You could handle the problem of converting UTC to user local time by using website-wide DisplayTemplate for DateTime.

从你的观点,你会用@ Html.DisplayFor(N => n.MyDateTimeProperty)

From your Views you would use @Html.DisplayFor(n => n.MyDateTimeProperty)

第二个问题是强硬应对。从用户的本地时间转换为UTC,你可以重写 DefaultModelBinder 。具体方法的SetProperty 。下面是说明了这一点天真的实现。它仅适用于日期时间,但可以很容易地扩展为日期时间?。然后将其设置为在Global.asax您的默认文件夹

The second problem is tougher to tackle. To convert from user local time to UTC you could override the DefaultModelBinder. Specifically the method SetProperty. Here is a naive implementation that demonstrates the point. It only applies for DateTime but could easily be extended to DateTime?. Then set it up as your Default binder in the Global.asax

public class MyDefaultModelBinder : DefaultModelBinder
{
    protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, object value)
    {
        //special case for DateTime
        if(propertyDescriptor.PropertyType == typeof(DateTime))
        {
            if (propertyDescriptor.IsReadOnly)
            {
                return;
            }

            try
            {
                if(value != null)
                {
                    DateTime dt = (DateTime)value;
                    propertyDescriptor.SetValue(bindingContext.Model, dt.ToUniversalTime());
                }
            }
            catch (Exception ex)
            {
                string modelStateKey = CreateSubPropertyName(bindingContext.ModelName, propertyDescriptor.Name);
                bindingContext.ModelState.AddModelError(modelStateKey, ex);
            }
        }
        else
        {
            //handles all other types
            base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
        }
    }
}

这篇关于时区战略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆