2013-01-18 15:54:40 +01:00
< ? php
/* Copyright ( C ) 2013 Laurent Destailleur < eldy @ users . sourceforge . net >
2023-05-07 14:31:35 +02:00
* Copyright ( C ) 2023 Alexandre Janniaux < alexandre . janniaux @ gmail . com >
2024-11-14 00:16:43 +01:00
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
2013-01-18 15:54:40 +01:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
* or see https :// www . gnu . org /
2013-01-18 15:54:40 +01:00
*/
/**
* \file test / phpunit / DateLibTzFranceTest . php
* \ingroup test
* \brief PHPUnit test
* \remarks To run this script as CLI : phpunit filename . php
*/
global $conf , $user , $langs , $db ;
//define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
2014-05-01 19:57:53 +02:00
//require_once 'PHPUnit/Autoload.php';
2013-01-18 15:54:40 +01:00
require_once dirname ( __FILE__ ) . '/../../htdocs/master.inc.php' ;
require_once dirname ( __FILE__ ) . '/../../htdocs/core/lib/date.lib.php' ;
2024-02-16 23:26:32 +01:00
require_once dirname ( __FILE__ ) . '/CommonClassTest.class.php' ;
2013-01-18 15:54:40 +01:00
2021-01-14 15:09:08 +01:00
if ( empty ( $user -> id )) {
2013-01-18 15:54:40 +01:00
print " Load permissions for admin user nb 1 \n " ;
$user -> fetch ( 1 );
2024-11-14 00:16:43 +01:00
$user -> loadRights ();
2013-01-18 15:54:40 +01:00
}
2024-02-19 15:28:21 +01:00
$conf -> global -> MAIN_DISABLE_ALL_MAILS = 1 ;
2013-01-18 15:54:40 +01:00
/**
* Class for PHPUnit tests
*
* @ backupGlobals disabled
* @ backupStaticAttributes enabled
* @ remarks backupGlobals must be disabled to have db , conf , user and lang not erased .
*/
2024-02-16 23:26:32 +01:00
class DateLibTzFranceTest extends CommonClassTest
2013-01-18 15:54:40 +01:00
{
2021-01-14 15:09:08 +01:00
/**
* setUpBeforeClass
*
* @ return void
*/
2022-09-21 17:55:04 +02:00
public static function setUpBeforeClass () : void
2021-03-02 02:45:39 +01:00
{
2021-01-14 15:09:08 +01:00
global $conf , $user , $langs , $db ;
2021-03-02 02:06:09 +01:00
if ( getServerTimeZoneString () != 'Europe/Paris' && getServerTimeZoneString () != 'Europe/Berlin' ) {
2021-10-31 17:17:21 +01:00
print " \n " . __METHOD__ . " This PHPUnit test can be launched manually only onto a server with PHP timezone set to TZ=Europe/Paris, not a TZ= " . getServerTimeZoneString () . " . \n " ;
print " You can launch the test from command line with: \n " ;
print " php -d date.timezone='Europe/Paris' phpunit DateLibTzFranceTest.php \n " ;
die ( 1 );
2021-03-02 02:06:09 +01:00
}
2021-01-14 15:09:08 +01:00
$db -> begin (); // This is to have all actions inside a transaction even if test launched without suite.
print __METHOD__ . " \n " ;
}
/**
* testDolPrintDateTzFrance
* Same than official testDolPrintDate but with parameter tzoutput that is false = 'tzserver' .
* This test works only onto a server using TZ + 1 Europe / Paris .
*
* You can use http :// www . epochconverter . com / to generate more tests .
*
* @ return void
*/
public function testDolPrintDateTzFrance ()
{
global $conf , $user , $langs , $db ;
2024-02-19 15:28:21 +01:00
$conf = $this -> savconf ;
$user = $this -> savuser ;
$langs = $this -> savlangs ;
$db = $this -> savdb ;
2021-01-14 15:09:08 +01:00
// Check %Y-%m-%d %H:%M:%S format
2024-02-19 15:28:21 +01:00
$result = dol_print_date ( 0 , '%Y-%m-%d %H:%M:%S' , false );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( '1970-01-01 01:00:00' , $result );
// Check %Y-%m-%d %H:%M:%S format
2024-02-19 15:28:21 +01:00
$result = dol_print_date ( 16725225600 , '%Y-%m-%d %H:%M:%S' , false );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( '2500-01-01 01:00:00' , $result );
// Check %Y-%m-%d %H:%M:%S format
2024-02-19 15:28:21 +01:00
$result = dol_print_date ( - 1830384000 , '%Y-%m-%d %H:%M:%S' , false ); // http://www.epochconverter.com/
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( '1912-01-01 01:00:00' , $result ); // dol_print_date use a timezone, not epoch converter as it did not exists this year
// Specific cas during war
// 1940, no timezone
2024-02-19 15:28:21 +01:00
$result = dol_print_date ( - 946771200 , '%Y-%m-%d %H:%M:%S' , false ); // http://www.epochconverter.com/
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( '1940-01-01 01:00:00' , $result ); // dol_print_date use a modern timezone, not epoch converter as it did not exists this year
// 1941, timezone is added by germany to +2 (same for 1942)
2024-02-19 15:28:21 +01:00
$result = dol_print_date ( - 915148800 , '%Y-%m-%d %H:%M:%S' , false ); // http://www.epochconverter.com/
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( '1941-01-01 01:00:00' , $result ); // dol_print_date use a modern timezone, epoch converter use historic timezone
// 1943, timezone is +1
2024-02-19 15:28:21 +01:00
$result = dol_print_date ( - 852076800 , '%Y-%m-%d %H:%M:%S' , false ); // http://www.epochconverter.com/
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( '1943-01-01 01:00:00' , $result );
// test with negative timezone
2024-02-19 15:28:21 +01:00
$result = dol_print_date ( - 1 , '%Y-%m-%d %H:%M:%S' , false ); // http://www.epochconverter.com/
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( '1970-01-01 00:59:59' , $result );
// Check dayhour format for fr_FR
2024-02-19 15:28:21 +01:00
$outputlangs = new Translate ( '' , $conf );
2021-01-14 15:09:08 +01:00
$outputlangs -> setDefaultLang ( 'fr_FR' );
$outputlangs -> load ( " main " );
2024-02-19 15:28:21 +01:00
$result = dol_print_date ( 0 + 24 * 3600 , 'dayhour' , false , $outputlangs );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( '02/01/1970 01:00' , $result );
// Check day format for en_US
2024-02-19 15:28:21 +01:00
$outputlangs = new Translate ( '' , $conf );
2021-01-14 15:09:08 +01:00
$outputlangs -> setDefaultLang ( 'en_US' );
$outputlangs -> load ( " main " );
2024-02-19 15:28:21 +01:00
$result = dol_print_date ( 0 + 24 * 3600 , 'day' , false , $outputlangs );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( '01/02/1970' , $result );
// Check %a and %b format for en_US
2024-02-19 15:28:21 +01:00
$result = dol_print_date ( 0 , '%a %b' , false , $outputlangs );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( 'Thu Jan' , $result );
return $result ;
}
2013-01-18 15:54:40 +01:00
}