Class: TimeSegment

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#daysObject (readonly)

Returns the value of attribute days.



9
10
11
# File 'lib/time_segment.rb', line 9

def days
  @days
end

#hoursObject (readonly)

Returns the value of attribute hours.



9
10
11
# File 'lib/time_segment.rb', line 9

def hours
  @hours
end

#in_secondsObject (readonly)

Returns the value of attribute in_seconds.



9
10
11
# File 'lib/time_segment.rb', line 9

def in_seconds
  @in_seconds
end

#minutesObject (readonly)

Returns the value of attribute minutes.



9
10
11
# File 'lib/time_segment.rb', line 9

def minutes
  @minutes
end

#monthsObject (readonly)

Returns the value of attribute months.



9
10
11
# File 'lib/time_segment.rb', line 9

def months
  @months
end

#secondsObject (readonly)

Returns the value of attribute seconds.



9
10
11
# File 'lib/time_segment.rb', line 9

def seconds
  @seconds
end

#yearsObject (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_daysObject



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_hoursObject



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_minutesObject



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_monthsObject

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_yearsObject

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

Returns:

  • (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 options = {}
  options = { scope: :'datetime.distance_in_words' }.merge!(options)

  I18n.with_options :locale => options[:locale], :scope => options[: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