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

Support for date segment to take a timezone argument #1337

Closed
Closed
Show file tree
Hide file tree
Changes from 4 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
10 changes: 8 additions & 2 deletions powerline/segments/common/time.py
Expand Up @@ -3,21 +3,27 @@

from datetime import datetime

from pytz import (timezone, utc)
Copy link
Contributor

Choose a reason for hiding this comment

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

It is not correct to use parenthesis here.

Copy link
Author

Choose a reason for hiding this comment

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

I'm not a python guy, mind mentioning the correct usage?

Copy link
Contributor

Choose a reason for hiding this comment

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

It is not technically incorrect. Just you can drop the parenthesis (leaving everything else as-is) and according to the current style you should drop it unless you are going to have multiline import.


def date(pl, format='%Y-%m-%d', istime=False):

def date(pl, format='%Y-%m-%d', istime=False, tz=None):
'''Return the current date.

:param str format:
strftime-style date format string
:param bool istime:
If true then segment uses ``time`` highlight group.
:param string tz:
Timezone parseable by pytz
Copy link
Contributor

Choose a reason for hiding this comment

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

Sentence does not end with a dot.


Divider highlight group used: ``time:divider``.

Highlight groups used: ``time`` or ``date``.
'''

tztime = utc.localize(datetime.utcnow()).astimezone(timezone(tz)) if tz else datetime.now()
return [{
'contents': datetime.now().strftime(format),
'contents': tztime.strftime(format),
'highlight_groups': (['time'] if istime else []) + ['date'],
'divider_highlight_group': 'time:divider' if istime else None,
}]
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Expand Up @@ -124,7 +124,9 @@ def get_version():
packages=find_packages(exclude=('tests', 'tests.*')),
include_package_data=True,
zip_safe=False,
install_requires=[],
install_requires=[
'pytz'
],
Copy link
Contributor

Choose a reason for hiding this comment

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

You should make this optional.

Copy link
Author

Choose a reason for hiding this comment

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

Are you saying making the feature altogether optional if they have pytz installed? That makes sense.

extras_require={
'docs': [
'Sphinx',
Expand Down
8 changes: 5 additions & 3 deletions tests/test_segments.py
Expand Up @@ -684,9 +684,11 @@ class TestTime(TestCommon):

def test_date(self):
pl = Pl()
with replace_attr(self.module, 'datetime', Args(now=lambda: Args(strftime=lambda fmt: fmt))):
self.assertEqual(self.module.date(pl=pl), [{'contents': '%Y-%m-%d', 'highlight_groups': ['date'], 'divider_highlight_group': None}])
self.assertEqual(self.module.date(pl=pl, format='%H:%M', istime=True), [{'contents': '%H:%M', 'highlight_groups': ['time', 'date'], 'divider_highlight_group': 'time:divider'}])
with replace_attr(self.module, 'datetime', Args(now=lambda: Args(strftime=lambda fmt: fmt), utcnow=lambda: Args(strftime=lambda fmt: fmt))):
with replace_attr(self.module, 'utc', Args(localize=lambda lcl: Args(astimezone=lambda tz: Args(strftime=lambda fmt: fmt)))):
self.assertEqual(self.module.date(pl=pl), [{'contents': '%Y-%m-%d', 'highlight_groups': ['date'], 'divider_highlight_group': None}])
self.assertEqual(self.module.date(pl=pl, format='%H:%M', istime=True), [{'contents': '%H:%M', 'highlight_groups': ['time', 'date'], 'divider_highlight_group': 'time:divider'}])
self.assertEqual(self.module.date(pl=pl, format='%H:%M', istime=True, tz="America/New_York"), [{'contents': '%H:%M', 'highlight_groups': ['time', 'date'], 'divider_highlight_group': 'time:divider'}])

def test_fuzzy_time(self):
time = Args(hour=0, minute=45)
Expand Down