Module: Fugit

Defined in:
lib/fugit.rb,
lib/fugit/at.rb,
lib/fugit/nat.rb,
lib/fugit/cron.rb,
lib/fugit/misc.rb,
lib/fugit/parse.rb,
lib/fugit/duration.rb

Defined Under Namespace

Modules: At, Nat Classes: Cron, Duration

Constant Summary collapse

VERSION =
'1.11.0'
DAY_S =
(24 * 3600).freeze
YEAR_S =
(365 * DAY_S).freeze

Class Method Summary collapse

Class Method Details

.determine_type(s) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/fugit/parse.rb', line 66

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 = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fugit/parse.rb', line 30

def do_parse(s, opts={})

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

  result = nil
  errors = []

  %i[ cron duration nat at ]
    .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) ⇒ Object



16
# File 'lib/fugit/parse.rb', line 16

def do_parse_at(s); ::Fugit::At.do_parse(s); end

.do_parse_cron(s) ⇒ Object



13
# File 'lib/fugit/parse.rb', line 13

def do_parse_cron(s); ::Fugit::Cron.do_parse(s); end

.do_parse_cronish(s, opts = {}) ⇒ Object



60
61
62
63
64
# File 'lib/fugit/parse.rb', line 60

def do_parse_cronish(s, opts={})

  parse_cronish(s) ||
  fail(ArgumentError.new("not cron or 'natural' cron string: #{s.inspect}"))
end

.do_parse_duration(s) ⇒ Object



14
# File 'lib/fugit/parse.rb', line 14

def do_parse_duration(s); ::Fugit::Duration.do_parse(s); end

.do_parse_in(s) ⇒ Object



17
# File 'lib/fugit/parse.rb', line 17

def do_parse_in(s); do_parse_duration(s); end

.do_parse_nat(s, opts = {}) ⇒ Object



15
# File 'lib/fugit/parse.rb', line 15

def do_parse_nat(s, opts={}); ::Fugit::Nat.do_parse(s, opts); end

.isostamp(show_date, show_time, show_usec, time) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# 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 = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/fugit/parse.rb', line 19

def parse(s, opts={})

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

  (opts[:cron] != false && parse_cron(s)) ||
  (opts[:duration] != false && parse_duration(s)) ||
  (opts[:nat] != false && parse_nat(s, opts)) ||
  (opts[:at] != false && parse_at(s)) ||
  nil
end

.parse_at(s) ⇒ Object



10
# File 'lib/fugit/parse.rb', line 10

def parse_at(s); ::Fugit::At.parse(s); end

.parse_cron(s) ⇒ Object



7
# File 'lib/fugit/parse.rb', line 7

def parse_cron(s); ::Fugit::Cron.parse(s); end

.parse_cronish(s, opts = {}) ⇒ Object



53
54
55
56
57
58
# File 'lib/fugit/parse.rb', line 53

def parse_cronish(s, opts={})

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

  r.is_a?(::Fugit::Cron) ? r : nil
end

.parse_duration(s) ⇒ Object



8
# File 'lib/fugit/parse.rb', line 8

def parse_duration(s); ::Fugit::Duration.parse(s); end

.parse_in(s) ⇒ Object



11
# File 'lib/fugit/parse.rb', line 11

def parse_in(s); parse_duration(s); end

.parse_nat(s, opts = {}) ⇒ Object



9
# File 'lib/fugit/parse.rb', line 9

def parse_nat(s, opts={}); ::Fugit::Nat.parse(s, opts); end

.time_to_plain_s(t = Time.now, z = true) ⇒ Object



28
29
30
31
# 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) ⇒ Object



23
24
25
26
# 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) ⇒ Object



33
34
35
36
# 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