module DR::DateOutput

Constants

Months_end

BUG: années bissextiles…

Months_names

Public Instance Methods

output_date(datetime, output_date: :abbr, output_date_length: :month, **opts) click to toggle source

output_date_length: granularity :year/:month/:day/:all output_date: :num, :string, :abbr

# File lib/dr/parse/date_parse.rb, line 75
def output_date(datetime, output_date: :abbr, output_date_length: :month,
        **opts)
        lang=opts[:lang]||:en
        year,month,day,time=split_date(datetime)
        month=nil if output_date_length==:year
        day=nil if output_date_length==:month
        time=nil if output_date_length==:day
        return Formatter.localize({en: 'Present', fr: 'Présent'},**opts) if datetime==:now
        r=year
        case output_date
        when :num
                month.nil? ? (return r) : r+="-"+month
                day.nil? ? (return r) : r+="-"+day
                time.nil? ? (return r) : r+="T"+time
        when :abbr,:string
                return r if month.nil?
                month_name=Months_names[lang][month.to_i]
                month_name=abbr_month(month_name) if output_date==:abbr
                r=month_name+" "+r
                return r if day.nil?
                r=day+" "+r
                return r if time.nil?
                r+=" "+time
        end
        r
end
split_date(datetime) click to toggle source

ex: split 2014-07-28T19:26:20+0200 into year,month,day,time

# File lib/dr/parse/date_parse.rb, line 50
def split_date(datetime)
        datetime=Time.now.iso8601 if datetime == :now
        date,time=datetime.to_s.split("T")
        year,month,day=date.split("-")
        return year,month,day,time
end
to_time(datetime, complete_date: :first, **opts) click to toggle source

Convert a Date/string into a Time

# File lib/dr/parse/date_parse.rb, line 27
def to_time(datetime, complete_date: :first, **opts)
        require 'time'
        return Time.now if datetime == :now
        begin
                fallback=Time.new(0) #supply the missing components
                return Time.parse(datetime,fallback)
        rescue ArgumentError
                year,month,day,time=split_date(datetime)
                case complete_date
                when :first
                        month="01" if month == nil
                        day="01" if day == nil
                        time="00:00:00" if day == nil
                when :last
                        month="12" if month == nil
                        day=Months_end[month.to_i].to_s if day == nil
                        time="23:59:59" if day == nil
                end
                return Time.parse("#{year}-#{month}-#{day}T#{time}",fallback)
        end
end

Private Instance Methods

abbr_month(month, lang: :en, **_opts) click to toggle source
# File lib/dr/parse/date_parse.rb, line 68
        def abbr_month(month, lang: :en, **_opts)
        return month if month.length <= 4
        return month[0..2]+(lang==:en ? '.' : '')
end