Issue2524

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

Created on 2016-10-07.07:58:52 by tmeagher, last changed 2017-01-27.17:25:38 by stefan.richthofer.

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())
History
Date User Action Args
2017-01-27 17:25:38stefan.richthofersetnosy: + stefan.richthofer
2016-10-07 07:58:52tmeaghercreate