Wrong Iteration over time #1503

Closed
ustuntas opened this Issue Jun 6, 2011 · 4 comments

Comments

Projects
None yet
3 participants
@ustuntas

ustuntas commented Jun 6, 2011

I am not sure this is a bug or this is my mistake,

My rails version is 3.0.05 and ruby version is ruby 1.9.2p0

When i want to iterate over time like that

(Time.now.to_s(:db)..(Time.now + 1.hour).to_s(:db)).each {|t| puts t }

It gives me like that

...
2011-06-06 12:99:60
2011-06-06 12:99:61
2011-06-06 12:99:62
2011-06-06 12:99:63
2011-06-06 12:99:64
2011-06-06 12:99:65
2011-06-06 12:99:66
2011-06-06 12:99:67
2011-06-06 12:99:68
2011-06-06 12:99:69
2011-06-06 12:99:70
2011-06-06 12:99:71
2011-06-06 12:99:72
2011-06-06 12:99:73
2011-06-06 12:99:74
2011-06-06 12:99:75
2011-06-06 12:99:76
2011-06-06 12:99:77
2011-06-06 12:99:78
2011-06-06 12:99:79
2011-06-06 12:99:80
...

Regards,

Murat USTUNTAS

@htanata

This comment has been minimized.

Show comment
Hide comment
@htanata

htanata Jun 6, 2011

Contributor

What you're doing is incrementing the String value of the time. What you should do is something like this, where the time is converted to integer first, since Ruby can't iterate on Time range:

(Time.now.to_i..(Time.now.to_i + 1.hour)).each { |t| puts Time.at(t).to_s(:db) }
Contributor

htanata commented Jun 6, 2011

What you're doing is incrementing the String value of the time. What you should do is something like this, where the time is converted to integer first, since Ruby can't iterate on Time range:

(Time.now.to_i..(Time.now.to_i + 1.hour)).each { |t| puts Time.at(t).to_s(:db) }
@Roman2K

This comment has been minimized.

Show comment
Hide comment
@Roman2K

Roman2K Jun 6, 2011

@htanata
With your suggestion, potential time zone information (from the starting time) is lost, going from Time to Integer and back to Time. I suggest:

start = Time.now
(0.seconds .. 1.hour).each do |offset|
  time = start + offset
  puts time.to_s(:db)
end

Roman2K commented Jun 6, 2011

@htanata
With your suggestion, potential time zone information (from the starting time) is lost, going from Time to Integer and back to Time. I suggest:

start = Time.now
(0.seconds .. 1.hour).each do |offset|
  time = start + offset
  puts time.to_s(:db)
end
@ustuntas

This comment has been minimized.

Show comment
Hide comment
@ustuntas

ustuntas Jun 6, 2011

Thank you for your suggestions..

I changed the code as Roman2K says.. It is ok now..

Regards,

Murat USTUNTAS

ustuntas commented Jun 6, 2011

Thank you for your suggestions..

I changed the code as Roman2K says.. It is ok now..

Regards,

Murat USTUNTAS

@ustuntas ustuntas closed this Jun 6, 2011

@htanata

This comment has been minimized.

Show comment
Hide comment
@htanata

htanata Jun 6, 2011

Contributor

You're right, @Roman2K. That's a better way of doing it :)

Contributor

htanata commented Jun 6, 2011

You're right, @Roman2K. That's a better way of doing it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment