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

Add quarter granularity #1048

Merged
merged 5 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ repos:
hooks:
- id: isort
- repo: https://github.com/asottile/pyupgrade
rev: v2.29.0
rev: v2.23.3
Copy link
Member

Choose a reason for hiding this comment

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

Rebase this file from master since it was updated in a recent commit.

hooks:
- id: pyupgrade
args: [--py36-plus]
Expand All @@ -34,17 +34,18 @@ repos:
- id: rst-directive-colons
- id: rst-inline-touching-normal
- repo: https://github.com/psf/black
rev: 21.9b0
rev: 21.7b0
hooks:
- id: black
args: [--safe, --quiet, --target-version=py36]
- repo: https://github.com/pycqa/flake8
rev: 4.0.1
additional_dependencies: [regex==2020.1.8]
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is necessary if the CI build for this change passes when the PR is submitted. The lint error you are seeing must be a local caching issue. Try deleting the pre-commit cache: rm -rf /Users/ali/.cache/pre-commit/.

- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
additional_dependencies: [flake8-bugbear]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v0.910-1'
rev: 'v0.910'
hooks:
- id: mypy
additional_dependencies: [types-python-dateutil]
10 changes: 8 additions & 2 deletions arrow/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"day",
"week",
"month",
"quarter",
"year",
]

Expand Down Expand Up @@ -132,6 +133,7 @@ class Arrow:
_SECS_PER_DAY: Final[int] = 60 * 60 * 24
_SECS_PER_WEEK: Final[int] = 60 * 60 * 24 * 7
_SECS_PER_MONTH: Final[float] = 60 * 60 * 24 * 30.5
_SECS_PER_QUARTER: Final[float] = 60 * 60 * 24 * 30.5 * 3
_SECS_PER_YEAR: Final[int] = 60 * 60 * 24 * 365

_SECS_MAP: Final[Mapping[TimeFrameLiteral, float]] = {
Expand All @@ -141,6 +143,7 @@ class Arrow:
"day": _SECS_PER_DAY,
"week": _SECS_PER_WEEK,
"month": _SECS_PER_MONTH,
"quarter": _SECS_PER_QUARTER,
"year": _SECS_PER_YEAR,
}

Expand Down Expand Up @@ -1245,12 +1248,14 @@ def humanize(
delta = sign * delta_second / self._SECS_PER_WEEK
elif granularity == "month":
delta = sign * delta_second / self._SECS_PER_MONTH
elif granularity == "quarter":
delta = sign * delta_second / self._SECS_PER_QUARTER
elif granularity == "year":
delta = sign * delta_second / self._SECS_PER_YEAR
else:
raise ValueError(
"Invalid level of granularity. "
"Please select between 'second', 'minute', 'hour', 'day', 'week', 'month' or 'year'."
"Please select between 'second', 'minute', 'hour', 'day', 'week', 'month', 'quarter' or 'year'."
)

if trunc(abs(delta)) != 1:
Expand All @@ -1275,6 +1280,7 @@ def gather_timeframes(_delta: float, _frame: TimeFrameLiteral) -> float:
delta = float(delta_second)
frames: Tuple[TimeFrameLiteral, ...] = (
"year",
"quarter",
"month",
"week",
"day",
Expand All @@ -1288,7 +1294,7 @@ def gather_timeframes(_delta: float, _frame: TimeFrameLiteral) -> float:
if len(timeframes) < len(granularity):
raise ValueError(
"Invalid level of granularity. "
"Please select between 'second', 'minute', 'hour', 'day', 'week', 'month' or 'year'."
"Please select between 'second', 'minute', 'hour', 'day', 'week', 'month', 'quarter' or 'year'."
)

return locale.describe_multi(timeframes, only_distance=only_distance)
Expand Down
6 changes: 6 additions & 0 deletions arrow/locales.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
"weeks",
"month",
"months",
"quarter",
"quarters",
"year",
"years",
]
Expand Down Expand Up @@ -98,6 +100,8 @@ class Locale:
"weeks": "",
"month": "",
"months": "",
"quarter": "",
"quarters": "",
"year": "",
"years": "",
}
Expand Down Expand Up @@ -314,6 +318,8 @@ class EnglishLocale(Locale):
"weeks": "{0} weeks",
"month": "a month",
"months": "{0} months",
"quarter": "a quarter",
"quarters": "{0} quarters",
"year": "a year",
"years": "{0} years",
}
Expand Down
2 changes: 2 additions & 0 deletions tests/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1935,6 +1935,8 @@ def test_granularity(self):
assert later506.humanize(self.now, granularity="week") == "in 82 weeks"
assert self.now.humanize(later506, granularity="month") == "18 months ago"
assert later506.humanize(self.now, granularity="month") == "in 18 months"
assert self.now.humanize(later506, granularity="quarter") == "6 quarters ago"
jadchaar marked this conversation as resolved.
Show resolved Hide resolved
assert later506.humanize(self.now, granularity="quarter") == "in 6 quarters"
assert self.now.humanize(later506, granularity="year") == "a year ago"
assert later506.humanize(self.now, granularity="year") == "in a year"

Expand Down