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

Wrong date string format. #17

Closed
ghost opened this issue Oct 14, 2021 · 3 comments
Closed

Wrong date string format. #17

ghost opened this issue Oct 14, 2021 · 3 comments

Comments

@ghost
Copy link

ghost commented Oct 14, 2021

Hi, thank you for this library.

I am trying to read an sdmx file (at this url: 'https://www.i14y.admin.ch/api/CodeLists/CL_HGDE_KT/exports/SDMX-ML/2.1?annotations=false') and I get errors due to the date string format. The library only allows the format %Y-%m-%d but I have date time format of the type 1978-12-31T23:00:00 in my file. Would it be possible to update the library to allow this format ?

Something like this in 'set_date_from_string' in model/utils.py

def set_date_from_string(value: str, format_: str = "%Y-%m-%dT%H:%M:%S"):
    """Generic function to format a string to datetime

    Args: value: The value to be validated.
    format_: A regex pattern to validate if the string has a specific format

    Returns:
        A datetime object

    Raises:
        ValueError: If the value violates the format constraint.
    """

    if value is None:
        return None
    
    for fmt in (format_, "%Y-%m-%d", "%Y-%m-%dT%H:%M:%S"):
        try:
            return datetime.strptime(value, fmt)
        except ValueError:
            pass
        
    raise ValueError(f"Wrong date string format. The formats {format_} "
                     f"or %Y-%m-%d or %Y-%m-%dT%H:%M:%S "
                     f"should be followed. {str(value)} passed")

Thank you :)

@javihern98
Copy link
Collaborator

Hi @paulineml . Thanks for using our library. We will include this changes as soon as possible.

Lines 618 and 624 of model/base.py should also be changed to include more formats. We need to check with the schemas all the possibilities:

    @validFrom.setter
    def validFrom(self, value):
        self._validFrom = set_date_from_string(value)

        #  self._validFrom = value

    @validTo.setter
    def validTo(self, value):
        self._validTo = set_date_from_string(value)
        # self._validTo = value

@ghost
Copy link
Author

ghost commented Oct 26, 2021

@javihern98 great, thank you

@javihern98
Copy link
Collaborator

Hi @paulineml . Version 1.2 of the library supports now all formats. Sorry for the delay in publishing it as we have been redesigning the whole reading and writing processes to adapt the library to SDMX 3.0 in the near future.

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

No branches or pull requests

1 participant