简体   繁体   English

将服务器中的.NET DateTime值保留为客户端中分配的值

[英]Keep .NET DateTime value in the server as assigned in client

In my WPF .NET client application I assign a DateTime variable the current Time value: WPF .NET客户端应用程序中,我为DateTime变量分配当前的Time值:

class MyClass
{
  public DateTime CreatedDate {get; set;}
}

MyClass myClass = new MyClass();
myClass.CreatedDate = DateTime.Now;

After the assignment I send the object to a WCF service located in Europe (UTC+1) that saves the object in the database. 分配后,我将对象发送到位于欧洲WCF服务 (UTC + 1),该服务将对象保存在数据库中。 If client and service are in the same TimeZone there are no issues. 如果客户端和服务在同一TimeZone ,则没有问题。 However if I change TimeZone , for instance my client is in (UTC-6) and service in (UCT+1), the service will read the CreatedDate value as it is DateTime.Now on the service so (UCT+1) instead of (UCT-6) as assigned on the client. 但是,如果我更改TimeZone ,例如我的客户端位于(UTC-6)而服务位于(UCT + 1),则该服务将读取CreatedDate值,因为它是DateTime.Now在服务上所以(UCT + 1)而不是(UCT-6)分配给客户端。 If set CreatedDate on the client (UTC-6) the 31/10/2014 at 20:00 in the service it will be stored as (UTC+1) the 01/11/2014 at 01:00 and this affects some processes on my system. 如果在服务中的客户端(UTC-6)上于2014年10月31日设置了CreatedDate (UTC-6),它将在2014年11月11日在01:00存储为(UTC + 1) ,则这会影响到我的系统。

This is not the behavior that I would like to have, I want the service always to store that Date as submitted by the client. 这不是我想要的行为,我希望服务始终存储该客户端提交的日期。

I have access to the code for both server and client, I tried to set when assign the CreatedDate as: 我可以访问服务器和客户端的代码,在将CreatedDate分配CreatedDate尝试设置:

myClass.CreatedDate = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified);

with no luck. 没有运气。 The only solution I can think about is to use string instead of DateTime but that implies a lot of changes in the code. 我能想到的唯一解决方案是使用字符串而不是DateTime,但这意味着代码中有很多更改。 Anybody know a way to ignorne the TimeZone settings? 有人知道忽略TimeZone设置的方法吗?

You have a few different options: 您有几种选择:

  • Keep everything as DateTime , but work with UTC values instead of local values. 将所有内容保留为DateTime ,但使用UTC值而不是本地值。 That means using DateTime.UtcNow to fetch the current time. 这意味着使用DateTime.UtcNow获取当前时间。

    On the client, you can generate a display value using ToLocalTime to convert the value from UTC to the local time zone of the computer the client code is running on. 在客户端上,可以使用ToLocalTime生成显示值,以将值从UTC转换为运行客户端代码的计算机的本地时区。

  • Switch to DateTimeOffset . 切换到DateTimeOffset This ensures when transmitting the value from the client to the server that you retain the time zone offset - which tells you how far away from UTC that particular timestamp is. 这样可以确保在将值从客户端传输到服务器时,您保留时区偏移量-告诉您特定时间戳与UTC的距离。

    You can get the current time using either DateTimeOffset.UtcNow , or DateTimeOffset.Now . 您可以使用获取当前时间DateTimeOffset.UtcNow ,或DateTimeOffset.Now The latter has the advantage of retaining the local time as the client understood it. 后者的优点是保留了客户所理解的当地时间。 If you plan to do any aggregate analysis of the data based on local date/time, then you will need this information. 如果计划基于本地日期/时间对数据进行任何汇总分析,则将需要此信息。 Remember, "today" is not the same for everyone on the Earth simultaneously. 请记住,地球上每个人的“今天”并不相同。

  • If you have another way to communicate the time zone of the client, or if you know this already from outside information, then you can transmit either a DateTime or a DateTimeOffset , and then use the TimeZoneInfo class to do conversions on the server. 如果您有其他的沟通方式,客户端的时区,或者如果你知道这一点已经从外部的信息,那么你就可以传输一个DateTimeDateTimeOffset ,然后使用TimeZoneInfo类做服务器上的转换。

    There is one "gotcha" however. 但是有一个“陷阱”。 If you transmit a local DateTime that is in a daylight saving time ambiguity period (during the "fall-back" transition), then you can potentially convert incorrectly. 如果传输的本地 DateTime处于夏令时模糊时间段内(在“后备”过渡期间),则可能会错误地进行转换。 Using either UTC or DateTimeOffset will avoid this problem. 使用UTC或DateTimeOffset可以避免此问题。

See also The Case Against DateTime.Now , and DateTime vs DateTimeOffset . 另请参见反对DateTime.Now的情况以及DateTime与DateTimeOffset的情况

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM