On 16 June 2010 15:54, Andy Jeffries <a...@andyjeffries.co.uk> wrote:
> These values are then converted to seconds and stored as decimal in the DB >> (I allow fractions of a second, too (tenth, hundredth, millisecond). when >> they are retrieved form the DB, I convert the seconds to a string format of, >> for example, 01:39:11 (from 5951 seconds). If fractions are used, they are >> just appended to the string. (non of this is elegant IMO) >> >> That's all done in my Converter class. I am going to write a Calculator >> class to compare all times entered for an event and picking the closest >> times to how long the actual event took. >> >> So my question is, I know I can't use a Time object but it would be really >> nice to know if there is something out there that could help me with this as >> to write my own Calculator is going to be a bit of a pain. >> > > If you have two floating point values representing seconds (and parts of) > why can't you just compare the two floating point values? > > What part are you stuck with? Can you give us some code with a comment on > which bit you can't do. > > > Andy > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-t...@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscr...@googlegroups.com<rubyonrails-talk%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > I don't have a problem with comparing floats, but I might need to find the 10 closest times to the actual event time and 4 of those times could be less than the actual event time and 6 could be higher. I don't mind rolling my own and am quite looking forward to it, but I don't want to re-invent the proverbial wheel. FWIW, here's the conversion code to convert integer values to their respective seconds. @@time_constants = {:years => YEAR, :months => MONTH, :weeks => WEEK, :days => DAY, :hours => HOUR, :mins => MIN } def to_decimal(time_units={}) raise ArgumentError if time_units.empty? time_units.each_pair do |key, value| ## skip seconds and fractions thereof next unless @@time_constants.has_key?(key) ## convert integer time units into seconds @decimal_time += @@time_constants[key] * value end ## whack on the seconds @decimal_time += time_units[:secs] if time_units.has_key?(:secs) ## need validation (can't have hundtedth without tenth, for example) fraction = '' fraction = '.' + time_units[:tenth].to_s if time_units.has_key?(:tenth) fraction << time_units[:hundredth].to_s if time_units.has_key?(:hundredth) fraction << time_units[:millisecond].to_s if time_units.has_key?(:millisecond) ## concat fraction string to @decimal_time, convert to float for DB @decimal_time = (@decimal_time.to_s << fraction).to_f end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.