### Home Classes Methods

### In Files

- lib/rbot/core/utils/parse_time.rb

### Methods

- ::parse_period
- ::time_unit

### Class/Module Index

Quicksearch

- Irc
- Irc::AsciiCasemap
- Irc::BanlistMessage
- Irc::BasicUserMessage
- Irc::Bot
- Irc::Bot::Auth
- Irc::Bot::Auth::BotOwnerClass
- Irc::Bot::Auth::BotUser
- Irc::Bot::Auth::Command
- Irc::Bot::Auth::DefaultBotUserClass
- Irc::Bot::Auth::InvalidPassword
- Irc::Bot::Auth::ManagerClass
- Irc::Bot::Auth::PermissionSet
- Irc::Bot::Config
- Irc::Bot::Config::ArrayValue
- Irc::Bot::Config::BooleanValue
- Irc::Bot::Config::EnumValue
- Irc::Bot::Config::FloatValue
- Irc::Bot::Config::IntegerValue
- Irc::Bot::Config::ManagerClass
- Irc::Bot::Config::StringValue
- Irc::Bot::Config::Value
- Irc::Bot::Config::Wizard
- Irc::Bot::DataFilter
- Irc::Bot::DataStream
- Irc::Bot::Language
- Irc::Bot::MessageMapper
- Irc::Bot::MessageMapper::Failure
- Irc::Bot::MessageMapper::FriendlyFailure
- Irc::Bot::MessageMapper::NoActionFailure
- Irc::Bot::MessageMapper::NoMatchFailure
- Irc::Bot::MessageMapper::NotPrivateFailure
- Irc::Bot::MessageMapper::NotPublicFailure
- Irc::Bot::MessageMapper::PartialMatchFailure
- Irc::Bot::MessageParameter
- Irc::Bot::MessageTemplate
- Irc::Bot::Plugins
- Irc::Bot::Plugins::BotModule
- Irc::Bot::Plugins::CoreBotModule
- Irc::Bot::Plugins::Plugin
- Irc::Bot::Plugins::PluginManagerClass
- Irc::Bot::Plugins::RecurseTooDeep
- Irc::Bot::Plugins::RemoteBotModule
- Irc::Bot::Plugins::RemoteCoreBotModule
- Irc::Bot::Plugins::RemotePlugin
- Irc::Bot::Registry
- Irc::Bot::Registry::Accessor
- Irc::Bot::RemoteDispatcher
- Irc::Bot::RemoteMessage
- Irc::Bot::RemoteObject
- Irc::Bot::Wordlist
- Irc::BotConfig
- Irc::Casemap
- Irc::Channel
- Irc::Channel::Mode
- Irc::Channel::ModeHash
- Irc::Channel::ModeTypeA
- Irc::Channel::ModeTypeB
- Irc::Channel::ModeTypeC
- Irc::Channel::ModeTypeD
- Irc::Channel::Topic
- Irc::Channel::UserMode
- Irc::ChannelList
- Irc::Client
- Irc::DBHash
- Irc::DBTree
- Irc::InviteMessage
- Irc::JoinMessage
- Irc::KickMessage
- Irc::MessageQueue
- Irc::ModeChangeMessage
- Irc::MotdMessage
- Irc::NamesMessage
- Irc::Netmask
- Irc::NetmaskDb
- Irc::NetmaskDb::Tree
- Irc::NetmaskList
- Irc::NickMessage
- Irc::NoSuchTargetMessage
- Irc::NoticeMessage
- Irc::PartMessage
- Irc::PrivMessage
- Irc::QueueRing
- Irc::QuitMessage
- Irc::RfcCasemap
- Irc::Server
- Irc::ServerOrCasemap
- Irc::Socket
- Irc::Socket::IdentityFilter
- Irc::StrictRfcCasemap
- Irc::TopicMessage
- Irc::UnknownMessage
- Irc::User
- Irc::UserList
- Irc::UserMessage
- Irc::Utils
- Irc::Utils::HttpUtil
- Irc::Utils::HttpUtil::CachedObject
- Irc::Utils::ParseTime
- Irc::WelcomeMessage
- Irc::WhoisMessage
- BDB
- BDB::CIBtree
- MonitorMixin
- MonitorMixin::ConditionVariable
- Net
- Net::HTTPResponse
- Regexp
- Regexp::Irc
- Singleton
- Singleton::SingletonClassMethods
- Timer
- Timer::Action
- TokyoCabinet
- TokyoCabinet::CIBDB
- UnicodePlugin
- UnicodePlugin::UnicodeFilter
- Array
- ArrayOf
- AuthModule
- BasicsModule
- ConditionVariable
- ConfigModule
- DottedIndex
- Enumerable
- Exception
- FiltersModule
- GetText
- GetTextVersionError
- HttpUtilPlugin
- IrcLogModule
- Module
- Numeric
- Object
- Range
- RemoteModule
- ServerError
- String
- Symbol
- UserDataModule
- WordlistModule
- YAML

No matching classes.

# Irc::Utils::ParseTime

### Constants

ENTIES
ENTIES_RX
FLOAT_RX
FRACTIONS
FRACTION_RX
LITNUM_RX
ONE_TO_NINE
ONE_TO_NINE_RX
TEENS_ETC
TEENS_ETC_RX
UNITSPEC_RX

### Public Class Methods

parse_period(str) click to toggle source

example: half an hour, two and a half weeks, 5 seconds, an hour and 5 minutes

    # File lib/rbot/core/utils/parse_time.rb, line 107
    def ParseTime.parse_period(str)
      clean = str.gsub(/\s+/, ' ').strip

      sofar = 0
      until clean.empty?
        if clean.sub!(/^(#{FRACTION_RX})\s+#{UNITSPEC_RX}/, '')
          # fraction followed by unit
          num = FRACTIONS[$1.intern]
          unit = ParseTime.time_unit($2)
        elsif clean.sub!(/^#{FLOAT_RX}\s*(?:\s+and\s+(#{FRACTION_RX})\s+)?#{UNITSPEC_RX}/, '')
          # float plus optional fraction followed by unit
          num = $1.to_f
          frac = $2
          unit = ParseTime.time_unit($3)
          clean.strip!
          if frac.nil? and clean.sub!(/^and\s+(#{FRACTION_RX})/, '')
            frac = $1
          end
          if frac
            num += FRACTIONS[frac.intern]
          end
        elsif clean.sub!(/^(?:#{LITNUM_RX})\s+(?:and\s+(#{FRACTION_RX})\s+)?#{UNITSPEC_RX}/, '')
          if $1
            num = ONE_TO_NINE[$1.intern]
          elsif $2
            num = TEENS_ETC[$2.intern]
          elsif $3
            num = ENTIES[$3.intern]
            if $4
              num += ONE_TO_NINE[$4.intern]
            end
          end
          frac = $5
          unit = ParseTime.time_unit($6)
          clean.strip!
          if frac.nil? and clean.sub!(/^and\s+(#{FRACTION_RX})/, '')
            frac = $1
          end
          if frac
            num += FRACTIONS[frac.intern]
          end
        else
          raise "invalid time string: #{clean} (parsed #{sofar} so far)"
        end
        sofar += num * unit
        clean.sub!(/^and\s+/, '')
      end
      return sofar
    end

time_unit(str) click to toggle source

str must much UNITSPEC_RX

    # File lib/rbot/core/utils/parse_time.rb, line 83
    def ParseTime.time_unit(str)
      case str[0,1].intern
      when :s
        1
      when :m
        if str[1,1] == 'o'
          # months
          3600*24*30
        else
          #minutes
          60
        end
      when :h
        3600
      when :d
        3600*24
      when :w
        3600*24*7
      when :y
        3600*24*365
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.