Sha256: 66353cbaef0d61951a737ccbb6ff2f04ce18d182ca928591c83e314e4a346c36

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

module TimeCrisis
  class Date
    DATE_FORMATS = {
      :short        => "%e %b",
      :long         => "%B %e, %Y",
      :db           => "%Y-%m-%d",
      :number       => "%Y%m%d",
      :rfc822       => "%e %b %Y"
    }

    def to_formatted_s(format = :default)
      if formatter = DATE_FORMATS[format]
        if formatter.respond_to?(:call)
          formatter.call(self).to_s
        else
          strftime(formatter)
        end
      else
        to_default_s
      end
    end
    alias_method :to_default_s, :to_s
    alias_method :to_s, :to_formatted_s

    def readable_inspect
      "#<TimeCrisis::Date #{strftime('%a, %d %b %Y')}>"
    end
    alias_method :default_inspect, :inspect
    alias_method :inspect, :readable_inspect

    def to_tc_date
      self
    end

    def to_tc_datetime(form = :local)
      offset = form == :local ? (Time.zone.now.utc_offset rescue Time.now.utc_offset) : 0
      ::TimeCrisis::DateTime.civil(year, month, day, 0, 0, 0, 0, offset)
    end
    
    def to_tc_time(form = :local)
      ::TimeCrisis::Time.send(form, year, month, day)
    end

    def to_date
      ::Date.civil(year, month, day)
    end

    def to_datetime
      ::DateTime.civil(year, month, day, 0, 0, 0, 0)
    end

    def to_time(form = :local)
      ::Time.send(form, year, month, day)
    end

    def xmlschema
      to_tc_datetime.xmlschema
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
time_crisis-0.3.5 lib/time_crisis/support/ext/date/conversions.rb