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

I added on function called time_diff() on the arrow.py file to count the time difference in different time zone #1146

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions arrow/arrow.py
Expand Up @@ -1090,6 +1090,33 @@ def to(self, tz: TZ_EXPR) -> "Arrow":
fold=getattr(dt, "fold", 0),
)

def time_diff(self, tz: TZ_EXPR) -> int:
"""Returns a integer represent the time difference between two timezone. Early is positive, otherwise, negative

:param tz: A :ref:int.

Usage::

>>> utc.timeDef('US/Pacific')
8

"""

if not isinstance(tz, dt_tzinfo):
tz = parser.TzinfoParser.parse(tz)

dt = self._datetime.astimezone(tz)

if dt.date == self._datetime.date:
return self._datetime.hour - dt.hour
elif dt.date > self._datetime.date:
return self._datetime.hour - dt.hour + 24
else:
return self._datetime.hour - dt.hour - 24


return self.datetime.hour - dt.hour

# string output and formatting

def format(
Expand Down