module DateTimePrecision

Constants

CENTURY
DATE_ATTRIBUTES
DATE_ATTRIBUTE_PRECISIONS
DAY
DECADE
HOUR
ISO8601_DATE_FRAGMENTS
ISO8601_TIME_FRAGMENTS
MICROSECONDS_SUPPORTED
MIN
MONTH
NEW_DEFAULTS

Default values for y,m,d,h,m,s,frac

NONE
PATCH_VERSION
SEC
USEC
VERSION
YEAR

Public Class Methods

included(base) click to toggle source
# File lib/date_time_precision/lib.rb, line 170
  def self.included(base)
    # Redefine any conversion methods so precision is preserved
    [:to_date, :to_time, :to_datetime].each do |conversion_method|
      # If the conversion method is already defined, patch it
      orig = :"orig_#{conversion_method}"
      if base.method_defined?(conversion_method) && !base.instance_methods(false).map(&:to_sym).include?(orig)
        base.class_eval do
          alias_method orig, conversion_method
          define_method(conversion_method) do |*args|
            d = send(orig, *args)
            d.precision = [self.precision, d.class::MAX_PRECISION].min
            DATE_ATTRIBUTES.each do |attribute|
              d.instance_variable_set(:"@#{attribute}_set", self.instance_variable_get(:"@#{attribute}_set"))
            end
            d
          end
        end
      else
        # Define our own conversion methods by converting to hash first
        require 'date_time_precision/format/hash'
        base.class_eval do
          define_method(conversion_method) do
            to_h.send(conversion_method)
          end
        end
      end

      base.send :public, conversion_method
    end

    # Extend with this module's class methods
    base.extend(ClassMethods)

    # Define attribute query methods, including:
    # year?, mon?, day?, hour?, min?, sec?, usec?
    DATE_ATTRIBUTE_PRECISIONS.each do |attribute_name, precision|
      #next unless precision <= base::MAX_PRECISION

      base.class_eval <<-EOM, __FILE__, __LINE__
        def #{attribute_name}?
          return !@#{attribute_name}_set.nil? ? @#{attribute_name}_set : (self.precision >= #{precision})
        end

        def #{attribute_name}_set=(val)
          @#{attribute_name}_set = !!val
        end
        protected :#{attribute_name}_set=
      EOM
    end

    base.class_eval <<-EOM, __FILE__, __LINE__
      def attributes_set(*vals)
        #{DATE_ATTRIBUTES.map{|attribute| "@#{attribute}_set"}.join(', ')} = *(vals.flatten.map{|v| !!v})
      end
    EOM

    base.instance_eval do
      alias_method :month?, :mon?

      alias_method :mday, :day
      alias_method :mday?, :day?
    end
  end
precision(val) click to toggle source
# File lib/date_time_precision/lib.rb, line 78
def self.precision(val)
  case val
  when Date,Time,DateTime
    val.precision
  when Hash
    case
    when val[:sec_frac], val[:subsec], val[:usec]
      FRAC
    when val[:sec]
      SEC
    when val[:min]
      MIN
    when val[:hour]
      HOUR
    when val[:mday], val[:day], val[:d]
      DAY
    when val[:mon], val[:month], val[:m]
      MONTH
    when val[:year], val[:y]
      YEAR
    when val[:decade]
      DECADE
    when val[:century]
      CENTURY
    else
      NONE
    end
  when Array
    val.index{|v| v.nil?} || val.length
  else
    NONE
  end
end

Public Instance Methods

as_json(*args) click to toggle source
# File lib/date_time_precision/format/json.rb, line 20
def as_json(*args)
  to_h
end
century() click to toggle source
# File lib/date_time_precision/lib.rb, line 138
def century
  year_with_bce_adjustment = (self.year > 0) ? self.year : self.year + 100
  year_with_bce_adjustment - year_with_bce_adjustment % 100
end
decade() click to toggle source
# File lib/date_time_precision/lib.rb, line 133
def decade
  year_with_bce_adjustment = (self.year > 0) ? self.year : self.year + 10
  (year_with_bce_adjustment - year_with_bce_adjustment % 10)
end
fragments() click to toggle source
# File lib/date_time_precision/lib.rb, line 112
def fragments
  frags = []
  frags << year if year?
  frags << month if month?
  frags << day if day?
  frags << hour if hour?
  frags << min if min?
  frags << sec if sec?
  frags << usec if usec?
  frags
end
partial_match?(date2) click to toggle source

Returns true if dates partially match (i.e. one is a partial date of the other)

# File lib/date_time_precision/lib.rb, line 125
def partial_match?(date2)
  self.class::partial_match?(self, date2)
end
precision() click to toggle source

Returns the precision for this Date/Time object, or the maximum precision if none was specified

# File lib/date_time_precision/lib.rb, line 69
def precision
  @precision = self.class::MAX_PRECISION unless @precision
  return @precision
end
precision=(prec) click to toggle source
# File lib/date_time_precision/lib.rb, line 74
def precision=(prec)
  @precision = [prec,self.class::MAX_PRECISION].min
end
to_h(format = nil) click to toggle source
# File lib/date_time_precision/format/hash.rb, line 39
def to_h(format = nil)
  keys = Hash::DATE_FORMATS[format || :default]
  
  Hash[keys.each_with_index.map do |key,i|
    attribute_name = Hash::DATE_FORMATS[:ruby][i]
    [key, self.send(attribute_name)] if self.send("#{attribute_name}?")
  end.compact]
end
to_json(opts = {}) click to toggle source
# File lib/date_time_precision/format/json.rb, line 24
def to_json(opts = {})
  to_h.to_json(opts)
end

Protected Instance Methods

normalize_new_args(args) click to toggle source
# File lib/date_time_precision/lib.rb, line 129
def normalize_new_args(args)
  self.class.normalize_new_args(args)
end