Class: TimeSegment
- Inherits:
-
Object
- Object
- TimeSegment
- Defined in:
- lib/time_segment.rb,
lib/time_segment/version.rb
Constant Summary collapse
- DAY_IN_SEC =
86400
- MONTH_IN_SEC =
2629746
- YEAR_IN_SEC =
31556952
- VERSION =
"0.0.2"
Instance Attribute Summary collapse
-
#days ⇒ Object
readonly
Returns the value of attribute days.
-
#hours ⇒ Object
readonly
Returns the value of attribute hours.
-
#in_seconds ⇒ Object
readonly
Returns the value of attribute in_seconds.
-
#minutes ⇒ Object
readonly
Returns the value of attribute minutes.
-
#months ⇒ Object
readonly
Returns the value of attribute months.
-
#seconds ⇒ Object
readonly
Returns the value of attribute seconds.
-
#years ⇒ Object
readonly
Returns the value of attribute years.
Class Method Summary collapse
Instance Method Summary collapse
- #in_days ⇒ Object
- #in_hours ⇒ Object
- #in_minutes ⇒ Object
-
#in_months ⇒ Object
1 month = 30.436875 days = 2629746 = 30.436875 * 86400 seconds.
-
#in_years ⇒ Object
1 year = 365.2425 days = 31556952 seconds.
- #include?(time) ⇒ Boolean
-
#initialize(from, to = nil) ⇒ TimeSegment
constructor
A new instance of TimeSegment.
- #round(options = {}) ⇒ Object
Constructor Details
#initialize(from, to = nil) ⇒ TimeSegment
Returns a new instance of TimeSegment.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/time_segment.rb', line 11 def initialize from,to=nil to = Time.now if to.nil? from = from.to_time if from.respond_to?(:to_time) to = to.to_time if to.respond_to?(:to_time) if from > to @from, @to = to,from else @from, @to = from,to end @in_seconds = (@to - @from).round.to_i @years,remainder = @in_seconds.divmod YEAR_IN_SEC @months, remainder = remainder.divmod MONTH_IN_SEC @days, remainder = remainder.divmod DAY_IN_SEC @hours, remainder = remainder.divmod 3600 @minutes, remainder = remainder.divmod 60 @seconds = remainder end |
Instance Attribute Details
#days ⇒ Object (readonly)
Returns the value of attribute days.
9 10 11 |
# File 'lib/time_segment.rb', line 9 def days @days end |
#hours ⇒ Object (readonly)
Returns the value of attribute hours.
9 10 11 |
# File 'lib/time_segment.rb', line 9 def hours @hours end |
#in_seconds ⇒ Object (readonly)
Returns the value of attribute in_seconds.
9 10 11 |
# File 'lib/time_segment.rb', line 9 def in_seconds @in_seconds end |
#minutes ⇒ Object (readonly)
Returns the value of attribute minutes.
9 10 11 |
# File 'lib/time_segment.rb', line 9 def minutes @minutes end |
#months ⇒ Object (readonly)
Returns the value of attribute months.
9 10 11 |
# File 'lib/time_segment.rb', line 9 def months @months end |
#seconds ⇒ Object (readonly)
Returns the value of attribute seconds.
9 10 11 |
# File 'lib/time_segment.rb', line 9 def seconds @seconds end |
#years ⇒ Object (readonly)
Returns the value of attribute years.
9 10 11 |
# File 'lib/time_segment.rb', line 9 def years @years end |
Class Method Details
.between(from, to) ⇒ Object
33 34 35 |
# File 'lib/time_segment.rb', line 33 def self.between(from, to) self.new(from,to) end |
Instance Method Details
#in_days ⇒ Object
47 48 49 50 |
# File 'lib/time_segment.rb', line 47 def in_days @in_days = (@in_seconds/86400) if @in_days.nil? @in_days end |
#in_hours ⇒ Object
42 43 44 45 |
# File 'lib/time_segment.rb', line 42 def in_hours @in_hours = (@in_seconds/3600) if @in_hours.nil? @in_hours end |
#in_minutes ⇒ Object
37 38 39 40 |
# File 'lib/time_segment.rb', line 37 def in_minutes @in_minutes = (@in_seconds/60) if @in_minutes.nil? @in_minutes end |
#in_months ⇒ Object
1 month = 30.436875 days = 2629746 = 30.436875 * 86400 seconds
53 54 55 56 |
# File 'lib/time_segment.rb', line 53 def in_months @in_months = (@in_seconds/MONTH_IN_SEC) if @in_months.nil? @in_months end |
#in_years ⇒ Object
1 year = 365.2425 days = 31556952 seconds
59 60 61 |
# File 'lib/time_segment.rb', line 59 def in_years @years end |
#include?(time) ⇒ Boolean
63 64 65 66 67 68 69 70 |
# File 'lib/time_segment.rb', line 63 def include? time t = time.to_time if time.respond_to? :to_time if t and t >= @from and t <= @to then true else false end end |
#round(options = {}) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/time_segment.rb', line 72 def round = {} = { scope: :'datetime.distance_in_words' }.merge!() I18n. :locale => [:locale], :scope => [:scope] do |i18n| if 0 < self.in_years then i18n.t :over_x_years, :count => @years elsif 0 < self.in_months then i18n.t :x_months, :count => @in_months elsif 0 < self.in_days then i18n.t :x_days, :count => @in_days elsif 0 < self.in_hours then i18n.t :about_x_hours, :count => @in_hours elsif 0 < self.in_minutes then i18n.t :x_minutes, :count => @in_minutes elsif 0 < self.in_seconds then i18n.t :x_seconds, :count => @in_seconds else i18n.t :less_than_x_seconds, :count => 1 end end end |