Sha256: c406220339428e7ea5d6c7239bfb7743fad05417d9214e08274c556c2f212173

Contents?: true

Size: 1.96 KB

Versions: 27

Compression:

Stored size: 1.96 KB

Contents

class Date
  FOS_JD_OFFSET_DAYS = Date.parse('1899-12-31').jd
  DATE_FORMATS[:mdy] = "%m/%d/%Y"

  def self.from_fos_days(days)
    self.jd(FOS_JD_OFFSET_DAYS + days)
  end

  def to_fos_days
    jd - FOS_JD_OFFSET_DAYS
  end
end

module TimeFunctions

  def self.included(klass)
    klass.send(:include,TimeFunctions::InstanceMethods)
    klass.send(:extend,TimeFunctions::ClassMethods)
  end

  module InstanceMethods
    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(1).to_s
    end

    def in_hundredths_rounded_XOJET_style
      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 = hour*60 + min
      (minutes/60.0 + ([9, 21, 33, 45, 57].include?( min ) ? 0.001 : -0.001)).round(2)
      #minutes/60.0
    end

    def in_tenths_rounded_XOJET_style
      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 = hour*60 + min
      (minutes/60.0 + ([9, 21, 33, 45, 57].include?( min ) ? 0.01 : -0.01)).round(1)
    end
  end

  module ClassMethods
    # creates utc time from days and minutes
    def from_fos_date_time(days, minutes)
      from_fos_time(days*1440 + minutes)
    end

    # creates utc time from minutes
    def from_fos_time(minutes)
      DateTime.new(1899, 12, 31).advance(:minutes=>minutes)
    end
  end
end

class Time
  include TimeFunctions
  DATE_FORMATS[:hm] = "%H:%M"

  alias :original_to_s :to_s

  def to_s(args=nil)
    args = :hm unless args
    self.original_to_s(args)
  end
end

class DateTime
  include TimeFunctions
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
fossil-0.3.22 lib/sequel/fos_dates.rb
fossil-0.3.21 lib/sequel/fos_dates.rb
fossil-0.3.20 lib/sequel/fos_dates.rb
fossil-0.3.19 lib/sequel/fos_dates.rb
fossil-0.3.18 lib/sequel/fos_dates.rb
fossil-0.3.17 lib/sequel/fos_dates.rb
fossil-0.3.16 lib/sequel/fos_dates.rb
fossil-0.3.15 lib/sequel/fos_dates.rb
fossil-0.3.14 lib/sequel/fos_dates.rb
fossil-0.3.13 lib/sequel/fos_dates.rb
fossil-0.3.12 lib/sequel/fos_dates.rb
fossil-0.3.11 lib/sequel/fos_dates.rb
fossil-0.3.10 lib/sequel/fos_dates.rb
fossil-0.3.9 lib/sequel/fos_dates.rb
fossil-0.3.8 lib/sequel/fos_dates.rb
fossil-0.3.7 lib/sequel/fos_dates.rb
fossil-0.3.6 lib/sequel/fos_dates.rb
fossil-0.3.5 lib/sequel/fos_dates.rb
fossil-0.3.4 lib/sequel/fos_dates.rb
fossil-0.3.3 lib/sequel/fos_dates.rb