From d30abeb008014ae7959f739a3d9780c104289007 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Tue, 31 Jan 2023 00:56:44 +0100 Subject: [PATCH] Move date parse/format helpers in dedicated class in EesyPHP namespace --- includes/cli.php | 15 +++++------ includes/functions.php | 27 -------------------- includes/templates.php | 3 ++- src/Date.php | 56 ++++++++++++++++++++++++++++++++++++++++++ src/functions.php | 28 --------------------- 5 files changed, 66 insertions(+), 63 deletions(-) create mode 100644 src/Date.php diff --git a/includes/cli.php b/includes/cli.php index a894777..ba1bfa9 100644 --- a/includes/cli.php +++ b/includes/cli.php @@ -2,6 +2,7 @@ use EesyPHP\Check; use EesyPHP\Cli; +use EesyPHP\Date; use EesyPHP\I18n; use EesyPHP\Log; @@ -22,7 +23,7 @@ function print_item_info($item) { printf(_("Item #%s:\n"), $item['id']); printf("\t"._("ID: %s")."\n", $item['id']); 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( "\t"._("Description: %s")."\n", ($item['description']?"'".$item['description']."'":_("Not set")) @@ -99,7 +100,7 @@ function cli_list($command_args) { array( $info['id'], $info['name'], - format_time($info['date']), + Date :: format($info['date']), $info['status'], ($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)"); $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)); $error = false; @@ -248,24 +249,24 @@ function cli_cron($command_args) { if ($item['date'] < $limit) { if ($just_try) { 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'])) { 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 { 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; } } else { 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']) ); } } diff --git a/includes/functions.php b/includes/functions.php index fbfc42a..9bbf85b 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -88,33 +88,6 @@ function handle_item_post_data(&$info, $enabled_fields=null, $required_fields=nu /* * 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) { if (!$digit && $digit!==0) $digit=2; diff --git a/includes/templates.php b/includes/templates.php index bcacba7..766658a 100644 --- a/includes/templates.php +++ b/includes/templates.php @@ -1,5 +1,6 @@ =1099511627776)