Skip to content

Commit

Permalink
Issue #42: Wrap up customizable timestamp implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwiz committed Oct 31, 2017
1 parent bd4394c commit 7298ae9
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 25 deletions.
13 changes: 1 addition & 12 deletions phplib/REST/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function GET(array $get) {
'Defaults' => $this->generateDefaultData(User::getSchema()),
],
'Nonce' => Nonce::get(),
'Timezone' => $this->getTimezone(),
'Timezone' => Util::getDefaultTimezone(),
];
list($ret) = Hook::call('rest.data', [$ret]);

Expand All @@ -112,17 +112,6 @@ private function generateUsers() {
return $users;
}


/**
* Gets Timezone from DB backed config.
* Defaults to 'UTC' if unset.
* @return string timezone for use with moment-timezone.
*/
private function getTimezone() {
$config = new DBConfig;
return Util::validateTimezone($config['timezone']);
}

/**
* Generate type names for enums.
* @param string[] $types The list of enum values.
Expand Down
6 changes: 3 additions & 3 deletions phplib/REST/Searches.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,10 @@ public function stats($data) {
);

$ret = DB::query(implode(' ', $sql), $vals, DB::VAL);
$stats['Last alert'] = is_null($ret) ? 'N/A':gmdate(DATE_RSS, $ret);
$stats['Last alert'] = is_null($ret) ? 'N/A':Util::formatDateTime($ret);

$stats['Last execution'] = $model['last_execution_date'] == 0 ? 'N/A':gmdate(DATE_RSS, $model['last_execution_date']);
$stats['Last successful execution'] = $model['last_success_date'] == 0 ? 'N/A':gmdate(DATE_RSS, $model['last_success_date']);
$stats['Last execution'] = $model['last_execution_date'] == 0 ? 'N/A':Util::formatDateTime($model['last_execution_date']);
$stats['Last successful execution'] = $model['last_success_date'] == 0 ? 'N/A':Util::formatDateTime($model['last_success_date']);

$data['stats'] = $stats;

Expand Down
3 changes: 1 addition & 2 deletions phplib/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ public function randomizeAPIKey() {
public function getTimezone() {
$timezone = Util::validateTimezone($this->obj['timezone'], null);
if(is_null($timezone)) {
$config = new DBConfig;
$timezone = Util::validateTimezone($config['timezone']);
$timezone = Util::getDefaultTimezone();
}
return $timezone;
}
Expand Down
66 changes: 64 additions & 2 deletions phplib/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ public static function getHost() {

/**
* Convert dates to unix timestamps (in milliseconds).
* @param string|null Format.
* @param int[]|string[] Dates.
* @param string|null $format Format.
* @param int[]|string[] $dates Dates.
* @return int[] Timestamps.
*/
public static function parseDates($format, array $dates) {
Expand Down Expand Up @@ -185,6 +185,45 @@ public static function parseDates($format, array $dates) {
return $ret;
}

/**
* Format a date in a consistent format.
* @param int $tz Timestamp.
* @return string Date string.
*/
public static function formatDate($tz) {
$timezone = date_default_timezone_get();
date_default_timezone_set(self::getTimezone());
$ret = strftime('%G-%m-%d', $tz);
date_default_timezone_set($timezone);
return $ret;
}

/**
* Format a time in a consistent format.
* @param int $tz Timestamp.
* @return string Time string.
*/
public static function formatTime($tz) {
$timezone = date_default_timezone_get();
date_default_timezone_set(self::getTimezone());
$ret = strftime('%T%z', $tz);
date_default_timezone_set($timezone);
return $ret;
}

/**
* Format a datetime in a consistent format.
* @param int $tz Timestamp.
* @return string DateTime string.
*/
public static function formatDateTime($tz) {
$timezone = date_default_timezone_get();
date_default_timezone_set(self::getTimezone());
$ret = strftime('%G-%m-%d %T%z', $tz);
date_default_timezone_set($timezone);
return $ret;
}

/**
* Validate the timezone given. If invalid, default to UTC.
* @param string Timezone string.
Expand All @@ -193,4 +232,27 @@ public static function parseDates($format, array $dates) {
public static function validateTimezone($timezone, $default='UTC') {
return in_array($timezone, timezone_identifiers_list()) ? $timezone:$default;
}

/**
* Gets Timezone from User or DB backed config.
* Defaults to 'UTC' if unset.
* @return string Timezone string.
*/
public static function getTimezone() {
$user = Auth::getUser();
if($user !== null) {
return $user->getTimezone();
}
return self::getDefaultTimezone();
}

/**
* Get timezone from DB backed config.
* Defaults to 'UTC' if unset.
* @return string Timezone string.
*/
public static function getDefaultTimezone() {
$config = new DBConfig;
return self::validateTimezone($config['timezone']);
}
}
4 changes: 2 additions & 2 deletions templates/modules/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
<a style="<?= $button_style ?>" href="<?= $base_url ?>/alert/<?= $action['alert_id'] ?>">View</a>
</td>
<td style="<?= $cell_style ?>">
<span style="white-space: nowrap;"><?= strftime('%G-%m-%d', $alert['alert_date']) ?></span>
<span style="white-space: nowrap;"><?= strftime('%T', $alert['alert_date']) ?></span>
<span style="white-space: nowrap;"><?= Util::formatDate($alert['alert_date']) ?></span>
<span style="white-space: nowrap;"><?= Util::formatTime($alert['alert_date']) ?></span>
</td>
<td style="<?= $cell_style ?>; white-space: nowrap">
<?= Util::escape($action->getDescription()) ?>
Expand Down
4 changes: 2 additions & 2 deletions templates/modules/alert_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
<tr>
<th style="<?= $h_cell_style ?>">Date</th>
<td style="<?= $cell_style ?>">
<span style="white-space: nowrap;"><?= strftime('%G-%m-%d', $alert['alert_date']) ?></span>
<span style="white-space: nowrap;"><?= strftime('%T', $alert['alert_date']) ?></span>
<span style="white-space: nowrap;"><?= Util::formatDate($alert['alert_date']) ?></span>
<span style="white-space: nowrap;"><?= Util::formatTime($alert['alert_date']) ?></span>
</td>
</tr>
<?php foreach($alertkeys as $alertkey): ?>
Expand Down
4 changes: 2 additions & 2 deletions templates/modules/alert_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
<?php endif ?>
</td>
<td style="<?= $cell_style ?> width: 1px;">
<span style="white-space: nowrap;"><?= strftime('%G-%m-%d', $alert['alert_date']) ?></span>
<span style="white-space: nowrap;"><?= strftime('%T', $alert['alert_date']) ?></span>
<span style="white-space: nowrap;"><?= Util::formatDate($alert['alert_date']) ?></span>
<span style="white-space: nowrap;"><?= Util::formatTime($alert['alert_date']) ?></span>
</td>
<?php endif ?>
<?php foreach($alertkeys as $alertkey): ?>
Expand Down

0 comments on commit 7298ae9

Please sign in to comment.