我应该使用DateTime.Now或DateTime.UtcNow在HttpCookie.Expires和HttpCachePolicy.SetExpires? [英] Should I use DateTime.Now or DateTime.UtcNow in HttpCookie.Expires and HttpCachePolicy.SetExpires?

查看:167
本文介绍了我应该使用DateTime.Now或DateTime.UtcNow在HttpCookie.Expires和HttpCachePolicy.SetExpires?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该使用DateTime.Now或DateTime.UtcNow在 HttpCookie.Expires HttpCachePolicy.SetExpires

Should I use DateTime.Now or DateTime.UtcNow in HttpCookie.Expires and HttpCachePolicy.SetExpires?

时候,Cookies发送GMT的时间,但我不知道是否我送什么发生 DateTime.Now.AddDays(3)如果我将是GMT + 5。同样的,过期的HTTP标头(14.21秒)

Cookies are sending 'GMT' time, but I don't know what happen if I send DateTime.Now.AddDays(3) if I would be in GMT+5. Same with Expires HTTP header (sec 14.21).

我应该怎么使用?

推荐答案

它不会在这种情况下,无所谓。

It doesn't matter in this case.

在内部,第一件事 .SetExpires 所做的就是将您提供的日期时间为UTC,设置它的cookie之前。

Internally, the first thing .SetExpires does is convert your supplied datetime into UTC, before setting it on the cookie.

记住,只要你的日期时间消费者正确地使用DateTime类,那么这两个是相同的 - 它只是一个基线为UTC,另一种是不:

Bear in mind, as long as your datetime consumer uses the DateTime class correctly, then the two are the same - it is just that one is "baselined" to UTC and the other isn't:

20110701T14:00:00-1:00 (British Summer Time)

20110701T13:00:00+0:00 (UTC)

再present完全相同的日期时间,即下午1点UTC。

represent exactly the same datetime, namely 1pm UTC.

只要消费者能正确处理这(它似乎,有看着反射器),那么它没有什么区别。

As long as the consumer handles this correctly (which it seems to, having looked in reflector) then it makes no difference.

如果你服用并在将它作为一个时间字符串,那么当然,它可能使一个区别,但在这种情况下

If you were taking this and passing it in as a time string, then of course, it may well make a difference, but not in this case.

您可以看到下面的code中的影响(假设你是不是在自己的UTC - 如果你是 - 改变你的设置进行测试)。他们都输出相同的日期时间,一旦你问它转换为UTC。

You can see the effect with the following code (assuming you are not in UTC yourself - if you are - change your settings to test!). They both output the same datetime, once you've asked for it to be converted to UTC.

WriteDateTime(DateTime.Now);
WriteDateTime(DateTime.UtcNow);

public static void WriteDateTime(DateTime dateTime)
{
   Console.WriteLine(dateTime.ToUniversalTime().ToLongTimeString());   
}

这篇关于我应该使用DateTime.Now或DateTime.UtcNow在HttpCookie.Expires和HttpCachePolicy.SetExpires?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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