1
# tz.py - example of datetime objects with time zones
2
# -*- encoding: utf8 -*-
4
# Copyright (C) 2004 Federico Di Gregorio <fog@debian.org>
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by the
8
# Free Software Foundation; either version 2, or (at your option) any later
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
13
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16
## put in DSN your DSN string
20
## don't modify anything below this line (except for experimenting)
26
from psycopg2.tz import ZERO, LOCAL, FixedOffsetTimezone
31
print "Opening connection using dns:", DSN
32
conn = psycopg2.connect(DSN)
36
curs.execute("CREATE TABLE test_tz (t timestamp with time zone)")
39
curs.execute("DROP TABLE test_tz")
40
curs.execute("CREATE TABLE test_tz (t timestamp with time zone)")
43
d = datetime.datetime(1971, 10, 19, 22, 30, 0, tzinfo=LOCAL)
44
curs.execute("INSERT INTO test_tz VALUES (%s)", (d,))
45
print "Inserted timestamp with timezone:", d
46
print "Time zone:", d.tzinfo.tzname(d), "offset:", d.tzinfo.utcoffset(d)
48
tz = FixedOffsetTimezone(-5*60, "EST")
49
d = datetime.datetime(1971, 10, 19, 22, 30, 0, tzinfo=tz)
50
curs.execute("INSERT INTO test_tz VALUES (%s)", (d,))
51
print "Inserted timestamp with timezone:", d
52
print "Time zone:", d.tzinfo.tzname(d), "offset:", d.tzinfo.utcoffset(d)
54
curs.execute("SELECT * FROM test_tz")
55
d = curs.fetchone()[0]
56
curs.execute("INSERT INTO test_tz VALUES (%s)", (d,))
57
print "Inserted SELECTed timestamp:", d
58
print "Time zone:", d.tzinfo.tzname(d), "offset:", d.tzinfo.utcoffset(d)
60
curs.execute("SELECT * FROM test_tz")
62
u = d[0].utcoffset() or ZERO
63
print "UTC time: ", d[0] - u
64
print "Local time:", d[0]
65
print "Time zone:", d[0].tzinfo.tzname(d[0]), d[0].tzinfo.utcoffset(d[0])
68
curs.execute("DROP TABLE test_tz")