Skip to content

Commit

Permalink
[Form] Fixed date handling classes to use server timezone by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Schussek authored and fabpot committed Feb 16, 2011
1 parent 0a260b9 commit 9569262
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/Symfony/Component/Form/DateField.php
Expand Up @@ -38,8 +38,8 @@
* * days: An array of days for the day select tag.
*
* * format: The date format type to use for displaying the data. Default: medium.
* * data_timezone: The timezone of the data. Default: UTC.
* * user_timezone: The timezone of the user entering a new value. Default: UTC.
* * data_timezone: The timezone of the data. Default: server timezone.
* * user_timezone: The timezone of the user entering a new value. Default: server timezone.
*
*/
class DateField extends HybridField
Expand Down Expand Up @@ -113,8 +113,8 @@ protected function configure()
$this->addOption('days', range(1, 31));

$this->addOption('format', self::MEDIUM, self::$formats);
$this->addOption('data_timezone', 'UTC');
$this->addOption('user_timezone', 'UTC');
$this->addOption('data_timezone', date_default_timezone_get());
$this->addOption('user_timezone', date_default_timezone_get());

$this->formatter = new \IntlDateFormatter(
\Locale::getDefault(),
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/DateTimeField.php
Expand Up @@ -103,8 +103,8 @@ protected function configure()
$this->addOption('minutes', range(0, 59));
$this->addOption('seconds', range(0, 59));

$this->addOption('data_timezone', 'UTC');
$this->addOption('user_timezone', 'UTC');
$this->addOption('data_timezone', date_default_timezone_get());
$this->addOption('user_timezone', date_default_timezone_get());
$this->addOption('date_format', DateField::MEDIUM, self::$dateFormats);

$this->add(new DateField('date', array(
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/TimeField.php
Expand Up @@ -85,8 +85,8 @@ protected function configure()
$this->addOption('minutes', range(0, 59));
$this->addOption('seconds', range(0, 59));

$this->addOption('data_timezone', 'UTC');
$this->addOption('user_timezone', 'UTC');
$this->addOption('data_timezone', date_default_timezone_get());
$this->addOption('user_timezone', date_default_timezone_get());

if ($this->getOption('widget') == self::INPUT) {
$this->add(new TextField('hour', array('max_length' => 2)));
Expand Down
Expand Up @@ -32,8 +32,8 @@ class DateTimeToArrayTransformer extends BaseDateTimeTransformer
*/
protected function configure()
{
$this->addOption('input_timezone', 'UTC');
$this->addOption('output_timezone', 'UTC');
$this->addOption('input_timezone', date_default_timezone_get());
$this->addOption('output_timezone', date_default_timezone_get());
$this->addOption('pad', false);
$this->addOption('fields', array('year', 'month', 'day', 'hour', 'minute', 'second'));

Expand Down
Expand Up @@ -27,8 +27,8 @@ class DateTimeToStringTransformer extends Configurable implements ValueTransform
*/
protected function configure()
{
$this->addOption('input_timezone', 'UTC');
$this->addOption('output_timezone', 'UTC');
$this->addOption('input_timezone', date_default_timezone_get());
$this->addOption('output_timezone', date_default_timezone_get());
$this->addOption('format', 'Y-m-d H:i:s');

parent::configure();
Expand Down
Expand Up @@ -27,8 +27,8 @@ class DateTimeToTimestampTransformer extends Configurable implements ValueTransf
*/
protected function configure()
{
$this->addOption('input_timezone', 'UTC');
$this->addOption('output_timezone', 'UTC');
$this->addOption('input_timezone', date_default_timezone_get());
$this->addOption('output_timezone', date_default_timezone_get());

parent::configure();
}
Expand Down
66 changes: 61 additions & 5 deletions tests/Symfony/Tests/Component/Form/DateFieldTest.php
Expand Up @@ -25,7 +25,12 @@ protected function setUp()

public function testSubmit_fromInput_dateTime()
{
$field = new DateField('name', array('widget' => 'input', 'type' => DateField::DATETIME));
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'input',
'type' => DateField::DATETIME,
));

$field->submit('2.6.2010');

Expand All @@ -35,7 +40,12 @@ public function testSubmit_fromInput_dateTime()

public function testSubmit_fromInput_string()
{
$field = new DateField('name', array('widget' => 'input', 'type' => DateField::STRING));
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'input',
'type' => DateField::STRING,
));

$field->submit('2.6.2010');

Expand All @@ -45,7 +55,12 @@ public function testSubmit_fromInput_string()

public function testSubmit_fromInput_timestamp()
{
$field = new DateField('name', array('widget' => 'input', 'type' => DateField::TIMESTAMP));
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'input',
'type' => DateField::TIMESTAMP,
));

$field->submit('2.6.2010');

Expand Down Expand Up @@ -78,7 +93,11 @@ public function testSubmit_fromInput_raw()

public function testSubmit_fromChoice()
{
$field = new DateField('name', array('widget' => DateField::CHOICE));
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => DateField::CHOICE,
));

$input = array(
'day' => '2',
Expand All @@ -96,7 +115,12 @@ public function testSubmit_fromChoice()

public function testSubmit_fromChoice_empty()
{
$field = new DateField('name', array('widget' => DateField::CHOICE, 'required' => false));
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => DateField::CHOICE,
'required' => false,
));

$input = array(
'day' => '',
Expand Down Expand Up @@ -128,6 +152,8 @@ public function testSetData_differentTimezones()
public function testIsYearWithinRange_returnsTrueIfWithin()
{
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'input',
'years' => array(2010, 2011),
));
Expand All @@ -140,6 +166,8 @@ public function testIsYearWithinRange_returnsTrueIfWithin()
public function testIsYearWithinRange_returnsTrueIfEmpty()
{
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'input',
'years' => array(2010, 2011),
));
Expand All @@ -152,6 +180,8 @@ public function testIsYearWithinRange_returnsTrueIfEmpty()
public function testIsYearWithinRange_returnsTrueIfEmpty_choice()
{
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'choice',
'years' => array(2010, 2011),
));
Expand All @@ -168,6 +198,8 @@ public function testIsYearWithinRange_returnsTrueIfEmpty_choice()
public function testIsYearWithinRange_returnsFalseIfNotContained()
{
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'input',
'years' => array(2010, 2012),
));
Expand All @@ -180,6 +212,8 @@ public function testIsYearWithinRange_returnsFalseIfNotContained()
public function testIsMonthWithinRange_returnsTrueIfWithin()
{
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'input',
'months' => array(6, 7),
));
Expand All @@ -192,6 +226,8 @@ public function testIsMonthWithinRange_returnsTrueIfWithin()
public function testIsMonthWithinRange_returnsTrueIfEmpty()
{
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'input',
'months' => array(6, 7),
));
Expand All @@ -204,6 +240,8 @@ public function testIsMonthWithinRange_returnsTrueIfEmpty()
public function testIsMonthWithinRange_returnsTrueIfEmpty_choice()
{
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'choice',
'months' => array(6, 7),
));
Expand All @@ -220,6 +258,8 @@ public function testIsMonthWithinRange_returnsTrueIfEmpty_choice()
public function testIsMonthWithinRange_returnsFalseIfNotContained()
{
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'input',
'months' => array(6, 8),
));
Expand All @@ -232,6 +272,8 @@ public function testIsMonthWithinRange_returnsFalseIfNotContained()
public function testIsDayWithinRange_returnsTrueIfWithin()
{
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'input',
'days' => array(6, 7),
));
Expand All @@ -244,6 +286,8 @@ public function testIsDayWithinRange_returnsTrueIfWithin()
public function testIsDayWithinRange_returnsTrueIfEmpty()
{
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'input',
'days' => array(6, 7),
));
Expand All @@ -256,6 +300,8 @@ public function testIsDayWithinRange_returnsTrueIfEmpty()
public function testIsDayWithinRange_returnsTrueIfEmpty_choice()
{
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'choice',
'days' => array(6, 7),
));
Expand All @@ -272,6 +318,8 @@ public function testIsDayWithinRange_returnsTrueIfEmpty_choice()
public function testIsDayWithinRange_returnsFalseIfNotContained()
{
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'input',
'days' => array(6, 8),
));
Expand All @@ -284,6 +332,8 @@ public function testIsDayWithinRange_returnsFalseIfNotContained()
public function testIsPartiallyFilled_returnsFalseIfInput()
{
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'input',
));

Expand All @@ -295,6 +345,8 @@ public function testIsPartiallyFilled_returnsFalseIfInput()
public function testIsPartiallyFilled_returnsFalseIfChoiceAndCompletelyEmpty()
{
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'choice',
));

Expand All @@ -310,6 +362,8 @@ public function testIsPartiallyFilled_returnsFalseIfChoiceAndCompletelyEmpty()
public function testIsPartiallyFilled_returnsFalseIfChoiceAndCompletelyFilled()
{
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'choice',
));

Expand All @@ -325,6 +379,8 @@ public function testIsPartiallyFilled_returnsFalseIfChoiceAndCompletelyFilled()
public function testIsPartiallyFilled_returnsTrueIfChoiceAndDayEmpty()
{
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'choice',
));

Expand Down

0 comments on commit 9569262

Please sign in to comment.