Skip to content

feat: add global --timezone option#2

Merged
snomiao merged 1 commit into
mainfrom
feat/timezone-option
Feb 17, 2026
Merged

feat: add global --timezone option#2
snomiao merged 1 commit into
mainfrom
feat/timezone-option

Conversation

@snomiao
Copy link
Copy Markdown
Owner

@snomiao snomiao commented Feb 17, 2026

Ported from insanum/gcalcli#870 by @nfultz.

Summary

Why

Users who need to view/manage calendars in a timezone different from their system timezone had no clean way to do this.

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings February 17, 2026 11:13
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a new global --timezone CLI option intended to override the system timezone for all time-related operations in gcalcli.

Changes:

  • Introduces --timezone as a program-level option in the argument parser.
  • Applies the timezone override by setting TZ and calling time.tzset() during CLI startup.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
gcalcli/cli.py Applies the timezone override at runtime via TZ + time.tzset().
gcalcli/argparsers.py Adds --timezone to global/program options.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread gcalcli/cli.py
Comment on lines +230 to +232
if parsed_args.timezone:
os.environ['TZ'] = parsed_args.timezone
time.tzset()
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

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

This new global timezone behavior isn’t covered by tests. Adding a test that passes --timezone and asserts the TZ override is applied (e.g., os.environ['TZ'] set and time.tzset invoked via monkeypatch, plus at least one time-parsing path producing the expected offset) would help prevent regressions.

Copilot uses AI. Check for mistakes.
Comment thread gcalcli/argparsers.py
'given',
},
'--locale': {'default': '', 'type': str, 'help': 'System locale'},
'--timezone': {'default': '', 'type': str, 'help': 'Timezone to use'},
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

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

The --timezone help text is quite generic, and users may not know what format is expected (e.g., IANA tzdb name like America/Los_Angeles). Clarifying the expected timezone identifier (and any platform caveats) in the option help would make the flag easier to use and reduce invalid-value failures.

Suggested change
'--timezone': {'default': '', 'type': str, 'help': 'Timezone to use'},
'--timezone': {
'default': '',
'type': str,
'help': "Timezone to use (IANA tzdb name, e.g. 'America/Los_Angeles')",
},

Copilot uses AI. Check for mistakes.
Comment thread gcalcli/cli.py
Comment on lines +230 to +232
if parsed_args.timezone:
os.environ['TZ'] = parsed_args.timezone
time.tzset()
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

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

--timezone is applied after argparse has already converted date/time arguments (e.g., via utils.get_time_from_str() / tzlocal()), so values like start/end may be interpreted in the system timezone instead of the requested one. To make the flag effective, apply the TZ override (and any needed tzset) before any parsing/type conversion that depends on local time (e.g., by doing a minimal pre-parse for --timezone / config defaults, setting TZ, then running the full parser).

Copilot uses AI. Check for mistakes.
Comment thread gcalcli/cli.py

if parsed_args.timezone:
os.environ['TZ'] = parsed_args.timezone
time.tzset()
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

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

time.tzset() is not available on all platforms (notably Windows) and can also fail for invalid TZ values, which would currently crash the CLI. Consider guarding with hasattr(time, 'tzset') and handling AttributeError/OSError to emit a clear error message (and non-zero exit) when the timezone cannot be applied.

Suggested change
time.tzset()
if hasattr(time, 'tzset'):
try:
time.tzset()
except (AttributeError, OSError) as exc:
printer.err_msg(
f"Failed to apply timezone '{parsed_args.timezone}': {exc}"
)
sys.exit(1)
else:
printer.err_msg(
"Setting timezone via the TZ environment variable is not "
"supported on this platform."
)
sys.exit(1)

Copilot uses AI. Check for mistakes.
@snomiao snomiao merged commit 430d0f1 into main Feb 17, 2026
6 checks passed
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.

3 participants