处理日期仅限于.Net中的时区

时间:2022-10-28 20:45:06

Ok - a bit of a mouthful. So the problem I have is this - I need to store a Date for expiry where only the date part is required and I don't want any timezone conversion. So for example if I have an expiry set to "08 March 2008" I want that value to be returned to any client - no matter what their timezone is. The problem with remoting it as a DateTime is that it gets stored/sent as "08 March 2008 00:00", which means for clients connecting from any timezone West of me it gets converted and therefore flipped to "07 March 2008" Any suggestions for cleanly handling this scenario ? Obviously sending it as a string would work. anything else ? thanks, Ian

好的 - 有点拗口。所以我遇到的问题是 - 我需要存储一个到期日期,其中只需要日期部分,我不希望任何时区转换。因此,例如,如果我将期限设置为“2008年3月8日”,我希望将该值返回给任何客户 - 无论他们的时区是什么。将其作为DateTime进行远程处理的问题是它被存储/发送为“2008年3月8日00:00”,这意味着对于从我西部的任何时区连接的客户,它将被转换,因此转为“2008年3月7日”任何建议为了干净地处理这种情况?显然将它作为字符串发送将起作用。还要别的吗 ?谢谢,伊恩

7 个解决方案

#1


0  

You could create a struct Date that provides access to the details you want/need, like:

您可以创建一个结构日期,以提供对您想要/需要的详细信息的访问权限,例如:

public struct Date
{
    public int Month; //or string instead of int
    public int Day;
    public int Year;
}

This is lightweight, flexible and gives you full control.

这是轻量级,灵活的,让您完全控制。

#2


1  

I'm not sure what remoting technology you're referring to, but this is a real problem with WCF, which only currently supports serializing DateTime as xs:DateTime, inappropriate for a date-only value where you are not interested in timezones.

我不确定你指的是什么远程处理技术,但这是WCF的一个真正问题,它目前只支持将DateTime序列化为xs:DateTime,不适用于你对时区不感兴趣的仅限日期的值。

.NET 3.5 introduces the new DateTimeOffset type, which is good for transferring a DateTime between timezones, but doesn't help with the date-only scenario.

.NET 3.5引入了新的DateTimeOffset类型,它适用于在时区之间传输DateTime,但对于仅限日期的方案没有帮助。

Ideally WCF needs to optionally support xs:Date for serializing dates as requested here:

理想情况下,WCF需要选择性地支持xs:日期序列化日期,如下所示:

http://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=349215

#3


1  

I do it like this: Whenever I have a date in memory or stored in a file it is always in a DateTime in UTC. When I show the date to the user it is always a string. When I convert between the string and the DateTime I also do the time zone conversion.

我是这样做的:每当我在内存中有一个日期或存储在一个文件中时,它总是在UTC的DateTime中。当我向用户显示日期时,它始终是一个字符串。当我在字符串和DateTime之间转换时,我也进行时区转换。

This way I never have to deal with time zones in my logic, only in the presentation.

这样我就不必在逻辑中处理时区,只在演示中处理。

#4


0  

You can send it as UTC Time

您可以将其作为UTC时间发送

dateTime1.ToUniversalTime()

#5


0  

I think sending as a timestamp string would be the quickest / easiest way although you could look at forcing a locale to stop the time conversion from occuring.

我认为发送时间戳字符串将是最快/最简单的方法,虽然你可以看看强制语言环境停止时间转换发生。

#6


0  

The easiest way I've handled this on apps in the past is to just store the date as a string in yyyy-mm-dd format. It's unambigious and doesn't get automatically translated by anything.

我过去在应用程序上处理此问题的最简单方法是将日期存储为yyyy-mm-dd格式的字符串。它是不明显的,不会被任何东西自动翻译。

Yes, it's a pain...

是的,这很痛苦......

#7


0  

Why don't you send it as a string then convert it back to a date type as needed? This way it will not be converted over different timezones. Keep it simple.

为什么不将它作为字符串发送然后根据需要将其转换回日期类型?这样它就不会在不同的时区转换。把事情简单化。

Edit: I like the Struct idea, allows for good functionality.

编辑:我喜欢Struct的想法,允许良好的功能。

#1


0  

You could create a struct Date that provides access to the details you want/need, like:

您可以创建一个结构日期,以提供对您想要/需要的详细信息的访问权限,例如:

public struct Date
{
    public int Month; //or string instead of int
    public int Day;
    public int Year;
}

This is lightweight, flexible and gives you full control.

这是轻量级,灵活的,让您完全控制。

#2


1  

I'm not sure what remoting technology you're referring to, but this is a real problem with WCF, which only currently supports serializing DateTime as xs:DateTime, inappropriate for a date-only value where you are not interested in timezones.

我不确定你指的是什么远程处理技术,但这是WCF的一个真正问题,它目前只支持将DateTime序列化为xs:DateTime,不适用于你对时区不感兴趣的仅限日期的值。

.NET 3.5 introduces the new DateTimeOffset type, which is good for transferring a DateTime between timezones, but doesn't help with the date-only scenario.

.NET 3.5引入了新的DateTimeOffset类型,它适用于在时区之间传输DateTime,但对于仅限日期的方案没有帮助。

Ideally WCF needs to optionally support xs:Date for serializing dates as requested here:

理想情况下,WCF需要选择性地支持xs:日期序列化日期,如下所示:

http://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=349215

#3


1  

I do it like this: Whenever I have a date in memory or stored in a file it is always in a DateTime in UTC. When I show the date to the user it is always a string. When I convert between the string and the DateTime I also do the time zone conversion.

我是这样做的:每当我在内存中有一个日期或存储在一个文件中时,它总是在UTC的DateTime中。当我向用户显示日期时,它始终是一个字符串。当我在字符串和DateTime之间转换时,我也进行时区转换。

This way I never have to deal with time zones in my logic, only in the presentation.

这样我就不必在逻辑中处理时区,只在演示中处理。

#4


0  

You can send it as UTC Time

您可以将其作为UTC时间发送

dateTime1.ToUniversalTime()

#5


0  

I think sending as a timestamp string would be the quickest / easiest way although you could look at forcing a locale to stop the time conversion from occuring.

我认为发送时间戳字符串将是最快/最简单的方法,虽然你可以看看强制语言环境停止时间转换发生。

#6


0  

The easiest way I've handled this on apps in the past is to just store the date as a string in yyyy-mm-dd format. It's unambigious and doesn't get automatically translated by anything.

我过去在应用程序上处理此问题的最简单方法是将日期存储为yyyy-mm-dd格式的字符串。它是不明显的,不会被任何东西自动翻译。

Yes, it's a pain...

是的,这很痛苦......

#7


0  

Why don't you send it as a string then convert it back to a date type as needed? This way it will not be converted over different timezones. Keep it simple.

为什么不将它作为字符串发送然后根据需要将其转换回日期类型?这样它就不会在不同的时区转换。把事情简单化。

Edit: I like the Struct idea, allows for good functionality.

编辑:我喜欢Struct的想法,允许良好的功能。