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

DateTime and Date Converters #366

Closed
captaincolonelfox opened this issue Dec 24, 2020 · 4 comments
Closed

DateTime and Date Converters #366

captaincolonelfox opened this issue Dec 24, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@captaincolonelfox
Copy link

I was trying to create such dataclass

@dataclass
class GetByDate:
    class Meta:
        namespace = NAMESPACE

    date: datetime = field(
        default=None,
        metadata={
            "name": "Date",
            "type": "Element"
        }
    )

But when I trying to proceed request with date in it I got a warning:

/xsdata/formats/converter.py:123: ConverterWarning: No converter registered for `<class 'datetime.datetime'>`
  warnings.warn(f"No converter registered for `{data_type}`", ConverterWarning)

And maybe it would be nice to have datetime converter which will try to parse from most common datetimes formats such as

for datetimes:

  • %y-%m-%dT%H:%M:%S
  • %y-%m-%d %H:%M:%S
  • ...and so on

for dates:

  • %y-%m-%d
  • %d-%m-%y
  • ...and so on

And by default it can serialize datetimes to %y-%m-%dT%H:%M:%S or a local time format.
So a user can specify in a serializer config which format he wants to have

@tefra
Copy link
Owner

tefra commented Dec 24, 2020

I have avoided mapping the xs:date, xs:dateTime, xs:time to python datetime for some time now because of all the possible formats, but it's definitely in my todos.

For now there is a way to register a custom converter see here and the full script here

@tefra tefra added the enhancement New feature or request label Dec 24, 2020
@captaincolonelfox
Copy link
Author

Awesome, will look forward for this changes!

because of all the possible formats

Not sure if it will be useful for you, but you can do try: ... except: continue on a list of different formats and return the first succeeded

And thank you, I didn't know you can implement your own converter. I guess I will do it for now

@tefra
Copy link
Owner

tefra commented Jan 6, 2021

Hey @captaincolonelfox

Take a look here, I added support for custom date/time formats and standard iso 8601 xml date/times

It will be included in the next release

@tefra tefra closed this as completed Jan 6, 2021
@captaincolonelfox
Copy link
Author

@tefra Awesome, thank you for your work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants