Ejemplo n.º 1
0
 function normalize($date)
 {
     assert('! is_object($date) or $date instanceOf Modyllic_Token');
     $is_object = is_object($date);
     if ($is_object) {
         $value = $date->value();
         $unquoted = $date->unquote();
     } else {
         $value = $unquoted = $date;
     }
     if ($date instanceof Modyllic_Token_Reserved or !$is_object and Modyllic_SQL::is_reserved($value)) {
         return $value;
     }
     if (is_numeric($value) and $value == 0) {
         return "'0000-00-00 00:00:00'";
     }
     if ($is_object and !$date instanceof Modyllic_Token_String) {
         throw new Exception("Invalid date value: {$date}");
     }
     if (preg_match('/^(\\d{1,4})-(\\d\\d?)-(\\d\\d?)(?: (\\d\\d?)(?::(\\d\\d?)(?::(\\d\\d?))?)?)?$/', $unquoted, $matches)) {
         $year = $matches[1];
         $mon = $matches[2];
         $day = $matches[3];
         $hour = isset($matches[4]) ? $matches[4] : 0;
         $min = isset($matches[5]) ? $matches[5] : 0;
         $sec = isset($matches[6]) ? $matches[6] : 0;
         #list( $full, $year, $mon, $day, $hour, $min, $sec ) = $matches;
         return sprintf("'%04d-%02d-%02d %02d:%02d:%02d'", $year, $mon, $day, $hour, $min, $sec);
     } else {
         throw new Exception("Invalid date value: {$date}");
     }
 }
Ejemplo n.º 2
0
 function normalize($year)
 {
     $is_object = is_object($year);
     $value = $is_object ? $year->value() : $year;
     $unquoted = $is_object ? $year->unquote() : $year;
     if ($year instanceof Modyllic_Token_Reserved or !$is_object and Modyllic_SQL::is_reserved($value)) {
         return $value;
     }
     if ($year instanceof Modyllic_Token_Num or !$is_object and is_numeric($year)) {
         $plain = $value + 0;
         if ($plain == 0) {
             return "'0000'";
         } else {
             if ($plain > 0 and $plain < 70) {
                 return "'20{$plain}'";
             } else {
                 if ($plain >= 70 and $plain < 100) {
                     return "'19{$plain}'";
                 } else {
                     if ($plain > 1900 and $plain < 2155) {
                         return "'{$plain}'";
                     }
                 }
             }
         }
     } else {
         if (!$is_object or $year instanceof Modyllic_Token_String) {
             $plain = $unquoted + 0;
             if ($plain >= 0 and $plain < 70) {
                 return "'20{$plain}'";
             } else {
                 if ($plain >= 70 and $plain < 100) {
                     return "'19{$plain}'";
                 }
             }
         }
     }
     throw new Exception("Expected a valid year, got: {$year}");
 }