-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: DateTime.MaxDate can trip you up
PostPosted: Thu Jan 24, 2008 7:33 pm 
Newbie

Joined: Fri Jan 18, 2008 7:45 pm
Posts: 18
Location: Eugene, OR
Be careful when inserting a very precise datetime into SQL Server. (2000 or 2005, I think 2008 upped the precision on datetime.) Specifically be very careful when inserting a value of DateTime.MaxValue

If you assign MaxValue to your entity, save it, then later check for entity.Time == DateTime.MaxValue you'll probably get highly confused. This is easily solved by converting the MaxValue to the highest precision that SQL Server can handle. To do this I wrote an extension method for DateTime as follows:

Code:
    public static class DateTimeExtensions
    {
        public static DateTime ToSQLDateTime(this DateTime dt)
        {
            return new DateTime(dt.Ticks - (dt.Ticks % 10000000));           
        }
    }


Now it'll show up along with ToShortTimeString, ToLocalTime etc.
Code:
if(entity.Time == DateTime.MaxValue.ToSqlDateTime())

...

_________________
Woil / Will Shaver / http://primedigit.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.