11import io
22import logging
3- from datetime import tzinfo
3+ from datetime import tzinfo , datetime
44
55import pytz
66
1212from clickhouse_connect .common import version
1313from clickhouse_connect .datatypes .registry import get_from_name
1414from clickhouse_connect .datatypes .base import ClickHouseType
15- from clickhouse_connect .driver .common import dict_copy , StreamContext , coerce_int
15+ from clickhouse_connect .driver .common import dict_copy , StreamContext , coerce_int , coerce_bool
1616from clickhouse_connect .driver .constants import CH_VERSION_WITH_PROTOCOL , PROTOCOL_VERSION_WITH_LOW_CARD
1717from clickhouse_connect .driver .exceptions import ProgrammingError
1818from clickhouse_connect .driver .external import ExternalData
@@ -42,7 +42,7 @@ def __init__(self,
4242 uri : str ,
4343 query_retries : int ,
4444 server_host_name : Optional [str ],
45- apply_server_timezone : Optional [bool ]):
45+ apply_server_timezone : Optional [Union [ str , bool ] ]):
4646 """
4747 Shared initialization of ClickHouse Connect client
4848 :param database: database name
@@ -52,14 +52,16 @@ def __init__(self,
5252 self .query_limit = coerce_int (query_limit )
5353 self .query_retries = coerce_int (query_retries )
5454 self .server_host_name = server_host_name
55- self .apply_server_timezone = apply_server_timezone is True
5655 self .server_tz = pytz .UTC
5756 self .server_version , server_tz , self .database = \
5857 tuple (self .command ('SELECT version(), timezone(), currentDatabase()' , use_database = False ))
5958 try :
6059 self .server_tz = pytz .timezone (server_tz )
6160 except UnknownTimeZoneError :
6261 logger .warning ('Warning, server is using an unrecognized timezone %s, will use UTC default' , server_tz )
62+ offsets_differ = datetime .now ().astimezone ().utcoffset () != datetime .now (tz = self .server_tz ).utcoffset ()
63+ self .apply_server_timezone = apply_server_timezone == 'always' or (
64+ coerce_bool (apply_server_timezone ) and offsets_differ )
6365 readonly = 'readonly'
6466 if not self .min_version ('19.17' ):
6567 readonly = common .get_setting ('readonly' )
0 commit comments