Ejemplo n.º 1
0
 public function testScanForTz()
 {
     $parser = Horde_Date_Parser::factory();
     $tokenizer = $parser->componentFactory('Timezone');
     $token = new Horde_Date_Parser_Token('test');
     $results = $tokenizer->scan(array($token));
     $this->assertEmpty($results[0]->getTag('timezone'));
     $token = new Horde_Date_Parser_Token('EST');
     $results = $tokenizer->scan(array($token));
     $this->assertEquals('tz', $results[0]->getTag('timezone'));
 }
Ejemplo n.º 2
0
 public function testPreNormalizeCase()
 {
     $parser = Horde_Date_Parser::factory();
     $this->assertEquals('this day', $parser->preNormalize('Today'));
     $this->assertEquals('this day', $parser->preNormalize('today'));
     $this->assertEquals('this day', $parser->preNormalize('toDay'));
     $this->assertEquals('next day', $parser->preNormalize('Tommorow'));
     $this->assertEquals('next day', $parser->preNormalize('tommorow'));
     $this->assertEquals('next day', $parser->preNormalize('TOMMOROW'));
     $this->assertEquals('next day', $parser->preNormalize('tomorow'));
     $this->assertEquals('last day', $parser->preNormalize('Yesterday'));
     $this->assertEquals('last day', $parser->preNormalize('yesterday'));
     $this->assertEquals('12:00', $parser->preNormalize('Noon'));
     $this->assertEquals('12:00', $parser->preNormalize('noon'));
     $this->assertEquals('24:00', $parser->preNormalize('Midnight'));
     $this->assertEquals('24:00', $parser->preNormalize('midnight'));
 }
Ejemplo n.º 3
0
 /**
  * Wed Aug 16 14:00:00 UTC 2006
  */
 public function setUp()
 {
     $this->now = new Horde_Date('2006-08-16 14:00:00');
     $this->parser = Horde_Date_Parser::factory(array('now' => $this->now));
 }
Ejemplo n.º 4
0
 /**
  * Imports one or more tasks parsed from a string.
  *
  * @param string $text      The text to parse into
  * @param string $tasklist  The tasklist into which the task will be
  *                          imported.  If 'null', the user's default
  *                          tasklist will be used.
  *
  * @return array  The UIDs of all tasks that were added.
  */
 public static function createTasksFromText($text, $tasklist = null)
 {
     if ($tasklist === null) {
         $tasklist = self::getDefaultTasklist(Horde_Perms::EDIT);
     } elseif (!self::hasPermission($tasklist, Horde_Perms::EDIT)) {
         throw new Horde_Exception_PermissionDenied();
     }
     $storage = $GLOBALS['injector']->getInstance('Nag_Factory_Driver')->create($tasklist);
     $dateParser = Horde_Date_Parser::factory(array('locale' => $GLOBALS['prefs']->getValue('language')));
     $quickParser = new Nag_QuickParser();
     $tasks = $quickParser->parse($text);
     $uids = array();
     foreach ($tasks as &$task) {
         if (!is_array($task)) {
             $name = $task;
             $task = array($name);
         }
         $r = $dateParser->parse($task[0], array('return' => 'result'));
         if ($d = $r->guess()) {
             $name = $r->untaggedText();
             $due = $d->timestamp();
         } else {
             $name = $task[0];
             $due = 0;
         }
         // Look for tags to be added in the text.
         $pattern = '/#\\w+/';
         $tags = array();
         if (preg_match_all($pattern, $name, $results)) {
             $tags = $results[0];
             $name = str_replace($tags, '', $name);
             $tags = array_map(function ($x) {
                 return substr($x, -(strlen($x) - 1));
             }, $tags);
         } else {
             $tags = '';
         }
         if (isset($task['parent'])) {
             $newTask = $storage->add(array('name' => $name, 'due' => $due, 'parent' => $tasks[$task['parent']]['id'], 'tags' => $tags));
         } else {
             $newTask = $storage->add(array('name' => $name, 'due' => $due, 'tags' => $tags));
         }
         $uids[] = $newTask[1];
         $task['id'] = $newTask[0];
     }
     return $uids;
 }
Ejemplo n.º 5
0
 /**
  * Imports an event parsed from a string.
  *
  * @param string $text      The text to parse into an event
  * @param string $calendar  The calendar into which the event will be
  *                          imported.  If 'null', the user's default
  *                          calendar will be used.
  *
  * @return array  The UID of all events that were added.
  * @throws Kronolith_Exception
  */
 public function quickAdd($text, $calendar = null)
 {
     $text = trim($text);
     if (strpos($text, "\n") !== false) {
         list($title, $description) = explode($text, "\n", 2);
     } else {
         $title = $text;
         $description = '';
     }
     $title = trim($title);
     $description = trim($description);
     $dateParser = Horde_Date_Parser::factory(array('locale' => $GLOBALS['language']));
     $r = $dateParser->parse($title, array('return' => 'result'));
     if (!($d = $r->guess())) {
         throw new Horde_Exception(sprintf(_("Cannot parse event description \"%s\""), Horde_String::truncate($text)));
     }
     $title = $r->untaggedText();
     $kronolith_driver = self::getDriver(null, $calendar);
     $event = $kronolith_driver->getEvent();
     $event->initialized = true;
     $event->title = $title;
     $event->description = $description;
     $event->start = $d;
     $event->end = $d->add(array('hour' => 1));
     $event->save();
     return $event;
 }
Ejemplo n.º 6
0
 /**
  * Perform the search
  *
  * @param integer $page     The page number
  * @param integer $perpage  The number of results per page.
  *
  * @return Nag_Task
  */
 protected function _search($page, $perpage)
 {
     global $injector, $prefs;
     if (!empty($this->_due)) {
         $parser = Horde_Date_Parser::factory(array('locale' => $GLOBALS['prefs']->getValue('language')));
         $date = $parser->parse($this->_due[1]);
         $date->mday += $this->_due[0];
         $date = $date->timestamp();
     } else {
         $date = false;
     }
     // Get the full, sorted task list.
     $tasks = Nag::listTasks(array('tasklists' => $this->_tasklists, 'completed' => $this->_completed, 'include_history' => false));
     if (!empty($this->_search)) {
         $pattern = '/' . preg_quote($this->_search, '/') . '/i';
     }
     $search_results = new Nag_Task();
     if (!empty($this->_tags)) {
         $tagged_tasks = $injector->getInstance('Nag_Tagger')->search($this->_tags, array('list' => $GLOBALS['display_tasklists']));
     }
     $tasks->reset();
     while ($task = $tasks->each()) {
         if (!empty($date)) {
             if (empty($task->due) || $task->due > $date) {
                 continue;
             }
         }
         // If we have a search string and it doesn't match name|desc continue
         if (!empty($this->_search) && !($this->_mask & self::MASK_NAME && preg_match($pattern, $task->name)) && !($this->_mask & self::MASK_DESC && preg_match($pattern, $task->desc))) {
             continue;
         }
         // No tags to search? Add it to results. Otherwise, make sure it
         // has the requested tags.
         if (empty($this->_tags) || in_array($task->uid, $tagged_tasks)) {
             $search_results->add($task);
         }
     }
     // Now that we have filtered results, load all tags at once.
     $search_results->loadTags();
     return $search_results;
 }
Ejemplo n.º 7
0
<?php

set_include_path(dirname(dirname(dirname(dirname(__DIR__)))) . '/lib:' . get_include_path());
require 'Horde/Autoloader/Default.php';
$t = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : 'this fortnight';
$opts = count($_SERVER['argv']) > 1 ? array() : array('now' => new Horde_Date('2006-08-16 14:00:00'));
$d = Horde_Date_Parser::parse($t, $opts);
echo "{$d}\n";