Class: Time

Inherits:
Object
  • Object
show all
Defined in:
lib/rbfind/humansiz.rb

Constant Summary collapse

TIME_UNITS =
{ seconds: 60, minutes: 60, hours: 24, days: 7, weeks: nil }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.startObject



153
154
155
# File 'lib/rbfind/humansiz.rb', line 153

def start
  @start ||= Time.now
end

.str_to_sec(str) ⇒ Object



123
124
125
126
# File 'lib/rbfind/humansiz.rb', line 123

def str_to_sec str
  str =~ /(\d*) *(\w*)/
  to_sec $1.to_i, $2
end

.to_sec(num, unit) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/rbfind/humansiz.rb', line 115

def to_sec num, unit
  TIME_UNITS.each_pair do |nam,val|
    return num if nam.start_with? unit
    num *= val.to_i
    num.nonzero? or break
  end
  raise "No time unit: #{unit}."
end

.to_unit(n) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/rbfind/humansiz.rb', line 108

def to_unit n
  u = TIME_UNITS.each_pair do |nam,val|
    break nam if not val or n < val
    n /= val
  end
  "#{n}#{u[0]}"
end

Instance Method Details

#longObject



143
144
145
146
147
148
149
150
# File 'lib/rbfind/humansiz.rb', line 143

def long
  s = Time.start
  if year == s.year && month == s.month && day == s.day then
    strftime "==%H:%M:%S"
  else
    strftime "%Y-%m-%d"
  end
end

#lsishObject

:call-seq:

time.lsish()  -> str

Build a time string like in ls -l. When the year is the current, show the time. While ls doesn’t show the seconds, this will allways include them.

Time.now.lsish           #=> " 8. Oct 15:15:19"
file.stat.mtime.lsish    #=> " 1. Apr 2008    "


139
140
141
# File 'lib/rbfind/humansiz.rb', line 139

def lsish
  strftime "%e. %b " + (year == Time.start.year ? "%H:%M:%S" : "%Y    ")
end