Differences in Time v1.8.6 and 1.9.1

Hello,
I wrote a ruby-script that used Time on a machine with v1.9.1. When I
now try to use it on a machine with v1.8.6, the Time-part is acting up.
It looks to me like the strftime-option gets ignored totally in 1.8.6.
It should give me todays date + the number of the last hour ie
‘2010-05-11 13’, if the time was 14:27 when the script ran, but it gives
me either ‘Tue May 11 …’ or ‘Thu Jan 01 … 1970’. On the 1.9.1 system
it worked just fine, but it has never worked on the 1.8.6 system.

Was there a major rewrite of Time between 1.8.6 and 1.9.1 or is my
script just plain wrong?

Write the smallest test program you can which demonstrates the issue. In
particular, remove the mysql dependency and see if you can replicate the
problem. Then you can either eliminate or show that mysql is the cause
of the problem.

As far as I can see here, strftime works the same on both:

$ ruby -v
ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]
$ ruby -e ‘puts Time.now.strftime("%Y-%m-%d %H")’
2010-05-11 13
$ ruby19 -v
ruby 1.9.2dev (2009-07-18 trunk 24186) [i686-linux]
$ ruby19 -e ‘puts Time.now.strftime("%Y-%m-%d %H")’
2010-05-11 13

Yes, that worked for me too.

But I’m still stuck, as I need the mysql-access. Why would mysql break
the Time functionality btw?

Daniel He wrote:

Yes, that worked for me too.

But I’m still stuck, as I need the mysql-access. Why would mysql break
the Time functionality btw?

I’m not saying it is; but you asserted that “the strftime-option gets
ignored totally in 1.8.6”, and I don’t see any evidence of that so far.

What you need to do is to make a smallest-possible test case. Keep
trimming code out of your program until it stops demonstrating the
problem. In the process of doing this, you will probably discover for
yourself where the problem lies and how to work around it. If you can’t,
then at least you’ll have a simple test case which can be used by others
to reproduce it.

We can’t run your code as it is, because we don’t have the mysql
database it depends on; and even if we did, we probably couldn’t be
bothered setting it up unless you’ve first demonstrated that mysql is
an essential part of reproducing your issue.

Just glancing at your code I can see it does some weird things:

    t = Time.now.strftime("%Y-%m-%d %H").to_i

t = Time.now

    print t.to_s + "\n"
    t2 = t.to_i - 3600
    t2 = Time.at(t2)
    print t2.to_s + "\n"
    t3 = t2.to_s.slice! 0..12
    print t3.to_s + "\n"

That strftime line is just a very strange way of extracting the year
(only) from the time:

t = Time.now.strftime("%Y-%m-%d %H")
=> “2010-05-11 14”

t.to_i
=> 2010

You haven’t even shown us what those print lines show. Are they what you
expect? If not, what do you see, and what did you expect to see? Or is
the problem occurring later on in the code?

[ruby 1.8]

 Returns a string representing _time_. Equivalent to calling
 +Time#strftime+ with a format string of ``+%a+ +%b+ +%d+ +%H:%M:%S+
 +%Z+ +%Y+''.

[ruby 1.9]

 Returns a string representing _time_. Equivalent to calling
 +Time#strftime+ with a format string of ``+%Y-%m-%d+ +%H:%M:%S+
 +%z+'' for a local time and ``+%Y-%m-%d+ +%H:%M:%S+ +UTC+'' for a
 UTC time.

Thanks, there was a rewrite of Time between versions after all. Now it’s
working fine!

You haven’t even shown us what those print lines show. Are they what you
expect? If not, what do you see, and what did you expect to see? Or is
the problem occurring later on in the code?

If I run just those lines, I get:

[1.8.6p114]
2010
Thu Jan 01 00:33:30 +0100 1970
Thu Jan 01 00

[1.9.2preview2]
2010
1970-01-01 00:33:30 +0100
1970-01-01 00

So the difference is simply that Time#to_s has a different default
format.

$ ruby -e ‘puts Time.now.to_s’
Tue May 11 15:02:06 +0100 2010
$ ruby19 -e ‘puts Time.now.to_s’
2010-05-11 15:02:08 +0100

If that’s the problem, then use Time#strftime to make it explicit what
format you want.

The documentation of Time#to_s is clear:

[ruby 1.8]

 Returns a string representing _time_. Equivalent to calling
 +Time#strftime+ with a format string of ``+%a+ +%b+ +%d+ +%H:%M:%S+
 +%Z+ +%Y+''.

    Time.now.to_s   #=> "Wed Apr 09 08:56:04 CDT 2003"

[ruby 1.9]

 Returns a string representing _time_. Equivalent to calling
 +Time#strftime+ with a format string of ``+%Y-%m-%d+ +%H:%M:%S+
 +%z+'' for a local time and ``+%Y-%m-%d+ +%H:%M:%S+ +UTC+'' for a
 UTC time.

    Time.now.to_s       #=> "2007-10-05 16:09:51 +0900"
    Time.now.utc.to_s   #=> "2007-10-05 07:09:51 UTC"

Remember also that Time.at(2010) will give you the time 2,010 seconds
after midnight at 1 Jan 1970.