Message11682

Author tmeagher
Recipients tmeagher
Date 2017-11-26.11:32:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1511695965.08.0.213398074469.issue2647@psf.upfronthosting.co.za>
In-reply-to
Content
The Py.newDateTime and Py.newTime methods construct new datetime.datetime and datetime.time objects. The date/time information is calculated based on the time in millis on the java.sql.Timestamp/Time object provided to those methods.
However, the timezone is not set on the python objects. This means that while these types are "naive" in python, when converting back to Java via the __tojava__ method, the returned time will be incorrect if the system is not in the UTC timezone.

Example:

long timeMillis = System.currentTimeMillis();
		
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
Timestamp inputTimestamp = new Timestamp(timeMillis);
		
PyObject pyDateTime = Py.newDatetime(inputTimestamp);
Timestamp resultTimestamp = (Timestamp)pyDateTime.__tojava__(java.sql.Timestamp.class);
		
System.out.println("Input DateTime:");
System.out.println("formatted:" + dateFormat.format(inputTimestamp));
System.out.println("millis:"+inputTimestamp.getTime());

System.out.println("Output DateTime:");
//These should both match the input datetime formatted string & time in millis, but won't if the system is not in UTC.
System.out.println("formatted:"+dateFormat.format(resultTimestamp));
System.out.println("millis:"+resultTimestamp.getTime());
		
SimpleDateFormat timeFormat = new SimpleDateFormat( "HH:mm:ss");		
java.sql.Time inputTime = new java.sql.Time(timeMillis);		
PyObject pyTime = Py.newTime(inputTime);
java.sql.Time resultTime = (java.sql.Time)pyTime.__tojava__(java.sql.Time.class);

		
System.out.println("Input Time:");
System.out.println("formatted:" + timeFormat.format(inputTime));
System.out.println("millis:"+inputTime.getTime());

//The millis on Time doesn't need to match the input (since the date component may exist in the original Java object, but is
//ignored when converting back to Java (which is ok), but the actual hh:mm:ss the time represents should be the same as the original
//On a non-UTC system, this is not the case.
System.out.println("Output Time:");
System.out.println("formatted:"+timeFormat.format(resultTime));
System.out.println("millis:"+resultTime.getTime());
History
Date User Action Args
2017-11-26 11:32:45tmeaghersetrecipients: + tmeagher
2017-11-26 11:32:45tmeaghersetmessageid: <1511695965.08.0.213398074469.issue2647@psf.upfronthosting.co.za>
2017-11-26 11:32:44tmeagherlinkissue2647 messages
2017-11-26 11:32:43tmeaghercreate