public function parse($value, $defaultToCurrentTime = true)
{
if (is_int($value) || is_float($value)) {
return $value;
} else {
if (!is_string($value)) {
throw new TInvalidDataValueException('date_to_parse_must_be_string', $value);
}
}
if (empty($this->pattern)) {
return time();
}
$date = time();
if ($this->length(trim($value)) < 1) {
return $defaultToCurrentTime ? $date : null;
}
$pattern = $this->pattern;
$i_val = 0;
$i_format = 0;
$pattern_length = $this->length($pattern);
$c = '';
$token = '';
$x = null;
$y = null;
if ($defaultToCurrentTime) {
$year = "{$date['year']}";
$month = $date['mon'];
$day = $date['mday'];
} else {
$year = null;
$month = null;
$day = null;
}
while ($i_format < $pattern_length) {
$c = $this->charAt($pattern, $i_format);
$token = '';
while ($this->charEqual($pattern, $i_format, $c) && $i_format < $pattern_length) {
$token .= $this->charAt($pattern, $i_format++);
}
if ($token == 'yyyy' || $token == 'yy' || $token == 'y') {
if ($token == 'yyyy') {
$x = 4;
$y = 4;
}
if ($token == 'yy') {
$x = 2;
$y = 2;
}
if ($token == 'y') {
$x = 2;
$y = 4;
}
$year = $this->getInteger($value, $i_val, $x, $y);
if ($year === null) {
return null;
}
//throw new TInvalidDataValueException('Invalid year', $value);
$i_val += strlen($year);
if (strlen($year) == 2) {
$iYear = (int) $year;
if ($iYear > 70) {
$year = $iYear + 1900;
} else {
$year = $iYear + 2000;
}
}
$year = (int) $year;
} elseif ($token == 'MM' || $token == 'M') {
$month = $this->getInteger($value, $i_val, $this->length($token), 2);
$iMonth = (int) $month;
if ($month === null || $iMonth < 1 || $iMonth > 12) {
return null;
}
//throw new TInvalidDataValueException('Invalid month', $value);
$i_val += strlen($month);
$month = $iMonth;
} elseif ($token == 'dd' || $token == 'd') {
$day = $this->getInteger($value, $i_val, $this->length($token), 2);
$iDay = (int) $day;
if ($day === null || $iDay < 1 || $iDay > 31) {
return null;
}
//throw new TInvalidDataValueException('Invalid day', $value);
$i_val += strlen($day);
$day = $iDay;
} else {
if ($this->substring($value, $i_val, $this->length($token)) != $token) {
return null;
} else {
$i_val += $this->length($token);
}
}
}
if ($i_val != $this->length($value)) {
return null;
}
//throw new TInvalidDataValueException("Pattern '{$this->pattern}' mismatch", $value);
if (!$defaultToCurrentTime && ($month === null || $day === null || $year === null)) {
return null;
} else {
if (empty($year)) {
$year = date('Y');
}
$day = (int) $day <= 0 ? 1 : (int) $day;
$month = (int) $month <= 0 ? 1 : (int) $month;
$s = new TDateTimeStamp();
return $s->getTimeStamp(0, 0, 0, $month, $day, $year);
}
}