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", ]
PERC_DAY =

Windows has no “%e”.

Time.now.strftime("%e") =~ /\d/ ? "%e" : "%d"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.str_to_sec(str) ⇒ Object



112
113
114
115
# File 'lib/rbfind/humansiz.rb', line 112

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

.to_sec(num, unit) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/rbfind/humansiz.rb', line 105

def to_sec num, unit
  TIME_UNITS.each_slice 2 do |nam,val|
    return num if nam.start_with? unit
    num *= val
  end
  raise "No time unit: #{unit}."
end

.to_unit(n) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/rbfind/humansiz.rb', line 98

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

Instance Method Details

#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    "


128
129
130
131
# File 'lib/rbfind/humansiz.rb', line 128

def lsish
  strftime "#{PERC_DAY}. %b " +
            (year == Time.now.year ? "%H:%M:%S" : "%Y    ")
end