require 'date' class Date FOS_JD_OFFSET_DAYS = Date.parse('1899-12-31').jd unless defined?(FOS_JD_OFFSET_DAYS) def self.from_fos_days(days) self.jd(FOS_JD_OFFSET_DAYS + days) end def to_fos_days jd - FOS_JD_OFFSET_DAYS end ######## from Ruby Cookbook ######### def to_gm_time to_time_special(new_offset, :gm) end def to_local_time to_time_special(new_offset(DateTime.now.offset-offset), :local) end private def to_time_special(dest, method) #Convert a fraction of a day to a number of microseconds usec = (dest.send(:sec_fraction) * 60 * 60 * 24 * (10**6)).to_i Time.send(method, dest.year, dest.month, dest.day, dest.send(:hour), dest.send(:min), dest.send(:sec), usec) end end module TimeFunctions def as_minutes hour*60 + min end def in_hundredths return 0 if hour + min == 0 minutes = hour*60 + min val = "#{minutes/60}.#{((100*(minutes.modulo(60)))/60).to_s.rjust(2, '0')}" BigDecimal.new(val, 2).round(2).to_f end # xos is rounding XOJET STYLE, which is a funky copy of how ipc did it def in_hundredths_rounded_xos return 0 if hour + min == 0 # It was determined that "ODD" increments of .05 (ie .15, .35, .55, .75, .95) of an hour (ie. 9,21,33,45,57 minutes) rounded UP whereas even increments of .05 rounded DOWN - sn minutes = as_minutes (minutes/60.0 + ([9, 21, 33, 45, 57].include?(min) ? 0.001 : -0.001)).round(2) end # xos is rounding XOJET STYLE, which is a funky copy of how ipc did it def in_tenths_rounded_xos return 0 if hour + min == 0 # It was determined that "ODD" increments of .05 (ie .15, .35, .55, .75, .95) of an hour (ie. 9,21,33,45,57 minutes) rounded UP whereas even increments of .05 rounded DOWN - sn minutes = as_minutes (minutes/60.0 + ([9, 21, 33, 45, 57].include?(min) ? 0.01 : -0.01)).round(1) end def hm strftime("%H:%M") end def to_fos_days to_date.to_fos_days end end class Time include TimeFunctions alias :original_to_s :to_s def to_s(args=nil) return self.strftime('%H:%M') if args == :hm or !args self.original_to_s(args) end # using a date of 2000,1,1 is arbitrary def self.from_fos_time(minutes) minutes = 0 unless minutes Time.utc(2000, 1, 1) + minutes.minutes end class << self alias from_minutes from_fos_time end ######## from Ruby Cookbook ######### def to_datetime # Convert seconds + microseconds into a fractional number of seconds seconds = sec + Rational(usec, 10**6) # Convert a UTC offset measured in minutes to one measured in a # fraction of a day. offset = Rational(utc_offset, 60 * 60 * 24) DateTime.new(year, month, day, hour, min, seconds, offset) end # This is a view "February 01, 2010 01:40" that be parsed directly by javascript # ie. in javascript you can say: Date.parse( "this to long view" ) and you # will get a date object def long_view self.strftime('%B %d, %Y %H:%M') end end class DateTime include TimeFunctions # creates utc time from days and minutes def self.from_fos_date_time(days, minutes) days = 0 unless days minutes = 0 unless minutes (Date.from_fos_days(days).to_gm_time + minutes.minutes).to_datetime end def view to_s end # This is a view "February 01, 2010 01:40" that be parsed directly by javascript # ie. in javascript you can say: Date.parse( "this to long view" ) and you # will get a date object def long_view self.strftime('%B %d, %Y %H:%M') end # Converts self to a Ruby Date object; time portion is discarded def to_date ::Date.new(year, month, day) end unless method_defined?(:to_date) end