Skip to content

Commit 5ce71ce

Browse files
author
Skrol29
committed
TBS 3.12.1
1 parent 9daeb5f commit 5ce71ce

File tree

4 files changed

+113
-35
lines changed

4 files changed

+113
-35
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

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

7+
## [3.12.1] - 2020-10-24
8+
9+
### Fixed
10+
11+
- Parameter 'frm' displays '1970-01-01' for a date over '2038-01-19 03:14:07' on a PHP 32-bits plateform.
12+
713
## [3.12.0] - 2020-10-12
814

915
### Added

tbs_class.php

Lines changed: 84 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*
44
* TinyButStrong - Template Engine for Pro and Beginners
55
*
6-
* @version 3.12.0 for PHP 5, 7, 8
7-
* @date 2020-10-12
6+
* @version 3.12.1 for PHP 5, 7, 8
7+
* @date 2020-10-21
88
* @link http://www.tinybutstrong.com Web site
99
* @author http://www.tinybutstrong.com/onlyyou.html
1010
* @license http://opensource.org/licenses/LGPL-3.0 LGPL-3.0
@@ -655,7 +655,7 @@ class clsTinyButStrong {
655655
public $ExtendedMethods = array();
656656
public $ErrCount = 0;
657657
// Undocumented (can change at any version)
658-
public $Version = '3.12.0';
658+
public $Version = '3.12.1';
659659
public $Charset = '';
660660
public $TurboBlock = true;
661661
public $VarPrefix = '';
@@ -3731,21 +3731,27 @@ function meth_PlugIn_SetEvent($PlugInId, $Event, $NewRef='') {
37313731

37323732
}
37333733

3734+
/**
3735+
* Convert any value to a string without specific formating.
3736+
*/
37343737
static function meth_Misc_ToStr($Value) {
37353738
if (is_string($Value)) {
37363739
return $Value;
37373740
} elseif(is_object($Value)) {
37383741
if (method_exists($Value,'__toString')) {
37393742
return $Value->__toString();
37403743
} elseif (is_a($Value, 'DateTime')) {
3744+
// ISO date-time format
37413745
return $Value->format('c');
37423746
}
37433747
}
37443748
return @(string)$Value; // (string) is faster than strval() and settype()
37453749
}
37463750

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

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

3760-
// Select the format
3766+
// Found the format according to the value (positive|negative|zero|null)
3767+
37613768
if (is_numeric($Value)) {
3769+
// Numeric:
37623770
if (is_string($Value)) $Value = 0.0 + $Value;
37633771
if ($Value>0) {
37643772
$FrmStr = &$Frm[0];
37653773
} elseif ($Value<0) {
37663774
$FrmStr = &$Frm[1];
37673775
if ($Frm['abs']) $Value = abs($Value);
3768-
} else { // zero
3776+
} else {
3777+
// zero
37693778
$FrmStr = &$Frm[2];
37703779
$Minus = '';
37713780
}
37723781
$CheckNumeric = false;
37733782
} else {
3783+
// date|
37743784
$Value = $this->meth_Misc_ToStr($Value);
37753785
if ($Value==='') {
3776-
return $Frm[3]; // Null value
3786+
// Null value
3787+
return $Frm[3];
37773788
} else {
3789+
// Date conversion
37783790
$t = strtotime($Value); // We look if it's a date
3779-
if (($t===-1) || ($t===false)) { // Date not recognized
3791+
if (($t===-1) || ($t===false)) {
3792+
// Date not recognized
37803793
return $Frm[1];
3781-
} elseif ($t===943916400) { // Date to zero
3794+
} elseif ($t===943916400) {
3795+
// Date to zero in some softwares
37823796
return $Frm[2];
3783-
} else { // It's a date
3797+
} else {
3798+
// It's a date
37843799
$Value = $t;
37853800
$FrmStr = &$Frm[0];
37863801
}
@@ -3794,7 +3809,7 @@ function meth_Misc_Format(&$Value,&$PrmLst) {
37943809
}
37953810

37963811
switch ($Frm['type']) {
3797-
case 'num' :
3812+
case 'num':
37983813
// NUMERIC
37993814
if ($CheckNumeric) {
38003815
if (is_numeric($Value)) {
@@ -3810,29 +3825,9 @@ function meth_Misc_Format(&$Value,&$PrmLst) {
38103825
$Value = substr_replace($Frm['Str'],$Value,$Frm['Pos'],$Frm['Len']);
38113826
return $Value;
38123827
break;
3813-
case 'date' :
3828+
case 'date':
38143829
// DATE
3815-
if (is_object($Value)) {
3816-
$Value = $this->meth_Misc_ToStr($Value);
3817-
}
3818-
if (is_string($Value)) {
3819-
if ($Value==='') return '';
3820-
$x = strtotime($Value);
3821-
if (($x===-1) || ($x===false)) {
3822-
if (!is_numeric($Value)) $Value = 0;
3823-
} else {
3824-
$Value = &$x;
3825-
}
3826-
} else {
3827-
if (!is_numeric($Value)) return $this->meth_Misc_ToStr($Value);
3828-
}
3829-
if ($Frm['loc'] || isset($PrmLst['locale'])) {
3830-
$x = strftime($Frm['str_loc'],$Value);
3831-
$this->meth_Conv_Str($x,false); // may have accent
3832-
return $x;
3833-
} else {
3834-
return date($Frm['str_us'],$Value);
3835-
}
3830+
return $this->meth_Misc_DateFormat($Value, $Frm);
38363831
break;
38373832
default:
38383833
return $Frm['string'];
@@ -3841,6 +3836,62 @@ function meth_Misc_Format(&$Value,&$PrmLst) {
38413836

38423837
}
38433838

3839+
function meth_Misc_DateFormat(&$Value, $Frm) {
3840+
3841+
if (is_object($Value)) {
3842+
$Value = $this->meth_Misc_ToStr($Value);
3843+
}
3844+
3845+
if ($Value==='') return '';
3846+
3847+
// Note : DateTime object is supported since PHP 5.2
3848+
// So we could simplify this function using only DateTime instead of timestamp.
3849+
3850+
// Now we try to get the timestamp
3851+
if (is_string($Value)) {
3852+
// Any string value is assumed to be a formated date.
3853+
// If you whant a string value to be a considered to a a time stamp, then use prefixe '@' accordding to the
3854+
$x = strtotime($Value);
3855+
// In case of error return false (return -1 for PHP < 5.1.0)
3856+
if (($x===false) || ($x===-1)) {
3857+
if (!is_numeric($Value)) {
3858+
// At this point the value is not recognized as a date
3859+
// Special fix for PHP 32-bit and date > '2038-01-19 03:14:07' => strtotime() failes
3860+
if (PHP_INT_SIZE === 4) { // 32-bit
3861+
try {
3862+
$date = new DateTime($Value);
3863+
return $date->format($Frm['str_us']);
3864+
// 'locale' cannot be supported in this case because strftime() has to equilavent with DateTime
3865+
} catch (Exception $e) {
3866+
// We take an arbitrary value in order to avoid formating error
3867+
$Value = 0; // '1970-01-01'
3868+
// echo $e->getMessage();
3869+
}
3870+
} else {
3871+
// We take an arbirtary value in order to avoid formating error
3872+
$Value = 0; // '1970-01-01'
3873+
}
3874+
}
3875+
} else {
3876+
$Value = &$x;
3877+
}
3878+
} else {
3879+
if (!is_numeric($Value)) {
3880+
// It’s not a timestamp, thus we return the non formated value
3881+
return $this->meth_Misc_ToStr($Value);
3882+
}
3883+
}
3884+
3885+
if ($Frm['loc'] || isset($PrmLst['locale'])) {
3886+
$x = strftime($Frm['str_loc'],$Value);
3887+
$this->meth_Conv_Str($x,false); // may have accent
3888+
return $x;
3889+
} else {
3890+
return date($Frm['str_us'],$Value);
3891+
}
3892+
3893+
}
3894+
38443895
/**
38453896
* Apply combo parameters.
38463897
* @param array $PrmLst The existing list of combo

testunit/index.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@
3838
// launch tests
3939

4040
$tbs = new clsTinyButStrong();
41-
$test = new GroupTest('TinyButStrong v'.$tbs->Version.' (with PHP '.PHP_VERSION.')');
41+
42+
$bit = (PHP_INT_SIZE <= 4) ? '32' : '64' ;
43+
$title = "TinyButStrong v" . $tbs->Version . " (with PHP version " . PHP_VERSION . " , " . $bit . "-bits)";
44+
$test = new GroupTest($title);
45+
4246
$test->addTestCase(new FieldTestCase());
4347
$test->addTestCase(new BlockTestCase());
4448
$test->addTestCase(new BlockGrpTestCase());

testunit/testcase/FrmTestCase.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ function testNumericFormats() {
8686

8787
function testDateFormats() {
8888

89-
$d = mktime(21, 46, 33, 11, 30, 2001);
89+
// Values that are a timestamp
90+
$d = mktime(21, 46, 33, 11, 30, 2001);
9091
$ds = mktime(9, 8, 7, 2, 3, 2001); // sort, i.e. with (day<10) and (month<10) and (hour<10) and (minutes<10)
9192
$d1 = mktime(21, 46, 33, 11, 1, 2001); // first day of month
9293
$d2 = mktime(21, 46, 33, 11, 2, 2001); // second day of month
@@ -142,6 +143,22 @@ function testDateFormats() {
142143
$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
143144
$this->assertEqualMergeFieldStrings("{[a;frm=yyyy-mm-dd hh:nn:ss]}", array('a'=>''), "{}", "test unexpected values: empty string");
144145
$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
146+
147+
// Date over '2038-01-19 03:14:07' on a PHP 32-bits plateform
148+
if (PHP_INT_SIZE === 4) { // 32-bit
149+
if ($this->atLeastTBSVersion('3.12.1')) {
150+
$caption = "format date over 2038 on PHP 32-bits, >= TBS < 3.12.1";
151+
$result = "{2039/10/22}"; // fixed
152+
} else {
153+
$caption = "format date 2038 on PHP 32-bits, TBS < 3.12.1";
154+
$result = "{1970/01/01}"; // bugged
155+
}
156+
} else {
157+
$caption = "format date 2038 on PHP 64-bits";
158+
$result = "{2039/10/22}"; // normal
159+
}
160+
$this->assertEqualMergeFieldStrings("{[a;frm=yyyy/mm/dd]}", array('a'=>'2039-10-22'), $result, $caption);
161+
145162
}
146163

147164
function testConditionalFormats() {

0 commit comments

Comments
 (0)