Skip to content

Commit

Permalink
TBS 3.12.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Skrol29 committed Oct 24, 2020
1 parent 9daeb5f commit 5ce71ce
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 35 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [3.12.1] - 2020-10-24

### Fixed

- Parameter 'frm' displays '1970-01-01' for a date over '2038-01-19 03:14:07' on a PHP 32-bits plateform.

## [3.12.0] - 2020-10-12

### Added
Expand Down
117 changes: 84 additions & 33 deletions tbs_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*
* TinyButStrong - Template Engine for Pro and Beginners
*
* @version 3.12.0 for PHP 5, 7, 8
* @date 2020-10-12
* @version 3.12.1 for PHP 5, 7, 8
* @date 2020-10-21
* @link http://www.tinybutstrong.com Web site
* @author http://www.tinybutstrong.com/onlyyou.html
* @license http://opensource.org/licenses/LGPL-3.0 LGPL-3.0
Expand Down Expand Up @@ -655,7 +655,7 @@ class clsTinyButStrong {
public $ExtendedMethods = array();
public $ErrCount = 0;
// Undocumented (can change at any version)
public $Version = '3.12.0';
public $Version = '3.12.1';
public $Charset = '';
public $TurboBlock = true;
public $VarPrefix = '';
Expand Down Expand Up @@ -3731,21 +3731,27 @@ function meth_PlugIn_SetEvent($PlugInId, $Event, $NewRef='') {

}

/**
* Convert any value to a string without specific formating.
*/
static function meth_Misc_ToStr($Value) {
if (is_string($Value)) {
return $Value;
} elseif(is_object($Value)) {
if (method_exists($Value,'__toString')) {
return $Value->__toString();
} elseif (is_a($Value, 'DateTime')) {
// ISO date-time format
return $Value->format('c');
}
}
return @(string)$Value; // (string) is faster than strval() and settype()
}

/**
* Return the formated representation of a Date/Time or numeric variable using a 'VB like' format syntax instead of the PHP syntax.
*/
function meth_Misc_Format(&$Value,&$PrmLst) {
// This function return the formated representation of a Date/Time or numeric variable using a 'VB like' format syntax instead of the PHP syntax.

$FrmStr = $PrmLst['frm'];
$CheckNumeric = true;
Expand All @@ -3757,30 +3763,39 @@ function meth_Misc_Format(&$Value,&$PrmLst) {
// Manage Multi format strings
if ($Frm['type']=='multi') {

// Select the format
// Found the format according to the value (positive|negative|zero|null)

if (is_numeric($Value)) {
// Numeric:
if (is_string($Value)) $Value = 0.0 + $Value;
if ($Value>0) {
$FrmStr = &$Frm[0];
} elseif ($Value<0) {
$FrmStr = &$Frm[1];
if ($Frm['abs']) $Value = abs($Value);
} else { // zero
} else {
// zero
$FrmStr = &$Frm[2];
$Minus = '';
}
$CheckNumeric = false;
} else {
// date|
$Value = $this->meth_Misc_ToStr($Value);
if ($Value==='') {
return $Frm[3]; // Null value
// Null value
return $Frm[3];
} else {
// Date conversion
$t = strtotime($Value); // We look if it's a date
if (($t===-1) || ($t===false)) { // Date not recognized
if (($t===-1) || ($t===false)) {
// Date not recognized
return $Frm[1];
} elseif ($t===943916400) { // Date to zero
} elseif ($t===943916400) {
// Date to zero in some softwares
return $Frm[2];
} else { // It's a date
} else {
// It's a date
$Value = $t;
$FrmStr = &$Frm[0];
}
Expand All @@ -3794,7 +3809,7 @@ function meth_Misc_Format(&$Value,&$PrmLst) {
}

switch ($Frm['type']) {
case 'num' :
case 'num':
// NUMERIC
if ($CheckNumeric) {
if (is_numeric($Value)) {
Expand All @@ -3810,29 +3825,9 @@ function meth_Misc_Format(&$Value,&$PrmLst) {
$Value = substr_replace($Frm['Str'],$Value,$Frm['Pos'],$Frm['Len']);
return $Value;
break;
case 'date' :
case 'date':
// DATE
if (is_object($Value)) {
$Value = $this->meth_Misc_ToStr($Value);
}
if (is_string($Value)) {
if ($Value==='') return '';
$x = strtotime($Value);
if (($x===-1) || ($x===false)) {
if (!is_numeric($Value)) $Value = 0;
} else {
$Value = &$x;
}
} else {
if (!is_numeric($Value)) return $this->meth_Misc_ToStr($Value);
}
if ($Frm['loc'] || isset($PrmLst['locale'])) {
$x = strftime($Frm['str_loc'],$Value);
$this->meth_Conv_Str($x,false); // may have accent
return $x;
} else {
return date($Frm['str_us'],$Value);
}
return $this->meth_Misc_DateFormat($Value, $Frm);
break;
default:
return $Frm['string'];
Expand All @@ -3841,6 +3836,62 @@ function meth_Misc_Format(&$Value,&$PrmLst) {

}

function meth_Misc_DateFormat(&$Value, $Frm) {

if (is_object($Value)) {
$Value = $this->meth_Misc_ToStr($Value);
}

if ($Value==='') return '';

// Note : DateTime object is supported since PHP 5.2
// So we could simplify this function using only DateTime instead of timestamp.

// Now we try to get the timestamp
if (is_string($Value)) {
// Any string value is assumed to be a formated date.
// If you whant a string value to be a considered to a a time stamp, then use prefixe '@' accordding to the
$x = strtotime($Value);
// In case of error return false (return -1 for PHP < 5.1.0)
if (($x===false) || ($x===-1)) {
if (!is_numeric($Value)) {
// At this point the value is not recognized as a date
// Special fix for PHP 32-bit and date > '2038-01-19 03:14:07' => strtotime() failes
if (PHP_INT_SIZE === 4) { // 32-bit
try {
$date = new DateTime($Value);
return $date->format($Frm['str_us']);
// 'locale' cannot be supported in this case because strftime() has to equilavent with DateTime
} catch (Exception $e) {
// We take an arbitrary value in order to avoid formating error
$Value = 0; // '1970-01-01'
// echo $e->getMessage();
}
} else {
// We take an arbirtary value in order to avoid formating error
$Value = 0; // '1970-01-01'
}
}
} else {
$Value = &$x;
}
} else {
if (!is_numeric($Value)) {
// It’s not a timestamp, thus we return the non formated value
return $this->meth_Misc_ToStr($Value);
}
}

if ($Frm['loc'] || isset($PrmLst['locale'])) {
$x = strftime($Frm['str_loc'],$Value);
$this->meth_Conv_Str($x,false); // may have accent
return $x;
} else {
return date($Frm['str_us'],$Value);
}

}

/**
* Apply combo parameters.
* @param array $PrmLst The existing list of combo
Expand Down
6 changes: 5 additions & 1 deletion testunit/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
// launch tests

$tbs = new clsTinyButStrong();
$test = new GroupTest('TinyButStrong v'.$tbs->Version.' (with PHP '.PHP_VERSION.')');

$bit = (PHP_INT_SIZE <= 4) ? '32' : '64' ;
$title = "TinyButStrong v" . $tbs->Version . " (with PHP version " . PHP_VERSION . " , " . $bit . "-bits)";
$test = new GroupTest($title);

$test->addTestCase(new FieldTestCase());
$test->addTestCase(new BlockTestCase());
$test->addTestCase(new BlockGrpTestCase());
Expand Down
19 changes: 18 additions & 1 deletion testunit/testcase/FrmTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ function testNumericFormats() {

function testDateFormats() {

$d = mktime(21, 46, 33, 11, 30, 2001);
// Values that are a timestamp
$d = mktime(21, 46, 33, 11, 30, 2001);
$ds = mktime(9, 8, 7, 2, 3, 2001); // sort, i.e. with (day<10) and (month<10) and (hour<10) and (minutes<10)
$d1 = mktime(21, 46, 33, 11, 1, 2001); // first day of month
$d2 = mktime(21, 46, 33, 11, 2, 2001); // second day of month
Expand Down Expand Up @@ -142,6 +143,22 @@ function testDateFormats() {
$this->assertEqualMergeFieldStrings("{[a;frm=yyyy-mm-dd hh:nn:ss]}", array('a'=>true), "{1}", "test unexpected values: true"); // TBS performs an implicite conversion from true to string
$this->assertEqualMergeFieldStrings("{[a;frm=yyyy-mm-dd hh:nn:ss]}", array('a'=>''), "{}", "test unexpected values: empty string");
$this->assertEqualMergeFieldStrings("{[a;frm=yyyy-mm-dd hh:nn:ss]}", array('a'=>0), "{".date('Y-m-d H:i:s', 0)."}", "test unexpected values: 0"); // 0 is a timsetamp for the start of unix dates

// Date over '2038-01-19 03:14:07' on a PHP 32-bits plateform
if (PHP_INT_SIZE === 4) { // 32-bit
if ($this->atLeastTBSVersion('3.12.1')) {
$caption = "format date over 2038 on PHP 32-bits, >= TBS < 3.12.1";
$result = "{2039/10/22}"; // fixed
} else {
$caption = "format date 2038 on PHP 32-bits, TBS < 3.12.1";
$result = "{1970/01/01}"; // bugged
}
} else {
$caption = "format date 2038 on PHP 64-bits";
$result = "{2039/10/22}"; // normal
}
$this->assertEqualMergeFieldStrings("{[a;frm=yyyy/mm/dd]}", array('a'=>'2039-10-22'), $result, $caption);

}

function testConditionalFormats() {
Expand Down

0 comments on commit 5ce71ce

Please sign in to comment.