- Refactor WP DateTime helper
- Add a unit test helper to stub out Wordpress functions
This commit is contained in:
@@ -3,22 +3,22 @@ namespace MailPoet\WP;
|
||||
|
||||
class DateTime {
|
||||
|
||||
const INTERNAL_DATE_FORMAT = 'Y-m-d';
|
||||
const INTERNAL_TIME_FORMAT = 'H:i:s';
|
||||
const INTERNAL_DATE_TIME_FORMAT = 'Y-m-d H:i:s';
|
||||
const DEFAULT_DATE_FORMAT = 'Y-m-d';
|
||||
const DEFAULT_TIME_FORMAT = 'H:i:s';
|
||||
const DEFAULT_DATE_TIME_FORMAT = 'Y-m-d H:i:s';
|
||||
|
||||
function __construct() {
|
||||
}
|
||||
|
||||
function getTimeFormat() {
|
||||
$time_format = get_option('time_format');
|
||||
if (empty($time_format)) $time_format = self::INTERNAL_TIME_FORMAT;
|
||||
if (empty($time_format)) $time_format = self::DEFAULT_TIME_FORMAT;
|
||||
return $time_format;
|
||||
}
|
||||
|
||||
function getDateFormat() {
|
||||
$date_format = get_option('date_format');
|
||||
if (empty($date_format)) $date_format = self::INTERNAL_DATE_FORMAT;
|
||||
if (empty($date_format)) $date_format = self::DEFAULT_DATE_FORMAT;
|
||||
return $date_format;
|
||||
}
|
||||
|
||||
@@ -32,15 +32,15 @@ class DateTime {
|
||||
return $this->getCurrentTime($format);
|
||||
}
|
||||
|
||||
function getTime($time, $format=false) {
|
||||
function formatTime($timestamp, $format=false) {
|
||||
if (empty($format)) $format = $this->getTimeFormat();
|
||||
|
||||
return date($format, $time);
|
||||
return date($format, $timestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a list of time strings within an interval,
|
||||
* formatted and mapped from INTERNAL_TIME_FORMAT to Wordpress time strings.
|
||||
* formatted and mapped from DEFAULT_TIME_FORMAT to Wordpress time strings.
|
||||
*/
|
||||
function getTimeInterval(
|
||||
$start_time='00:00:00',
|
||||
@@ -49,15 +49,15 @@ class DateTime {
|
||||
) {
|
||||
$steps = array();
|
||||
|
||||
$internal_time = $start_time;
|
||||
$timestamp = strtotime($internal_time);
|
||||
$formatted_time = $start_time;
|
||||
$timestamp = strtotime($formatted_time);
|
||||
|
||||
for ($step = 0; $step < $total_steps; $step += 1) {
|
||||
$wordpress_time = $this->getTime($timestamp);
|
||||
$steps[$internal_time] = $wordpress_time;
|
||||
$formatted_time = $this->formatTime($timestamp, self::DEFAULT_TIME_FORMAT);
|
||||
$label_time = $this->formatTime($timestamp);
|
||||
$steps[$formatted_time] = $label_time;
|
||||
|
||||
$timestamp = strtotime($time_step, $timestamp);
|
||||
$internal_time = $this->getTime($timestamp, self::INTERNAL_TIME_FORMAT);
|
||||
}
|
||||
|
||||
return $steps;
|
||||
|
Reference in New Issue
Block a user