~ubuntu-branches/ubuntu/hardy/psycopg2/hardy

« back to all changes in this revision

Viewing changes to examples/tz.py

  • Committer: Bazaar Package Importer
  • Author(s): Fabio Tranchitella
  • Date: 2006-08-09 10:28:30 UTC
  • Revision ID: james.westby@ubuntu.com-20060809102830-grac1dsp24uyqfp4
Tags: upstream-2.0.4
ImportĀ upstreamĀ versionĀ 2.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# tz.py - example of datetime objects with time zones
 
2
# -*- encoding: utf8 -*-
 
3
#
 
4
# Copyright (C) 2004 Federico Di Gregorio  <fog@debian.org>
 
5
#
 
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
 
9
# version.
 
10
#
 
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
 
14
# for more details.
 
15
 
 
16
## put in DSN your DSN string
 
17
 
 
18
DSN = 'dbname=test'
 
19
 
 
20
## don't modify anything below this line (except for experimenting)
 
21
 
 
22
import sys
 
23
import psycopg2
 
24
import datetime
 
25
 
 
26
from psycopg2.tz import ZERO, LOCAL, FixedOffsetTimezone
 
27
 
 
28
if len(sys.argv) > 1:
 
29
    DSN = sys.argv[1]
 
30
 
 
31
print "Opening connection using dns:", DSN
 
32
conn = psycopg2.connect(DSN)
 
33
curs = conn.cursor()
 
34
 
 
35
try:
 
36
    curs.execute("CREATE TABLE test_tz (t timestamp with time zone)")
 
37
except:
 
38
    conn.rollback()
 
39
    curs.execute("DROP TABLE test_tz")
 
40
    curs.execute("CREATE TABLE test_tz (t timestamp with time zone)")
 
41
conn.commit()
 
42
 
 
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)
 
47
 
 
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)
 
53
 
 
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)
 
59
 
 
60
curs.execute("SELECT * FROM test_tz")
 
61
for d in curs:
 
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])
 
66
    
 
67
 
 
68
curs.execute("DROP TABLE test_tz")
 
69
conn.commit()