Created
July 5, 2012 19:45
-
-
Save gdamjan/3055986 to your computer and use it in GitHub Desktop.
Retardations in python's datetime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/python2 | |
| import psycopg2 | |
| from datetime import datetime | |
| import pytz, time | |
| tzname = 'UTC' | |
| TZ = pytz.timezone(tzname) | |
| test_datetime = datetime.fromtimestamp(1341446400) | |
| def connect(DSN = ""): | |
| conn = psycopg2.connect(dsn=DSN) | |
| cr = conn.cursor() | |
| cr.execute("set TIMEZONE='%s'" % tzname) | |
| conn.commit() | |
| return conn, conn.cursor() | |
| def setup(): | |
| conn, cr = connect() | |
| cr.execute('drop table if exists test_ts_tz') | |
| cr.execute('''CREATE TABLE test_ts_tz ( | |
| tt timestamp without time zone | |
| )''') | |
| conn.commit() | |
| print 'Setup complete' | |
| def insert(): | |
| conn, cr = connect() | |
| cr.execute('''TRUNCATE test_ts_tz''') | |
| cr.execute('''INSERT INTO test_ts_tz VALUES ( %s )''', [test_datetime]) | |
| conn.commit() | |
| print test_datetime.strftime('%s'), test_datetime.isoformat() | |
| def select(): | |
| conn, cr = connect() | |
| cr.execute('''SELECT tt FROM test_ts_tz''') | |
| dt, = cr.fetchone() | |
| print dt.strftime('%s'), dt.isoformat() | |
| assert dt == test_datetime | |
| if __name__ == '__main__': | |
| import sys | |
| if len(sys.argv) < 2: | |
| print 'setup/insert/select' | |
| sys.exit(1) | |
| cmd = sys.argv[1] | |
| if cmd == 'setup': | |
| setup() | |
| elif cmd == 'insert': | |
| setup() | |
| insert() | |
| elif cmd == 'select': | |
| select() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/python2 | |
| import psycopg2 | |
| from datetime import datetime | |
| import pytz, time | |
| tzname = 'Europe/Skopje' | |
| TZ = pytz.timezone(tzname) | |
| test_datetime = datetime.fromtimestamp(1341446400, TZ) | |
| def connect(DSN = ""): | |
| conn = psycopg2.connect(dsn=DSN) | |
| cr = conn.cursor() | |
| cr.execute("set TIMEZONE='%s'" % tzname) | |
| conn.commit() | |
| return conn, conn.cursor() | |
| def setup(): | |
| conn, cr = connect() | |
| cr.execute('drop table if exists test_ts_tz') | |
| cr.execute('''CREATE TABLE test_ts_tz ( | |
| tt timestamp with time zone | |
| )''') | |
| conn.commit() | |
| print 'Setup complete' | |
| def insert(): | |
| conn, cr = connect() | |
| cr.execute('''TRUNCATE test_ts_tz''') | |
| cr.execute('''INSERT INTO test_ts_tz VALUES ( %s )''', [test_datetime]) | |
| conn.commit() | |
| print test_datetime.strftime('%s'), test_datetime.isoformat() | |
| def select(): | |
| conn, cr = connect() | |
| cr.execute('''SELECT tt FROM test_ts_tz''') | |
| dt, = cr.fetchone() | |
| print dt.strftime('%s'), dt.isoformat() | |
| assert dt == test_datetime | |
| if __name__ == '__main__': | |
| import sys | |
| if len(sys.argv) < 2: | |
| print 'setup/insert/select' | |
| sys.exit(1) | |
| cmd = sys.argv[1] | |
| if cmd == 'setup': | |
| setup() | |
| elif cmd == 'insert': | |
| setup() | |
| insert() | |
| elif cmd == 'select': | |
| select() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| echo | |
| echo "try with the default timezone environment" | |
| echo | |
| echo timezone in db column | |
| python psycopg-timestamp-tz.py insert | |
| python psycopg-timestamp-tz.py select | |
| echo no timezone in db column | |
| python psycopg-timestamp-notz.py insert | |
| python psycopg-timestamp-notz.py select | |
| echo | |
| echo "try with with TZ=Asia/Tokyo" | |
| echo | |
| export TZ=Asia/Tokyo | |
| echo timezone in db column | |
| python psycopg-timestamp-tz.py insert | |
| python psycopg-timestamp-tz.py select | |
| echo no timezone in db column | |
| python psycopg-timestamp-notz.py insert | |
| python psycopg-timestamp-notz.py select | |
| echo | |
| echo "try with with TZ=America/Mexico_City" | |
| echo | |
| export TZ=America/Mexico_City | |
| echo timezone in db column | |
| python psycopg-timestamp-tz.py insert | |
| python psycopg-timestamp-tz.py select | |
| echo no timezone in db column | |
| python psycopg-timestamp-notz.py insert | |
| python psycopg-timestamp-notz.py select | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/python2 | |
| # retardations in python's datetime | |
| import pytz | |
| TZ = pytz.timezone('Europe/Skopje') | |
| from datetime import datetime | |
| x1 = datetime.now(tz=TZ) | |
| x2 = datetime(x1.year, x1.month, x1.day, tzinfo=TZ) | |
| assert x1.tzinfo == x2.tzinfo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment