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

Exception when calling DateTime.astimezone #188

Closed
mattmess1221 opened this issue Apr 12, 2022 · 1 comment · Fixed by #189
Closed

Exception when calling DateTime.astimezone #188

mattmess1221 opened this issue Apr 12, 2022 · 1 comment · Fixed by #189

Comments

@mattmess1221
Copy link

When attempting to use tomlkit.items.DateTime.astimezone(), a TypeError is raised.

    date = date.astimezone()
TypeError: __new__() missing 2 required positional arguments: 'trivia' and 'raw'

The current workaround is to reinitialize the object using the datetime module.

date = datetime.combine(date.date(), date.time())
date = date.astimezone()
@mattmess1221
Copy link
Author

mattmess1221 commented Apr 15, 2022

I've looked at the source more closely, and I've determined this is caused because DateTime does not implement replace and astimezone. I believe this is an issue for the same reason add and sub need to be implemented.

I suggest an implementation similar to __add__ and __sub__ for astimezone and replace. replace() should also be implemented in Time and Date.

The following tests were used to test this solution.

import pytz

import tomlkit


def test_datetime_astimezone():
    # US/Pacific time zone
    assert tomlkit.datetime("2022-12-25 12:23:34-05:00").astimezone(pytz.timezone("US/Pacific")).as_string() == "2022-12-25T09:23:34-08:00"


def test_datetime_astimezone_noargs():
    # Use the local timezone
    tomlkit.datetime("2022-12-25 12:23:34-05:00").astimezone()


def test_datetime_replace():
    assert tomlkit.datetime("2022-12-25 12:23:34-05:00").replace(year=2021).as_string() == "2021-12-25T12:23:34-05:00"

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

Successfully merging a pull request may close this issue.

1 participant