Class: SakaiInfo::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/sakai-info/util.rb

Constant Summary collapse

FILESIZE_LABELS =

misc support functions

["bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]

Class Method Summary collapse

Class Method Details

.format_entity_date(raw) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/sakai-info/util.rb', line 40

def self.format_entity_date(raw)
  if raw =~ /^(....)(..)(..)(..)(..)(..).*$/
    # I believe these are usually in UTC
    Time.utc($1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i).getlocal
  else
    raw
  end
end

.format_filesize(i_size) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sakai-info/util.rb', line 16

def self.format_filesize(i_size)
  size = i_size.to_f
  negative = false

  if size < 0
    negative = true
    size = -size
  end

  label = 0
  (FILESIZE_LABELS.size - 1).times do
    if size >= 1024.0
      size = size / 1024.0
      label += 1
    end
  end

  if size >= 100.0 or label == 0
    "#{negative ? "-" : ""}#{size.to_i.to_s} #{FILESIZE_LABELS[label]}"
  else
    "#{negative ? "-" : ""}#{sprintf("%.1f", size)} #{FILESIZE_LABELS[label]}"
  end
end