Class: Time
Class Method Summary collapse
- .delta(year, month = nil, day = nil) ⇒ Object
- .extract_from_attributes!(attrs, field, method = :local) ⇒ Object
-
.parse_from_attributes(attrs, field, method = :local) ⇒ Object
Used for getting multifield attributes like those generated by a select_datetime into a new Time object.
Instance Method Summary collapse
- #to_delta(delta_type = :day) ⇒ Object
-
#to_ordinalized_s(format = :default) ⇒ Object
Time.now.to_ordinalized_s :long => “February 28th, 2006 21:10”.
Class Method Details
.delta(year, month = nil, day = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/time_hacks.rb', line 20 def delta(year, month = nil, day = nil) from = Time.zone.local(year, month || 1, day || 1) to = if !day.blank? from.advance :days => 1 elsif !month.blank? from.advance :months => 1 else from.advance :years => 1 end return [from.midnight, to.midnight-1] end |
.extract_from_attributes!(attrs, field, method = :local) ⇒ Object
3 4 5 6 7 |
# File 'lib/time_hacks.rb', line 3 def extract_from_attributes!(attrs, field, method = :local) parse_from_attributes(attrs, field, method).tap do |time| attrs.delete_if { |k, v| k.to_s =~ /^#{field}.+/ } end unless attrs.blank? end |
.parse_from_attributes(attrs, field, method = :local) ⇒ Object
Used for getting multifield attributes like those generated by a select_datetime into a new Time object. For example if you have following params={:meetup=>{:"time(1i)=>..."}}
just do following:
Time.parse_from_attributes(params[:meetup], :time)
15 16 17 18 |
# File 'lib/time_hacks.rb', line 15 def parse_from_attributes(attrs, field, method = :local) attrs = attrs.keys.sort.grep(/^#{field.to_s}\(.+\)$/).map { |k| attrs[k] } attrs.any? ? Time.zone.send(method, *attrs) : nil end |
Instance Method Details
#to_delta(delta_type = :day) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/time_hacks.rb', line 34 def to_delta(delta_type = :day) case delta_type when :year then self.class.delta(year) when :month then self.class.delta(year, month) else self.class.delta(year, month, day) end end |
#to_ordinalized_s(format = :default) ⇒ Object
Time.now.to_ordinalized_s :long
> “February 28th, 2006 21:10”
44 45 46 47 48 |
# File 'lib/time_hacks.rb', line 44 def to_ordinalized_s(format = :default) format = DATE_FORMATS[format] return to_default_s if format.nil? strftime(format.gsub(/%d/, '_%d_')).gsub(/_(\d+)_/) { |s| s.to_i.ordinalize } end |