From 4d6ba96115a7087a60fa64f2d4ac4e74ecc3ed7b Mon Sep 17 00:00:00 2001
From: nathangray DateTime::to($ts,'Y-m-d H:i:s')=".DateTime::to($ts,'Y-m-d H:i:s')." DateTime($ts)->format('Y-m-d H:i:s')=".$et->format('Y-m-d H:i:s')." DateTime($ts)->format('Y-m-d H:i:s')=".$dt->format('Y-m-d H:i:s')." Exception: ".$e->getMessage()." user timezone = ".($GLOBALS['egw_info']['user']['preferences']['common']['tz'] = 'UTC').", server timezone = ".date_default_timezone_get()." 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'))."
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); + } +}