From a03bcd353f45a3f840eb55480b2403579ba3a25b Mon Sep 17 00:00:00 2001 From: danilo Date: Tue, 27 Feb 2007 22:22:09 +0000 Subject: [PATCH] Updated smarty_core_write_file() and smarty_modifier_date_format() to speed up Windows detection. Emulated more parameters for Windows in smarty_modifier_date_format() and fixed some old ones. Updated the docs to tell what parameters are emulated on Windows. Updated NEWS file. --- NEWS | 6 ++-- .../language-modifier-date-format.xml | 4 ++- libs/internals/core.write_file.php | 6 +--- libs/plugins/modifier.date_format.php | 33 +++++++++++-------- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/NEWS b/NEWS index e43db8ed..df9c9075 100644 --- a/NEWS +++ b/NEWS @@ -3,8 +3,10 @@ than $length (Sylvinus, messju) - fix handling of %I with mysql timestamps in the date_format modifier (danilo, boots) -- update smarty_core_write_file() to better recognize Windows (boots) -- emulate %R in the date_format modifier on Windows (danilo, boots) +- update smarty_core_write_file() and smarty_modifier_date_format() to better + recognize Windows (boots, danilo) +- emulate %h, %n, %r, %R, %t in the date_format modifier on Windows + (danilo, boots) Version 2.6.16 (Dec 1st, 2006) ------------------------------- diff --git a/docs/en/designers/language-modifiers/language-modifier-date-format.xml b/docs/en/designers/language-modifiers/language-modifier-date-format.xml index aabe9ba2..6898b3e3 100644 --- a/docs/en/designers/language-modifiers/language-modifier-date-format.xml +++ b/docs/en/designers/language-modifiers/language-modifier-date-format.xml @@ -79,7 +79,9 @@ You may have more or less conversion specifiers available depending on your system's strftime() function where PHP was compiled. Check your - system's manpage for a full list of valid specifiers. + system's manpage for a full list of valid specifiers. However, a few of + the specifiers are emulated on Windows. These are: %D, %e, %h, %l, %n, + %r, %R, %t, %T. diff --git a/libs/internals/core.write_file.php b/libs/internals/core.write_file.php index 0afa5b54..8a3a3b39 100644 --- a/libs/internals/core.write_file.php +++ b/libs/internals/core.write_file.php @@ -15,10 +15,6 @@ */ function smarty_core_write_file($params, &$smarty) { - static $OS; - if (is_null($OS)) { - $OS = substr(PHP_OS,0,3); - } $_dirname = dirname($params['filename']); if ($params['create_dirs']) { @@ -41,7 +37,7 @@ function smarty_core_write_file($params, &$smarty) fwrite($fd, $params['contents']); fclose($fd); - if ($OS == 'WIN' || !@rename($_tmp_file, $params['filename'])) { + if (DIRECTORY_SEPARATOR == '\\' || !@rename($_tmp_file, $params['filename'])) { // On platforms and filesystems that cannot overwrite with rename() // delete the file before renaming it -- because windows always suffers // this, it is short-circuited to avoid the initial rename() attempt diff --git a/libs/plugins/modifier.date_format.php b/libs/plugins/modifier.date_format.php index 085bbdcd..8cf7d5e1 100644 --- a/libs/plugins/modifier.date_format.php +++ b/libs/plugins/modifier.date_format.php @@ -8,7 +8,7 @@ /** * Include the {@link shared.make_timestamp.php} plugin */ -require_once $smarty->_get_plugin_filepath('shared','make_timestamp'); +require_once $smarty->_get_plugin_filepath('shared', 'make_timestamp'); /** * Smarty date_format modifier plugin * @@ -28,22 +28,29 @@ require_once $smarty->_get_plugin_filepath('shared','make_timestamp'); * @return string|void * @uses smarty_make_timestamp() */ -function smarty_modifier_date_format($string, $format="%b %e, %Y", $default_date=null) +function smarty_modifier_date_format($string, $format = '%b %e, %Y', $default_date = '') { - if (substr(PHP_OS,0,3) == 'WIN') { - $hours = strftime('%I', smarty_make_timestamp($string)); - $short_hours = ( $hours < 10 ) ? substr( $hours, -1) : $hours; - $_win_from = array ('%e', '%T', '%D', '%l', '%R'); - $_win_to = array ('%#d', '%H:%M:%S', '%m/%d/%y', $short_hours, '%H:%M'); - $format = str_replace($_win_from, $_win_to, $format); - } - if($string != '') { - return strftime($format, smarty_make_timestamp($string)); - } elseif (isset($default_date) && $default_date != '') { - return strftime($format, smarty_make_timestamp($default_date)); + if ($string != '') { + $timestamp = smarty_make_timestamp($string); + } elseif ($default_date != '') { + $timestamp = smarty_make_timestamp($default_date); } else { return; } + if (DIRECTORY_SEPARATOR == '\\') { + $_win_from = array('%D', '%h', '%n', '%r', '%R', '%t', '%T'); + $_win_to = array('%m/%d/%y', '%b', "\n", '%I:%M:%S %p', '%H:%M', "\t", '%H:%M:%S'); + if (strpos($format, '%e') !== false) { + $_win_from[] = '%e'; + $_win_to[] = sprintf('%\' 2d', date('j', $timestamp)); + } + if (strpos($format, '%l') !== false) { + $_win_from[] = '%l'; + $_win_to[] = sprintf('%\' 2d', date('h', $timestamp)); + } + $format = str_replace($_win_from, $_win_to, $format); + } + return strftime($format, $timestamp); } /* vim: set expandtab: */