View Issue Details

IDProjectCategoryView StatusLast Update
0017747mantisbtinstallationpublic2015-03-15 19:58
ReportervboctorAssigned Todregad 
PrioritynormalSeverityminorReproducibilityhave not tried
Status closedResolutionfixed 
Product Version1.3.0-beta.1 
Target Version1.3.0-beta.2Fixed in Version1.3.0-beta.2 
Summary0017747: Timezone errors during install
Description

I hit some timezone related issues when testing on PHP 5.4. The issues occur when running install and when running the SOAP unit tests. It is due to timezone not being set before using date function.

See additional information for phpunit errors

Should we explicitly set to the timezone to UTC at the top of install.php and as part of bootstrap or somewhere where it runs before the test cases?

Additional Information

1) CategoryTest::testAddRenameDeleteCategory
date(): 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 the timezone 'UTC' for now, but please set date.timezone to select your timezone.

/Users/vboctor/php/mantisbt-master/tests/soap/CategoryTest.php:128
/Users/vboctor/php/mantisbt-master/tests/soap/CategoryTest.php:55

2) VersionTest::testAddVersion
Exception: 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 the timezone 'UTC' for now, but please set date.timezone to select your timezone.

/Users/vboctor/php/mantisbt-master/tests/soap/SoapBase.php:276
/Users/vboctor/php/mantisbt-master/tests/soap/VersionTest.php:71

3) VersionTest::testUpdateVersion
Exception: 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 the timezone 'UTC' for now, but please set date.timezone to select your timezone.

TagsNo tags attached.

Relationships

related to 0017751 closeddregad Timezone selection list does not include 'UTC' 

Activities

vboctor

vboctor

2014-10-13 03:30

manager   ~0041561

Associated pull request.
https://github.com/mantisbt/mantisbt/pull/380

dregad

dregad

2014-10-14 03:56

developer   ~0041567

This is a valid issue, but I don't think the change proposed in the PR is the right way to fix it. I see 2 problems:

  1. Defaulting TZ to UTC in the installer does avoid the warning, but will always preset the timezone selection list to UTC [1], regardless of PHP configuration. In other words, if I have set Europe/Zurich in php.ini, I would expect that value to be defaulted in the MantisBT installer's selection list, and not UTC. I believe it would be better to address this issue in core.php - will submit another PR as alternative fix.

  2. PHP Unit tests should be running on a correct and valid MantisBT setup, therefore we should not squelch errors or warnings to execute them. Or am I missing something ?

[1] There's another issue with this - see related 0017751

dregad

dregad

2014-10-15 09:11

developer   ~0041592

Please review alternative fix https://github.com/mantisbt/mantisbt/issues/382

dregad

dregad

2014-12-14 19:02

developer  

fix-17747-alternative.patch (2,645 bytes)
commit 078aab706d8a41c587519e7a2b362ac00b774c26
Author: Damien Regad <dregad@mantisbt.org>
Date:   2014-10-14 16:38:34 +0200

    ALT FIX: timezone initialization in core.php
    
    This is my original proposed fix, vs the one that was implemented in
    master which is based on grangeway's proposal.
    
    Prior to this, core.php would trigger a PHP warning on PHP >= 5.3 when
    date_default_timezone_get() is not able to determine the timezone and
    defaults to UTC (e.g. when php.ini date.timezone is not set).
    
    The timezone is determined in the following order
      1. user preferences
      2. MantisBT configuration ($g_default_timezone)
      3. php.ini setting (date.timezone)
      4. UTC (set by date_default_timezone_get())
    
    Fixes #17747

diff --git a/core.php b/core.php
index 8c31462..fc53e95 100644
--- a/core.php
+++ b/core.php
@@ -238,22 +238,31 @@ if( !isset( $g_login_anonymous ) ) {
 
 # Set the current timezone
 if( !defined( 'MANTIS_MAINTENANCE_MODE' ) ) {
-	require_api( 'authentication_api.php' );
-
-	# To reduce overhead, we assume that the timezone configuration is valid,
-	# i.e. it exists in timezone_identifiers_list(). If not, a PHP NOTICE will
-	# be raised. Use admin checks to validate configuration.
-	@date_default_timezone_set( config_get_global( 'default_timezone' ) );
-	$t_tz = @date_default_timezone_get();
-	config_set_global( 'default_timezone', $t_tz, true );
+	# Get MantisBT default timezone
+	$t_default_timezone = config_get_global( 'default_timezone' );
+	if( !in_array( $t_default_timezone, timezone_identifiers_list() ) ) {
+		# If not valid, fallback to system timezone, or UTC if not set
+		$t_default_timezone = @date_default_timezone_get();
+		config_set_global( 'default_timezone', $t_default_timezone, true );
+	}
 
+	# Now determine current timezone according to user's preferences
+	require_api( 'authentication_api.php' );
 	if( auth_is_user_authenticated() ) {
 		# Determine the current timezone according to user's preferences
 		require_api( 'user_pref_api.php' );
-		$t_tz = user_pref_get_pref( auth_get_current_user_id(), 'timezone' );
-		@date_default_timezone_set( $t_tz );
+		$t_user_timezone = user_pref_get_pref( auth_get_current_user_id(), 'timezone' );
+
+		# fallback to default timezone if not valid
+		if( in_array( $t_user_timezone, timezone_identifiers_list() ) ) {
+			$t_user_timezone = $t_default_timezone;
+		}
+	} else {
+		$t_user_timezone = $t_default_timezone;
 	}
-	unset( $t_tz );
+
+	date_default_timezone_set( $t_user_timezone );
+	unset( $t_user_timezone, $t_default_timezone );
 }
 
 # Cache current user's collapse API data
dregad

dregad

2014-12-14 19:03

developer   ~0042020

I uploaded the alternative fix I developed to fix this issue, for the record.

Related Changesets

MantisBT: master 6d0b96cb

2014-10-14 10:38:34

dregad

Details Diff
Fix timezone initialization in core.php

Prior to this, core.php would trigger a PHP warning on PHP >= 5.3 when
date_default_timezone_get() is not able to determine the timezone and
defaults to UTC (e.g. when php.ini date.timezone is not set).

The timezone is determined in the following order
1. user preferences
2. MantisBT configuration ($g_default_timezone)
3. php.ini setting (date.timezone)
4. UTC (set by date_default_timezone_get())

Fixes 0017747
mod - core.php Diff File
mod - core/date_api.php Diff File

Issue History

Date Modified Username Field Change
2014-10-13 03:15 vboctor New Issue
2014-10-13 03:30 vboctor Note Added: 0041561
2014-10-13 03:42 vboctor Assigned To => vboctor
2014-10-13 03:42 vboctor Status new => assigned
2014-10-13 10:57 dregad Relationship added related to 0017751
2014-10-14 03:56 dregad Note Added: 0041567
2014-10-15 09:11 dregad Note Added: 0041592
2014-10-26 02:00 vboctor Assigned To vboctor => dregad
2014-12-08 02:10 atrol Target Version 1.3.0-beta.1 => 1.3.0-beta.2
2014-12-13 17:18 vboctor Product Version 1.3.0dev => 1.3.0-beta.1
2014-12-14 18:40 dregad Changeset attached => MantisBT master 6d0b96cb
2014-12-14 18:40 dregad Status assigned => resolved
2014-12-14 18:40 dregad Resolution open => fixed
2014-12-14 18:40 dregad Fixed in Version => 1.3.0-beta.2
2014-12-14 19:02 dregad File Added: fix-17747-alternative.patch
2014-12-14 19:03 dregad Note Added: 0042020
2015-03-15 19:58 dregad Status resolved => closed