2004-10-19 07:17:12 +04:00
|
|
|
# tz.py - example of datetime objects with time zones
|
2005-11-16 20:30:45 +03:00
|
|
|
# -*- encoding: utf8 -*-
|
2004-10-19 07:17:12 +04:00
|
|
|
#
|
2010-02-13 01:34:53 +03:00
|
|
|
# Copyright (C) 2004-2010 Federico Di Gregorio <fog@debian.org>
|
2004-10-19 07:17:12 +04:00
|
|
|
#
|
2010-02-13 01:34:53 +03:00
|
|
|
# psycopg2 is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2004-10-19 07:17:12 +04:00
|
|
|
#
|
2010-02-13 01:34:53 +03:00
|
|
|
# psycopg2 is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
# License for more details.
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
## put in DSN your DSN string
|
|
|
|
|
|
|
|
DSN = 'dbname=test'
|
|
|
|
|
|
|
|
## don't modify anything below this line (except for experimenting)
|
|
|
|
|
2005-06-13 10:25:06 +04:00
|
|
|
import sys
|
|
|
|
import psycopg2
|
2004-10-19 07:17:12 +04:00
|
|
|
import datetime
|
|
|
|
|
2005-06-13 10:25:06 +04:00
|
|
|
from psycopg2.tz import ZERO, LOCAL, FixedOffsetTimezone
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
DSN = sys.argv[1]
|
|
|
|
|
2017-12-04 05:47:19 +03:00
|
|
|
print("Opening connection using dsn:", DSN)
|
2005-06-13 10:25:06 +04:00
|
|
|
conn = psycopg2.connect(DSN)
|
2004-10-19 07:17:12 +04:00
|
|
|
curs = conn.cursor()
|
|
|
|
|
|
|
|
try:
|
|
|
|
curs.execute("CREATE TABLE test_tz (t timestamp with time zone)")
|
|
|
|
except:
|
|
|
|
conn.rollback()
|
|
|
|
curs.execute("DROP TABLE test_tz")
|
|
|
|
curs.execute("CREATE TABLE test_tz (t timestamp with time zone)")
|
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
d = datetime.datetime(1971, 10, 19, 22, 30, 0, tzinfo=LOCAL)
|
|
|
|
curs.execute("INSERT INTO test_tz VALUES (%s)", (d,))
|
2017-12-04 05:47:19 +03:00
|
|
|
print("Inserted timestamp with timezone:", d)
|
|
|
|
print("Time zone:", d.tzinfo.tzname(d), "offset:", d.tzinfo.utcoffset(d))
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
tz = FixedOffsetTimezone(-5*60, "EST")
|
|
|
|
d = datetime.datetime(1971, 10, 19, 22, 30, 0, tzinfo=tz)
|
|
|
|
curs.execute("INSERT INTO test_tz VALUES (%s)", (d,))
|
2017-12-04 05:47:19 +03:00
|
|
|
print("Inserted timestamp with timezone:", d)
|
|
|
|
print("Time zone:", d.tzinfo.tzname(d), "offset:", d.tzinfo.utcoffset(d))
|
2004-10-19 07:17:12 +04:00
|
|
|
|
2005-10-17 20:04:43 +04:00
|
|
|
curs.execute("SELECT * FROM test_tz")
|
|
|
|
d = curs.fetchone()[0]
|
|
|
|
curs.execute("INSERT INTO test_tz VALUES (%s)", (d,))
|
2017-12-04 05:47:19 +03:00
|
|
|
print("Inserted SELECTed timestamp:", d)
|
|
|
|
print("Time zone:", d.tzinfo.tzname(d), "offset:", d.tzinfo.utcoffset(d))
|
2005-10-17 20:04:43 +04:00
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
curs.execute("SELECT * FROM test_tz")
|
|
|
|
for d in curs:
|
|
|
|
u = d[0].utcoffset() or ZERO
|
2017-12-04 05:47:19 +03:00
|
|
|
print("UTC time: ", d[0] - u)
|
|
|
|
print("Local time:", d[0])
|
|
|
|
print("Time zone:", d[0].tzinfo.tzname(d[0]), d[0].tzinfo.utcoffset(d[0]))
|
2017-12-02 08:37:49 +03:00
|
|
|
|
2005-10-17 20:04:43 +04:00
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
curs.execute("DROP TABLE test_tz")
|
|
|
|
conn.commit()
|