Module: DR::DateOutput

Extended by:
DateOutput
Included in:
DateOutput, DateRange
Defined in:
lib/dr/parse/date_parse.rb

Constant Summary collapse

Months_end =

BUG: années bissextiles...

{1 => 31, 2 => 28, 3 => 31, 4 => 30,
5 => 31, 6 => 30, 7 => 31, 8 => 31,
9 => 30, 10 => 31, 11 => 30, 12 => 31}
Months_names =
{en: {
1 => 'January', 2 => 'February', 3 => 'March',
4 => 'April', 5 => 'May', 6 => 'June',
7 => 'July', 8 => 'August', 9 => 'September',
10 => 'October', 11 => 'November', 12 => 'December'},
fr: {
1 => 'Janvier', 2 => 'Février', 3 => 'Mars',
4 => 'Avril', 5 => 'Mai', 6 => 'Juin',
7 => 'Juillet', 8 => 'Août', 9 => 'Septembre',
10 => 'Octobre', 11 => 'Novembre', 12 => 'Décembre'}}

Instance Method Summary collapse

Instance Method Details

#output_date(datetime, output_date: :abbr, output_date_length: :month, **opts) ⇒ Object

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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) ⇒ Object

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



50
51
52
53
54
55
# 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) ⇒ Object

Convert a Date/string into a Time



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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