CultureInfo具有几个日期时间表示 [英] CultureInfo with several datetime representations

查看:633
本文介绍了CultureInfo具有几个日期时间表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个WCF服务,它需要一些 DateTime 对象并使用它,我们有一个客户端,它以格式 dd / mm发送/ yyyy 。但是这个服务也是从JS发出的,它应该发送准确的 DateTime ,这就是为什么使用 yyyy-MM-ddTHH:mm:ss.fffzzz 格式。



是否可以创建一种采用两种格式的文化,不要令人厌恶的如果 if(DateTime.TryParse(format1,out dt)|| DateTime.TryParse(format2,out dt)|| DateTime.TryParse(format3,out dt)|| .. 。)



此代码的另一个缺点是我们还必须将其复制为 DateTimeOffset 以及

解决方案

这是ASP.NET MVC如何处理问题。我认为相同的原则适用于使用其他技术(如WCF)实现的服务。这些约定由Web服务使用。



ASP.NET MVC(包括Web API)对查询字符串中传递的参数执行不区分文化的解析。因此,URL中的日期必须采用通用格式yyyy-mm-dd。表单值(或请求正文中的任何值)预期为服务器指定的格式。这意味着设置为英国文化的服务器将期望日期为英国日期格式:dd-mm-yyyy。



为什么不对文化不敏感的解析执行URL中的项目?



为了对URL中的项目执行文化不敏感的解析,有一个非常好的,合乎逻辑的方便的原因。想象一下,我看到一个销售项目,并且销售从和日期在URL中的。如果我通过电子邮件将该链接发送给我的朋友(我住在加拿大)在德国,我希望他们看到同一页面。如果在URL中的日期执行了文化敏感的解析,那么我的朋友可能会呈现完全不同的销售,或者可能会呈现一个不被发现的页面 - 这不会很好。



所以为了回答你的问题,客户端调用你的WCF服务应该以服务指定的格式提供服务的日期,除非日期在URL中,在这种情况下,客户端应该以通用格式提供yyyy -mm-dd。



另一个选项是让客户端向服务提供文化,例如SOAP头,服务将使用该文化解析。


Assume we have a WCF service which takes some DateTime object and works with it and we have a client which sends it in format dd/mm/yyyy. But this service is also called from JS which should send exact DateTime, this is why it is using yyyy-MM-ddTHH:mm:ss.fffzzz format.

Is it possible to create a Culture which is taking two formats and do no write disgusting ifs like if (DateTime.TryParse(format1, out dt) || DateTime.TryParse(format2, out dt) || DateTime.TryParse(format3, out dt) || ...)

Another disadvantage of this code is that we also have to duplicate it for DateTimeOffset as well

解决方案

This is how ASP.NET MVC handles the issue. I think the same principles apply to a service implemented using another technology such as WCF. These conventions are used by web services.

ASP.NET MVC (including Web API) performs a culture-insensitive parsing on parameters passed in the querystring. Therefore, dates in the URL have to be in the universal format yyyy-mm-dd. Form values (or any value in request body) are expected to be in the format specified by the server. This means that a server set to the UK culture will expect dates to be in the UK date format: dd-mm-yyyy.

Why is culture-insensitive parsing performed on items in the URL?

There is a very good, logical and convenient reason why culture-insensitive parsing is performed on items in the URL. Imagine I see an item on sale and the sale has from and to dates in the URL. If I email that link to my friend (I live in Canada) in Germany, I want them to see the same page. If a culture-sensitive parse was performed on the date in the URL, my friend may be presented a totally different sale or may be presented a page not found-that will not be good.

So to answer your question, the client calling your WCF service should provide the date to your service in the format specified by the service unless the date is in the URL in which case the client should provide it in the universal format yyyy-mm-dd.

Another option is for the client to provide the culture to the service, for example, in the SOAP header, and the service will use that culture when parsing.

这篇关于CultureInfo具有几个日期时间表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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