time repeater - definitely doesn't work yet, but parses
authorChuck Hagenbuch <chuck@horde.org>
Tue, 3 Feb 2009 04:12:24 +0000 (23:12 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Tue, 3 Feb 2009 04:12:24 +0000 (23:12 -0500)
framework/Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeater/Time.php

index 424311c..0c9de67 100644 (file)
@@ -10,31 +10,34 @@ class Horde_Date_Parser_Locale_Base_Repeater_Time extends Horde_Date_Parser_Loca
         switch (strlen($t)) {
         case 1:
         case 2:
-            hours = t.to_i;
-            hours == 12 ? Tick.new(0 * 60 * 60, true) : Tick.new(hours * 60 * 60, true);
-            $this->type = $hours;
+            $hours = (int)$t;
+            $this->type = ($hours == 12) ?
+                new Horde_Date_Tick(0, true) :
+                new Horde_Date_Tick($hours * 3600, true);
             break;
 
         case 3:
-            $this->type = Tick.new((t[0..0].to_i * 60 * 60) + (t[1..2].to_i * 60), true);
+            $this->type = new Horde_Date_Tick($t[0] * 3600 + (int)substr($t, 1, 2) * 60, true);
             break;
 
         case 4:
-            $ambiguous = time =~ /:/ && t[0..0].to_i != 0 && t[0..1].to_i <= 12;
-            hours = t[0..1].to_i;
-            hours == 12 ? Tick.new(0 * 60 * 60 + t[2..3].to_i * 60, ambiguous) : Tick.new(hours * 60 * 60 + t[2..3].to_i * 60, ambiguous);
-            $this->type = $hours;
+            $ambiguous = (strpos($time, ':') !== false) && ($t[0] != 0) && ((int)substr($t, 0, 2) <= 12);
+            $hours = (int)substr($t, 0, 2);
+            $this->type = ($hours == 12) ?
+                new Horde_Date_Tick((int)substr($t, 2, 2) * 60, $ambiguous) :
+                new Horde_Date_Tick($hours * 60 * 60 + (int)substr($t, 2, 2) * 60, $ambiguous);
             break;
 
         case 5:
-            $this->type = Tick.new(t[0..0].to_i * 60 * 60 + t[1..2].to_i * 60 + t[3..4].to_i, true);
+            $this->type = new Horde_Date_Tick($t[0] * 3600 + (int)substr($t, 1, 2) * 60 + (int)substr($t, 3, 2), true);
             break;
 
         case 6:
-            $ambiguous = time =~ /:/ && t[0..0].to_i != 0 && t[0..1].to_i <= 12;
-            $hours = t[0..1].to_i;
-            $hours == 12 ? Tick.new(0 * 60 * 60 + t[2..3].to_i * 60 + t[4..5].to_i, ambiguous) : Tick.new(hours * 60 * 60 + t[2..3].to_i * 60 + t[4..5].to_i, ambiguous);
-            $this->type = $hours;
+            $ambiguous = (strpos($time, ':') !== false) && ($t[0] != 0) && ((int)substr($t, 0, 2) <= 12);
+            $hours = (int)substr($t, 0, 2);
+            $this->type = ($hours == 12) ?
+                new Horde_Date_Tick((int)substr($t, 2, 2) * 60 + (int)substr($t, 4, 2), $ambiguous) :
+                new Horde_Date_Tick($hours * 60 * 60 + (int)substr($t, 2, 2) * 60 + (int)substr($t, 4, 2), $ambiguous);
             break;
 
         default:
@@ -42,74 +45,92 @@ class Horde_Date_Parser_Locale_Base_Repeater_Time extends Horde_Date_Parser_Loca
         }
     }
 
-  # Return the next past or future Span for the time that this Repeater represents
-  #   pointer - Symbol representing which temporal direction to fetch the next day
-  #             must be either :past or :future
-  def next(pointer)
-    super
-
-    half_day = 60 * 60 * 12
-    full_day = 60 * 60 * 24
-
-    first = false
-
-    unless @current_time
-      first = true
-      midnight = Time.local(@now.year, @now.month, @now.day)
-      yesterday_midnight = midnight - full_day
-      tomorrow_midnight = midnight + full_day
-
-      catch :done do
-        if pointer == :future
-          if @type.ambiguous?
-            [midnight + @type, midnight + half_day + @type, tomorrow_midnight + @type].each do |t|
-              (@current_time = t; throw :done) if t >= @now
-            end
-          else
-            [midnight + @type, tomorrow_midnight + @type].each do |t|
-              (@current_time = t; throw :done) if t >= @now
-            end
-          end
-        else # pointer == :past
-          if @type.ambiguous?
-            [midnight + half_day + @type, midnight + @type, yesterday_midnight + @type * 2].each do |t|
-              (@current_time = t; throw :done) if t <= @now
-            end
-          else
-            [midnight + @type, yesterday_midnight + @type].each do |t|
-              (@current_time = t; throw :done) if t <= @now
-            end
-          end
-        end
-      end
-
-      @current_time || raise("Current time cannot be nil at this point")
-    end
+    /**
+     * Return the next past or future Span for the time that this Repeater represents
+     *   pointer - Symbol representing which temporal direction to fetch the next day
+     *             must be either :past or :future
+     */
+    public function next($pointer)
+    {
+        parent::next($pointer);
+
+        $halfDay = 3600 * 12;
+        $fullDay = 3600 * 24;
+
+        $first = false;
+
+        if (!$this->currentTime) {
+            $first = true;
+            $midnight = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day));
+            $yesterdayMidnight = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day - 1));
+            $tomorrowMidnight = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day + 1));
+
+            if ($pointer == 'future') {
+                if ($this->type->ambiguous) {
+                    foreach (array($midnight + $this->type, $midnight + $halfDay + $this->type, $tomorrowMidnight + $this->type) as $t) {
+                        if ($t >= $this->now) {
+                            $this->currentTime = $t;
+                            break;
+                        }
+                    }
+                } else {
+                    foreach (array($midnight + $this->type, $tomorrowMidnight + $this->type) as $t) {
+                        if ($t >= $this->now) {
+                            $this->currentTime = $t;
+                            break;
+                        }
+                    }
+                }
+            } else {
+                if ($this->type->ambiguous) {
+                    foreach (array($midnight + $halfDay + $this->type, $midnight + $this->type, $yesterdayMidnight + $this->type * 2) as $t) {
+                        if ($t <= $this->now) {
+                            $this->currentTime = $t;
+                            break;
+                        }
+                    }
+                } else {
+                    foreach (array($midnight + $this->type, $yesterdayMidnight + $this->type) as $t) {
+                        if ($t <= $this->now) {
+                            $this->currentTime = $t;
+                            break;
+                        }
+                    }
+                }
+            }
+
+            if (!$this->currentTime) {
+                throw new Horde_Date_Parser_Exception('Current time cannot be null at this point');
+            }
+        }
 
-    unless first
-      increment = @type.ambiguous? ? half_day : full_day
-      @current_time += pointer == :future ? increment : -increment
-    end
+        if (!$first) {
+            $increment = $this->type->ambiguous ? $halfday : $fullDay;
+            $this->currentTime += ($pointer == 'future') ? $increment : -$increment;
+        }
 
-    Chronic::Span.new(@current_time, @current_time + width)
-  end
+        return new Horde_Date_Span($this->currentTime, $this->currentTime + $this->width());
+    }
 
-  def this(context = :future)
-    super
+    public function this($context = 'future')
+    {
+        parent::this($context);
 
-    context = :future if context == :none
+        if ($context == 'none') { $context = 'future'; }
+        return $this->next($context);
+    }
 
-    self.next(context)
-  end
+    public function width()
+    {
+        return 1;
+    }
 
-  def width
-    1
-  end
+    public function __toString()
+    {
+        return parent::__toString() . '-time-' . $this->type;
+    }
 
-  def to_s
-    super << '-time-' << @type.to_s
-  end
-end
+}
 
 class Horde_Date_Tick
 {