1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
# KInterbasDB Python Package - Type Conv : DateTime/eGenix mx.DateTime
#
# Version 3.3
#
# The following contributors hold Copyright (C) over their respective
# portions of code (see license.txt for details):
#
# [Original Author (maintained through version 2.0-0.3.1):]
# 1998-2001 [alex] Alexander Kuznetsov <alexan@users.sourceforge.net>
# [Maintainers (after version 2.0-0.3.1):]
# 2001-2002 [maz] Marek Isalski <kinterbasdb@maz.nu>
# 2002-2006 [dsr] David Rushby <woodsplitter@rocketmail.com>
# [Contributors:]
# 2001 [eac] Evgeny A. Cherkashin <eugeneai@icc.ru>
# 2001-2002 [janez] Janez Jere <janez.jere@void.si>
__all__ = (
# kinterbasdb-native date and time converters:
'date_conv_in', 'date_conv_out',
'time_conv_in', 'time_conv_out',
'timestamp_conv_in', 'timestamp_conv_out',
# DB API 2.0 standard date and time type constructors:
'Date', 'Time', 'Timestamp',
'DateFromTicks', 'TimeFromTicks', 'TimestampFromTicks',
)
import sys
from kinterbasdb.k_exceptions import *
# This conversion module uses mx.DateTime for its date/time operations.
try:
from mx import DateTime as mxDT
except ImportError:
raise ImportError('kinterbasdb uses the mx.DateTime module (from the'
' "eGenix mx Base Package") by default for date/time/timestamp'
' representation, but you do not have this package installed.'
'\nYou can either download the eGenix mx Base Package from'
'\nhttp://www.egenix.com/files/python/eGenix-mx-Extensions.html#Download-mxBASE'
'\nor tell kinterbasdb to use the Python standard library datetime'
' module instead, as explained at'
'\nhttp://kinterbasdb.sourceforge.net/dist_docs/usage.html#faq_fep_is_mxdatetime_required'
)
################################################################################
## DATE AND TIME
################################################################################
# kinterbasdb-native date and time converters:
def date_conv_in(mxDateTimeObj):
# Allow implicit param conv:
if mxDateTimeObj is None or isinstance(mxDateTimeObj, basestring):
return mxDateTimeObj
if not isinstance(mxDateTimeObj, mxDT.DateTimeType):
raise InterfaceError(
'Required type: %s ; supplied type: %s'
% ( str(mxDT.DateTimeType), str(type(mxDateTimeObj)) )
)
return mxDateTimeObj.tuple()[:3]
def date_conv_out(dateTuple):
if dateTuple is None:
return None
return mxDT.DateTime(*dateTuple)
def time_conv_in(mxDateTimeOrTimeDeltaObj):
# Allow implicit param conv:
if ( mxDateTimeOrTimeDeltaObj is None
or isinstance(mxDateTimeOrTimeDeltaObj, basestring)
):
return mxDateTimeOrTimeDeltaObj
if isinstance(mxDateTimeOrTimeDeltaObj, mxDT.DateTimeType):
timeTuple = mxDateTimeOrTimeDeltaObj.tuple()[3:6]
elif isinstance(mxDateTimeOrTimeDeltaObj, mxDT.DateTimeDeltaType):
timeTuple = mxDateTimeOrTimeDeltaObj.tuple()[1:]
else:
raise InterfaceError(
'Cannot convert object of type %s to native kinterbasdb tuple.'
% str(type(mxDateTimeOrTimeDeltaObj))
)
secondsFrac = timeTuple[2]
seconds = int(secondsFrac)
microseconds = int((secondsFrac - seconds) * 1000000)
return (timeTuple[0], timeTuple[1], seconds, microseconds)
def time_conv_out(timeTuple):
if timeTuple is None:
return None
if len(timeTuple) != 4:
return mxDT.Time(*timeTuple)
else:
(hour, minute, second, micros) = timeTuple
secondsFrac = second + micros / 1000000.0
return mxDT.Time(hour, minute, secondsFrac)
def timestamp_conv_in(mxDateTimeObj):
# Allow implicit param conv:
if mxDateTimeObj is None or isinstance(mxDateTimeObj, basestring):
return mxDateTimeObj
if not isinstance(mxDateTimeObj, mxDT.DateTimeType):
raise InterfaceError(
'Required type: %s ; supplied type: %s'
% ( str(mxDT.DateTimeType), str(type(mxDateTimeObj)) )
)
timestampTuple = mxDateTimeObj.tuple()
secondsFrac = timestampTuple[5]
seconds = int(secondsFrac)
microseconds = int((secondsFrac - seconds) * 1000000)
return timestampTuple[:5] + (seconds, microseconds)
def timestamp_conv_out(timestampTuple):
if timestampTuple is None:
return None
if len(timestampTuple) == 7:
(year, month, day, hour, minute, second, micros) = timestampTuple
secondsFrac = second + micros / 1000000.0
else:
(year, month, day, hour, minute, second) = timestampTuple
secondsFrac = second
return mxDT.DateTime(year, month, day, hour, minute, secondsFrac)
# DB API 2.0 standard date and time type constructors:
def Date(year, month, day):
try:
theDate = mxDT.DateTime(year, month, day)
except mxDT.Error, e:
raise DataError(str(e))
return theDate
def Time(hour, minute, second):
# mx DateTimeDeltas roll over when provided with an hour greater than
# 23, a minute greater than 59, and so on. That is not acceptable for our
# purposes.
if hour < 0 or hour > 23:
raise DataError("hour must be between 0 and 23")
if minute < 0 or minute > 59:
raise DataError("minute must be between 0 and 59")
if second < 0 or second > 59:
raise DataError("second must be between 0 and 59")
try:
theTime = mxDT.TimeDelta(hour, minute, second)
except mxDT.Error, e:
raise DataError(str(e))
return theTime
def Timestamp(year, month, day, hour, minute, second):
args = (year, month, day, hour, minute, second) # Yes, I know about the
# *args syntactical shortcut, but it's not particularly readable.
# mx mxDT's Timestamp constructor accepts negative values in the
# spirit of Python's negative list indexes, but I see no reason to allow
# that behavior in this DB API-compliance-obsessed module.
if 0 < len(filter(lambda x: x < 0, args)):
raise DataError("Values less than zero not allowed in Timestamp."
" (Received arguments %s)"
% repr(args)
)
try:
theStamp = mxDT.DateTime(*args)
except mxDT.Error, e:
raise DataError(str(e))
return theStamp
DateFromTicks = mxDT.DateFromTicks
TimeFromTicks = mxDT.TimeFromTicks
TimestampFromTicks = mxDT.TimestampFromTicks
|