Method: ALib::Util#timestamp

Defined in:
lib/alib-0.5.0/util.rb

#timestamp(arg = Time::now) ⇒ Object

nospace option - useful for constructing filenames



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/alib-0.5.0/util.rb', line 253

def timestamp arg = Time::now
#--{{{
  if Time === arg 
    arg.iso8601 2
  else
    opts =
      case arg
        when Hash
          opts = arg
        when Time
          {'time' => arg}
        else
          raise ArgumentError, "#{ arg.inspect } (#{ arg.class })"
      end
    time = getopt 'time', opts, Time::now
    local = getopt 'local', opts, false 
    nospace = getopt('nospace', opts, getopt('no_space', opts, false))
    dateonly = getopt('dateonly', opts, getopt('date_only', opts, false))
    time = time.utc unless local
    usec = "#{ time.usec }"
    usec << ('0' * (6 - usec.size)) if usec.size < 6 
    stamp =
    unless dateonly
      time.strftime('%Y-%m-%d %H:%M:%S.') << usec
    else
      time.strftime('%Y-%m-%d')
    end
    if nospace
      spc = TrueClass === nospace ? 'T' : "#{ nospace }"
      stamp.gsub! %r/\s+/, spc
    end
    stamp
  end
#--}}}
end