Frank Wierzbicki avatar Frank Wierzbicki committed f9ccaa0

Put Java import at top of file for both pep8 and for performance.

Comments (0)

Files changed (2)

 
 import time as _time
 import math as _math
+import sys as _sys
+
+if _sys.platform.startswith('java'):
+    from java.lang import Object
+    from java.sql import Date, Timestamp, Time
+    from java.util import Calendar
+    from org.python.core import Py
+
 
 MINYEAR = 1
 MAXYEAR = 9999
     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 _sys.platform.startswith('java'):
+        def __tojava__(self, java_class):
+            if java_class not in (Calendar, Date, Object):
+                return Py.NoConversion
 
-        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())
+            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
     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 _sys.platform.startswith('java'):
+        def __tojava__(self, java_class):
+            # TODO, if self.tzinfo is not None, convert time to UTC
+            if java_class not in (Calendar, Time, Object):
+                return Py.NoConversion
 
-        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())
+            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
     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 _sys.platform.startswith('java'):
+        def __tojava__(self, java_class):
+            # TODO, if self.tzinfo is not None, convert time to UTC
+            if java_class not in (Calendar, Timestamp, Object):
+                return Py.NoConversion
 
-        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 = Calendar.getInstance()
-        calendar.clear()
-        calendar.set(self.year, self.month - 1, self.day,
-                     self.hour, self.minute, self.second)
-
-        if java_class == Calendar:
-            calendar.set(Calendar.MILLISECOND, self.microsecond // 1000)
-            return calendar
-        else:
-            timestamp = Timestamp(calendar.getTimeInMillis())
-            timestamp.setNanos(self.microsecond * 1000)
-            return timestamp
+            if java_class == Calendar:
+                calendar.set(Calendar.MILLISECOND, self.microsecond // 1000)
+                return calendar
+            else:
+                timestamp = Timestamp(calendar.getTimeInMillis())
+                timestamp.setNanos(self.microsecond * 1000)
+                return timestamp
 
 
 datetime.min = datetime(1, 1, 1)

Lib/test/test_datetime_jy.py

+import unittest
+from test import test_support
+
+from datetime import timedelta
+from datetime import tzinfo
+from datetime import time
+from datetime import date, datetime
+
+from java.util import Calendar
+from java.sql import Date
+
+
+class TestJavaDatetime(unittest.TestCase):
+
+    def test_datetime(self):
+        self.assertTrue(hasattr(datetime, "__tojava__"))
+        x = datetime(2007, 1, 3)
+        y = x.__tojava__(Calendar)
+        self.assertTrue(isinstance(y, Calendar))
+
+    def test_date(self):
+        self.assertTrue(hasattr(date, "__tojava__"))
+        x = date(2007, 1, 3)
+        y = x.__tojava__(Calendar)
+        self.assertTrue(isinstance(y, Calendar))
+
+    def test_time(self):
+        self.assertTrue(hasattr(time, "__tojava__"))
+        x = time(1, 3)
+        y = x.__tojava__(Calendar)
+        self.assertTrue(isinstance(y, Calendar))
+
+
+if __name__ == '__main__':
+    unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.