Changeset: 0218c3beb6e5 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=0218c3beb6e5
Modified Files:
        clients/python2/monetdb/sql/pythonize.py
        clients/python3/monetdb/sql/pythonize.py
        sql/test/BugTracker-2014/Tests/python-microseconds.Bug-3439.SQL.py
        sql/test/BugTracker-2014/Tests/python-microseconds.Bug-3439.stable.out
Branch: default
Log Message:

Merge with Jan2014 branch.


diffs (237 lines):

diff --git a/clients/python2/monetdb/sql/pythonize.py 
b/clients/python2/monetdb/sql/pythonize.py
--- a/clients/python2/monetdb/sql/pythonize.py
+++ b/clients/python2/monetdb/sql/pythonize.py
@@ -27,35 +27,16 @@ from monetdb.exceptions import Programmi
 
 
 def _extract_timezone(data):
-    if data.find('+') != -1:
-        (dt, tz) = data.split("+")
-        (tzhour, tzmin) = [int(x) for x in tz.split(':')]
-    elif data.find('-') != -1:
-        (dt, tz) = data.split("-")
-        (tzhour, tzmin) = [int(x) for x in tz.split(':')]
-        tzhour *= -1
-        tzmin *= -1
+    sign_symbol = data[-6]
+
+    if sign_symbol == '+':
+        sign = 1
+    elif sign_symbol == '-':
+        sign = -1
     else:
         raise ProgrammingError("no + or - in %s" % data)
 
-    return dt, tzhour, tzmin
-
-def _extract_time(data, tzhour = 0, tzmin = 0):
-    time = data.split(':')
-    hour = int(time[0]) + tzhour
-    minute = int(time[1]) + tzmin
-    second = time[2]
-    if '.' in second:
-        second, microsecond = second.split('.')
-        while len(microsecond) < 6:
-            microsecond += '0'
-        while len(microsecond) > 6:
-            microsecond = microsecond[:-1]
-        microsecond = int(microsecond)
-    else:
-        microsecond = 0
-    second = int(second)
-    return hour, minute, second, microsecond
+    return data[:-6], datetime.timedelta(hours=sign * int(data[-5:-3]), 
minutes=sign * int(data[-2:]))
 
 def strip(data):
     """ returns a python string, with chopped off quotes,
@@ -71,39 +52,33 @@ def py_bool(data):
 def py_time(data):
     """ returns a python Time
     """
-    return Time(*_extract_time(data))
+    return datetime.datetime.strptime(data, '%H:%M:%S').time()
 
 
 def py_timetz(data):
     """ returns a python Time where data contains a tz code
     """
-    dt, tzhour, tzmin = _extract_timezone(data)
-    return Time(*_extract_time(dt, tzhour, tzmin))
+    t, timezone_delta = _extract_timezone(data)
+    return (datetime.datetime.strptime(t, '%H:%M:%S') + timezone_delta).time()
 
 
 def py_date(data):
     """ Returns a python Date
     """
-    return Date(*[int(x) for x in data.split('-')])
+    return datetime.datetime.strptime(data, '%Y-%m-%d').date()
 
 
 def py_timestamp(data):
     """ Returns a python Timestamp
     """
-    (datestr, timestr) = data.split(" ")
-    date = [int(x) for x in datestr.split('-')]
-    time = list(_extract_time(timestr))
-    return Timestamp(*(date + time))
+    return datetime.datetime.strptime(data, '%Y-%m-%d %H:%M:%S.%f')
 
 
 def py_timestamptz(data):
     """ Returns a python Timestamp where data contains a tz code
     """
-    dt, tzhour, tzmin = _extract_timezone(data)
-    (datestr, timestr) = dt.split(" ")
-    date = [int(x) for x in datestr.split('-')]
-    time = list(_extract_time(timestr, tzhour, tzmin))
-    return Timestamp(*(date + time))
+    dt, timezone_delta = _extract_timezone(data)
+    return datetime.datetime.strptime(dt, '%Y-%m-%d %H:%M:%S.%f') + 
timezone_delta
 
 
 mapping = {
diff --git a/clients/python3/monetdb/sql/pythonize.py 
b/clients/python3/monetdb/sql/pythonize.py
--- a/clients/python3/monetdb/sql/pythonize.py
+++ b/clients/python3/monetdb/sql/pythonize.py
@@ -28,35 +28,16 @@ from monetdb.exceptions import Programmi
 
 
 def _extract_timezone(data):
-    if data.find('+') != -1:
-        (dt, tz) = data.split("+")
-        (tzhour, tzmin) = [int(x) for x in tz.split(':')]
-    elif data.find('-') != -1:
-        (dt, tz) = data.split("-")
-        (tzhour, tzmin) = [int(x) for x in tz.split(':')]
-        tzhour *= -1
-        tzmin *= -1
+    sign_symbol = data[-6]
+
+    if sign_symbol == '+':
+        sign = 1
+    elif sign_symbol == '-':
+        sign = -1
     else:
         raise ProgrammingError("no + or - in %s" % data)
 
-    return dt, tzhour, tzmin
-
-def _extract_time(data, tzhour = 0, tzmin = 0):
-    time = data.split(':')
-    hour = int(time[0]) + tzhour
-    minute = int(time[1]) + tzmin
-    second = time[2]
-    if '.' in second:
-        second, microsecond = second.split('.')
-        while len(microsecond) < 6:
-            microsecond += '0'
-        while len(microsecond) > 6:
-            microsecond = microsecond[:-1]
-        microsecond = int(microsecond)
-    else:
-        microsecond = 0
-    second = int(second)
-    return hour, minute, second, microsecond
+    return data[:-6], datetime.timedelta(hours=sign * int(data[-5:-3]), 
minutes=sign * int(data[-2:]))
 
 def strip(data):
     """ returns a python string, with chopped off quotes,
@@ -75,39 +56,33 @@ def py_bool(data):
 def py_time(data):
     """ returns a python Time
     """
-    return Time(*_extract_time(data))
+    return datetime.datetime.strptime(data, '%H:%M:%S').time()
 
 
 def py_timetz(data):
     """ returns a python Time where data contains a tz code
     """
-    dt, tzhour, tzmin = _extract_timezone(data)
-    return Time(*_extract_time(dt, tzhour, tzmin))
+    t, timezone_delta = _extract_timezone(data)
+    return (datetime.datetime.strptime(t, '%H:%M:%S') + timezone_delta).time()
 
 
 def py_date(data):
     """ Returns a python Date
     """
-    return Date(*[int(x) for x in data.split('-')])
+    return datetime.datetime.strptime(data, '%Y-%m-%d').date()
 
 
 def py_timestamp(data):
     """ Returns a python Timestamp
     """
-    (datestr, timestr) = data.split(" ")
-    date = [int(x) for x in datestr.split('-')]
-    time = list(_extract_time(timestr))
-    return Timestamp(*(date + time))
+    return datetime.datetime.strptime(data, '%Y-%m-%d %H:%M:%S.%f')
 
 
 def py_timestamptz(data):
     """ Returns a python Timestamp where data contains a tz code
     """
-    dt, tzhour, tzmin = _extract_timezone(data)
-    (datestr, timestr) = dt.split(" ")
-    date = [int(x) for x in datestr.split('-')]
-    time = list(_extract_time(timestr, tzhour, tzmin))
-    return Timestamp(*(date + time))
+    dt, timezone_delta = _extract_timezone(data)
+    return datetime.datetime.strptime(dt, '%Y-%m-%d %H:%M:%S.%f') + 
timezone_delta
 
 
 mapping = {
diff --git a/sql/test/BugTracker-2014/Tests/python-microseconds.Bug-3439.SQL.py 
b/sql/test/BugTracker-2014/Tests/python-microseconds.Bug-3439.SQL.py
--- a/sql/test/BugTracker-2014/Tests/python-microseconds.Bug-3439.SQL.py
+++ b/sql/test/BugTracker-2014/Tests/python-microseconds.Bug-3439.SQL.py
@@ -2,8 +2,33 @@ import monetdb.sql, os
 
 dbh = 
monetdb.sql.Connection(port=int(os.getenv('MAPIPORT')),hostname=os.getenv('MAPIHOST'),database=os.getenv('TSTDB'))
 cursor = dbh.cursor();
+
 cursor.execute('create table bug3439 (ts timestamp)')
 cursor.execute("insert into bug3439 values ('2014-04-24 17:12:12.415')")
 cursor.execute('select * from bug3439')
 print cursor.fetchall()
 cursor.execute('drop table bug3439')
+
+cursor.execute('create table bug3439 (ts timestamp with time zone)')
+cursor.execute("insert into bug3439 values ('2014-04-24 17:12:12.415 -02:00')")
+cursor.execute('select * from bug3439')
+print cursor.fetchall()
+cursor.execute('drop table bug3439')
+
+cursor.execute('create table bug3439 (dt date)')
+cursor.execute("insert into bug3439 values ('2014-04-24')")
+cursor.execute('select * from bug3439')
+print cursor.fetchall()
+cursor.execute('drop table bug3439')
+
+cursor.execute('create table bug3439 (ts time)')
+cursor.execute("insert into bug3439 values ('17:12:12.415')")
+cursor.execute('select * from bug3439')
+print cursor.fetchall()
+cursor.execute('drop table bug3439')
+
+cursor.execute('create table bug3439 (ts time with time zone)')
+cursor.execute("insert into bug3439 values ('17:12:12.415 -02:00')")
+cursor.execute('select * from bug3439')
+print cursor.fetchall()
+cursor.execute('drop table bug3439')
diff --git 
a/sql/test/BugTracker-2014/Tests/python-microseconds.Bug-3439.stable.out 
b/sql/test/BugTracker-2014/Tests/python-microseconds.Bug-3439.stable.out
--- a/sql/test/BugTracker-2014/Tests/python-microseconds.Bug-3439.stable.out
+++ b/sql/test/BugTracker-2014/Tests/python-microseconds.Bug-3439.stable.out
@@ -52,6 +52,10 @@ Ready.
 # 17:15:02 >  
 
 [(datetime.datetime(2014, 4, 24, 17, 12, 12, 415000),)]
+[(datetime.datetime(2014, 4, 24, 19, 12, 12, 415000),)]
+[(datetime.date(2014, 4, 24),)]
+[(datetime.time(17, 12, 12),)]
+[(datetime.time(17, 12, 12),)]
 
 # 17:15:02 >  
 # 17:15:02 >  "Done."
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to