Skip to content
Snippets Groups Projects

Issue #3420126: Timezone is applied incorrectly to min and max values

Open Himanshu Jhaloya requested to merge issue/smart_date-3420126:3420126-timezone into 4.0.x
1 file
+ 51
42
Compare changes
  • Side-by-side
  • Inline
@@ -125,53 +125,62 @@ class Date extends CoreDate implements ContainerFactoryPluginInterface {
* Override parent method, which deals with dates as integers.
*/
protected function opBetween($field) {
$timezone = $this->getTimezone();
$granularity = $this->options['value']['granularity'];
// Convert value to DateTimePlus for additional processing.
$a = new DateTimePlus($this->value['min'], new \DateTimeZone($timezone));
$b = new DateTimePlus($this->value['max'], new \DateTimeZone($timezone));
$a = new DateTimePlus($this->value['min']);
$b = new DateTimePlus($this->value['max']);
// Preserve the timezone of min and max values.
$timezoneA = $a->getTimezone()->getName();
$timezoneB = $b->getTimezone()->getName();
// Ensure that both values are in the same timezone.
if ($timezoneA !== $timezoneB) {
throw new \Exception("Timezones for min and max values must be the same.");
}
// Granularity requires some conversion.
if ($granularity != 'second') {
$min = [
'year' => $a->format('Y'),
'month' => $a->format('n'),
'day' => $a->format('j'),
'hour' => $a->format('G'),
'minute' => $a->format('i'),
'second' => $a->format('s'),
];
$max = [
'year' => $b->format('Y'),
'month' => $b->format('n'),
'day' => $b->format('j'),
'hour' => $b->format('G'),
'minute' => $b->format('i'),
'second' => $b->format('s'),
];
switch ($granularity) {
case 'year':
$min['month'] = '01';
$max['month'] = '12';
$max['day'] = '31';
case 'month':
$min['day'] = '01';
if ($granularity != 'year') {
$max['day'] = $b->format('t');
}
case 'day':
$min['hour'] = '00';
$max['hour'] = '23';
case 'hour':
$min['minute'] = '00';
$max['minute'] = '59';
case 'minute':
$min['second'] = '00';
$max['second'] = '59';
}
// Update the range with our altered values.
$a = $a->createFromArray($min);
$b = $b->createFromArray($max);
$min = [
'year' => $a->format('Y'),
'month' => $a->format('n'),
'day' => $a->format('j'),
'hour' => $a->format('G'),
'minute' => $a->format('i'),
'second' => $a->format('s'),
];
$max = [
'year' => $b->format('Y'),
'month' => $b->format('n'),
'day' => $b->format('j'),
'hour' => $b->format('G'),
'minute' => $b->format('i'),
'second' => $b->format('s'),
];
switch ($granularity) {
case 'year':
$min['month'] = '01';
$max['month'] = '12';
$max['day'] = '31';
case 'month':
$min['day'] = '01';
if ($granularity != 'year') {
$max['day'] = $b->format('t');
}
case 'day':
$min['hour'] = '00';
$max['hour'] = '23';
case 'hour':
$min['minute'] = '00';
$max['minute'] = '59';
case 'minute':
$min['second'] = '00';
$max['second'] = '59';
}
// Update the range with our altered values.
$a = $a->createFromArray($min);
$b = $b->createFromArray($max);
}
// This is safe because we forced the provided values to DateTimePlus.
Loading