module Fugit

Constants

DAY_S
DO_PARSE_ORDER
VERSION
YEAR_S

Public Class Methods

determine_type(s) click to toggle source
# File lib/fugit/parse.rb, line 86
def determine_type(s)

  case self.parse(s)
  when ::Fugit::Cron then 'cron'
  when ::Fugit::Duration then 'in'
  when ::Time, ::EtOrbi::EoTime then 'at'
  else nil
  end
end
do_parse(s, opts={}) click to toggle source
# File lib/fugit/parse.rb, line 32
def do_parse(s, opts={})

  opts[:at] = opts[:in] if opts.has_key?(:in)

  result = nil
  errors = []

  DO_PARSE_ORDER
    .each { |k|
      begin
        result ||= (opts[k] != false && self.send("do_parse_#{k}", s))
      rescue => err
        errors << err
      end }

  return result if result

  raise(
    errors.find { |r| r.class != ArgumentError } ||
    errors.first ||
    ArgumentError.new("found no time information in #{s.inspect}"))
end
do_parse_at(s) click to toggle source
# File lib/fugit/parse.rb, line 18
def do_parse_at(s); ::Fugit::At.do_parse(s); end
do_parse_cron(s, opts={}) click to toggle source
# File lib/fugit/parse.rb, line 15
def do_parse_cron(s, opts={}); ::Fugit::Cron.do_parse(s, opts || {}); end
do_parse_cronish(s, opts={}) click to toggle source
# File lib/fugit/parse.rb, line 62
def do_parse_cronish(s, opts={})

  parse_cronish(s, opts) ||
  fail(ArgumentError.new("not cron or 'natural' cron string: #{s.inspect}"))
end
do_parse_duration(s) click to toggle source
# File lib/fugit/parse.rb, line 16
def do_parse_duration(s); ::Fugit::Duration.do_parse(s); end
do_parse_in(s) click to toggle source
# File lib/fugit/parse.rb, line 19
def do_parse_in(s); do_parse_duration(s); end
do_parse_nat(s, opts={}) click to toggle source
# File lib/fugit/parse.rb, line 17
def do_parse_nat(s, opts={}); ::Fugit::Nat.do_parse(s, opts || {}); end
isostamp(show_date, show_time, show_usec, time) click to toggle source
# File lib/fugit/misc.rb, line 10
def isostamp(show_date, show_time, show_usec, time)

  t = time || Time.now
  s = StringIO.new

  s << t.strftime('%Y-%m-%d') if show_date
  s << t.strftime('T%H:%M:%S') if show_time
  s << sprintf('.%06d', t.usec) if show_time && show_usec
  s << 'Z' if show_time && time.utc?

  s.string
end
parse(s, opts={}) click to toggle source
# File lib/fugit/parse.rb, line 21
def parse(s, opts={})

  opts[:at] = opts[:in] if opts.has_key?(:in)

  (opts[:cron] != false && parse_cron(s, opts || {})) || # 542ms 616ms
  (opts[:duration] != false && parse_duration(s)) || # 645ms # 534ms
  (opts[:nat] != false && parse_nat(s, opts || {})) || # 2s # 35s
  (opts[:at] != false && parse_at(s, opts || {})) || # 568ms 622ms
  nil
end
parse_at(s, opts={}) click to toggle source
# File lib/fugit/parse.rb, line 12
def parse_at(s, opts={}); ::Fugit::At.parse(s, opts || {}); end
parse_cron(s, opts={}) click to toggle source
# File lib/fugit/parse.rb, line 9
def parse_cron(s, opts={}); ::Fugit::Cron.parse(s, opts || {}); end
parse_cronish(s, opts={}) click to toggle source
# File lib/fugit/parse.rb, line 55
def parse_cronish(s, opts={})

  r = parse_cron(s, opts) || parse_nat(s, opts)

  r.is_a?(::Fugit::Cron) ? r : nil
end
parse_duration(s) click to toggle source
# File lib/fugit/parse.rb, line 10
def parse_duration(s); ::Fugit::Duration.parse(s); end
parse_in(s) click to toggle source
# File lib/fugit/parse.rb, line 13
def parse_in(s); parse_duration(s); end
parse_max(s, opts={}) click to toggle source
# File lib/fugit/parse.rb, line 68
def parse_max(s, opts={})

  s0 = s.lines.first

  (0..[ ::Fugit::Nat::MAX_INPUT_LENGTH, s0.length - 1 ].min).each do |i|

    s1 =
      s0[0, s0.length - i].rstrip
    f =
      opts[:cronish] ? parse_cronish(s1, opts) :
      parse(s1, opts)

    return [ s1, f ] if f
  end

  nil
end
parse_nat(s, opts={}) click to toggle source
# File lib/fugit/parse.rb, line 11
def parse_nat(s, opts={}); ::Fugit::Nat.parse(s, opts || {}); end
time_to_plain_s(t=Time.now, z=true) click to toggle source
# File lib/fugit/misc.rb, line 28
def time_to_plain_s(t=Time.now, z=true)

  t.strftime('%Y-%m-%d %H:%M:%S') + (z && t.utc? ? ' Z' : '')
end
time_to_s(t) click to toggle source
# File lib/fugit/misc.rb, line 23
def time_to_s(t)

  isostamp(true, true, false, t)
end
time_to_zone_s(t=Time.now) click to toggle source
# File lib/fugit/misc.rb, line 33
def time_to_zone_s(t=Time.now)

  t.strftime('%Y-%m-%d %H:%M:%S %Z %z')
end