Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 436791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:30:48+00:00 2026-05-12T20:30:48+00:00

The examples for Cache.Add uses DateTime.Now.Add to compute the expiration, i.e. it passes: DateTime.Now.AddSeconds(60)

  • 0

The examples for Cache.Add uses DateTime.Now.Add to compute the expiration, i.e. it passes:

 DateTime.Now.AddSeconds(60)

as the value of the absoluteExpiration parameter.

I’d have thought that computing it relative to DateTime.UtcNow would be more correct [as there is no ambiguity if Daylight Savings Time starts in the intervening time between now and the expiration point].

Before the introduction of DateTimeKind, I’d have guessed that there’s some ugly hacks in the cache management to make it do something appropriate if the time was not a UTC time.

In .NET 2.0 and later, I’m guessing that it should handle a DateTime calculated as DateTime.UtcNow.AddSeconds(60) correctly given that it has DateTime.Kind to use as an input in its inferences.

I’ve been confidently using DateTime.UtcNow as the base for years, but wasnt able to come up with a rationale that this is definitely the correct thing to do in the absence of anything pointing out the documentation has been highly misleading for 4+ years.

The questions?

  1. Despite much bingage and googling I wasnt able to find any authoritative discussion on this from MS – can anyone locate something regarding this?
  2. Is there any reason why using UtcNow wouldnt be more correct and/or safe?

(Yes, I could peruse the source and/or the Reflector’d source, but am looking for a full blow-by-blow lowdown!)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T20:30:48+00:00Added an answer on May 12, 2026 at 8:30 pm

    I reported this bug on Microsoft Connect some time ago, but it’s been closed as won’t fix.

    You still have a problem in .NET 2.0 if you specify your absolute expiration in local time.

    During one hour at the end of daylight savings time, your local time is ambiguous, so you can get unexpected results, i.e. the absolute expiration can be one hour longer than expected.

    In Europe, daylight savings time ended at 02:00 on 25 October 2009. The sample below illustrates that if you placed an item in the cache at 01:59 with an expiration of 2 minutes, it would remain in the cache for one hour and two minutes.

    DateTime startTime = new DateTime(2009, 10, 25, 1, 59,0);
    DateTime endTime = startTime.AddMinutes(2);
    // end time is two minutes after start time
    
    DateTime startUtcTime = startTime.ToUniversalTime();
    DateTime endUtcTime = endTime.ToUniversalTime();
    // end UTC time is one hour and two minutes after start UTC time
    
    Console.WriteLine("Start UTC time = " + startUtcTime.ToString());
    Console.WriteLine("End UTC time = " + endUtcTime.ToString());
    

    The workaround for .NET 2.0 or later is to specify the absolute expiration time in UTC as pointed out by Ruben.

    Microsoft should perhaps be recommending to use UTC in the examples for absolute expiration, but I guess there is the potential for confusion since this recommendation is only valid for .NET 2.0 and later.

    EDIT

    From the comments:

    But the exposure only occurs if the
    conversion happens during the overlap.
    The single conversion actually taking
    place is when you lodge the item with
    Cache.Add

    The problem will only happen if you insert an item in the cache with an AbsoluteExpiration time in local time during that one ambiguous hour at the end of daylight savings time.

    So for example, if your local time zone is Central European (GMT+1 in winter, GMT+2 in summer), and you execute the following code at 01:59:00 on 25 October 2009:

    DateTime absoluteExpiration = DateTime.Now.AddMinutes(2);
    Cache.Add(... absoluteExpiration ...)
    

    then the item will remain in the cache for one hour and two minutes, rather than the two minutes you would normally expect. This can be a problem for some highly time-critical applications (e.g. stock ticker, airline departures board).

    What’s happening here is (assuming European time, but the principle is the same for any time zone):

    • DateTime.Now = 2009-10-25 01:59:00 local. local=GMT+2, so UTC = 2009-10-24 23:59:00

    • .AddMinutes(2) = 2009-10-25 02:01:00 local. local = GMT+1, so UTC = 2009-11-25 01:01:00

    • Cache.Add internally converts the expiration time to UTC (2009-11-25 01:01:00) so the expiration is one hour and two minutes ahead of the current UTC time (23:59:00).

    If you use DateTime.UtcNow in place of DateTime.Now, the cache expiration will be two minutes (.NET 2.0 or later):

    DateTime absoluteExpiration = DateTime.UtcNow.AddMinutes(2);
    Cache.Add(... absoluteExpiration ...)
    

    From the comments:

    Or am I missing something?

    No you’re not. Your analysis is spot on and if your application is time-critical and runs during that period at the end of DST, you’re right to be using DateTime.UtcNow.

    The statement in Ruben’s answer that:

    you’re safe to use either as long as the Kind on the time you supply is set

    is incorrect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 265k
  • Answers 265k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer For printing you can use window.print(). There is no standard… May 13, 2026 at 12:33 pm
  • Editorial Team
    Editorial Team added an answer In your saveImage method, post a notification just after finishing… May 13, 2026 at 12:32 pm
  • Editorial Team
    Editorial Team added an answer That corresponds to the long (or Int64), a 64-bit integer.… May 13, 2026 at 12:32 pm

Related Questions

When writing a Java program, do I have influence on how the CPU will
I have an ASP.Net website which uses a MySQL database for the back end.
I have Apache HTTPD configured to add a cache header to requests for most
The standard example for implementing LRU Cache in Java points to the example depot

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.