Sha256: 3ff2661535c119bc97a3e02bdd97ca2c68203f566f7b5e0d84aaa1323526d810

Contents?: true

Size: 787 Bytes

Versions: 1

Compression:

Stored size: 787 Bytes

Contents

require "attentive/entities/core/time/duration"
require "date"
require "time"

Attentive::Entity.define "core.time",
    "noon",
    "midnight",
    %q{(?:(?<hours>\d\d?):(?<minutes>\d\d)\s*(?<pm>pm?))},
    %q{(?:(?<hours>\d\d?)\s*(?:am?|(?<pm>pm?)))},
    %q{(?:(?<hours>\d\d?):(?<minutes>\d\d))} do |match|

  minutes = 0

  if match.matched?("hours")
    hours = match["hours"].to_i
    hours += 12 if match.matched?("pm")
    minutes = match["minutes"].to_i if match.matched?("minutes")
  else
    case match.to_s
    when "noon" then hours = 12
    when "midnight" then hours = 0
    else nomatch!
    end
  end

  nomatch! if hours < 0 || hours > 24
  nomatch! if minutes < 0 || minutes > 60

  today = Date.today; Time.new(today.year, today.month, today.day, hours, minutes)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
attentive-0.3.5 lib/attentive/entities/core/time.rb