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

Serialize DateTimeInterface #1799

Open
martinstuecklschwaiger opened this issue Jan 23, 2025 · 1 comment · May be fixed by #1803
Open

Serialize DateTimeInterface #1799

martinstuecklschwaiger opened this issue Jan 23, 2025 · 1 comment · May be fixed by #1803

Comments

@martinstuecklschwaiger
Copy link
Contributor

Problem Statement

I want to serialize date objects (DateTimeInterface to be concrete).

They are used by nearly every PHP application. Serializing them is actually very simple, and sending them doesn't cause much overhead.

This is how this would look like in Sentry:
Image

Compare it to how these are currently displayed:
Image

Solution Brainstorm

In AbstractSerializer::serializeRecursively() we could add the following:

            if (\is_object($value)) {

                foreach ($classSerializers as $classSerializer) {
                   ...
                }

                if ($value instanceof \DateTimeInterface) {
                    return $this->serializeDate($value);
                }
    protected function serializeDate(\DateTimeInterface $date): string
    {
      $hasMicroseconds = $date->format("u") !== "000000";
      $formatted = $hasMicroseconds ? $date->format("Y-m-d H:i:s.u") : $date->format("Y-m-d H:i:s");
      $className = get_class($date);

      if ($date->getTimezone()) {
        // Also add timezone information
      }

      return "$className($formatted)";
    }
@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Jan 23, 2025
@cleptric
Copy link
Member

Would you be willing to open a PR?

@martinstuecklschwaiger martinstuecklschwaiger linked a pull request Jan 30, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

2 participants