Skip to content

Commit

Permalink
CC-5629 : Smart Block timestamps aren't saved in UTC time
Browse files Browse the repository at this point in the history
  • Loading branch information
naomiaro committed Dec 11, 2013
1 parent c5f30c2 commit e6d28b2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 61 deletions.
40 changes: 0 additions & 40 deletions airtime_mvc/application/common/DateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,26 +121,6 @@ function setDate($dateString)
$this->_dateTime = $dateTime->getTimestamp();
}

/**
* Calculate and return the timestamp for end of day today
* in local time.
*
* For example, if local time is 2PM on 2011-11-01,
* then the function would return 2011-11-02 00:00:00
*
* @return End of day timestamp in local timezone
*/
public static function GetDayEndTimestamp($time = "") {
$dateTime = $time == "" ? new DateTime(date("Y-m-d")) : new DateTime($time);
$dateTime->add(new DateInterval('P1D'));
return $dateTime->format('Y-m-d H:i:s');
}

public static function GetDayEndTimestampInUtc($time = "") {
$dayEndTimestamp = Application_Common_DateHelper::GetDayEndTimestamp($time);
return Application_Common_DateHelper::ConvertToUtcDateTimeString($dayEndTimestamp);
}

/**
* Find the epoch timestamp difference from "now" to the beginning of today.
*/
Expand Down Expand Up @@ -272,26 +252,6 @@ public static function ConvertToUtcDateTime($p_dateString, $timezone=null){

return $dateTime;
}

public static function ConvertToSpecificTimezoneDateTime($p_dateString, $timezone){
$dateTime = new DateTime($p_dateString, new DateTimeZone("UTC"));
$dateTime->setTimezone(new DateTimeZone($timezone));

return $dateTime;
}

public static function ConvertToLocalDateTime($p_dateString){
$dateTime = new DateTime($p_dateString, new DateTimeZone("UTC"));
$dateTime->setTimezone(new DateTimeZone(Application_Model_Preference::GetTimezone()));

return $dateTime;
}

public static function ConvertToUtcDateTimeString($p_dateString, $p_format="Y-m-d H:i:s"){
if (is_null($p_dateString) || strlen($p_dateString) == 0)
return $p_dateString;
return self::ConvertToUtcDateTime($p_dateString)->format($p_format);
}

/*
* Example input: "00:02:32.746562". Output is a DateInterval object
Expand Down
28 changes: 28 additions & 0 deletions airtime_mvc/application/forms/SmartBlockCriteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,31 @@ private function getLimitOptions()
public function init()
{
}

/*
* converts UTC timestamp citeria into user timezone strings.
*/
private function convertTimestamps(&$criteria)
{
$columns = array("utime", "mtime", "lptime");

foreach ($columns as $column) {

if (isset($criteria[$column])) {

foreach ($criteria[$column] as &$constraint) {

$constraint['value'] =
Application_Common_DateHelper::UTCStringToUserTimezoneString($constraint['value']);

if (isset($constraint['extra'])) {
$constraint['extra'] =
Application_Common_DateHelper::UTCStringToUserTimezoneString($constraint['extra']);
}
}
}
}
}

public function startForm($p_blockId, $p_isValid = false)
{
Expand All @@ -150,6 +175,9 @@ public function startForm($p_blockId, $p_isValid = false)

$bl = new Application_Model_Block($p_blockId);
$storedCrit = $bl->getCriteria();

//need to convert criteria to be displayed in the user's timezone if there's some timestamp type.
self::convertTimestamps($storedCrit["crit"]);

/* $modRoadMap stores the number of same criteria
* Ex: 3 Album titles, and 2 Track titles
Expand Down
39 changes: 21 additions & 18 deletions airtime_mvc/application/models/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -1197,13 +1197,26 @@ public function storeCriteriaIntoDb($p_criteriaData)
$critKeys = array_keys($p_criteriaData['criteria']);
for ($i = 0; $i < count($critKeys); $i++) {
foreach ($p_criteriaData['criteria'][$critKeys[$i]] as $d) {

$field = $d['sp_criteria_field'];
$value = $d['sp_criteria_value'];

if ($field == 'utime' || $field == 'mtime' || $field == 'lptime') {
$value = Application_Common_DateHelper::UserTimezoneStringToUTCString($value);
}

$qry = new CcBlockcriteria();
$qry->setDbCriteria($d['sp_criteria_field'])
$qry->setDbCriteria($field)
->setDbModifier($d['sp_criteria_modifier'])
->setDbValue($d['sp_criteria_value'])
->setDbValue($value)
->setDbBlockId($this->id);

if (isset($d['sp_criteria_extra'])) {

if ($field == 'utime' || $field == 'mtime' || $field == 'lptime') {
$d['sp_criteria_extra'] = Application_Common_DateHelper::UserTimezoneStringToUTCString($d['sp_criteria_extra']);
}

$qry->setDbExtra($d['sp_criteria_extra']);
}
$qry->save();
Expand Down Expand Up @@ -1413,28 +1426,18 @@ public function getListofFilesMeetCriteria()
foreach ($storedCrit["crit"] as $crit) {
$i = 0;
foreach ($crit as $criteria) {
//$spCriteriaPhpName = self::$criteria2PeerMap[$criteria['criteria']];
$spCriteria = $criteria['criteria'];
$spCriteriaModifier = $criteria['modifier'];

$column = CcFilesPeer::getTableMap()->getColumnByPhpName(self::$criteria2PeerMap[$spCriteria]);
// if the column is timestamp, convert it into UTC
if ($column->getType() == PropelColumnTypes::TIMESTAMP) {
$spCriteriaValue = Application_Common_DateHelper::ConvertToUtcDateTimeString($criteria['value']);
/* Check if only a date was supplied and trim
* the time after it is converted to UTC time
*/
if (strlen($criteria['value']) <= 10) {
//extract date only from timestamp in db
$spCriteria = 'date('.$spCriteria.')';
$spCriteriaValue = substr($spCriteriaValue, 0, 10);
}

//data should already be in UTC, do we have to do anything special here anymore?
if ($column->getType() == PropelColumnTypes::TIMESTAMP) {

$spCriteriaValue = $criteria['value'];

if (isset($criteria['extra'])) {
$spCriteriaExtra = Application_Common_DateHelper::ConvertToUtcDateTimeString($criteria['extra']);
if (strlen($criteria['extra']) <= 10) {
$spCriteriaExtra = substr($spCriteriaExtra, 0, 10);
}
$spCriteriaExtra = $criteria['extra'];
}
} elseif ($spCriteria == "bit_rate" || $spCriteria == 'sample_rate') {
// multiply 1000 because we store only number value
Expand Down
3 changes: 1 addition & 2 deletions airtime_mvc/application/models/Datatables.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ private static function buildWhereClauseForAdvancedSearch($dbname2searchTerm)
}
}
}

Logging::info($where);

return $where;
}
/*
Expand Down
2 changes: 1 addition & 1 deletion airtime_mvc/public/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ input.input_text.sp_input_text{
}

input.input_text.sp_extra_input_text{
width: 60px !important;
width: 139px !important;
}

.sp_text_font{
Expand Down

0 comments on commit e6d28b2

Please sign in to comment.