Skip to content

Commit

Permalink
Add quarter granularity
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammed Ali Zubair authored and Mohammed Ali Zubair committed Oct 16, 2021
1 parent c35134b commit d727d5b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
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
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]
- 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"
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

0 comments on commit d727d5b

Please sign in to comment.