Changeset View
Changeset View
Standalone View
Standalone View
src/view/form/control/AphrontFormDateControlValue.php
Show First 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | public static function newFromParts( | ||||
return $value; | return $value; | ||||
} | } | ||||
public static function newFromRequest(AphrontRequest $request, $key) { | public static function newFromRequest(AphrontRequest $request, $key) { | ||||
$value = new AphrontFormDateControlValue(); | $value = new AphrontFormDateControlValue(); | ||||
$value->viewer = $request->getViewer(); | $value->viewer = $request->getViewer(); | ||||
$datetime = $request->getStr($key); | |||||
if (strlen($datetime)) { | |||||
$date = $datetime; | |||||
$time = null; | |||||
} else { | |||||
$date = $request->getStr($key.'_d'); | $date = $request->getStr($key.'_d'); | ||||
$time = $request->getStr($key.'_t'); | $time = $request->getStr($key.'_t'); | ||||
} | |||||
// If this looks like an epoch timestamp, prefix it with "@" so that | // If we have the individual parts, we read them preferentially. If we do | ||||
// DateTime() reads it as one. Assume small numbers are a "Ymd" digit | // not, try to read the key as a raw value. This makes it so that HTTP | ||||
// string instead of an epoch timestamp for a time in 1970. | // prefilling is overwritten by the control value if the user changes it. | ||||
if (ctype_digit($date) && ($date > 30000000)) { | if (!strlen($date) && !strlen($time)) { | ||||
$date = '@'.$date; | $date = $request->getStr($key); | ||||
$time = null; | $time = null; | ||||
} | } | ||||
$value->valueDate = $date; | $value->valueDate = $date; | ||||
$value->valueTime = $time; | $value->valueTime = $time; | ||||
$formatted = $value->getFormattedDateFromDate( | $formatted = $value->getFormattedDateFromDate( | ||||
$value->valueDate, | $value->valueDate, | ||||
▲ Show 20 Lines • Show All 125 Lines • ▼ Show 20 Lines | private function getFormattedDateFromDate($date, $time) { | ||||
); | ); | ||||
return array($date, $time); | return array($date, $time); | ||||
} | } | ||||
private function newDateTime($date, $time) { | private function newDateTime($date, $time) { | ||||
$date = $this->getStandardDateFormat($date); | $date = $this->getStandardDateFormat($date); | ||||
$time = $this->getStandardTimeFormat($time); | $time = $this->getStandardTimeFormat($time); | ||||
try { | try { | ||||
$datetime = new DateTime("{$date} {$time}"); | // We need to provide the timezone in the constructor, and also set it | ||||
// explicitly. If the date is an epoch timestamp, the timezone in the | |||||
// constructor is ignored. If the date is not an epoch timestamp, it is | |||||
// used to parse the date. | |||||
$zone = $this->getTimezone(); | |||||
$datetime = new DateTime("{$date} {$time}", $zone); | |||||
$datetime->setTimezone($zone); | |||||
} catch (Exception $ex) { | } catch (Exception $ex) { | ||||
return null; | return null; | ||||
} | } | ||||
// Set the timezone explicitly because it is ignored in the constructor | |||||
// if the date is an epoch timestamp. | |||||
$zone = $this->getTimezone(); | |||||
$datetime->setTimezone($zone); | |||||
return $datetime; | return $datetime; | ||||
} | } | ||||
private function getFormattedDateFromParts( | private function getFormattedDateFromParts( | ||||
$year, | $year, | ||||
$month, | $month, | ||||
$day, | $day, | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | private function getStandardDateFormat($date) { | ||||
$normalized = preg_replace('/[^a-z]/', '', $normalized); | $normalized = preg_replace('/[^a-z]/', '', $normalized); | ||||
$normalized = preg_replace('/day\z/', '', $normalized); | $normalized = preg_replace('/day\z/', '', $normalized); | ||||
$normalized = preg_replace('/s\z/', '', $normalized); | $normalized = preg_replace('/s\z/', '', $normalized); | ||||
if (isset($colloquial[$normalized])) { | if (isset($colloquial[$normalized])) { | ||||
return $colloquial[$normalized]; | return $colloquial[$normalized]; | ||||
} | } | ||||
// If this looks like an epoch timestamp, prefix it with "@" so that | |||||
// DateTime() reads it as one. Assume small numbers are a "Ymd" digit | |||||
// string instead of an epoch timestamp for a time in 1970. | |||||
if (ctype_digit($date) && ($date > 30000000)) { | |||||
$date = '@'.$date; | |||||
} | |||||
$separator = $this->getFormatSeparator(); | $separator = $this->getFormatSeparator(); | ||||
$parts = preg_split('@[,./:-]@', $date); | $parts = preg_split('@[,./:-]@', $date); | ||||
return implode($separator, $parts); | return implode($separator, $parts); | ||||
} | } | ||||
private function getStandardTimeFormat($time) { | private function getStandardTimeFormat($time) { | ||||
$colloquial = array( | $colloquial = array( | ||||
'crack of dawn' => '5:00 AM', | 'crack of dawn' => '5:00 AM', | ||||
Show All 25 Lines |