oracle / python-oracledb

Python driver for Oracle Database conforming to the Python DB API 2.0 specification. This is the renamed, new major release of cx_Oracle
https://oracle.github.io/python-oracledb
Other
328 stars 66 forks source link

Timezone autodetection in thin mode is wrong #257

Closed szpajder closed 9 months ago

szpajder commented 10 months ago
  1. What versions are you using?

Oracle 19c

platform.platform: Linux-6.4.10-arch1-1-x86_64-with-glibc2.38
sys.maxsize > 2**32: True
platform.python_version: 3.11.3
  1. Is it an error or a hang or a crash?

Error.

  1. What error(s) or behavior you are seeing?
$ date
Wed Nov 22 16:27:47 CET 2023
$ python test.py
(datetime.datetime(2023, 11, 22, 17, 27, 52), '+02:00')

test.py is a script listed below. The current system timezone is CET (ie. winter time, which is +01:00) however oracledb autodetects it as +02:00 (summer time, CEST), regardless of the time of the year. As a result, all timestamps read from a database are OK during summer time, but off by 1 hour in winter time.

This is due to an incorrect autodetection logic in _get_alter_timezone_statement():

        if tz_repr is None:
            timezone = -time.altzone if time.daylight else -time.timezone

According to the docs, time.daylight indicates whether DST is defined for the current timezone, ie. if there is a time, past, present or future when daylight saving time applies. It does not indicate that DST is currently in use.

If I replace the above line with:

          timezone = time.localtime().tm_gmtoff

then it works fine, regardless of whether DST is currently in use or not.

  1. Does your application call init_oracle_client()?

No.

  1. Include a runnable Python script that shows the problem.
import oracledb
with oracledb.connect(host='...', port=..., user='...',password='...',sid='...') as conn:
 with conn.cursor() as cursor:
  for row in cursor.execute('select current_date, sessiontimezone from dual'):
   print(row)
anthony-tuininga commented 10 months ago

Thank you for the clear explanation. I will make the suggested change!

anthony-tuininga commented 10 months ago

I have pushed a patch that should correct this bug. If you are able to build from source you can verify that it works for you.

szpajder commented 10 months ago

I've made exactly the same change and tested it before posting this issue - it works fine.

anthony-tuininga commented 9 months ago

The patch has been included in version 2.0.0 which was just released.