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

Added get_relative_delta and get_tzinfo in datetime API #2310

Merged
merged 11 commits into from
May 17, 2023
Merged

Conversation

xispa
Copy link
Member

@xispa xispa commented May 16, 2023

Description of the issue/feature this PR addresses

This Pull Request adds the function get_relative_delta in dtime api

Current behavior before PR

There is no handy function to get the relative delta between two dates

Desired behavior after PR is merged

There is a handy function to get the relative delta between two dates

--
I confirm I have tested this PR thoroughly and coded it according to PEP8
and Plone's Python styleguide standards.

@xispa xispa requested a review from ramonski May 16, 2023 10:51
Copy link
Contributor

@ramonski ramonski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, the dateutil seems to provide some mighty functionalities.
Is it a dependency of Plone or who actually requires it?

Only some minor changes required before we can merge it.

def get_relative_delta(from_dtime, to_dtime=None):
"""Returns the relative delta between two datetimes. If to_dtime is None,
compares current datetime with from_dtime
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind to provide the docstrings in API functions in the same way we did for the to_localized_time, e.g.:

def get_relative_delta(from_dtime, to_dtime=None):
    """Calculate the relative delta between two dates
    
    If `to_dtime` is None, the current datetime is used.

    :param from_dtime:
    :type from_dtime: str/datetime/DateTime
    :param to_dtime:  
    :type to_dtime: str/datetime/DateTime
    :returns: relativedelta object, e.g. `relativedelta(hours=+3)`
    :rtype: object
    """

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with 0aed612

from_dtime = to_dt(from_dtime)
to_dtime = to_dt(to_dtime)
if not all([from_dtime, to_dtime]):
return None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better raise an Exception if the parameters can not be converted to valid dates.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with ec73794

try:
return pytz.timezone(get_timezone(dt))
except pytz.UnknownTimeZoneError:
return default
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make this an own API function (outside of this function)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with 5e4f661

@xispa xispa changed the title Added get_relative_delta in datetime API Added get_relative_delta and get_tzinfo in datetime API May 17, 2023
@ramonski ramonski merged commit 8cc6ed2 into 2.x May 17, 2023
@ramonski ramonski deleted the api-dtime-extras branch May 17, 2023 07:25
@xispa
Copy link
Member Author

xispa commented May 18, 2023

Cool, the dateutil seems to provide some mighty functionalities. Is it a dependency of Plone or who actually requires it?

Is used by some plone.* packages, like plone.event, plone.resource, plone.app.caching, etc. It is even used in senaite.databox

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

Successfully merging this pull request may close these issues.

2 participants