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

Set session timezone to local timezone on startup #1482

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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 AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ Contributors:
* Chris Rose (offbyone/offby1)
* Mathieu Dupuy (deronnax)
* Chris Novakovic
* Max Smolin (maximsmol)

Creator:
--------
Expand Down
1 change: 1 addition & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Features
--------
* Add a `--ping` command line option; allows pgcli to replace `pg_isready`
* Changed the packaging metadata from setup.py to pyproject.toml
* The session time zone setting is set to the system time zone by default

Bug fixes:
----------
Expand Down
26 changes: 26 additions & 0 deletions pgcli/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from zoneinfo import ZoneInfoNotFoundError
from configobj import ConfigObj, ParseError
from pgspecial.namedqueries import NamedQueries
from .config import skip_initial_comment
Expand All @@ -23,6 +24,7 @@
from cli_helpers.utils import strip_ansi
from .explain_output_formatter import ExplainOutputFormatter
import click
import tzlocal

try:
import setproctitle
Expand Down Expand Up @@ -1624,6 +1626,30 @@ def cli(
else:
pgcli.connect(database, host, user, port)

local_tz = None
try:
local_tz = tzlocal.get_localzone_name()

if local_tz is None:
click.secho(
"No local time zone configuration found",
err=True,
fg="yellow",
)
except ZoneInfoNotFoundError as e:
# e.args[0] is the pre-formatted message which includes a list
# of conflicting sources
click.secho(e.args[0], err=True, fg="yellow")

if local_tz is not None:
pgcli.pgexecute.set_timezone(local_tz)
else:
click.secho(
"Continuing with the default server time zone",
err=True,
fg="yellow",
)

if list_databases:
cur, headers, status = pgcli.pgexecute.full_databases()

Expand Down
7 changes: 7 additions & 0 deletions pgcli/pgexecute.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,3 +881,10 @@ def casing(self):

def explain_prefix(self):
return "EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON) "

def set_timezone(self, timezone: str):
query = psycopg.sql.SQL("set time zone {}").format(
psycopg.sql.Identifier(timezone)
)
with self.conn.cursor() as cur:
cur.execute(query)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies = [
# task manager. Also setproctitle is a hard dependency to install in Windows,
# so we'll only install it if we're not in Windows.
"setproctitle >= 1.1.9; sys_platform != 'win32' and 'CYGWIN' not in sys_platform",
"tzlocal >= 5.2",
]
dynamic = ["version"]

Expand Down