module ICFS

Investigative Case File System

Constants

PermAction

permission to manage actions

PermManage

permission to manage case

PermRead

permission to read case

PermSearch

global permission to search

PermWrite

permission to write case

TagAction

edits an action

TagCase

edits the case

TagIndex

edits an index

TagNone

no tags

TimeDelta

A time delta spec

TimeEmpty

empty time spec

TimeFuture

future time spec

TimeHistory

historic time spec

TimeRel

a relative spec

TimeZone

A timezone spec

UserCase

user group

Version

version: major, minor, patch

VersionPre

version pre-release

VersionString

version string

Public Class Methods

_time_adjust(num, type) click to toggle source

Adjust the time

@api private

# File lib/icfs.rb, line 116
def self._time_adjust(num, type)
  case type
  when :year
    ary = Time.now.utc.to_a
    dte = Date.new(ary[5], ary[4], ary[3])
    dte = dte << (-12 * num)
    return Time.utc(dte.year, dte.month, dte.day,
        ary[2], ary[1], ary[0]).to_i
  when :month
    ary = Time.now.utc.to_a
    dte = Date.new(ary[5], ary[4], ary[3])
    dte = dte << (-1 * num)
    return Time.utc(dte.year, dte.month, dte.day,
        ary[2], ary[1], ary[0]).to_i
  when :week
    return (Time.now + num * 7*24*60*60).to_i
  when :day
    return (Time.now + num * 24*60*60).to_i
  when :hour
    return (Time.now + num * 60*60).to_i
  when :minute
    return (Time.now + num * 60).to_i
  when :second
    return (Time.now + num).to_i
  else
    return nil
  end
end
_time_type(str) click to toggle source

Pull a time type

@api private

# File lib/icfs.rb, line 89
def self._time_type(str)
  case str.downcase
  when 'y', 'yr', 'yrs', 'year', 'years'
    return :year
  when 'm', 'mon', 'mons', 'month', 'months'
    return :month
  when 'w', 'wk', 'wks', 'week', 'weeks'
    return :week
  when 'd', 'day', 'days'
    return :day
  when 'h', 'hr', 'hrs', 'hour', 'hours'
    return :hour
  when 'min', 'mins', 'minute', 'minutes'
    return :minute
  when 's', 'sec', 'secs', 'second', 'seconds'
    return :second
  else
    return nil
  end
end
hash(str) click to toggle source

Hash a string

# File lib/icfs.rb, line 71
def self.hash(str)
  Digest::SHA256.hexdigest(str)
end
hash_temp(tf) click to toggle source

Hash a tempfile

# File lib/icfs.rb, line 79
def self.hash_temp(tf)
  Digest::SHA256.file(tf.path).hexdigest
end
time_local(tme, cfg) click to toggle source

Get epoch time as local

@param tme [Integer] the epoch time @param cfg [Config] the config

# File lib/icfs.rb, line 242
def self.time_local(tme, cfg)
    Time.at(tme).getlocal(cfg.get('tz')).strftime('%F %T')
end
time_parse(str, cfg) click to toggle source

Parse a time string

@param str [String] the time string @param cfg [Config] the config

Handles:

  • blank or now

  • [now] +\- <num> <type>

  • next\prev <type>

  • in <num> <type>

  • <num> <type> ago

  • a specifc parseable time

# File lib/icfs.rb, line 190
def self.time_parse(str, cfg)
  return nil if( !str || !str.is_a?(String) )

  # empty
  if ma = TimeEmpty.match(str)
    return Time.now.to_i

  # delta
  elsif ma = TimeDelta.match(str)
    num = ma[3].to_i
    num = num * -1 if ma[2] == '-'
    type = ICFS._time_type(ma[4])
    return ICFS._time_adjust(num, type)

  # relative
  elsif ma = TimeRel.match(str)
    num = (ma[1].downcase == 'next') ? 1 : -1
    type = ICFS._time_type(ma[2])
    return ICFS._time_adjust(num, type)

  # future
  elsif ma = TimeFuture.match(str)
    num = ma[1].to_i
    type = ICFS._time_type(ma[2])
    return ICFS._time_adjust(num, type)

  # history
  elsif ma = TimeHistory.match(str)
    p ma
    num = -1 * ma[1].to_i
    type = ICFS._time_type(ma[2])
    return ICFS._time_adjust(num, type)

  # parse a time spec
  else
    ma = TimeZone.match(str)
    tstr = ma ? str : str + cfg.get('tz')
    return Time.parse(tstr).to_i

  end

rescue ArgumentError
  return nil
end
time_relative(tme) click to toggle source

Get relative display time from epoch

@param tme [Integer] the epoch time @return [String] Relative time string

# File lib/icfs.rb, line 264
def self.time_relative(tme)
  rel = Time.now.to_i - tme
  abs = rel.abs

  # time periods
  if abs >= (2*365*24*60*60)
    num = abs / (365*24*60*60)
    txt = 'years'
  elsif abs >= (3*7*24*60*60)
    num = abs / (7*24*60*60)
    txt = 'weeks'
  elsif abs >= (3*24*60*60)
    num = abs / (24*60*60)
    txt = 'days'
  elsif abs >= (3*60*60)
    num = abs / (60*60)
    txt = 'hours'
  elsif abs >= 2*60
    num = abs / 60
    txt = 'minutes'
  else
    num = 0
    txt = 'just now'
  end

  # description
  desc = '%d %s' % [num, txt]

  # before/after
  if num == 0
    final = 'just now'
  elsif rel < 0
    final = 'in ' + desc
  else
    final = desc + ' ago'
  end

  return final
end
time_weekday(tme, cfg) click to toggle source

Get epoch time as local with weekday

@param tme [Integer] the epoch time @param cfg [Config] the config

# File lib/icfs.rb, line 253
def self.time_weekday(tme, cfg)
    Time.at(tme).getlocal(cfg.get('tz')).strftime('%F %T (%a)')
end