rubymotion-community / motion-support

Commonly useful extensions to the standard library for RubyMotion
MIT License
132 stars 28 forks source link

end of day or year rounding issues #35

Open dam13n opened 8 years ago

dam13n commented 8 years ago

If I store an end_of_year time object in NSUserDefaults, there is a rounding error due to the microseconds that ups it to the beginning of the next year. See below (where $defaults is my NSUserDefaults object):

Time.local(2015, 12, 31, 23, 59, 59, Rational(999999999, 1000))
=> 2015-12-31 23:59:59 -0800

$defaults[:filters_date_to] = Time.local(2015, 12, 31, 23, 59, 59, Rational(999999999, 1000))
=> 2015-12-31 23:59:59 -0800

$defaults[:filters_date_to]
=> 2016-01-01 00:00:00 -0800

motion-support code in question is below. I don't know a safe way to fix this w/o a dirty hacking, so it's more of any FYI to other users for now.

# Returns a new Time representing the end of the day, 23:59:59.999999 (.999999999 in ruby1.9)
  def end_of_day
    change(
      :hour => 23,
      :min => 59,
      :sec => 59,
      :usec => Rational(999999999, 1000)
    )
  end
tkadauke commented 8 years ago

Are you able to track down whether this is a motion-support but or a RubyMotion bug? There used to be some rounding issues in RubyMotion in the past.

dam13n commented 8 years ago

@tkadauke No, didn't look into it further. I just used some workaround in my code.