Skip to content

Date deserialization (DateTime object) #582

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

Closed
bobvandevijver opened this issue Jun 1, 2017 · 6 comments
Closed

Date deserialization (DateTime object) #582

bobvandevijver opened this issue Jun 1, 2017 · 6 comments

Comments

@bobvandevijver
Copy link
Contributor

I'm having an issue with the deserialization of a DateTime object which only contains a date: the time is set to zero, and is not serialized. I have the following property:

  /**
   * @var DateTime
   *
   * @ORM\Column(name="signin_start", type="date")
   * @JMS\Expose()
   * @JMS\Type("DateTime<'d-m-Y'>")
   * @Assert\NotNull
   * @Assert\Date
   */
  protected $signin_start;

When I deserialize this, I will get a DateTime object which has the current time set, due to this line (L184) in the DateHandler:

$datetime = \DateTime::createFromFormat($format, (string)$data, $timezone);

What would be best practice to resolve this issue? Does adding a timezone work, or should I serialize the complete DateTime class including the zero time, to ensure correct functioning (which adds extra data). Or should we adjust the serializer to set the time to zero when it is not given?

@goetas
Copy link
Collaborator

goetas commented Jun 1, 2017

Interesting... PHP behaves differently when using the constructor and createFromFormat

https://3v4l.org/W7fgD

@bobvandevijver
Copy link
Contributor Author

bobvandevijver commented Jun 1, 2017

Right. From the PHP docs:

If format does not contain the character ! then portions of the generated time which are not specified in format will be set to the current system time.
If format contains the character !, then portions of the generated time not provided in format, as well as values to the left-hand side of the !, will be set to corresponding values from the Unix epoch.

So basically, if I set the format to contain the exclamation mark, it should be fixed for my case. Should we do this by default?

There is also a pipe (d-m-Y|), which should reset the not-set fields.

@bobvandevijver
Copy link
Contributor Author

Okay, so it works when I use one of those specific characters during deserialization only, but as the type is also used during serialization, it fails with an unrecognized format (as the output becomes 01-06-2017|). Any suggestions?

@goetas goetas self-assigned this Jun 2, 2017
@goetas
Copy link
Collaborator

goetas commented Jun 17, 2017

is not the first time I see this type of problem... basically the root cause is that the dateformat can be different when serializing and when deserializing...

PR with to allow serialization and (optional) deserialization format are welcome! :)

@bobvandevijver
Copy link
Contributor Author

I've been looking into the optional deserialization parameter for the annotation, which should be fairly simple to add when using the existing annotation driver:

@JMS\Type("DateTime<'d-m-Y','','|d-m-Y'>")

Then you will always need to supply the timezone, when you want to supply the deserialization format. I would like to put instead of the timezone, but yeah, BC-breaking changes 😄

What is you opinion on adding a new type, specific for deserialization? So the @DeserializationType annotation, which is used only during deserialization (if given), with the same behavior as the @Type annotation?

@goetas
Copy link
Collaborator

goetas commented Jun 23, 2017

solved with schmittjoh/serializer#788

@goetas goetas closed this as completed Jun 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants