method

complete_frags

Importance_0
v1_8_7_330 - Show latest stable - 0 notes - Class: Date
complete_frags(elem) private

No documentation

This method has no description. You can help the Ruby community by adding new notes.

Hide source
# File lib/date.rb, line 809
  def self.complete_frags(elem) # :nodoc:
    i = 0
    g = [[:time, [:hour, :min, :sec]],
         [nil, [:jd]],
         [:ordinal, [:year, :yday, :hour, :min, :sec]],
         [:civil, [:year, :mon, :mday, :hour, :min, :sec]],
         [:commercial, [:cwyear, :cweek, :cwday, :hour, :min, :sec]],
         [:wday, [:wday, :hour, :min, :sec]],
         [:wnum0, [:year, :wnum0, :wday, :hour, :min, :sec]],
         [:wnum1, [:year, :wnum1, :wday, :hour, :min, :sec]],
         [nil, [:cwyear, :cweek, :wday, :hour, :min, :sec]],
         [nil, [:year, :wnum0, :cwday, :hour, :min, :sec]],
         [nil, [:year, :wnum1, :cwday, :hour, :min, :sec]]].
      collect{|k, a| e = elem.values_at(*a).compact; [k, a, e]}.
      select{|k, a, e| e.size > 0}.
      sort_by{|k, a, e| [e.size, i -= 1]}.last

    d = nil

    if g && g[0] && (g[1].size - g[2].size) != 0
      d ||= Date.today

      case g[0]
      when :ordinal
        elem[:year] ||= d.year
        elem[:yday] ||= 1
      when :civil
        g[1].each do |e|
          break if elem[e]
          elem[e] = d.__send__(e)
        end
        elem[:mon]  ||= 1
        elem[:mday] ||= 1
      when :commercial
        g[1].each do |e|
          break if elem[e]
          elem[e] = d.__send__(e)
        end
        elem[:cweek] ||= 1
        elem[:cwday] ||= 1
      when :wday
        elem[:jd] ||= (d - d.wday + elem[:wday]).jd
      when :wnum0
        g[1].each do |e|
          break if elem[e]
          elem[e] = d.__send__(e)
        end
        elem[:wnum0] ||= 0
        elem[:wday]  ||= 0
      when :wnum1
        g[1].each do |e|
          break if elem[e]
          elem[e] = d.__send__(e)
        end
        elem[:wnum1] ||= 0
        elem[:wday]  ||= 0
      end
    end

    if g && g[0] == :time
      if self <= DateTime
        d ||= Date.today
        elem[:jd] ||= d.jd
      end
    end

    elem[:hour] ||= 0
    elem[:min]  ||= 0
    elem[:sec]  ||= 0
    elem[:sec] = [elem[:sec], 59].min

    elem
  end
Register or log in to add new notes.