G
Guest
What determines if two DateTime objects compare equal using the DateTime
Equals method? I am wrapping DateTime to provide some time-zone awareness
and have run into a problem with the DateTime Equals. A short contrived
example that shows the problem I am having is the following:
DateTime foo1 = DateTime.Now;
DateTime foo2 = foo1.ToUniversalTime();
DateTime foo3 = foo2.ToLocalTime();
After this, foo1, foo2, and foo3 all have identical values for Year, Month,
Day, Hours, Minutes, Seconds, and Millisecond properties, but NOT the Ticks
property which is different between all three. The DateTime equals
comparing any two of the objects returns false....apparently because the
Ticks?
Is this indeed the case, and do all conversions alter the Ticks count - in
effect meaning that converted values are not 'round-trippable' and the
resulting objects not comparable for equality?
I am considering overriding Equals to take care of this -- for this
application we don't need the Ticks for anything but I want to make sure I am
not missing something..
Thank you,
Equals method? I am wrapping DateTime to provide some time-zone awareness
and have run into a problem with the DateTime Equals. A short contrived
example that shows the problem I am having is the following:
DateTime foo1 = DateTime.Now;
DateTime foo2 = foo1.ToUniversalTime();
DateTime foo3 = foo2.ToLocalTime();
After this, foo1, foo2, and foo3 all have identical values for Year, Month,
Day, Hours, Minutes, Seconds, and Millisecond properties, but NOT the Ticks
property which is different between all three. The DateTime equals
comparing any two of the objects returns false....apparently because the
Ticks?
Is this indeed the case, and do all conversions alter the Ticks count - in
effect meaning that converted values are not 'round-trippable' and the
resulting objects not comparable for equality?
I am considering overriding Equals to take care of this -- for this
application we don't need the Ticks for anything but I want to make sure I am
not missing something..
Thank you,