Topic: LINUX

Rails DateTime.to_time, Time, and a case of ‘Why do you need to do that?’

balboa_clock

The problem: I needed to display a warning to a user if the data they were looking at was more than 90 days old.

The solution: Create a method that takes 2 dates (either DateTime or Time), and returns the number of days, or hours between them.

  def self.difference_in_dates(date1, date2, unit = 1.day)
    return nil if date1.nil? || date2.nil? || unit == 0
      (( date1.to_time - date2.to_time ) / unit).round.abs
  end

The problem was simple enough, and my tests were all passing, so I moved on to my next task.

That code has been out in production for several months, but earlier this week, a new developer told me he got an error when running the test:

  NoMethodError: undefined method `to_f'
    for Mon, 21 Sep 2009 14:29:38 -0500:DateTime

(we're running this in Rails 2.0.2)

I looked at the code, knowing it was working before, ran the unit tests myself, and didn't see the issue. Now I'm on Windows and everyone else is on a mac, so as soon as I run into an issue that no one else has seen I want to prove if its a Windows problem. But wait, this test has been running in our Continuous Integration server (Hudson) for months, and no one else on the team ever had any issues with it, and the code has been working in production without any errors in the logs.

I jumped into rails script/console to see what's up, and here's what I found:

 >> x = DateTime.now
=> Wed, 23 Sep 2009 00:00:00 +0000
>>; x.to_time
=> Wed Sep 23 00:00:00 UTC 2009
>> x.to_time.to_f
=> 1253664000.0

Which is what I expected, but when I asked the other developer to run that same instruction, he got an error.

>> DateTime.now.to_time.to_f
NoMethodError: undefined method `to_f'
  for Mon, 21 Sep 2009 14:29:38 -0500:DateTime

What's up with that? We're running the same code, and all of our libraries are the same version. Looking at the date value in his error, I saw the timezone, and decided to try this variation locally:

>>  x = DateTime.parse("2009-09-21T14:29:38-0500")
=> Mon, 21 Sep 2009 14:29:38 -0500
>> x.to_time.class
=>; DateTime

So I'm gathering that when there is a timezone and you ask DateTime.to_time, its just going to give you back a DateTime.
Continue reading »

What will Happen to a Giant

StayPuft

Although I’ve seen so many players rise and fall over the years, what has inspired this post is the irony of what is happening to Microsoft. In the early years of my technology career it was Microsoft’s ownership of the PC O/S market that primarily allowed other compatible hardware manufacturers to create innovation and eventually marginalize IBM’s dominance of that market.

In the early years of PCs, the applications on top of the O/S weren’t even Microsoft applications. It was only later that Microsoft started developing their own applications and with them the predatory practice of squeezing out other application providers to rule the desktop.

Continue reading »

Launch: Pathfinder Newsletter

    Get a monthly update on best practices for delivering successful software.

    Subscribe via email


    Subscribe via RSS      RSS icon

Topics

Search

WordPress

Comments about this site: info@pathf.com