Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime comparing by internal ticks?

I looked at DateTime Equals implementation :

public bool Equals(DateTime value)
{
    return (this.InternalTicks == value.InternalTicks);
}

and then look at internalticks

internal long InternalTicks
{
    [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
    get
    {
        return (((long) this.dateData) & 0x3fffffffffffffffL);
    }
}

And then I noticed this number : 0x3fffffffffffffffL

which is : 4611686018427387903

But more interesting is its binary representation :

00111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111
^^
||

Please notice the arrows

I could have understand if only the left arrow would have been 0 ( positive representation)

  • But why the second one is also 0 ?

  • Also , why would i every want it to be & with a 1111.... number ? if I want to display 5 I don't have to do 5 & 1 , just 5.

Any help?

like image 753
Royi Namir Avatar asked Nov 13 '12 12:11

Royi Namir


1 Answers

You can get this kind of information from the Reference Source. The most relevant declarations in dd/ndp/clr/src/bcl/system/datetime.cs:

    private const UInt64 TicksMask             = 0x3FFFFFFFFFFFFFFF;
    private const UInt64 FlagsMask             = 0xC000000000000000;
    private const UInt64 LocalMask             = 0x8000000000000000;
    private const Int64 TicksCeiling           = 0x4000000000000000;
    private const UInt64 KindUnspecified       = 0x0000000000000000;
    private const UInt64 KindUtc               = 0x4000000000000000;
    private const UInt64 KindLocal             = 0x8000000000000000;
    private const UInt64 KindLocalAmbiguousDst = 0xC000000000000000;
    private const Int32 KindShift = 62;

Note how the Kind values map to those two bits.

    public DateTime(long ticks, DateTimeKind kind) {
        // Error checking omitted
        //...
        this.dateData = ((UInt64)ticks | ((UInt64)kind << KindShift));
    }
like image 51
Hans Passant Avatar answered Oct 07 '22 07:10

Hans Passant