From: Billy G. A. <bal...@us...> - 2002-12-14 05:13:38
|
Update of /cvsroot/pypgsql/pypgsql/pyPgSQL In directory sc8-pr-cvs1:/tmp/cvs-serv14457/pyPgSQL Modified Files: PgSQL.py Log Message: 13DEC2002 bga - Corrected a problem with interval <-> DateTimeDelta casting. [Bug #653044] Index: PgSQL.py =================================================================== RCS file: /cvsroot/pypgsql/pypgsql/pyPgSQL/PgSQL.py,v retrieving revision 1.28 retrieving revision 1.29 diff -C2 -d -r1.28 -r1.29 *** PgSQL.py 6 Dec 2002 05:20:12 -0000 1.28 --- PgSQL.py 14 Dec 2002 05:13:34 -0000 1.29 *************** *** 30,33 **** --- 30,35 ---- # Date Ini Description | # --------- --- ------------------------------------------------------- | + # 13DEC2002 bga - Corrected a problem with interval <-> DateTimeDelta | + # casting. [Bug #653044] | # 06DEC2002 bga - Corrected problem found by Adam Buraczewski in the | # __setupTransaction function. | *************** *** 385,394 **** TimestampFromTicks = DateTime.TimestampFromTicks #-------------------------------+ ! # And also the DateTime types | #-------------------------------+ DateTimeType = DateTime.DateTimeType DateTimeDeltaType = DateTime.DateTimeDeltaType #-----------------------------------------------------------------------+ --- 387,403 ---- TimestampFromTicks = DateTime.TimestampFromTicks + #-----------------------------------------------+ + # The DateTimeDelta type for PgInterval support | + #-----------------------------------------------+ + + DateTimeDelta = DateTime.DateTimeDelta + #-------------------------------+ ! # Also the DateTime types | #-------------------------------+ DateTimeType = DateTime.DateTimeType DateTimeDeltaType = DateTime.DateTimeDeltaType + DateTimeDelta = DateTime.DateTimeDelta #-----------------------------------------------------------------------+ *************** *** 511,526 **** self.__conn = None ! def interval2datetimedelta(self, s): ! """Parses both 7.0.x and 7.1.x styles of PostgreSQL INTERVALs""" ! if s.startswith("-"): ! factor = -1 ! else: ! factor = 1 ! s = s.replace('days ', '').replace("-", "") ! l = s.split() ! if len(l) != 2: ! l = ['0'] + l ! return factor * (DateTime.DateTimeDelta(int(l[0])) \ ! + DateTime.DateTimeDeltaFrom(l[1])) def parseArray(self, s): --- 520,548 ---- self.__conn = None ! def interval2DateTimeDelta(self, s): ! """Parses PostgreSQL INTERVALs. ! The expected format is [[[-]YY years] [-]DD days] [-]HH:MM:SS.ss""" ! parser = DateTime.Parser.DateTimeDeltaFromString ! ! ydh = s.split() ! ago = 1 ! ! result = DateTimeDelta(0) ! ! # Convert any years using 365.2425 days per year, which is PostgreSQL's ! # 's assumption about the number of days in a year. ! if ydh[1].lower().startswith('year'): ! result += parser('%s days' % ((int(ydh[0]) * 365.2425),)) ! ydh = ydh[2:] ! ! # Converts any days and adds it to the years (as an interval) ! if ydh[1].lower().startswith('day'): ! result += parser('%s days' % (ydh[0],)) ! ydh = ydh[2:] ! ! # Adds in the hours, minutes, seconds (as an interval) ! result += parser(ydh[0]) ! ! return result def parseArray(self, s): *************** *** 726,730 **** else: if _ftv == PG_INTERVAL: ! return self.interval2datetimedelta(value) else: return DateTime.ISO.ParseAny(value) --- 748,752 ---- else: if _ftv == PG_INTERVAL: ! return self.interval2DateTimeDelta(value) else: return DateTime.ISO.ParseAny(value) *************** *** 2066,2069 **** --- 2088,2093 ---- # _quoteall() -- transforms all elements of a list or diction- | # ary using _quote. | + # dateTimeDelta2Interval() -- converts a DateTimeDelta type into | + # a string PostgreSQL accepts as a interval. | #-----------------------------------------------------------------------+ *************** *** 2138,2143 **** elif hasattr(_i, '_quote'): _j = '%s%s,' % (_j, _i._quote(1)) ! elif type(_i) in [DateTime.DateTimeType, DateTime.DateTimeDeltaType]: _j = '%s"%s",' % (_j, _i) elif type(_i) is PgInt2Type or isinstance(_i, PgInt8Type): _j = '%s%s,' % (_j, str(_i)) --- 2162,2169 ---- elif hasattr(_i, '_quote'): _j = '%s%s,' % (_j, _i._quote(1)) ! elif type(_i) is DateTimeType: _j = '%s"%s",' % (_j, _i) + elif type(_i) is DateTime.DateTimeDeltaType: + _j = '%s"%s",' % (_j, dateTimeDelta2Interval(_i)) elif type(_i) is PgInt2Type or isinstance(_i, PgInt8Type): _j = '%s%s,' % (_j, str(_i)) *************** *** 2163,2168 **** elif hasattr(value, '_quote'): return value._quote() ! elif type(value) in [DateTimeType, DateTimeDeltaType]: return "'%s'" % value elif type(value) is StringType: return PgQuoteString(value) --- 2189,2196 ---- elif hasattr(value, '_quote'): return value._quote() ! elif type(value) is DateTimeType: return "'%s'" % value + elif type(value) is DateTimeDeltaType: + return "'%s'" % dateTimeDelta2Interval(value) elif type(value) is StringType: return PgQuoteString(value) *************** *** 2193,2196 **** --- 2221,2247 ---- return t + + def dateTimeDelta2Interval(interval): + """ + DateTimeDelta2Interval - Converts a DateTimeDelta to an interval string\n + The input format is [+-]DD:HH:MM:SS.ss\n + The output format is DD days HH:MM:SS.ss [ago]\n + """ + + if type(interval) is DateTimeDeltaType: + s = str(interval) + ago = '' + if s[0] == '-': + ago = ' ago' + s = s[1:] + else: + ago = '' + s = s.split(':') + if len(s) < 4: + return '%s:%s:%s %s' % (s[0], s[1], s[2], ago) + + return '%s days %s:%s:%s %s' % (s[0], s[1], s[2], s[3], ago) + else: + raise TypeException, "DateTimeDelta2Interval requires a DataTimeDelta." #-----------------------------------------------------------------------+ |