Permalink
Browse files

Issue #42: Wrap up customizable timestamp implementation

  • Loading branch information...
kiwiz committed Oct 31, 2017
1 parent bd4394c commit 7298ae9d9ac51c241037638ec162edeb7289380c
View
@@ -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]);
@@ -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.
View
@@ -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;
View
@@ -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;
}
View
@@ -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) {
@@ -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.
@@ -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']);
}
}
@@ -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()) ?>
@@ -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): ?>
@@ -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): ?>

0 comments on commit 7298ae9

Please sign in to comment.