The greatest thing DHH ever did
I was writing some SQL Server code to strip the time portion off a date/time value - it took me about half an hour to find a way of doing it that didn't kill performance (as I needed the calculation doing in the where clause of a select statement). The code I came up with was:
declare @DateAndTime datetime
declare @DateOnly datetime
select @DateAndTime = getdate()
select @DateOnly = cast(floor(cast(@DateAndTime as float)) as datetime)
select @DateAndTime as DateAndTime, @DateOnly as DateOnly
Half an hour for something that ought to be really simple. It made me think that date/time handling is pretty poor in nearly all languages.
Which is why the best thing that DHH ever did (if it wasn't him, my apologies to the author) was:
10.minutes.from_now
2.months.ago
Not quite solving the problem described above but absolute genius and the way dates and times should work.
UPDATE
The next version of our application is going to have a denormalised date only field that is populated on a trigger. Ugly but better than the where clause hack shown above.
UPDATE TWO
What's better than
10.minutes.ago
Obviously 10 minutes ago.
No comments:
Post a Comment