Issue2524

classification
Title: datetime <-> time conversion incorrect in non UTC times
Type: behaviour Severity: normal
Components: Core Versions: Jython 2.7
Milestone: Jython 2.7.1
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: stefan.richthofer Nosy List: stefan.richthofer, tmeagher
Priority: normal Keywords:

Created on 2016-10-07.07:58:52 by tmeagher, last changed 2017-07-11.15:08:03 by zyasoft.

Messages
msg10964 (view) Author: Tim Meagher (tmeagher) Date: 2016-10-07.07:58:50
Similar to issue 2504, but believe this is different enough to warrant a different bug report.

The datetime.py module shipped with jython (present in the latest beta as well) appears to have a bug with python<->time conversion.



Specifically, at line 1489 in datetime.py it defines the tojava conversion.

This sets the long time since epoch in ms on a java.util.Calendar 
which may have been initialized with a specific timezone.

However, the reconstruction of the epoch ms is based on the hour, 
minute, second, microsecond within the datetime.time object which are relative to the timezone in the datetime.time.tzinfo if one is present.

The calculation of epoch_ms will to return what the timeInMillis would be if the time was in UTC.

If it’s in another timezone, then this won’t be a correct ms time… 



Basically, the change I would be recommending would be something like that below :


        def __tojava__(self, java_class):
            if java_class not in (Calendar, Time, Object):
                return Py.NoConversion
            calendar = _make_java_calendar(self)
            if calendar == Py.NoConversion:
                return Py.NoConversion
            
            #Remove epoch ms calculation....
            #epoch_ms = (self.hour * 3600 + self.minute * 60 + self.second) * 1000 + self.microsecond // 1000
            #calendar.setTimeInMillis(epoch_ms)
                
            #initialize to epoch time - effectively clear out the current date from the calendar.
            #if a time is really just signalling to ignore the date component, maybe not strictly necessary?
            cal.setTimeInMillis(0);

            #now setup the calendar to have the details populated from this time.
            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())
msg11183 (view) Author: Stefan Richthofer (stefan.richthofer) Date: 2017-03-05.01:01:14
Tim, this change looks good to me and I see the problem with the current version.
Do you have a code sample demonstrating the issue? So we can turn it into a test...?
msg11203 (view) Author: Stefan Richthofer (stefan.richthofer) Date: 2017-03-08.00:29:49
Fixed by applying the suggested patch as of https://hg.python.org/jython/rev/0522ab9e72e9.
History
Date User Action Args
2017-07-11 15:08:03zyasoftsetmilestone: Jython 2.7.1
2017-03-28 05:28:22zyasoftsetstatus: pending -> closed
2017-03-08 00:29:50stefan.richthofersetstatus: open -> pending
resolution: fixed
messages: + msg11203
priority: normal
assignee: stefan.richthofer
type: behaviour
2017-03-05 01:01:14stefan.richthofersetmessages: + msg11183
2017-01-27 17:25:38stefan.richthofersetnosy: + stefan.richthofer
2016-10-07 07:58:52tmeaghercreate