From: <le...@us...> - 2008-07-15 17:16:20
|
Revision: 4941 http://jython.svn.sourceforge.net/jython/?rev=4941&view=rev Author: leosoto Date: 2008-07-15 10:16:16 -0700 (Tue, 15 Jul 2008) Log Message: ----------- Implemented __tojava__ on datetime.time, datetime.date and datetime.datetime. On the current implementation, they can be converted to java.sql.Time, java.sql.Date, java.sql.Timestamp, respectively. All of them can also be converted to java.util.Calendar. Only one caveat: the tzinfo attribute of times and datetimes is currently ignored Modified Paths: -------------- branches/asm/Lib/datetime.py Modified: branches/asm/Lib/datetime.py =================================================================== --- branches/asm/Lib/datetime.py 2008-07-15 16:24:18 UTC (rev 4940) +++ branches/asm/Lib/datetime.py 2008-07-15 17:16:16 UTC (rev 4941) @@ -981,6 +981,24 @@ def __reduce__(self): return (self.__class__, self.__getstate()) + def __tojava__(self, java_class): + from java.lang import Object + from java.sql import Date + from java.util import Calendar + from org.python.core import Py + + if java_class not in (Calendar, Date, Object): + return Py.NoConversion + + calendar = Calendar.getInstance() + calendar.clear() + calendar.set(self.year, self.month - 1, self.day) + if java_class == Calendar: + return calendar + else: + return Date(calendar.getTimeInMillis()) + + _date_class = date # so functions w/ args named "date" can get at the class date.min = date(1, 1, 1) @@ -1346,6 +1364,28 @@ def __reduce__(self): return (time, self.__getstate()) + def __tojava__(self, java_class): + # TODO, if self.tzinfo is not None, convert time to UTC + from java.lang import Object + from java.sql import Time + from java.util import Calendar + from org.python.core import Py + + if java_class not in (Calendar, Time, Object): + return Py.NoConversion + + calendar = Calendar.getInstance() + calendar.clear() + calendar.set(Calendar.HOUR_OF_DAY, self.hour) + calendar.set(Calendar.MINUTE, self.minute) + calendar.set(Calendar.SECOND, self.second) + calendar.set(Calendar.MILLISECOND, self.microsecond // 1000) + if java_class == Calendar: + return calendar + else: + return Time(calendar.getTimeInMillis()) + + _time_class = time # so functions w/ args named "time" can get at the class time.min = time(0, 0, 0) @@ -1799,7 +1839,28 @@ def __reduce__(self): return (self.__class__, self.__getstate()) + def __tojava__(self, java_class): + # TODO, if self.tzinfo is not None, convert time to UTC + from java.lang import Object + from java.sql import Timestamp + from java.util import Calendar + from org.python.core import Py + if java_class not in (Calendar, Timestamp, Object): + return Py.NoConversion + + calendar = Calendar.getInstance() + calendar.clear() + calendar.set(self.year, self.month - 1, self.day, + self.hour, self.minute, self.second) + calendar.set(Calendar.MILLISECOND, self.microsecond // 1000) + + if java_class == Calendar: + return calendar + else: + return Timestamp(calendar.getTimeInMillis()) + + datetime.min = datetime(1, 1, 1) datetime.max = datetime(9999, 12, 31, 23, 59, 59, 999999) datetime.resolution = timedelta(microseconds=1) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |