class SimpleTimespan

Attributes

days[R]
hours[R]
minutes[R]
nanoseconds[R]
nsec[R]
sec[R]
seconds[R]
years[R]

Public Class Methods

new(sec_with_fraction) click to toggle source
# File lib/simple_timespan.rb, line 3
def initialize(sec_with_fraction)
  @nanoseconds = ((sec_with_fraction % 1) * (10 ** 9)).round
  # Convert to whole seconds
  _remaining_time = sec_with_fraction.floor
  @seconds = _remaining_time % 60
  # Convert to Minutes
  _remaining_time /= 60
  @minutes = _remaining_time % 60
  # Convert to Hours
  _remaining_time /= 60
  @hours = _remaining_time % 24
  # Convert to Days
  _remaining_time /= 24
  @days = _remaining_time % 365
  # Convert to Years
  _remaining_time /= 365
  @years = _remaining_time
end

Public Instance Methods

microseconds() click to toggle source
# File lib/simple_timespan.rb, line 27
def microseconds
  nanoseconds / 1000
end
Also aliased as: usec
milliseconds() click to toggle source
# File lib/simple_timespan.rb, line 33
def milliseconds
  nanoseconds / 1_000_000
end
Also aliased as: msec
msec()
Alias for: milliseconds
to_days() click to toggle source
# File lib/simple_timespan.rb, line 89
def to_days
  (self.to_years * 365) + self.days
end
to_hours() click to toggle source
# File lib/simple_timespan.rb, line 93
def to_hours
  (self.to_days * 24) + self.hours
end
to_minutes() click to toggle source
# File lib/simple_timespan.rb, line 97
def to_minutes
  (self.to_hours * 60) + self.minutes
end
to_s() click to toggle source
# File lib/simple_timespan.rb, line 39
def to_s
  result = []
  if years > 0
    result << "#{years} #{year_or_years_string}"
  end

  if days > 0
    result << "#{days} #{day_or_days_string}"
  end

  if hours > 0
    result << "#{hours} #{hour_or_hours_string}"
  end

  if minutes > 0
    result << "#{minutes} #{minute_or_minutes_string}"
  end

  if (seconds > 0) || result.none?
    result << "#{seconds} #{second_or_seconds_string}"
  end

  result.join(' ')
end
to_s_ago() click to toggle source

Returns a string that describes the Timespan as a time in the past.

i.e., 2 years ago

# File lib/simple_timespan.rb, line 68
def to_s_ago
  prefix =
    if !years.zero?
      "#{years} #{year_or_years_string}"
    elsif !days.zero?
      "#{days} #{day_or_days_string}"
    elsif !hours.zero?
      "#{hours} #{hour_or_hours_string}"
    elsif !minutes.zero?
      "#{minutes} #{minute_or_minutes_string}"
    elsif !seconds.zero?
      "#{seconds} #{second_or_seconds_string}"
    end

  prefix + ' ago'
end
to_seconds() click to toggle source
# File lib/simple_timespan.rb, line 101
def to_seconds
  (self.to_minutes * 60) + self.seconds
end
to_years() click to toggle source
# File lib/simple_timespan.rb, line 85
def to_years
  self.years
end
usec()
Alias for: microseconds

Private Instance Methods

day_or_days_string() click to toggle source
# File lib/simple_timespan.rb, line 111
def day_or_days_string
  (days == 1) ? 'day' : 'days'
end
hour_or_hours_string() click to toggle source
# File lib/simple_timespan.rb, line 115
def hour_or_hours_string
  (hours == 1) ? 'hour' : 'hours'
end
minute_or_minutes_string() click to toggle source
# File lib/simple_timespan.rb, line 119
def minute_or_minutes_string
  (minutes == 1) ? 'minute' : 'minutes'
end
second_or_seconds_string() click to toggle source
# File lib/simple_timespan.rb, line 123
def second_or_seconds_string
  (seconds == 1) ? 'second' : 'seconds'
end
year_or_years_string() click to toggle source
# File lib/simple_timespan.rb, line 107
def year_or_years_string
  (years == 1) ? 'year' : 'years'
end