From 35800836dd315e12e7d552ee0f64519b9f60c693 Mon Sep 17 00:00:00 2001 From: Vincent Sels Date: Mon, 16 Jan 2012 21:29:17 +0100 Subject: [PATCH] Adds support for date format d/m/Y When the date format is set to 'd/m/Y', the php strtotime function does not work correctly, it will try to parse the string as m/d/Y instead. The new function 'strtotime_safe' will check the $g_short_date_format setting, and if set to 'd/m/Y' it will fix this. --- bug_report.php | 9 ++------- bug_update.php | 12 ++---------- core/bug_api.php | 2 +- core/date_api.php | 23 +++++++++++++++++++++++ core/version_api.php | 2 +- 5 files changed, 29 insertions(+), 19 deletions(-) diff --git a/bug_report.php b/bug_report.php index 32e86f2..18d1ff6 100644 --- a/bug_report.php +++ b/bug_report.php @@ -77,14 +77,9 @@ $t_bug_data->status = config_get( 'bug_submit_status' ); $t_bug_data->summary = gpc_get_string( 'summary' ); $t_bug_data->description = gpc_get_string( 'description' ); - $t_bug_data->steps_to_reproduce = gpc_get_string( 'steps_to_reproduce', config_get( 'default_bug_steps_to_reproduce' ) ); + $t_bug_data->steps_to_reproduce = gpc_get_string( 'steps_to_reproduce', config_get( 'default_bug_steps_to_reproduce' ) ); $t_bug_data->additional_information = gpc_get_string( 'additional_info', config_get ( 'default_bug_additional_info' ) ); - $t_bug_data->due_date = gpc_get_string( 'due_date', ''); - if ( is_blank ( $t_bug_data->due_date ) ) { - $t_bug_data->due_date = date_get_null(); - } else { - $t_bug_data->due_date = $t_bug_data->due_date; - } + $t_bug_data->due_date = strtotime_safe( gpc_get_string( 'due_date', null ) ); $f_file = gpc_get_file( 'file', null ); /** @todo (thraxisp) Note that this always returns a structure */ # size = 0, if no file diff --git a/bug_update.php b/bug_update.php index 6b440c4..ff6f3ce 100644 --- a/bug_update.php +++ b/bug_update.php @@ -73,22 +73,14 @@ $t_bug_data->platform = gpc_get_string( 'platform', $t_bug_data->platform ); $t_bug_data->version = gpc_get_string( 'version', $t_bug_data->version ); $t_bug_data->build = gpc_get_string( 'build', $t_bug_data->build ); - $t_bug_data->fixed_in_version = gpc_get_string( 'fixed_in_version', $t_bug_data->fixed_in_version ); + $t_bug_data->fixed_in_version = gpc_get_string( 'fixed_in_version', $t_bug_data->fixed_in_version ); $t_bug_data->view_state = gpc_get_int( 'view_state', $t_bug_data->view_state ); $t_bug_data->summary = gpc_get_string( 'summary', $t_bug_data->summary ); - $t_due_date = gpc_get_string( 'due_date', null ); + $t_bug_data->due_date = strtotime_safe( gpc_get_string( 'due_date', null ) ); if( access_has_project_level( config_get( 'roadmap_update_threshold' ), $t_bug_data->project_id ) ) { $t_bug_data->target_version = gpc_get_string( 'target_version', $t_bug_data->target_version ); } - - if( $t_due_date !== null) { - if ( is_blank ( $t_due_date ) ) { - $t_bug_data->due_date = 1; - } else { - $t_bug_data->due_date = strtotime( $t_due_date ); - } - } $t_bug_data->description = gpc_get_string( 'description', $t_bug_data->description ); $t_bug_data->steps_to_reproduce = gpc_get_string( 'steps_to_reproduce', $t_bug_data->steps_to_reproduce ); diff --git a/core/bug_api.php b/core/bug_api.php index fc62c47..8760f31 100644 --- a/core/bug_api.php +++ b/core/bug_api.php @@ -171,7 +171,7 @@ class BugData { break; case 'due_date': if ( !is_numeric( $value ) ) { - $value = strtotime($value); + $value = strtotime_safe( $value ); } break; } diff --git a/core/date_api.php b/core/date_api.php index 7f6b56e..5be0409 100644 --- a/core/date_api.php +++ b/core/date_api.php @@ -307,3 +307,26 @@ function date_finish_calendar( $p_field_name, $p_button_name ) { echo "\n"; } } + +/** + * Fixes 0013332: Due date not saved successfully when date-format is set to 'd/m/Y' + * The normal strtotime can't handle the format d/m/Y, since it will interpret + * it as m/d/Y. To determine whether this is the case, this function looks + * at the short_date_format setting. + * Also, if the passed argument is null and parameter $p_allow_null is false (default), + * date_get_null() is returned. + * @param string $p_date + * @param bool $p_allow_null + * @return number + */ +function strtotime_safe( $p_date, $p_allow_null = false ) { + if( !$p_allow_null && ( $p_date == null || is_blank ( $p_date ) || date_is_null( $p_date ) ) ) { + return date_get_null(); + } + + if ( config_get( 'short_date_format' ) == 'd/m/Y' ) { + return strtotime( str_replace( '/', '-', $p_date ) ); + } else { + return strtotime( $p_date ); + } +} diff --git a/core/version_api.php b/core/version_api.php index c716510..471d20e 100644 --- a/core/version_api.php +++ b/core/version_api.php @@ -49,7 +49,7 @@ class VersionData { if( $value == '' ) { $value = date_get_null(); } else { - $value = strtotime( $value ); + $value = strtotime_safe( $value ); if ( $value === false ) { trigger_error( ERROR_INVALID_DATE_FORMAT, ERROR ); } -- 1.7.6.msysgit.0