From 4d6ba96115a7087a60fa64f2d4ac4e74ecc3ed7b Mon Sep 17 00:00:00 2001 From: nathangray Date: Mon, 6 Feb 2017 13:06:04 -0700 Subject: [PATCH] Move commented-out DateTime tests into their own class --- api/src/DateTime.php | 32 --------- api/src/test/DateTimeTest.php | 120 ++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 32 deletions(-) create mode 100644 api/src/test/DateTimeTest.php diff --git a/api/src/DateTime.php b/api/src/DateTime.php index 91b12eba1b..22b636b97a 100644 --- a/api/src/DateTime.php +++ b/api/src/DateTime.php @@ -693,35 +693,3 @@ class DateTime extends \DateTime } } DateTime::init(); - -/* -if (isset($_SERVER['SCRIPT_FILENAME']) && $_SERVER['SCRIPT_FILENAME'] == __FILE__) // some tests -{ - // test timestamps/dates before 1970 - foreach(array('19690811',-3600,'-119322000') as $ts) - { - try { - echo "

DateTime::to($ts,'Y-m-d H:i:s')=".DateTime::to($ts,'Y-m-d H:i:s')."

\n"; - $et = new DateTime($ts); - echo "

DateTime($ts)->format('Y-m-d H:i:s')=".$et->format('Y-m-d H:i:s')."

\n"; - $dt = new DateTime($ts); - echo "

DateTime($ts)->format('Y-m-d H:i:s')=".$dt->format('Y-m-d H:i:s')."

\n"; - } catch(\Exception $e) { - echo "

Exception: ".$e->getMessage()."

\n"; - } - } - // user time is UTC - echo "

user timezone = ".($GLOBALS['egw_info']['user']['preferences']['common']['tz'] = 'UTC').", server timezone = ".date_default_timezone_get()."

\n"; - - $time = time(); - echo "

time=$time=".date('Y-m-d H:i:s',$time)."(server) =".DateTime::server2user($time,'Y-m-d H:i:s')."(user) =".DateTime::server2user($time,'ts')."(user)=".date('Y-m-d H:i:s',DateTime::server2user($time,'ts'))."

\n"; - - echo "DateTime::to(array('full' => '20091020', 'hour' => 12, 'minute' => 0))='".DateTime::to(array('full' => '20091020', 'hour' => 12, 'minute' => 0))."'

\n"; - - $ts = DateTime::to(array('full' => '20091027', 'hour' => 10, 'minute' => 0),'ts'); - echo "

2009-10-27 10h UTC timestamp=$ts --> server time = ".DateTime::user2server($ts,'')." --> user time = ".DateTime::server2user(DateTime::user2server($ts),'')."

\n"; - - $ts = DateTime::to(array('full' => '20090627', 'hour' => 10, 'minute' => 0),'ts'); - echo "

2009-06-27 10h UTC timestamp=$ts --> server time = ".DateTime::user2server($ts,'')." --> user time = ".DateTime::server2user(DateTime::user2server($ts),'')."

\n"; -} -*/ diff --git a/api/src/test/DateTimeTest.php b/api/src/test/DateTimeTest.php new file mode 100644 index 0000000000..3b8f9f6db1 --- /dev/null +++ b/api/src/test/DateTimeTest.php @@ -0,0 +1,120 @@ +getName()); + } + + /** + * Test that dates before 1970 are handled correctly + */ + public function testBefore1970() + { + $checklist = array( + // Use sub array instead of key/value pair to preserve type + // Timestamp Expected + array('19690811', '1969-08-11 00:00:00'), + array(-3600, '1969-12-31 23:00:00'), + array('-119322000', '1966-03-21 23:00:00') + ); + foreach($checklist as $test) + { + list($ts, $expected) = $test; + $this->checkTimestamp($ts, $expected); + } + } + + /** + * Check timezone conversion is sane - convert back and forth between same + * timezone + */ + public function testUserTimeEqualsServerTime() + { + $time = time(); + $this->assertEquals($time, DateTime::server2user($time, 'ts')); + $this->assertEquals(date('Y-m-d H:i:s',$time), DateTime::server2user($time,'Y-m-d H:i:s')); + + + $this->assertEquals('2009-10-20, 12:00',DateTime::to(array('full' => '20091020', 'hour' => 12, 'minute' => 0))); + + $ts = DateTime::to(array('full' => '20091027', 'hour' => 10, 'minute' => 0),'ts'); + $this->assertEquals(DateTime::user2server($ts,''), DateTime::server2user(DateTime::user2server($ts),'')); + + $ts = DateTime::to(array('full' => '20090627', 'hour' => 10, 'minute' => 0),'ts'); + $this->assertEquals(DateTime::user2server($ts,''), DateTime::server2user(DateTime::user2server($ts),'')); + } + + /** + * Test timezone conversion with actual changes + */ + public function testTimezoneConversion() + { + // Set server to UTC + $server_tz = date_default_timezone_get(); + date_default_timezone_set('UTC'); + + // Set user to Berlin (UTC-1) + DateTime::setUserPrefs('Europe/Berlin'); + $ts = DateTime::to(array('full' => '20091027', 'hour' => 10, 'minute' => 0),'ts'); + $this->assertEquals('2009-10-27 09:00:00', DateTime::user2server($ts,'Y-m-d H:i:s')); + + // Set user to Cape Verde (UTC+1) + DateTime::setUserPrefs('Atlantic/Cape_Verde'); + $ts = DateTime::to(array('full' => '20091027', 'hour' => 10, 'minute' => 0),'ts'); + $this->assertEquals('2009-10-27 11:00:00', DateTime::user2server($ts,'Y-m-d H:i:s')); + + date_default_timezone_set($server_tz); + } + + /** + * Check that a timestamp matches expectations + * + * @param string|int $ts Something that looks like a time + * @param string $expected Expected time, in Y-m-d H:i:s format + */ + protected function checkTimestamp($ts, $expected) + { + $fail_message = "Checking $ts = $expected"; + $this->assertEquals($expected, DateTime::to($ts,'Y-m-d H:i:s'), $fail_message); + + $dt = new DateTime($ts); + $this->assertEquals($expected, $dt->format('Y-m-d H:i:s'), $fail_message); + } +}