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

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

Cookies send GMT time, but I donโ€™t know what will happen if I send DateTime.Now.AddDays(3) if I am at GMT +5. Same thing with the HTTP header expiring (sec. 14.21) .

What should i use?

+18
gmt globalization
Jan 31 '11 at 10:21
source share
2 answers

In this case, it does not matter.

Inside, the first thing .SetExpires does is convert the time you set to UTC before setting it to a cookie.

Remember that as long as your datetime consumer uses the DateTime class correctly, the two are the same - it's just โ€œbasicโ€ for UTC, and the other is not:

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

and

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

represent exactly the same time and time, namely 1pm UTC.

As long as the consumer processes correctly (which seems to be by looking into the reflector), this does not matter.

If you take this and pass it as a time string, then of course it may matter, but not in this case.

You can see the effect with the following code (if you are not in UTC on your own - if you are - change your settings to check!). They both output the same time as soon as you asked him to convert to UTC.

 WriteDateTime(DateTime.Now); WriteDateTime(DateTime.UtcNow); public static void WriteDateTime(DateTime dateTime) { Console.WriteLine(dateTime.ToUniversalTime().ToLongTimeString()); } 
+16
Jan 31 '11 at 11:07
source

You should use the DateTime.UtcNow method because it is the time standard used for cookies. UTC is equivalent to GMT.

From MSDN: System.DateTime.UtcNow

Gets a DateTime object that is set to the current date and time on this computer, expressed as coordinated Universal Time (UTC).

Refer to this for an explanation between the two.

+1
Jan 31 '11 at 10:43
source



All Articles