Module: Cowtech::Extensions::DateTime::ClassMethods

Defined in:
lib/cowtech-extensions/datetime.rb

Instance Method Summary collapse

Instance Method Details

#custom_format(key = "date") ⇒ Object



65
66
67
# File 'lib/cowtech-extensions/datetime.rb', line 65

def custom_format(key = "date")
	self.custom_formats.fetch(key.to_s, "%d/%m/%Y")
end

#custom_formatsObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/cowtech-extensions/datetime.rb', line 54

def custom_formats
	@@custom_formats ||= {
		"date" => "%d/%m/%Y",
		"time" => "%H:%M:%S",
		"date-8601" => "%Y-%m-%d",
		"date-time-8601" => "%Y-%m-%d %H:%M:%S",
		"iso-8601" => "%FT%T%z",
		"update" => "%d/%m/%Y %H:%M"
	}
end

#default_setup_localeObject



31
32
33
34
35
36
# File 'lib/cowtech-extensions/datetime.rb', line 31

def default_setup_locale
	self.default_localized_months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
	self.default_localized_short_months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
	self.default_localized_days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
	self.default_localized_short_days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
end

#easter(year = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cowtech-extensions/datetime.rb', line 69

def easter(year = nil)
	day = 1
	month = 3
	year = Date.today.year if !year.is_integer?

	# GAUSS METHOD
	a = year % 19
	d = ((19 * a) + 24) % 30
	e = ((2 * (year % 4)) + (4 * (year % 7)) + (6 * d) + 5) % 7

	if d + e < 10 then
		day = d + e + 22
	else
		day = d + e - 9
		month = 4
	end

	if day == 26 && month == 4 then
		day = 19
	elsif day == 25 && month == 4 && d == 28 && e == 6 && a > 10 then
		day = 18
	end
	#END

	Date.civil(year, month, day)
end

#months(short = true) ⇒ Object



45
46
47
# File 'lib/cowtech-extensions/datetime.rb', line 45

def months(short = true)
	12.times.collect { |i| {value: (i + 1).to_s.rjust(2, "0"), description: self.send("localized_#{short ? "short_" : ""}months").at(i) } }
end

#setup_localeObject



38
39
40
41
42
43
# File 'lib/cowtech-extensions/datetime.rb', line 38

def setup_locale
	self.localized_months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
	self.localized_short_months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
	self.localized_days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
	self.localized_short_days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
end

#setup_localizationObject



26
27
28
29
# File 'lib/cowtech-extensions/datetime.rb', line 26

def setup_localization
	self.default_setup_locale
	self.setup_locale
end

#years(offset = 10, also_future = true) ⇒ Object



49
50
51
52
# File 'lib/cowtech-extensions/datetime.rb', line 49

def years(offset = 10, also_future = true)
	y = Date.today.year
	(y - offset..(also_future ? y + offset : y)).collect { |year| {value: year} }
end