tags:

views:

104

answers:

2

Hi

I'm trying to understand how the implementation of the Now attribute in DateTime works. My background is mainly Python and Haskell so I can't by my life understand how the Now attribute can "return" different values depending on when you use it.

My intuition says that Now should really be a function which does some low-level magic followed by some high-level magic and then returns a DateTime object with the correct time.

+15  A: 

DateTime.Now isn't an attribute, it's a static readonly property.

Under the covers a readonly property is just a function call that returns a value, so it can do any amount of processing it wants to.

Hope this helps.

Binary Worrier
Of course, I should have thought about that! Thanks.
Deniz Dogan
Yea, same goes for `DateTime.Today`, `DateTime.UtcNow`
o.k.w
+4  A: 

It's not an attribute, it's a static property on the DateTime class that looks like the following:

public static DateTime Now
{
    get
    {
        return UtcNow.ToLocalTime();
    }
}

UtcNow is another property on DateTime that returns the following:

return new DateTime((ulong) ((GetSystemTimeAsFileTime() + 
  0x701ce1722770000L) | 0x4000000000000000L));

GetSystemTimeAsFileTime is a Windows API Call.

Pete OHanlon