Project

General

Profile

Actions

Bug #72878

closed

wrong datetime handling, they are not UTC in db

Added by Oliver Pfaff about 8 years ago. Updated almost 7 years ago.

Status:
Closed
Priority:
Should have
Assignee:
-
Category:
-
Target version:
-
Start date:
2016-01-21
Due date:
% Done:

0%

Estimated time:
TYPO3 Version:
6.2
PHP Version:
5.5
Tags:
Complexity:
Is Regression:
No
Sprint Focus:

Description

Hello folks,
we have a problem with the handling of datetimes. Actually the datetime are saved with the actual timezone and not UTC.

The problem seems in the DataHandler.php

if ($isDateOrDateTimeField) {
   // Convert the timestamp back to a date/time
   $res['value'] = $res['value'] ? date($format, $res['value']) : $emptyValue;
}

with the date method we add again the timezone previously removed here:


public function checkValue_input_Eval($value, $evalArray, $is_in) {
        $res = array();
        $newValue = $value;
        $set = TRUE;
        foreach ($evalArray as $func) {
            switch ($func) {
                case 'int':

                case 'year':

                case 'time':

                case 'timesec':
                    $value = (int)$value;
                    break;
                case 'date':

                case 'datetime':
                    $value = (int)$value;
                    if ($value > 0 && !$this->dontProcessTransformations) {
                        $value -= date('Z', $value);
                    }
                    break;

so the datetime will be saved with their local timezone.

This Problem is pretty heavy because extbase expect datetime to be UTC.

protected function mapDateTime($value, $storageFormat = NULL, $targetType = 'DateTime') {
        if (empty($value) || $value === '0000-00-00' || $value === '0000-00-00 00:00:00') {
            // 0 -> NULL !!!
            return NULL;
        } elseif ($storageFormat === 'date' || $storageFormat === 'datetime') {
            // native date/datetime values are stored in UTC
            $utcTimeZone = new \DateTimeZone('UTC');
            $utcDateTime = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($targetType, $value, $utcTimeZone);
            $currentTimeZone = new \DateTimeZone(date_default_timezone_get());
            return $utcDateTime->setTimezone($currentTimeZone);
        } else {
            return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($targetType, date('c', $value));
        }
    }

I have fixed this you can see my changes here:
[[https://github.com/Xippo/TYPO3.CMS/pull/1/files]]

With this fix the datetimes are stored as UTC in the DB. The backend shows the right dates and times and Extbase give you datetimes with the right time. Before i had my timezone offset added on the DB time.

I don't have tested this in 7.6 but i think there is the same.

I have discovered another bug in Extbase, related too datetimes.
If you do somthing like that:

public function listAction() {
        $tests = $this->testRepository->findAll();
        /*
        /** @var \TEST\Test\Domain\Model\Test $object */
        $object = $tests->getFirst();

        $datetime = $object->getDatetime();
        $datetime->add(new \DateInterval('P0Y0DT0H1M'));
        $object->setDatetime($datetime);
        $this->testRepository->update($object);

        $this->view->assign('tests', $tests);
    }

Extbase will add every time the timezone offset, like you can see above in the mapDateTime function, but Extbase don't remove the offset on the way in the database so in my case the code above adds every time 1h when the list view is called.

I don't now if it is a bug but people have to now this because they have to set the datetimes on UTC by it self. Or we change the entire handling in TYPO3 and Extbase and store datetime with the local timezone but this don't seems to be a good idea.


Related issues 5 (3 open2 closed)

Related to TYPO3 Core - Bug #68849: Unstable persistence handling of DateTime (don't get what you set)Closed2015-08-07

Actions
Related to TYPO3 Core - Bug #68651: Datetime() properties have wrong timezoneAcceptedAndreas Wolf2015-07-30

Actions
Related to TYPO3 Core - Bug #64953: dbType date and datetime fields are saved in wrong timezoneClosed2015-02-10

Actions
Related to TYPO3 Core - Feature #61110: Support for timezones in all date fields in TYPO3 BENew2014-08-21

Actions
Related to TYPO3 Core - Bug #79613: Saving wrong Date into DB if Field is of type DATENew2017-02-03

Actions
Actions

Also available in: Atom PDF