Move date parse/format helpers in dedicated class in EesyPHP namespace
This commit is contained in:
parent
1fff8f0af4
commit
d30abeb008
5 changed files with 66 additions and 63 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use EesyPHP\Check;
|
use EesyPHP\Check;
|
||||||
use EesyPHP\Cli;
|
use EesyPHP\Cli;
|
||||||
|
use EesyPHP\Date;
|
||||||
use EesyPHP\I18n;
|
use EesyPHP\I18n;
|
||||||
use EesyPHP\Log;
|
use EesyPHP\Log;
|
||||||
|
|
||||||
|
@ -22,7 +23,7 @@ function print_item_info($item) {
|
||||||
printf(_("Item #%s:\n"), $item['id']);
|
printf(_("Item #%s:\n"), $item['id']);
|
||||||
printf("\t"._("ID: %s")."\n", $item['id']);
|
printf("\t"._("ID: %s")."\n", $item['id']);
|
||||||
printf("\t"._("Name: '%s'")."\n", $item['name']);
|
printf("\t"._("Name: '%s'")."\n", $item['name']);
|
||||||
printf("\t"._("Date: %s")."\n", format_time($item['date']));
|
printf("\t"._("Date: %s")."\n", Date :: format($item['date']));
|
||||||
printf(
|
printf(
|
||||||
"\t"._("Description: %s")."\n",
|
"\t"._("Description: %s")."\n",
|
||||||
($item['description']?"'".$item['description']."'":_("Not set"))
|
($item['description']?"'".$item['description']."'":_("Not set"))
|
||||||
|
@ -99,7 +100,7 @@ function cli_list($command_args) {
|
||||||
array(
|
array(
|
||||||
$info['id'],
|
$info['id'],
|
||||||
$info['name'],
|
$info['name'],
|
||||||
format_time($info['date']),
|
Date :: format($info['date']),
|
||||||
$info['status'],
|
$info['status'],
|
||||||
($info['description']?$info['description']:''),
|
($info['description']?$info['description']:''),
|
||||||
)
|
)
|
||||||
|
@ -240,7 +241,7 @@ function cli_cron($command_args) {
|
||||||
Log :: debug("cli_cron(): item max age = $item_max_age day(s)");
|
Log :: debug("cli_cron(): item max age = $item_max_age day(s)");
|
||||||
|
|
||||||
$limit = time() - ($item_max_age * 86400);
|
$limit = time() - ($item_max_age * 86400);
|
||||||
Log :: debug("Handle items expiration with creation date limit ".format_time($limit).".");
|
Log :: debug("Handle items expiration with creation date limit ".Date :: format($limit).".");
|
||||||
|
|
||||||
$items = search_items(array('all' => true));
|
$items = search_items(array('all' => true));
|
||||||
$error = false;
|
$error = false;
|
||||||
|
@ -248,24 +249,24 @@ function cli_cron($command_args) {
|
||||||
if ($item['date'] < $limit) {
|
if ($item['date'] < $limit) {
|
||||||
if ($just_try) {
|
if ($just_try) {
|
||||||
Log :: debug('Just-try mode: do not really delete item #%s (%s, creation date: %s)',
|
Log :: debug('Just-try mode: do not really delete item #%s (%s, creation date: %s)',
|
||||||
$item['id'], $item['name'], format_time($item['date'])
|
$item['id'], $item['name'], Date :: format($item['date'])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
else if (delete_item($item['id'])) {
|
else if (delete_item($item['id'])) {
|
||||||
Log :: info('Item #%s (%s) deleted (creation date: %s)',
|
Log :: info('Item #%s (%s) deleted (creation date: %s)',
|
||||||
$item['id'], $item['name'], format_time($item['date'])
|
$item['id'], $item['name'], Date :: format($item['date'])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Log :: error('Fail to delete item "%s" (%s, creation date: %s)',
|
Log :: error('Fail to delete item "%s" (%s, creation date: %s)',
|
||||||
$item['id'], $item['name'], format_time($item['date'])
|
$item['id'], $item['name'], Date :: format($item['date'])
|
||||||
);
|
);
|
||||||
$error = true;
|
$error = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Log :: debug('Item "%s" (%s) still valid (creation date: %s)',
|
Log :: debug('Item "%s" (%s) still valid (creation date: %s)',
|
||||||
$item['id'], $item['name'], format_time($item['date'])
|
$item['id'], $item['name'], Date :: format($item['date'])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,33 +88,6 @@ function handle_item_post_data(&$info, $enabled_fields=null, $required_fields=nu
|
||||||
/*
|
/*
|
||||||
* Parser/formater values helpers
|
* Parser/formater values helpers
|
||||||
*/
|
*/
|
||||||
$_date_format = "%d/%m/%Y";
|
|
||||||
$_date_time_format = "%d/%m/%Y %H:%M:%S";
|
|
||||||
function format_time($time, $with_time=true) {
|
|
||||||
global $_date_format, $_date_time_format;
|
|
||||||
if ($with_time)
|
|
||||||
return strftime($_date_time_format, $time);
|
|
||||||
return strftime($_date_format, $time);
|
|
||||||
}
|
|
||||||
|
|
||||||
function parse_date($date, $with_time=true) {
|
|
||||||
global $_date_format, $_date_time_format;
|
|
||||||
if ($with_time)
|
|
||||||
$ptime = strptime($date, $_date_time_format);
|
|
||||||
else
|
|
||||||
$ptime = strptime($date, $_date_format);
|
|
||||||
if(is_array($ptime)) {
|
|
||||||
return mktime(
|
|
||||||
$ptime['tm_hour'],
|
|
||||||
$ptime['tm_min'],
|
|
||||||
$ptime['tm_sec'],
|
|
||||||
$ptime['tm_mon']+1,
|
|
||||||
$ptime['tm_mday'],
|
|
||||||
$ptime['tm_year']+1900
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function format_size($size, $digit=False) {
|
function format_size($size, $digit=False) {
|
||||||
if (!$digit && $digit!==0) $digit=2;
|
if (!$digit && $digit!==0) $digit=2;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use EesyPHP\Date;
|
||||||
use EesyPHP\Hook;
|
use EesyPHP\Hook;
|
||||||
use EesyPHP\Log;
|
use EesyPHP\Log;
|
||||||
use EesyPHP\Tpl;
|
use EesyPHP\Tpl;
|
||||||
|
@ -74,7 +75,7 @@ function smarty_item_status($params) {
|
||||||
Tpl :: register_function('item_status','smarty_item_status');
|
Tpl :: register_function('item_status','smarty_item_status');
|
||||||
|
|
||||||
function smarty_format_time($params) {
|
function smarty_format_time($params) {
|
||||||
echo format_time($params['time'], (!isset($params['with_time']) || (bool)$params['with_time']));
|
echo Date :: format($params['time'], (!isset($params['with_time']) || (bool)$params['with_time']));
|
||||||
}
|
}
|
||||||
Tpl :: register_function('format_time','smarty_format_time');
|
Tpl :: register_function('format_time','smarty_format_time');
|
||||||
|
|
||||||
|
|
56
src/Date.php
Normal file
56
src/Date.php
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace EesyPHP;
|
||||||
|
|
||||||
|
class Date {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The date format
|
||||||
|
* @see strftime()
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public static string $date_format = "%d/%m/%Y";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The datetime format
|
||||||
|
* @see strftime()
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public static string $date_time_format = "%d/%m/%Y %H:%M:%S";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format a timestamp as date/datetime string
|
||||||
|
* @param int $time The timestamp to format
|
||||||
|
* @param bool $with_time If true, include time in formated string
|
||||||
|
* (optional, default: true)
|
||||||
|
*/
|
||||||
|
public static function format($time, $with_time=true) {
|
||||||
|
if ($with_time)
|
||||||
|
return strftime(self :: $date_time_format, $time);
|
||||||
|
return strftime(self :: $date_format, $time);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse a date/datetime string as timestamp
|
||||||
|
* @param string $date The date string to parse
|
||||||
|
* @param bool $with_time If true, consider the date string included time
|
||||||
|
* (optional, default: true)
|
||||||
|
*/
|
||||||
|
public static function parse($date, $with_time=true) {
|
||||||
|
if ($with_time)
|
||||||
|
$ptime = strptime($date, self :: $date_time_format);
|
||||||
|
else
|
||||||
|
$ptime = strptime($date, self :: $date_format);
|
||||||
|
if(is_array($ptime)) {
|
||||||
|
return mktime(
|
||||||
|
$ptime['tm_hour'],
|
||||||
|
$ptime['tm_min'],
|
||||||
|
$ptime['tm_sec'],
|
||||||
|
$ptime['tm_mon']+1,
|
||||||
|
$ptime['tm_mday'],
|
||||||
|
$ptime['tm_year']+1900
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,34 +7,6 @@ use EesyPHP\Log;
|
||||||
/*
|
/*
|
||||||
* Parser/formater values helpers
|
* Parser/formater values helpers
|
||||||
*/
|
*/
|
||||||
$_date_format = "%d/%m/%Y";
|
|
||||||
$_date_time_format = "%d/%m/%Y %H:%M:%S";
|
|
||||||
function format_time($time, $with_time=true) {
|
|
||||||
global $_date_format, $_date_time_format;
|
|
||||||
if ($with_time)
|
|
||||||
return strftime($_date_time_format, $time);
|
|
||||||
return strftime($_date_format, $time);
|
|
||||||
}
|
|
||||||
|
|
||||||
function parse_date($date, $with_time=true) {
|
|
||||||
global $_date_format, $_date_time_format;
|
|
||||||
if ($with_time)
|
|
||||||
$ptime = strptime($date, $_date_time_format);
|
|
||||||
else
|
|
||||||
$ptime = strptime($date, $_date_format);
|
|
||||||
if(is_array($ptime)) {
|
|
||||||
return mktime(
|
|
||||||
$ptime['tm_hour'],
|
|
||||||
$ptime['tm_min'],
|
|
||||||
$ptime['tm_sec'],
|
|
||||||
$ptime['tm_mon']+1,
|
|
||||||
$ptime['tm_mday'],
|
|
||||||
$ptime['tm_year']+1900
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function format_size($size, $digit=False) {
|
function format_size($size, $digit=False) {
|
||||||
if (!$digit && $digit!==0) $digit=2;
|
if (!$digit && $digit!==0) $digit=2;
|
||||||
if ($size>=1099511627776)
|
if ($size>=1099511627776)
|
||||||
|
|
Loading…
Reference in a new issue