At work today, I was stuck on an odd bug that had to deal with a datetime column. Or so I thought. Perhaps I just discovered the secret to time travel.
Observe:
>> "2010-10-10".to_time
=> Sun Oct 10 00:00:00 UTC 2010
>> "112-01-20".to_time
=> Fri Jan 20 00:00:00 UTC 2012
Apparently, there’s some weirdness with Ruby and the Time class where if you want the years 0-138AD, you can’t have them. That’s because they don’t exist. What you actually get when you try to turn the year 112AD into a Time object is space travel to the year 2012.
How to get around that? Use DateTime.
>> "112-01-20".to_datetime
=> Tue, 20 Jan 0112 00:00:00 +0000

Leave a Reply