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 new feature for showing time-zone difference in hrs #28

Closed
wants to merge 34 commits into from

Conversation

26tanishabanik
Copy link

Added new feature for showing time-zone differences with the local and the timezone entered by the user in hours. It also shows whether the user's time is ahead or behind the timezone entered.

26tanishabanik and others added 27 commits October 3, 2021 20:56
Copy link
Owner

@ibLeDy ibLeDy left a comment

Choose a reason for hiding this comment

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

Not ready to be merged yet, as we need to make some changes to fix the logic a bit.

@@ -29,13 +31,14 @@ def __init__(
).astimezone()

self.midnights = [local_midnight]

self.diff_dict: Dict[str, str] = {}
Copy link
Owner

Choose a reason for hiding this comment

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

As this is only used inside _get_headers, this assignment should not exist.

def _get_headers(self) -> List[str]:
headers: List[str] = []
if self.difference:
self.diff_dict = self.get_difference()
Copy link
Owner

Choose a reason for hiding this comment

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

As we are not assigning self.diff_dict as an instance variable, and as we will return a list of differences, this should be just:

            differences = self.get_differences()

Comment on lines +63 to +87
def get_difference(self) -> Dict[str, str]:
fmt = '%Y-%m-%d %H:%M'
hour = datetime.now().hour
diff_dict: Dict[str, str] = {}
tz0 = datetime.fromisoformat(
(self.foreign_zones[0] + timedelta(hours=hour)).strftime(fmt),
)
for idx, midnight in enumerate(self.foreign_zones):
if idx > 0:
tz1 = datetime.fromisoformat(
(midnight + timedelta(hours=hour)).strftime(fmt),
)
if tz0 < tz1:
diff_dict[str(midnight.tzinfo).upper()] = (
' (-' + str(tz1 - tz0).split(':')[0] + ')'
)
else:
diff_dict[str(midnight.tzinfo).upper()] = (
' (+' + str(tz0 - tz1).split(':')[0] + ')'
)
else:
tz0 = datetime.fromisoformat(
(midnight + timedelta(hours=hour)).strftime(fmt),
)
return diff_dict
Copy link
Owner

Choose a reason for hiding this comment

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

I think that we need some changes here:

  • This needs to return just the difference in hours instead of the final string. As we will calculate all differences here, we should return a List[int], so the method should be called get_differences.

As fromisoformat was introduced in Python 3.7, we cannot use it, because I want to maintain backwards compatibility, so we need to take another approach and just build datetime objects like this:

tz0 = datetime(
        self.midnights[0].year,
        self.midnights[0].month,
        self.midnights[0].day,
        self.midnights[0].hour
)
tz1 = datetime(
    midnight.year,
    midnight.month,
    midnight.day,
    midnight.hour,
)

Then we can calculate the difference in hours like this:

difference = abs(int(((tz0 - tz1).seconds / 60) / 60))

Comment on lines +93 to +106
if args.difference:
returncode = ComparisonView(
args.timezone,
args.zone,
args.difference,
args.hour,
).print_table()
else:
returncode = ComparisonView(
args.timezone,
args.zone,
False,
args.hour,
).print_table()
Copy link
Owner

Choose a reason for hiding this comment

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

This logic is not necessary, as we can just always pass args.difference to ComparisonView

'-d',
'--difference',
action='store_true',
help='show difference in hrs',
Copy link
Owner

Choose a reason for hiding this comment

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

Small change here, hrs should be hours.

Comment on lines 96 to 109
if self.zone:
headers.append(f'{header} ({midnight.tzname()})')

if self.difference:
if idx > 0:
headers.append(
f'{header} ({midnight.tzname()})'
+ self.diff_dict[str(midnight.tzinfo).upper()],
)
else:
headers.append(f'{header} ({midnight.tzname()})')
else:
headers.append(f'{header} ({midnight.tzname()})')
else:
headers.append(header)
Copy link
Owner

Choose a reason for hiding this comment

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

This logic should be fixed, as we want to show the difference if --difference or -d is present, it should not depend in any other argument.

Copy link
Author

Choose a reason for hiding this comment

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

But if the zone is only not there, what difference will it compute?

Copy link
Owner

@ibLeDy ibLeDy Oct 7, 2021

Choose a reason for hiding this comment

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

The only thing that zone does is show the zone name in the column header, as the argument help says:

help='show corresponding zone name in each column',

Copy link
Author

Choose a reason for hiding this comment

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

Okay, got it, I will make the changes and get back. I shall ask questions, if needed.

Copy link
Owner

Choose a reason for hiding this comment

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

Okay, great. Of course, If you have any questions please ask! :)

@ibLeDy
Copy link
Owner

ibLeDy commented Jul 12, 2022

Closing it because of inactivity and unresolved issues

@ibLeDy ibLeDy closed this Jul 12, 2022
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 this pull request may close these issues.

None yet

2 participants