我应该在 HttpCookie.Expires 和 HttpCachePolicy.SetExpires 中使用 DateTime.Now 还是 DateTime.UtcNow?
我应该在 HttpCookie.Expires
和 HttpCachePolicy.SetExpires
中使用 DateTime.Now 或 DateTime.UtcNow 吗?
Cookie 正在发送“GMT” 时间,但我不知道如果如果我的时间是 GMT+5,我会发送 DateTime.Now.AddDays(3)
。与HTTP 标头过期(第 14.21 秒) 相同。
我应该用什么?
Should I use DateTime.Now or DateTime.UtcNow in HttpCookie.Expires
and HttpCachePolicy.SetExpires
?
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).
What should I use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下并不重要。
在内部,
.SetExpires
所做的第一件事是将您提供的日期时间转换为 UTC,然后再将其设置到 cookie 上。请记住,只要您的日期时间使用者正确使用 DateTime 类,那么两者都是相同的 - 只是一个以 UTC 为“基线”,而另一个则不是:
并
表示完全相同的日期时间,即下午 1 点世界标准时间。
只要消费者正确处理这个问题(在反光镜中查看后似乎是这样),那么就没有什么区别。
如果您将其作为时间字符串传递,那么当然,它很可能会产生影响,但在本例中并非如此。
您可以使用以下代码查看效果(假设您自己不在 UTC 中 - 如果是 - 更改您的设置进行测试!)。一旦您要求将其转换为 UTC,它们都会输出相同的日期时间。
It doesn't matter in this case.
Internally, the first thing
.SetExpires
does is convert your supplied datetime into UTC, before setting it on the cookie.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:
and
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.
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.
您应该使用 DateTime.UtcNow 方法,因为这是 cookie 使用的时间标准。 UTC 相当于 GMT。
来自 MSDN:System.DateTime.UtcNow
请参阅此了解它们之间的说明。
You should be using DateTime.UtcNow method because thats the time standard used for cookies. UTC is equivilant to GMT.
From MSDN: System.DateTime.UtcNow
Refer to this for an explanation between them.