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

Dateformat decorator does not apply to DataclassWriter #51

Open
rosterloh opened this issue Jan 28, 2022 · 4 comments
Open

Dateformat decorator does not apply to DataclassWriter #51

rosterloh opened this issue Jan 28, 2022 · 4 comments
Assignees
Labels
enhancement New feature or request Evaluating Evaluating if the feature can be implemented

Comments

@rosterloh
Copy link

When reading datetime fields from a file the @dateformat() works well but when writing the same dataclass with DataclassWriter it does not write them to the file in this format.

@dfurtado
Copy link
Owner

Thanks for submitting this issue. 👍

I will investigate a good way of doing it and add a fix for it.

/Daniel

@dfurtado dfurtado self-assigned this Jan 28, 2022
@dfurtado dfurtado added enhancement New feature or request Evaluating Evaluating if the feature can be implemented labels Jan 28, 2022
@bleach31
Copy link

Thank you for a very good library. I look forward to this update.

If there is a workaround that can be done with the v1.4.0, could you please let me know?

@dfurtado
Copy link
Owner

Hi @bleach31 , thanks for your kind words.
I'm working on a solution for this and it should come out in the next release pretty soon. 🙂

@bleach31
Copy link

Thank you for your comment. I am looking forward to next release!

Anyway, here is workaround that I'm using so far.
I hope this will help someone until next release.

# Create mydatetime to override the behaviour of the initializing (new) and saving (str) functions of  datetime.

class mydatetime(datetime.datetime):
    def __new__(cls, *args, **kwargs):
        if len(args) == 1 and type(args[0]) is str:
            # Must not conflict with superclass implementations.
            # https://github.com/python/cpython/blob/d174ebe91ebc9f7388a22cc81cdc5f7be8bb8c9b/Lib/datetime.py#L1563
            return super().strptime(args[0], "%Y-%m-%d %H:%M:%S.%f")
        else:
            return super().__new__(cls, *args, *kwargs)

    def __str__(self):
        result = super().__str__()
        if int(self.microsecond) == 0:
            # In the default str function, when microsecond is 0, the . %f" is not output, so make it output.
            return result + ".0"
        else:
            return result

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

No branches or pull requests

3 participants