예제 #1
0
파일: interval.php 프로젝트: nicolaisi/adei
 static function ParseInterval($str)
 {
     $month = "(Jan(uary|\\.)?|Feb(ruary|\\.)?|Mar(ch|\\.)?|Apr(il|\\.)?|May(\\.)?|Jun(e|\\.)?|Jul(y|\\.)?|Aug(ust|\\.)?|Sep(tember|\\.)?|Oct(ober|\\.)?|Nov(ember|\\.)?|Dec(ember|\\.)?)";
     $year = "(\\d{4})";
     $day = "(\\b\\d{1,2}\\b)";
     $plus = "\\+\\s*(?<plus_value>\\d+)\\s*(?<plus_units>(s|sec|second|seconds|m|min|minute|minutes|h|hour|hours|d|day|days)\\.?)?";
     $timesep = ":";
     $time = "\\d{1,2}({$timesep}\\d{1,2}({$timesep}\\d{1,2})?)?";
     $timestamp = "[\\d.]+";
     $space = "\\s*";
     $dash = "{$space}-{$space}";
     $hcomma = "[\\s,;.T]+";
     $ycomma = "[\\s,;]+";
     $dcomma = "[\\s,;]+";
     if (preg_match("/^{$space}(?<year>{$year}){$space}\$/", $str, $m)) {
         // 2008
         list($from, $to, $duration) = INTERVAL::PICalc($str, $m['year']);
     } else {
         if (preg_match("/^{$space}(?<year1>{$year}){$dash}(?<year2>{$year}){$space}\$/", $str, $m)) {
             // 2006 - 2008
             list($from, $to, $duration) = INTERVAL::PICalc($str, $m['year1'], $m['year2']);
         } else {
             if (preg_match("/^{$space}(?<from>{$timestamp}){$space}-{$space}(?<to>{$timestamp}){$space}\$/", $str, $m)) {
                 if (!is_numeric($m['from']) || !is_numeric($m['to'])) {
                     throw new ADEIException(translate("Invalid interval (%s) is specified", $str));
                 }
                 if ($m['from'] > $m['to']) {
                     throw new ADEIException(translate("Invalid interval (%s) is specified: Start is above the end", $str));
                 }
                 return "{$m['from']}-{$m['to']}";
             } else {
                 if (preg_match("/^{$space}(?<month>{$month})({$ycomma}(?<year>{$year}))?{$space}\$/i", $str, $m)) {
                     // Feb., 2008
                     list($from, $to, $duration) = INTERVAL::PICalc($str, $m['year'], false, $m['month']);
                 } else {
                     if (preg_match("/^{$space}(?<month1>{$month}){$dash}(?<month2>{$month})({$ycomma}(?<year>{$year}))?{$space}\$/i", $str, $m)) {
                         // Feb. - Mar. 2008
                         list($from, $to, $duration) = INTERVAL::PICalc($str, $m['year'], false, $m['month1'], $m['month2']);
                     } else {
                         if (preg_match("/^{$space}(?<month>{$month}){$dcomma}(?<day>{$day})({$ycomma}(?<year>{$year}))?({$hcomma}(?<time1>{$time}){$space}-{$space}(?<time2>{$time}))?{$space}\$/i", $str, $m)) {
                             // Feb 21, 2008 15:00 - 17:00
                             list($from, $to, $duration) = INTERVAL::PICalc($str, $m['year'], false, $m['month'], false, $m['day'], false, $m['time1'], $m['time2']);
                         } else {
                             if (preg_match("/^{$space}(?<month>{$month}){$dcomma}(?<day1>{$day}){$dash}(?<day2>{$day})({$ycomma}(?<year>{$year}))?{$space}\$/i", $str, $m)) {
                                 // Feb 21 - 23, 2008
                                 list($from, $to, $duration) = INTERVAL::PICalc($str, $m['year'], false, $m['month'], false, $m['day1'], $m['day2']);
                             } else {
                                 if (preg_match("/^{$space}((?<time1>{$time}){$space}-{$space}(?<time2>{$time}){$hcomma})?(?<day1>{$day})({$dash}(?<day2>{$day}))?{$dcomma}(?<month>{$month})({$ycomma}(?<year>{$year}))?{$space}\$/i", $str, $m)) {
                                     // 21 - 23 Feb 2008 , 21 Feb 2008, 15:00:00 - 16:00:01 21 Feb 2008
                                     list($from, $to, $duration) = INTERVAL::PICalc($str, $m['year'], false, $m['month'], false, $m['day1'], $m['day2'], $m['time1'], $m['time2']);
                                 } else {
                                     if (preg_match("/^{$space}((?<year>{$year}){$ycomma})?(?<month1>{$month})({$dash}(?<month2>{$month}))?{$space}\$/i", $str, $m)) {
                                         // 2008, Feb. - Mar.
                                         list($from, $to, $duration) = INTERVAL::PICalc($str, $m['year'], false, $m['month1'], $m['month2']);
                                     } else {
                                         if (preg_match("/^{$space}((?<year>{$year}){$ycomma})?(?<month>{$month}){$dcomma}(?<day1>{$day})(({$dash}(?<day2>{$day}))|({$hcomma}(?<time1>{$time}){$space}-{$space}(?<time2>{$time})))?{$space}\$/i", $str, $m)) {
                                             // 2008, Feb 21 - 23     2008, Feb 21 15:00 - 17:00
                                             list($from, $to, $duration) = INTERVAL::PICalc($str, $m['year'], false, $m['month'], false, $m['day1'], $m['day2'], $m['time1'], $m['time2']);
                                         } else {
                                             if (preg_match("/^{$space}(?<from>.*){$plus}?{$space}\$/i", $str, $m)) {
                                                 // <date> +<duration>
                                                 $from = $m['from'];
                                                 $duration = $m['plus_value'];
                                                 switch (strtolower(substr($m['plus_units'], 0, 1))) {
                                                     case "m":
                                                         $duration *= 60;
                                                         break;
                                                     case "h":
                                                         $duration *= 3600;
                                                         break;
                                                     case "d":
                                                         $duration *= 86400;
                                                         break;
                                                 }
                                             } else {
                                                 if (substr_count($str, "-") == 1) {
                                                     $divider = "-";
                                                 } else {
                                                     if (preg_match_all("/\\s-\\s/", $str, $m) == 1) {
                                                         $divider = "\\s+-\\s+";
                                                     } else {
                                                         throw new ADEIException(translate("Unreckognized interval (%s) is specified", $str));
                                                     }
                                                 }
                                                 list($from, $to) = preg_split("/{$divider}/", $str);
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     #    echo "$from - $to + $duration\n";
     try {
         $fdate = new DateTime($from);
         if ($to) {
             $tdate = new DateTime($to);
         } else {
             $tdate = new DateTime($from);
         }
     } catch (Exception $e) {
         throw new ADEIException(translate("Unreckognized interval (%s) is specified", $str));
     }
     $from = $fdate->format("U");
     $to = $tdate->format("U") + $duration;
     if ($from > $to) {
         throw new ADEIException(translate("Invalid interval (%s) is specified: Start is above the end", $str));
     }
     return "{$from}-{$to}";
 }