Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rationale of this project #6

Closed
DGCarramona opened this issue Nov 5, 2020 · 2 comments
Closed

Rationale of this project #6

DGCarramona opened this issue Nov 5, 2020 · 2 comments
Labels
question Further information is requested

Comments

@DGCarramona
Copy link

Hello ! I just found this project. After reading the code, I understand that it take a datetime with any timezone and convert it to an utc datetime. But I don't understand why do this this way instead of simply using datetimetz? I guess if this lib exists, it's because the need exists but really I can't figure it 😃

@simPod
Copy link
Owner

simPod commented Nov 5, 2020

Hello, I believe it is because in most cases we always store UTC date so there's no need to bother with TZ then. With this type we can safely use TIMESTAMP type in postgres.

@simPod simPod added the question Further information is requested label Nov 5, 2020
@sjerdo
Copy link

sjerdo commented Nov 5, 2020

Hi! I recently started using this library to ensure the stored dates in a MySQL/MariaDB database are stored in UTC, unlike Doctrine DBAL DateTimeTypeTz which stores dates in the timezone set for the DateTime object. FYI: MySQL doesn't support storing timezone information for dates.

Previously the dates in my application were stored in UTC+1 or UTC+2 (for Daylight Saving Time). This raised some issues (e.g. duplicate datetimes), since my application should show data in chronological order.

Using this library I can store the exact UTC date at which moment an event occurred. For some dates I store the original timezone in a separate field for display purposes only.

Eg. as entity:

<?php

/**
 * @ORM\Entity
 */
class User {

   /**
     * The date of registration stored in UTC.
     *
     * @var \DateTime|null
     *
     * @ORM\Column(type="datetime", name="registration_date", nullable=true)
     */
    private $registrationDate;

    /**
     * The timezone of the registration date (eg. Europe/Amsterdam).
     *
     * @var string|null
     *
     * @ORM\Column(type="string", name="registration_date_tz", length=32, nullable=true)
     */
    private $registrationDateTimeZone;

    /**
     * @param \DateTimeInterface $registrationDate
     */
    public function setRegistrationDate(DateTimeInterface $registrationDate): void
    {
        $this->registrationDate = $registrationDate;
        $this->registrationDateTimeZone = $registrationDate->getTimezone()->getName();
    }

    /**
     * @return \DateTime
     */
    public function getRegistrationDate(): ?DateTime
    {
        if ($this->registrationDate === null) {
            return null;
        }

        $tz = new DateTimeZone($this->registrationDateTimeZone);
        return $this->registrationDate->setTimezone($tz);
    }
}

@simPod simPod closed this as completed in 8f75a75 Jan 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants