From 84d643347c81534132daa5514ae9657595e93a80 Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Tue, 16 Dec 2008 00:22:16 -0500 Subject: [PATCH] start adding base classes for date_parser and break out the locale-specific classes --- .../Horde_Date_Parser/lib/Horde/Date/Parser.php | 76 ++++-------- .../lib/Horde/Date/Parser/Exception.php | 4 + .../Date/Parser/{Parser.php => Locale/Base.php} | 133 ++++----------------- .../Date/Parser/{ => Locale/Base}/Grabber.php | 0 .../Date/Parser/{ => Locale/Base}/Handlers.php | 0 .../Date/Parser/{ => Locale/Base}/Ordinal.php | 0 .../Date/Parser/{ => Locale/Base}/Pointer.php | 0 .../Date/Parser/{ => Locale/Base}/Repeater.php | 0 .../Parser/{ => Locale/Base}/Repeaters/Day.php | 0 .../Parser/{ => Locale/Base}/Repeaters/DayName.php | 0 .../{ => Locale/Base}/Repeaters/DayPortion.php | 0 .../{ => Locale/Base}/Repeaters/Fortnight.php | 0 .../Parser/{ => Locale/Base}/Repeaters/Hour.php | 0 .../Parser/{ => Locale/Base}/Repeaters/Minute.php | 0 .../Parser/{ => Locale/Base}/Repeaters/Month.php | 0 .../{ => Locale/Base}/Repeaters/MonthName.php | 0 .../Parser/{ => Locale/Base}/Repeaters/Season.php | 0 .../{ => Locale/Base}/Repeaters/SeasonName.php | 0 .../Parser/{ => Locale/Base}/Repeaters/Second.php | 0 .../Parser/{ => Locale/Base}/Repeaters/Time.php | 0 .../Parser/{ => Locale/Base}/Repeaters/Week.php | 0 .../Parser/{ => Locale/Base}/Repeaters/Weekend.php | 0 .../Parser/{ => Locale/Base}/Repeaters/Year.php | 0 .../Horde/Date/Parser/{ => Locale/Base}/Scalar.php | 0 .../Date/Parser/{ => Locale/Base}/Separator.php | 0 .../{TimeZone.php => Locale/Base/Timezone.php} | 0 .../lib/Horde/Date/Parser/Tag.php | 21 ++++ .../lib/Horde/Date/Parser/Token.php | 54 +++++++++ .../Horde_Date_Parser/lib/Horde/Date/Span.php | 27 +++++ 29 files changed, 155 insertions(+), 160 deletions(-) create mode 100644 framework/Horde_Date_Parser/lib/Horde/Date/Parser/Exception.php rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{Parser.php => Locale/Base.php} (71%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Grabber.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Handlers.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Ordinal.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Pointer.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Repeater.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Repeaters/Day.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Repeaters/DayName.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Repeaters/DayPortion.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Repeaters/Fortnight.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Repeaters/Hour.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Repeaters/Minute.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Repeaters/Month.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Repeaters/MonthName.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Repeaters/Season.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Repeaters/SeasonName.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Repeaters/Second.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Repeaters/Time.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Repeaters/Week.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Repeaters/Weekend.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Repeaters/Year.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Scalar.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{ => Locale/Base}/Separator.php (100%) rename framework/Horde_Date_Parser/lib/Horde/Date/Parser/{TimeZone.php => Locale/Base/Timezone.php} (100%) create mode 100644 framework/Horde_Date_Parser/lib/Horde/Date/Parser/Tag.php create mode 100644 framework/Horde_Date_Parser/lib/Horde/Date/Parser/Token.php create mode 100644 framework/Horde_Date_Parser/lib/Horde/Date/Span.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser.php index 477240d97..75777a2bd 100644 --- a/framework/Horde_Date_Parser/lib/Horde/Date/Parser.php +++ b/framework/Horde_Date_Parser/lib/Horde/Date/Parser.php @@ -1,53 +1,29 @@ += 60 - minute += second / 60 - second = second % 60 - end - - if minute >= 60 - hour += minute / 60 - minute = minute % 60 - end - - if hour >= 24 - day += hour / 24 - hour = hour % 24 - end - - # determine if there is a day overflow. this is complicated by our crappy calendar - # system (non-constant number of days per month) - day <= 56 || raise("day must be no more than 56 (makes month resolution easier)") - if day > 28 - # no month ever has fewer than 28 days, so only do this if necessary - leap_year = (year % 4 == 0) && !(year % 100 == 0) - leap_year_month_days = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] - common_year_month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] - days_this_month = leap_year ? leap_year_month_days[month - 1] : common_year_month_days[month - 1] - if day > days_this_month - month += day / days_this_month - day = day % days_this_month - end - end - - if month > 12 - if month % 12 == 0 - year += (month - 12) / 12 - month = 12 - else - year += month / 12 - month = month % 12 - end - end - - Time.local(year, month, day, hour, minute, second) - end -end +} diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Exception.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Exception.php new file mode 100644 index 000000000..c6a8ac0f7 --- /dev/null +++ b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Exception.php @@ -0,0 +1,4 @@ +:guess). If no date or time can be found, # +nil+ will be returned. # @@ -11,8 +11,8 @@ module Chronic # [:context] # :past or :future (defaults to :future) # - # If your string represents a birthday, you can set :context to :past - # and if an ambiguous string is given, it will assume it is in the + # If your string represents a birthday, you can set :context to :past + # and if an ambiguous string is given, it will assume it is in the # past. Specify :future or omit to set a future context. # # [:now] @@ -27,16 +27,16 @@ module Chronic # By default, the parser will guess a single point in time for the # given date or time. If you'd rather have the entire time span returned, # set :guess to +false+ and a Chronic::Span will be returned. - # + # # [:ambiguous_time_range] # Integer or :none (defaults to 6 (6am-6pm)) # - # If an Integer is given, ambiguous times (like 5:00) will be + # If an Integer is given, ambiguous times (like 5:00) will be # assumed to be within the range of that time in the AM to that time # in the PM. For example, if you set it to 7, then the parser will # look for the time between 7am and 7pm. In the case of 5:00, it would # assume that means 5:00pm. If :none is given, no assumption - # will be made, and the first matching instance of that time will + # will be made, and the first matching instance of that time will # be used. def parse(text, specified_options = {}) # get options and set defaults if necessary @@ -45,40 +45,40 @@ module Chronic :guess => true, :ambiguous_time_range => 6} options = default_options.merge specified_options - + # ensure the specified options are valid specified_options.keys.each do |key| default_options.keys.include?(key) || raise(InvalidArgumentException, "#{key} is not a valid option key.") end [:past, :future, :none].include?(options[:context]) || raise(InvalidArgumentException, "Invalid value ':#{options[:context]}' for :context specified. Valid values are :past and :future.") - + # store now for later =) @now = options[:now] - + # put the text into a normal format to ease scanning text = self.pre_normalize(text) - + # get base tokens for each word @tokens = self.base_tokenize(text) - + # scan the tokens with each token scanner [Repeater].each do |tokenizer| @tokens = tokenizer.scan(@tokens, options) end - + [Grabber, Pointer, Scalar, Ordinal, Separator, TimeZone].each do |tokenizer| @tokens = tokenizer.scan(@tokens) end - + # strip any non-tagged tokens @tokens = @tokens.select { |token| token.tagged? } - + if Chronic.debug puts "+---------------------------------------------------" puts "| " + @tokens.to_s puts "+---------------------------------------------------" end - + # do the heavy lifting begin span = self.tokens_to_span(@tokens, options) @@ -86,7 +86,7 @@ module Chronic raise return nil end - + # guess a time within a span if required if options[:guess] return self.guess(span) @@ -94,7 +94,7 @@ module Chronic return span end end - + # Clean up the specified input text by stripping unwanted characters, # converting idioms to their canonical form, converting number words # to numbers (three => 3), and converting ordinal words to numeric @@ -121,23 +121,23 @@ module Chronic normalized_text.gsub!(/\b(hence|after|from)\b/, 'future') normalized_text = numericize_ordinals(normalized_text) end - + # Convert number words to numbers (three => 3) def numericize_numbers(text) #:nodoc: Numerizer.numerize(text) end - + # Convert ordinal words to numeric ordinals (third => 3rd) def numericize_ordinals(text) #:nodoc: text end - + # Split the text on spaces and convert each word into # a Token def base_tokenize(text) #:nodoc: text.split(' ').map { |word| Token.new(word) } end - + # Guess a specific time within the given span def guess(span) #:nodoc: return nil if span.nil? @@ -148,92 +148,5 @@ module Chronic end end end - - class Token #:nodoc: - attr_accessor :word, :tags - - def initialize(word) - @word = word - @tags = [] - end - - # Tag this token with the specified tag - def tag(new_tag) - @tags << new_tag - end - - # Remove all tags of the given class - def untag(tag_class) - @tags = @tags.select { |m| !m.kind_of? tag_class } - end - - # Return true if this token has any tags - def tagged? - @tags.size > 0 - end - - # Return the Tag that matches the given class - def get_tag(tag_class) - matches = @tags.select { |m| m.kind_of? tag_class } - #matches.size < 2 || raise("Multiple identical tags found") - return matches.first - end - - # Print this Token in a pretty way - def to_s - @word << '(' << @tags.join(', ') << ') ' - end - end - - # A Span represents a range of time. Since this class extends - # Range, you can use #begin and #end to get the beginning and - # ending times of the span (they will be of class Time) - class Span < Range - # Returns the width of this span in seconds - def width - (self.end - self.begin).to_i - end - - # Add a number of seconds to this span, returning the - # resulting Span - def +(seconds) - Span.new(self.begin + seconds, self.end + seconds) - end - - # Subtract a number of seconds to this span, returning the - # resulting Span - def -(seconds) - self + -seconds - end - - # Prints this span in a nice fashion - def to_s - '(' << self.begin.to_s << '..' << self.end.to_s << ')' - end - end - # Tokens are tagged with subclassed instances of this class when - # they match specific criteria - class Tag #:nodoc: - attr_accessor :type - - def initialize(type) - @type = type - end - - def start=(s) - @now = s - end - end - - # Internal exception - class ChronicPain < Exception #:nodoc: - - end - - # This exception is raised if an invalid argument is provided to - # any of Chronic's methods - class InvalidArgumentException < Exception - - end -end \ No newline at end of file +end diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Grabber.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Grabber.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Grabber.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Grabber.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Handlers.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Handlers.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Handlers.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Handlers.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Ordinal.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Ordinal.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Ordinal.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Ordinal.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Pointer.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Pointer.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Pointer.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Pointer.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeater.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeater.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeater.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeater.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Day.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Day.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Day.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Day.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/DayName.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/DayName.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/DayName.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/DayName.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/DayPortion.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/DayPortion.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/DayPortion.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/DayPortion.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Fortnight.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Fortnight.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Fortnight.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Fortnight.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Hour.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Hour.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Hour.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Hour.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Minute.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Minute.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Minute.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Minute.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Month.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Month.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Month.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Month.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/MonthName.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/MonthName.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/MonthName.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/MonthName.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Season.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Season.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Season.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Season.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/SeasonName.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/SeasonName.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/SeasonName.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/SeasonName.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Second.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Second.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Second.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Second.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Time.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Time.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Time.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Time.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Week.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Week.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Week.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Week.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Weekend.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Weekend.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Weekend.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Weekend.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Year.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Year.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Repeaters/Year.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeaters/Year.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Scalar.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Scalar.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Scalar.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Scalar.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Separator.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Separator.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/Separator.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Separator.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/TimeZone.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Timezone.php similarity index 100% rename from framework/Horde_Date_Parser/lib/Horde/Date/Parser/TimeZone.php rename to framework/Horde_Date_Parser/lib/Horde/Date/Parser/Locale/Base/Timezone.php diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Tag.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Tag.php new file mode 100644 index 000000000..a0796408a --- /dev/null +++ b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Tag.php @@ -0,0 +1,21 @@ +type = $type; + } + + public function start($s) + { + $this->now = $s; + } + +} diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Token.php b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Token.php new file mode 100644 index 000000000..16b3b70a5 --- /dev/null +++ b/framework/Horde_Date_Parser/lib/Horde/Date/Parser/Token.php @@ -0,0 +1,54 @@ +word = $word; + $this->tags = array(); + } + + /** + * Tag this token with the specified tag + */ + public function tag($new_tag) + { + $this->tags[] = $new_tag; + } + + /** + * Remove all tags of the given class + */ + public function untag($tag_class) + { + $this->tags = array_filter($this->tags, create_function('$t', 'return $t instanceof ' . $tag_class)); + } + + /** + * Return true if this token has any tags + */ + public function tagged() + { + return count($this->tags) > 0; + } + + /** + * Return the Tag that matches the given class + */ + public function getTag($tag_class) + { + $matches = array_filter($this->tags, create_function('$t', 'return $t instanceof ' . $tag_class)); + return array_shift($matches); + } + + /** + * Print this Token in a pretty way + */ + public function __toString() + { + return '(' . implode(', ', $this->tags) . ') '; + } + +} diff --git a/framework/Horde_Date_Parser/lib/Horde/Date/Span.php b/framework/Horde_Date_Parser/lib/Horde/Date/Span.php new file mode 100644 index 000000000..d61c24cc0 --- /dev/null +++ b/framework/Horde_Date_Parser/lib/Horde/Date/Span.php @@ -0,0 +1,27 @@ + # A Span represents a range of time. Since this class extends + # Range, you can use #begin and #end to get the beginning and + # ending times of the span (they will be of class Time) + class Span < Range + # Returns the width of this span in seconds + def width + (self.end - self.begin).to_i + end + + # Add a number of seconds to this span, returning the + # resulting Span + def +(seconds) + Span.new(self.begin + seconds, self.end + seconds) + end + + # Subtract a number of seconds to this span, returning the + # resulting Span + def -(seconds) + self + -seconds + end + + # Prints this span in a nice fashion + def to_s + '(' << self.begin.to_s << '..' << self.end.to_s << ')' + end + end + -- 2.11.0