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

issue #391: Watson now supports setting the TZ env variable. #411

Merged
merged 1 commit into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The `log` command output can now be filtered to exclude projects and tags via `--ignore-project` and `--ignore-tag` (#395)
- Python 3.8 support (#402)
- Python 3.9 support (#402)
- Support for the TZ environment variable to specify the local time zone (#391)

### Changed

Expand Down
6 changes: 3 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from itertools import combinations
from datetime import datetime, timedelta

from dateutil.tz import tzlocal
import pytest

from watson import cli
from watson.cli import local_tz_info


# Not all ISO-8601 compliant strings are recognized by arrow.get(str)
Expand Down Expand Up @@ -173,7 +173,7 @@ def test_report_invalid_date(runner, watson, test_dt):
@pytest.mark.parametrize('at_dt', VALID_TIMES_DATA)
def test_stop_valid_time(runner, watson, mocker, at_dt):
mocker.patch('arrow.arrow.dt_datetime', wraps=datetime)
start_dt = datetime(2019, 4, 10, 14, 0, 0, tzinfo=tzlocal())
start_dt = datetime(2019, 4, 10, 14, 0, 0, tzinfo=local_tz_info())
arrow.arrow.dt_datetime.now.return_value = start_dt
result = runner.invoke(cli.start, ['a-project'], obj=watson)
assert result.exit_code == 0
Expand All @@ -190,7 +190,7 @@ def test_stop_valid_time(runner, watson, mocker, at_dt):
def test_start_valid_time(runner, watson, mocker, at_dt):
# Simulate a start date so that 'at_dt' is older than now().
mocker.patch('arrow.arrow.dt_datetime', wraps=datetime)
start_dt = datetime(2019, 4, 10, 14, 0, 0, tzinfo=tzlocal())
start_dt = datetime(2019, 4, 10, 14, 0, 0, tzinfo=local_tz_info())
arrow.arrow.dt_datetime.now.return_value = (start_dt + timedelta(hours=1))
result = runner.invoke(cli.start, ['a-project', '--at', at_dt], obj=watson)
assert result.exit_code == 0
28 changes: 22 additions & 6 deletions watson/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import itertools
import json
import operator

import os
from dateutil import tz
from functools import reduce, wraps

Expand Down Expand Up @@ -67,10 +67,25 @@ def _raise_exclusive_error(self):
['`--{}`'.format(_) for _ in self.mutually_exclusive]))))


def local_tz_info() -> datetime.tzinfo:
"""Get the local time zone object, respects the TZ env variable."""
timezone = os.environ.get("TZ", None)
# If timezone is None or an empty string, gettz returns the local time
tzinfo = tz.gettz(timezone)
# gettz returns None if the timezone passed to gettz is invalid
if tzinfo is None:
raise click.ClickException(
f"Invalid timezone {timezone} specified, "
"please set the TZ environment variable with"
" a valid timezone."
)
return tzinfo


class DateTimeParamType(click.ParamType):
name = 'datetime'

def convert(self, value, param, ctx):
def convert(self, value, param, ctx) -> arrow:
if value:
date = self._parse_multiformat(value)
if date is None:
Expand All @@ -80,8 +95,9 @@ def convert(self, value, param, ctx):
)
# When we parse a date, we want to parse it in the timezone
# expected by the user, so that midnight is midnight in the local
# timezone, not in UTC. Cf issue #16.
date = date.replace(tzinfo=tz.tzlocal())
# timezone, or respect the TZ environment variable not in UTC.
# Cf issue #16.
date = date.replace(tzinfo=local_tz_info())
# Add an offset to match the week beginning specified in the
# configuration
if param.name == "week":
Expand All @@ -91,7 +107,7 @@ def convert(self, value, param, ctx):
start_time=date, week_start=week_start)
return date

def _parse_multiformat(self, value):
def _parse_multiformat(self, value) -> arrow:
date = None
for fmt in (None, 'HH:mm:ss', 'HH:mm'):
try:
Expand Down Expand Up @@ -1258,7 +1274,7 @@ def edit(watson, confirm_new_project, confirm_new_tag, id):
date_format = 'YYYY-MM-DD'
time_format = 'HH:mm:ss'
datetime_format = '{} {}'.format(date_format, time_format)
local_tz = tz.tzlocal()
local_tz = local_tz_info()

if id:
frame = get_frame_from_argument(watson, id)
Expand Down