Hi!
I'm having major issues to get chatrooms work in latest version (currently 2.9) in D6. I'm running on "PHP 5.3.5" server and created a chatroom with default settings and once I go to the room I get PHP fatal error.

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() [datetime.--construct]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Helsinki' for 'EET/2.0/no DST' instead' in /var/www/html/sites/all/modules/chatroom/chatroom.module:1217 Stack trace: #0 /var/www/html/sites/all/modules/chatroom/chatroom.module(1217): DateTime->__construct('@1295640434') #1 /var/www/html/sites/all/modules/chatroom/chatroom.module(1204): chatroom_get_message_time_string('1295633234') #2 /var/www/html/sites/all/modules/chatroom/chatroom.theme.inc(326): chatroom_chat_get_themed_message(Object(stdClass), Object(stdClass)) #3 [internal function]: theme_chatroom_chat(Object(stdClass)) #4 /var/www/html/ur in /var/www/html/sites/all/modules/chatroom/chatroom.module on line 1217

Comments

Anonymous’s picture

thanks for the report, i'll look into it.

iMiksu’s picture

Title: Uncaught exception with message 'DateTime' in chatroom.module:1217 when using PHP 5.3 » Uncaught exception with message 'DateTime' in chatroom.module:1217

Hi!
It seems that PHP requires to declare timezone before setting up DateTime object. In PHP 5.3 it throws an exception unlike 5.2 so this problem applies only in PHP 5.3.

However, this should respect Drupal user's timezone settings which is in offset in seconds. By doing little research I found that you do not have to use human friendly strings to setup timezone object (like in my case, it would be "Europe/Helsinki"). But you can also use format "Etc/GMT+X". So by this modification I became into this solution which fixes this issue.

// chatroom.module:1210
/**
 * Get the time string for a message from a UTC timestamp. 
 * 
 * @param mixed $timestamp 
 * @return string
 */
function chatroom_get_message_time_string($timestamp) {
  $offset = chatroom_get_user_timezone_offset()/60/60;
  $offset = $offset < 0 ? '-'.$offset : '+'.$offset;
  $date = new DateTime('@'.($timestamp+chatroom_get_user_timezone_offset()), new DateTimeZone('Etc/GMT'.$offset));
  return $date->format(variable_get('chatroom_msg_date_format', 'H:i:s'));
}

Hope this helps you to fix this issue!

iMiksu’s picture

Title: Uncaught exception with message 'DateTime' in chatroom.module:1217 » Uncaught exception with message 'DateTime' in chatroom.module:1217 when using PHP 5.3
Status: Active » Needs work
Anonymous’s picture

Title: Uncaught exception with message 'DateTime' in chatroom.module:1217 » Uncaught exception with message 'DateTime' in chatroom.module:1217 when using PHP 5.3
Status: Needs work » Fixed

thanks, committed modified version here:

http://drupal.org/cvs?commit=491418

dmuth’s picture

Version: 6.x-2.9 » 6.x-2.13
Status: Fixed » Patch (to be ported)

This is still happening as of 6.x-2.13, and causing my site to get "502 Server Errors"(!), so I wrote a patch:

Here's a diff from Git. Let me know if you need it in another format:

--- a/chatroom.module
+++ b/chatroom.module
@@ -1222,8 +1222,25 @@ function chatroom_chat_get_themed_message($message, $node) {
 function chatroom_get_message_time_string($timestamp) {
   $offset_in_hours = chatroom_get_user_timezone_offset() / 60 / 60;
   $offset_in_hours = $offset_in_hours >= 0 ? '+' . $offset_in_hours : $offset_in_hours;
-  $date = new DateTime('@' . ($timestamp + chatroom_get_user_timezone_offset()), new DateTimeZone('Etc/GMT' . $offset_in_hours));
-  return $date->format(variable_get('chatroom_msg_date_format', 'H:i:s'));
+
+       //$offset_in_hours = "foobar"; // Uncomment to cause the catch
+       try {
+                       $date_time_zone = new DateTimeZone('Etc/GMT' . $offset_in_hours);
+
+       } catch (Exception $e) {
+               $message = t("Whoa! Caught exception: %e. Trying again with NO time offset.", 
+                       array("%e" => $e->getMessage()));
+               watchdog("chatroom", $message);
+
+               $offset_in_hours = 0;
+               $date_time_zone = new DateTimeZone('Etc/GMT' . $offset_in_hours);
+
+       }
+
+       $date = new DateTime('@' . ($timestamp + chatroom_get_user_timezone_offset()), $date_time_zone);
+
+       return $date->format(variable_get('chatroom_msg_date_format', 'H:i:s'));
+
 }
 
 /**

From looking at my logs, I've seen crazy messages like these: DateTimeZone::__construct() [function.DateTimeZone---construct]: Unknown or bad timezone (Etc/GMT+21) in /path/to/modules/chatroom/chatroom.module on line 122

Given that that timezone was a string, and a number of some sort of was expected, this function is clearly being fed bad data from somewhere. The sanest thing to do in this place is to enclose the entire thing in a try/catch block, and log when there's any issues. Hopefully enough people logging when this issue happens will provide enough data on the underlying condition.

Anonymous’s picture

Status: Patch (to be ported) » Needs review

Thanks, will review shortly.

AlexisWilke’s picture

I'm not too sure, by the catch() uses new DateTimeZone('Etc/GMT' . $offset_in_hours); with the offset... 8-)

Anyway, I found this page which says that the function only supports 'UTC'.

http://us.php.net/manual/en/timezones.others.php

Now, I wish that the format_date() from Core work properly instead of how it currently works: not properly.

I did get this error too and I fixed my version with a simple:

return format_date($timestamp, 'custom', 'H:i:s');

This works for me, but I wonder... how does it work for other users?!

Thank you.
Alexis Wilke

AlexisWilke’s picture

I also noticed that you were using the same code in the chatroomread.php ... so if a fix is found for the .module, make sure the other file gets fixed too.

Thank you.
Alexis