<?php
/* Copyright (C) 2010 Laurent Destailleur  <eldy@users.sourceforge.net>
 * Copyright (C) 2023 Alexandre Janniaux   <alexandre.janniaux@gmail.com>
 * Copyright (C) 2024       Frédéric France         <frederic.france@free.fr>
 *
 * 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
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 * or see https://www.gnu.org/
 */

/**
 *      \file       test/phpunit/DateLibTest.php
 *		\ingroup    test
 *      \brief      PHPUnit test
 *		\remarks	To run this script as CLI:  phpunit filename.php
 */

global $conf,$user,$langs,$db,$mysoc;
//define('TEST_DB_FORCE_TYPE','mysql');	// This is to force using mysql driver
//require_once 'PHPUnit/Autoload.php';
require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
require_once dirname(__FILE__).'/../../htdocs/core/lib/date.lib.php';
require_once dirname(__FILE__).'/CommonClassTest.class.php';

if (empty($user->id)) {
	print "Load permissions for admin user nb 1\n";
	$user->fetch(1);
	$user->loadRights();
}
$conf->global->MAIN_DISABLE_ALL_MAILS = 1;

print "\n".$langs->trans("CurrentTimeZone").' : '.getServerTimeZoneString();
print "\n".$langs->trans("CurrentHour").' : '.dol_print_date(dol_now('gmt'), 'dayhour', 'tzserver');
print "\n";


/**
 * Class for PHPUnit tests
 *
 * @backupGlobals disabled
 * @backupStaticAttributes enabled
 * @remarks	backupGlobals must be disabled to have db,conf,user and lang not erased.
 */
class DateLibTest extends CommonClassTest
{
	/**
	 * testNumBetweenDay
	 *
	 * @return	void
	 */
	public function testNumBetweenDay()
	{
		global $conf,$user,$langs,$db;
		$conf = $this->savconf;
		$user = $this->savuser;
		$langs = $this->savlangs;
		$db = $this->savdb;

		// With same hours
		$date1 = dol_mktime(0, 0, 0, 1, 1, 2012, 'gmt');
		$date2 = dol_mktime(0, 0, 0, 1, 2, 2012, 'gmt');

		$result = num_between_day($date1, $date2, 1);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(2, $result);

		$result = num_between_day($date1, $date2, 0);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(1, $result);

		// With different hours
		$date1 = dol_mktime(0, 0, 0, 1, 1, 2012, 'gmt');
		$date2 = dol_mktime(12, 0, 0, 1, 2, 2012, 'gmt');

		$result = num_between_day($date1, $date2, 1);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(2, $result);

		$result = num_between_day($date1, $date2, 0);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(1, $result);

		// With different date before and after sunlight hour (day to change sunlight hour is 2014-03-30)
		$date1 = dol_mktime(0, 0, 0, 3, 28, 2014, true);
		$date2 = dol_mktime(0, 0, 0, 3, 31, 2014, true);

		$result = num_between_day($date1, $date2, 1);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(4, $result);

		$result = num_between_day($date1, $date2, 0);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(3, $result);

		return $result;
	}

	/**
	 * testNumPublicHoliday
	 *
	 * @return	void
	 */
	public function testNumPublicHoliday()
	{
		global $conf,$user,$langs,$db;
		$conf = $this->savconf;
		$user = $this->savuser;
		$langs = $this->savlangs;
		$db = $this->savdb;

		// With same hours - Tuesday/Wednesday jan 2013
		$date1 = dol_mktime(0, 0, 0, 1, 1, 2013, 'gmt');
		$date2 = dol_mktime(0, 0, 0, 1, 2, 2013, 'gmt');
		$date3 = dol_mktime(0, 0, 0, 1, 3, 2013, 'gmt');

		$result = num_public_holiday($date1, $date2, 'FR', 1);
		print __METHOD__." for Tuesday 1 - Wednesday 2 jan 2013 for FR result=".$result."\n";
		$this->assertEquals(1, $result, 'NumPublicHoliday for Tuesday 1 - Wednesday 2 jan 2013 for FR');   // 1 closed days (country france)

		$result = num_public_holiday($date1, $date2, 'XX', 1);
		print __METHOD__." for Tuesday 1 - Wednesday 2 jan 2013 for XX result=".$result."\n";
		$this->assertEquals(1, $result, 'NumPublicHoliday for Tuesday 1 - Wednesday 2 jan 2013 for XX');   // 1 closed days (country unknown)

		print '----'."\n";
		$result = num_public_holiday($date2, $date3, 'FR', 1);
		print __METHOD__." for Wednesday 2 - Thursday 3 jan 2013 for FR result=".$result."\n";
		$this->assertEquals(0, $result, 'NumPublicHoliday for Wednesday 2 - Thursday 3 jan 2013 for FR');   // no closed days

		// Check with easter monday
		$date1 = dol_mktime(0, 0, 0, 4, 21, 2019, 'gmt');
		$date2 = dol_mktime(0, 0, 0, 4, 23, 2019, 'gmt');

		$result = num_public_holiday($date1, $date2, 'XX', 1);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(1, $result, 'NumPublicHoliday including eastermonday for XX');   // 2 opened day, 1 closed days (sunday)

		$result = num_public_holiday($date1, $date2, 'FR', 1);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(2, $result, 'NumPublicHoliday including eastermonday for FR');   // 1 opened day, 2 closed days (sunday + easter monday)

		// Check for sunday/saturday - Friday 4 - Sunday 6 jan 2013
		$date1 = dol_mktime(0, 0, 0, 1, 4, 2013, 'gmt');
		$date2 = dol_mktime(0, 0, 0, 1, 6, 2013, 'gmt');

		$result = num_public_holiday($date1, $date2, 'FR', 1);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(2, $result, 'NumPublicHoliday for FR');   // 1 opened day, 2 closed days

		$result = num_public_holiday($date1, $date2, 'FR', 1, 1, 1);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(2, $result, 'NumPublicHoliday for FR');   // 1 opened day, 2 closed days

		$result = num_public_holiday($date1, $date2, 'FR', 1, 1, 0);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(1, $result, 'NumPublicHoliday for FR');   // 2 opened day, 1 closed days

		$result = num_public_holiday($date1, $date2, 'FR', 1, 0, 0);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(0, $result, 'NumPublicHoliday for FR');   // 3 opened day, 0 closed days

		$result = num_public_holiday($date1, $date2, 'XX', 1);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(2, $result, 'NumPublicHoliday for XX');   // 1 opened day, 2 closed days (even if country unknown)
	}

	/**
	 * testNumOpenDay
	 *
	 * @return	void
	 */
	public function testNumOpenDay()
	{
		global $conf,$user,$langs,$db;
		$conf = $this->savconf;
		$user = $this->savuser;
		$langs = $this->savlangs;
		$db = $this->savdb;


		// Check for sunday/sunday with time changing - Sunday 25 october 2025 - Sunday 1 november 2025
		$date1 = dol_mktime(0, 0, 0, 10, 26, 2025, 'gmt');
		$date2 = dol_mktime(0, 0, 0, 11, 1, 2025, 'gmt');
		$result = num_open_day($date1, $date2, 0, 1, 0, 'FR');
		print __METHOD__." result ".$result."\n";
		$this->assertEquals(5, $result, 'NumPublicHoliday for FR with date start before date change end inding after');


		// With same hours - Tuesday/Wednesday jan 2013
		$date1 = dol_mktime(0, 0, 0, 1, 1, 2013, 'gmt');	// tuesday
		$date2 = dol_mktime(0, 0, 0, 1, 2, 2013, 'gmt');	// wednesday
		$date3 = dol_mktime(0, 0, 0, 1, 3, 2013, 'gmt');	// thursday

		$result = num_open_day($date1, $date2, 0, 1, 0, 'FR');
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(1, $result, 'NumOpenDay Tuesday 1 - Wednesday 2 jan 2013 for FR');   // 1 opened days (country france)

		$result = num_open_day($date1, $date2, 0, 1, 0, 'XX');
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(1, $result, 'NumOpenDay Tuesday 1 - Wednesday 2 jan 2013 for XX');   // 1 opened days (country unknown)

		$result = num_open_day($date2, $date3, 0, 1, 0, 'FR');
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(2, $result, 'NumOpenDay Wednesday 2 - Thursday 3 jan 2013 for FR');   // 2 opened days

		// With same hours - Friday/Sunday jan 2013
		$date1 = dol_mktime(0, 0, 0, 1, 4, 2013, 'gmt');	// friday
		$date2 = dol_mktime(0, 0, 0, 1, 6, 2013, 'gmt');	// sunday

		$result = num_open_day($date1, $date2, 0, 1, 0, 'FR');
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(1, $result, 'NumOpenDay for FR');   // 1 opened day, 2 closed

		$result = num_open_day($date1, $date2, 'XX', 1);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(1, $result, 'NumOpenDay for XX');   // 1 opened day, 2 closes (even if country unknown)

		// Test option MAIN_NON_WORKING_DAYS_INCLUDE_SATURDAY and MAIN_NON_WORKING_DAYS_INCLUDE_SUNDAY
		$conf->global->MAIN_NON_WORKING_DAYS_INCLUDE_SATURDAY = 0;
		$result = num_open_day($date1, $date2, 0, 1, 0, 'FR');
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(2, $result, 'NumOpenDay for FR when saturday is a working day');   //2 opened day, 1 closed

		$conf->global->MAIN_NON_WORKING_DAYS_INCLUDE_SUNDAY = 0;
		$result = num_open_day($date1, $date2, 'XX', 1);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(3, $result, 'NumOpenDay for XX when saturday + sunday are working days');   // 3 opened day, 0 closes (even if country unknown)

		// Define specific dates for these tests
		$date_friday_4 = dol_mktime(0, 0, 0, 1, 4, 2013, 'gmt');   // Friday
		$date_saturday_5 = dol_mktime(0, 0, 0, 1, 5, 2013, 'gmt'); // Saturday
		$date_monday_7 = dol_mktime(0, 0, 0, 1, 7, 2013, 'gmt');   // Monday
		$date_friday_11 = dol_mktime(0, 0, 0, 1, 11, 2013, 'gmt');  // Following Friday

		// Case 1: Weekend Boundary (Friday morning -> Saturday morning)
		// Expected: 1 day. No half-day deduction for end date on a non-working day.
		// $starthalfday = 'morning', $endhalfday = 'morning' -> $halfday = 1
		$conf->global->MAIN_NON_WORKING_DAYS_INCLUDE_SATURDAY = 1;
		$conf->global->MAIN_NON_WORKING_DAYS_INCLUDE_SUNDAY = 1;
		$result = num_open_day($date_friday_4, $date_saturday_5, 0, 1, 1, 'FR');
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(1, $result, 'Case 1: Friday morning to Saturday morning should be 1 day');

		// Case 2: Full week with half-day (Friday morning -> Following Friday morning)
		// Expected: 5.5 days.
		// $starthalfday = 'morning', $endhalfday = 'morning' -> $halfday = 1
		$result = num_open_day($date_friday_4, $date_friday_11, 0, 1, 1, 'FR');
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(5.5, $result, 'Case 2: Friday morning to next Friday morning should be 5.5 days');

		// Case 3: Single Half-Day (Monday afternoon)
		// Expected: 0.5 days.
		// $starthalfday = 'afternoon' -> $halfday = -1
		$result = num_open_day($date_monday_7, $date_monday_7, 0, 1, -1, 'FR');
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(0.5, $result, 'Case 3: A single Monday afternoon should be 0.5 days');

		// Case 4: Standard Leave (Monday morning -> Friday evening)
		// Expected: 5 days.
		// $starthalfday = 'morning', $endhalfday = 'evening' -> $halfday = 0
		$result = num_open_day($date_monday_7, $date_friday_11, 0, 1, 0, 'FR');
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(5, $result, 'Case 4: Monday morning to Friday evening should be 5 days');
	}

	/**
	 * testConvertTime2Seconds
	 *
	 * @return	void
	 */
	public function testConvertTime2Seconds()
	{
		global $conf,$user,$langs,$db;
		$conf = $this->savconf;
		$user = $this->savuser;
		$langs = $this->savlangs;
		$db = $this->savdb;

		$result = convertTime2Seconds(1, 1, 2);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(3662, $result);

		return $result;
	}

	/**
	 * testConvertSecondToTime
	 *
	 * @return void
	 */
	public function testConvertSecondToTime()
	{
		global $conf,$user,$langs,$db;
		$conf = $this->savconf;
		$user = $this->savuser;
		$langs = $this->savlangs;
		$db = $this->savdb;

		$result = convertSecondToTime(0, 'all', 86400);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals('0', $result);

		$result = convertSecondToTime(86400, 'all', 86400);
		print __METHOD__." result=".$result."\n";
		$this->assertSame('1 '.$langs->trans("d"), $result);

		return $result;
	}

	/**
	 * testDolPrintDate
	 *
	 * @return void
	 */
	public function testDolPrintDate()
	{
		global $conf,$user,$langs,$db;
		$conf = $this->savconf;
		$user = $this->savuser;
		$langs = $this->savlangs;
		$db = $this->savdb;

		// Check %Y-%m-%d %H:%M:%S format
		$result = dol_print_date('1970-01-01', '%Y-%m-%d %H:%M:%S', true);	// A case for compatibility check
		print __METHOD__." result=".$result."\n";
		$this->assertEquals('1970-01-01 00:00:00', $result);


		// Check %Y-%m-%d %H:%M:%S format
		$result = dol_print_date(0, '%Y-%m-%d %H:%M:%S', true);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals('1970-01-01 00:00:00', $result);

		// Same with T and Z
		$result = dol_print_date(0, '%Y-%m-%dT%H:%M:%SZ', true);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals('1970-01-01T00:00:00Z', $result);

		// Check %Y-%m-%d %H:%M:%S format
		$result = dol_print_date(16725225600, '%Y-%m-%d %H:%M:%S', true);	// http://www.epochconverter.com/
		print __METHOD__." result=".$result."\n";
		$this->assertEquals('2500-01-01 00:00:00', $result);

		// Check %Y-%m-%d %H:%M:%S format
		$result = dol_print_date(-1830384000, '%Y-%m-%d %H:%M:%S', true);	// http://www.epochconverter.com/
		print __METHOD__." result=".$result."\n";
		$this->assertEquals('1912-01-01 00:00:00', $result);	// dol_print_date use TZ (good) but epoch converter does not use it.

		// Check %Y-%m-%d %H:%M:%S format
		$result = dol_print_date(-11676096000, '%Y-%m-%d %H:%M:%S', true);	// http://www.epochconverter.com/
		print __METHOD__." result=".$result."\n";
		$this->assertEquals('1600-01-01 00:00:00', $result);

		// test with negative timezone
		$result = dol_print_date(-1, '%Y-%m-%d %H:%M:%S', true);	// http://www.epochconverter.com/
		print __METHOD__." result=".$result."\n";
		$this->assertEquals('1969-12-31 23:59:59', $result);

		// Check dayhour format for fr_FR
		$outputlangs = new Translate('', $conf);
		$outputlangs->setDefaultLang('fr_FR');
		$outputlangs->load("main");

		$result = dol_print_date(0 + 24 * 3600, 'dayhour', true, $outputlangs);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals('02/01/1970 00:00', $result);

		// Check %a and %b format for fr_FR
		$result = dol_print_date(0, '%a %b %B', true, $outputlangs);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals('Jeu Jan. Janvier', $result);


		$result = dol_print_date(1619388000, '%Y-%m-%d %a', 'gmt', $outputlangs);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals('2021-04-25 Dim', $result);

		/* This test is disabled because result depends on TZ of server
		$result=dol_print_date(1619388000, '%Y-%m-%d %a', 'tzserver', $outputlangs);	// If TZ is +2, then result will be Lun for 1619388000
		print __METHOD__." result=".$result."\n";
		$this->assertEquals('2021-04-26 Lun', $result);
		*/

		// Check day format for en_US
		$outputlangs = new Translate('', $conf);
		$outputlangs->setDefaultLang('en_US');
		$outputlangs->load("main");

		$result = dol_print_date(0 + 24 * 3600, 'day', true, $outputlangs);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals('01/02/1970', $result);

		// Check %a and %b format for en_US
		$result = dol_print_date(0, '%a %b %B', true, $outputlangs);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals('Thu Jan January', $result);

		// Check date output in a use timezone
		$_SESSION['dol_tz'] = 1;
		$_SESSION['dol_dst'] = 0;
		$_SESSION['dol_tz_string'] = 'Europe/Paris';

		$result = dol_print_date(0, '%H', 'tzuserrel', $outputlangs);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals('01', $result);

		return $result;
	}

	/**
	 * testDolTimePlusDuree
	 *
	 * @return int
	 */
	public function testDolTimePlusDuree()
	{
		global $conf,$user,$langs,$db;
		$conf = $this->savconf;
		$user = $this->savuser;
		$langs = $this->savlangs;
		$db = $this->savdb;

		// Check dayhour format for fr_FR
		$outputlangs = new Translate('', $conf);
		$outputlangs->setDefaultLang('fr_FR');
		$outputlangs->load("main");

		$result = dol_print_date(dol_time_plus_duree(dol_time_plus_duree(dol_time_plus_duree(0, 1, 'm'), 1, 'y'), 1, 'd'), 'dayhour', 'gmt', $outputlangs);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals('02/02/1971 00:00', $result);

		// Add 4 days on a date just before daylight change
		$result = dol_print_date(dol_time_plus_duree(dol_mktime(0, 0, 0, 10, 24, 2025, 'gmt'), 4, 'd'), 'dayhour', 'gmt', $outputlangs);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals('28/10/2025 00:00', $result);


		// Imagine we are in a server with TZ Europe/Paris (UTC+1 +daylight)
		// A) If user want to store a "date_when" (of a recurring invoices) for "2025-10-01" (every 1st of month), we store it in db with idate() so with string value '2025-10-01 00:00:00'
		//     (because we want "date_when" as a day only field, so with no mention of TZ)
		// B) So after a read with jdate(), we retrieve in UTC the date in memory that is '30 September 22:00 UTC.'
		// C) If we add 1 month, we got '30 October 22:00 UTC.'
		// D) When we want to store it into db, we use idate() to convert it into server timezone and we got '2025-10-30 23:00:00' because the daylight is not the same, offset is 1h instead of 2h.
		// E) So next day processed will be the 30th instead of the 1st (because we take car of day only).
		/*
		$date_when = dol_mktime(22, 0, 0, 9, 30, 2025, 'gmt');
		$result = dol_print_date(dol_time_plus_duree(dol_mktime(22, 0, 0, 9, 30, 2025, 'gmt'), 1, 'm'), 'standard', 'gmt', $outputlangs);
		$result2 = $db->idate(dol_time_plus_duree(dol_mktime(22, 0, 0, 9, 30, 2025, 'gmt'), 1, 'm'));
		print "result A: ".$result."\n";
		$dateInfo = dol_getdate($date_when);
		var_dump($dateInfo);
		print "db->idate last day ".$db->idate(dol_mktime(23, 59, 59, 10, 31, 2025))."\n";
		print "doldprintdate ".dol_print_date(dol_mktime(23, 59, 59, 10, 31, 2025), 'standard', 'gmt')."\n";
		print "result B: ".$result2."\n";
		$this->assertEquals('2025-10-30 22:00:00', $result);
		$this->assertEquals('2025-10-30 23:00:00', $result2);
		*/
		$date_when_utc = dol_mktime(22, 0, 0, 9, 30, 2025, 'gmt');
		//$tz = new DateTimeZone('Europe/Paris');
		//$date_when_utc = (new DateTime('2025-10-01 00:00:00', $tz))->getTimestamp();
		$date_when_next_utc = dol_time_plus_duree($date_when_utc, 1, 'm', 0, 'Europe/Paris');
		$result1 = dol_print_date($date_when_utc, 'standard', 'gmt', $outputlangs);
		$result2 = dol_print_date($date_when_next_utc, 'standard', 'gmt', $outputlangs);
		print "XXX date_when_utc      = ".$result1."\n";
		print "XXX date_when_next_utc = ".$result2."\n";
		$this->assertEquals('2025-09-30 22:00:00', $result1);
		$this->assertEquals('2025-10-31 23:00:00', $result2);

		// With param for end of month
		$date_when_utc = dol_mktime(22, 0, 0, 9, 30, 2025, 'gmt');
		$date_when_next_utc = dol_time_plus_duree($date_when_utc, 1, 'm', 1, 'Europe/Paris');
		$result1 = dol_print_date($date_when_utc, 'standard', 'gmt', $outputlangs);
		$result2 = dol_print_date($date_when_next_utc, 'standard', 'gmt', $outputlangs);
		print "XXX date_when_utc      = ".$result1."\n";
		print "XXX date_when_next_utc = ".$result2."\n";
		$this->assertEquals('2025-09-30 22:00:00', $result1);
		$this->assertEquals('2025-10-31 23:00:00', $result2);

		// Rule for end of month applied in the timezone
		$date_when_utc = dol_mktime(22, 0, 0, 1, 30, 2025, 'gmt');
		$date_when_next_utc = dol_time_plus_duree($date_when_utc, 1, 'm', 1, 'Europe/Paris');
		$result1 = dol_print_date($date_when_utc, 'standard', 'gmt', $outputlangs);
		$result2 = dol_print_date($date_when_next_utc, 'standard', 'gmt', $outputlangs);
		print "XXX date_when_utc      = ".$result1."\n";
		print "XXX date_when_next_utc = ".$result2."\n";
		$this->assertEquals('2025-01-30 22:00:00', $result1);
		$this->assertEquals('2025-02-28 22:00:00', $result2);

		// Rule for end of month in GMT
		$result = dol_print_date(dol_time_plus_duree(dol_mktime(0, 0, 0, 1, 31, 2028, 'gmt'), 1, 'm', 1, 'gmt'), 'standard', 'gmt', $outputlangs);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals('2028-02-29 00:00:00', $result);

		return $result;
	}

	/**
	 * testDolStringToTime
	 *
	 * @return int
	 */
	public function testDolStringToTime()
	{
		global $conf,$user,$langs,$db;
		$conf = $this->savconf;
		$user = $this->savuser;
		$langs = $this->savlangs;
		$db = $this->savdb;

		$stime = '19700102';
		$result = dol_stringtotime($stime);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(86400, $result);

		$stime = '1970-01-01T02:00:00Z';
		$result = dol_stringtotime($stime);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(7200, $result);

		$stime = '1970-01-01 02:00:00';
		$result = dol_stringtotime($stime);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(7200, $result);

		$stime = '19700101T020000Z';
		$result = dol_stringtotime($stime);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(7200, $result);

		$stime = '19700101020000';
		$result = dol_stringtotime($stime);
		print __METHOD__." result=".$result."\n";
		$this->assertEquals(7200, $result);

		return $result;
	}

	/**
	 * testDolGetFirstDayWeek
	 *
	 * @return int
	 */
	public function testDolGetFirstDayWeek()
	{
		global $conf;

		$day = 3;
		$month = 2;
		$year = 2015;
		$conf->global->MAIN_START_WEEK = 1;	// start on monday
		$prev = dol_get_first_day_week($day, $month, $year);
		$this->assertEquals(2, (int) $prev['first_day']);		// monday for month 2, year 2014 is the 2

		$day = 3;
		$month = 2;
		$year = 2015;
		$conf->global->MAIN_START_WEEK = 0;	// start on sunday
		$prev = dol_get_first_day_week($day, $month, $year);
		$this->assertEquals(1, (int) $prev['first_day']);		// sunday for month 2, year 2015 is the 1st

		return 1;
	}

	/**
	 * Check previous weeks across month and year boundaries.
	 *
	 * @return void
	 */
	public function testDolGetPrevWeek()
	{
		global $conf;

		$oldstartweek = isset($conf->global->MAIN_START_WEEK) ? getDolGlobalInt('MAIN_START_WEEK') : null;
		$oldtimezone = date_default_timezone_get();
		try {
			date_default_timezone_set('UTC');
			$cases = array(
				array(1, 1, 3, 2024, array('year' => 2024, 'month' => 2, 'day' => 19)),
				array(1, 1, 1, 2025, array('year' => 2024, 'month' => 12, 'day' => 23)),
				array(1, 1, 3, 2023, array('year' => 2023, 'month' => 2, 'day' => 20)),
				array(1, 15, 3, 2024, array('year' => 2024, 'month' => 3, 'day' => 4)),
				array(1, 1, 6, 2026, array('year' => 2026, 'month' => 5, 'day' => 25)),
				array(1, 1, 1, 2024, array('year' => 2023, 'month' => 12, 'day' => 25)),
				array(0, 1, 3, 2024, array('year' => 2024, 'month' => 2, 'day' => 18)),
				array(0, 1, 1, 2025, array('year' => 2024, 'month' => 12, 'day' => 22)),
				array(0, 15, 3, 2024, array('year' => 2024, 'month' => 3, 'day' => 3))
			);
			foreach ($cases as $case) {
				$conf->global->MAIN_START_WEEK = $case[0];
				$this->assertSame($case[4], dol_get_prev_week($case[1], 0, $case[2], $case[3]));
			}
		} finally {
			date_default_timezone_set($oldtimezone);
			if ($oldstartweek === null) {
				unset($conf->global->MAIN_START_WEEK);
			} else {
				$conf->global->MAIN_START_WEEK = $oldstartweek;
			}
		}
	}

	/**
	 * testGetFirstDayOfEachWeek
	 *
	 * @return int
	 */
	public function testGetFirstDayOfEachWeek()
	{
		// June 2026 (no year overlap): weeks 23 to 27
		$TWeek = getWeekNumbersOfMonth(6, 2026);
		$this->assertEquals(array('23' => '01', '24' => '08', '25' => '15', '26' => '22', '27' => '29'), getFirstDayOfEachWeek($TWeek, 2026));

		// December 2025 ends with week 01 of 2026: week 01 starts on monday 2025-12-29
		$TWeek = getWeekNumbersOfMonth(12, 2025);
		$this->assertEquals(array('49' => '01', '50' => '08', '51' => '15', '52' => '22', '01' => '29'), getFirstDayOfEachWeek($TWeek, 2025));

		// January 2022 starts with week 52 of 2021 (monday 2021-12-27). Week 01 of 2022 starts on monday the 3rd,
		// week 02 on the 10th, ... (weeks 01 to 05 must not be shifted to next year)
		$TWeek = getWeekNumbersOfMonth(1, 2022);
		$this->assertEquals(array('52' => '27', '01' => '03', '02' => '10', '03' => '17', '04' => '24', '05' => '31'), getFirstDayOfEachWeek($TWeek, 2022));

		// January 2021 starts with week 53 of 2020 (monday 2020-12-28)
		$TWeek = getWeekNumbersOfMonth(1, 2021);
		$this->assertEquals(array('53' => '28', '01' => '04', '02' => '11', '03' => '18', '04' => '25'), getFirstDayOfEachWeek($TWeek, 2021));

		return 1;
	}

	/**
	 * testGetLastDayOfEachWeek
	 *
	 * @return int
	 */
	public function testGetLastDayOfEachWeek()
	{
		// June 2026 (no year overlap): weeks 23 to 27
		$TWeek = getWeekNumbersOfMonth(6, 2026);
		$this->assertEquals(array('23' => '07', '24' => '14', '25' => '21', '26' => '28', '27' => '05'), getLastDayOfEachWeek($TWeek, 2026));

		// December 2025 ends with week 01 of 2026: week 01 ends on sunday 2026-01-04
		$TWeek = getWeekNumbersOfMonth(12, 2025);
		$this->assertEquals(array('49' => '07', '50' => '14', '51' => '21', '52' => '28', '01' => '04'), getLastDayOfEachWeek($TWeek, 2025));

		// January 2022 starts with week 52 of 2021: week 52 ends on sunday 2022-01-02
		$TWeek = getWeekNumbersOfMonth(1, 2022);
		$this->assertEquals(array('52' => '02', '01' => '09', '02' => '16', '03' => '23', '04' => '30', '05' => '06'), getLastDayOfEachWeek($TWeek, 2022));

		return 1;
	}

	/**
	 * testDolGetFirstHour
	 *
	 * @return int
	 */
	public function testDolGetFirstHour()
	{
		global $conf;

		$now = 1800 + (24 * 3600 * 10);	// The 11th of january 1970 at 0:30 in UTC
		$result = dol_get_first_hour($now, 'gmt');
		print __METHOD__." now = ".$now.", dol_print_date(now, 'dayhourrfc', 'gmt') = ".dol_print_date($now, 'dayhourrfc', 'gmt').", result = ".$result.", dol_print_date(result, 'dayhourrfc', 'gmt') = ".dol_print_date($result, 'dayhourrfc', 'gmt')."\n";
		$this->assertEquals('1970-01-11T00:00:00Z', dol_print_date($result, 'dayhourrfc', 'gmt'));		// monday for month 2, year 2014 is the 2

		$now = 23.5 * 3600 + (24 * 3600 * 10);	// The 11th of january 1970 at 23:30 in UTC
		$result = dol_get_first_hour($now, 'gmt');
		print __METHOD__." now = ".$now.", dol_print_date(now, 'dayhourrfc', 'gmt') = ".dol_print_date($now, 'dayhourrfc', 'gmt').", result = ".$result.", dol_print_date(result, 'dayhourrfc', 'gmt') = ".dol_print_date($result, 'dayhourrfc', 'gmt')."\n";
		$this->assertEquals('1970-01-11T00:00:00Z', dol_print_date($result, 'dayhourrfc', 'gmt'));		// monday for month 2, year 2014 is the 2

		return 1;
	}

	/**
	 * testDolSqlDateFilter
	 *
	 * @return int
	 */
	public function testDolSqlDateFilter()
	{
		global $conf;

		$result = dolSqlDateFilter('field1', 0, 0, 1970, 0);
		print __METHOD__." result = ".$result."\n";
		$this->assertEquals(" AND field1 BETWEEN '1970-01-01 00:00:00' AND '1970-12-31 23:59:59'", $result, 'Test dolSqlDateFilter 1');

		/* If server/company is in TZ America/Bahia, and we need range with a date set in GMT
		$result = dolSqlDateFilter('field1', 0, 0, 1970, 0, 'gmt');
		print __METHOD__." result = ".$result."\n";
		$this->assertEquals(" AND field1 BETWEEN '1969-12-31 21:00:00' AND '1970-12-31 20:59:59'", $result, 'Test dolSqlDateFilter 2');
		*/

		return 1;
	}
}
