CodenameOne icon indicating copy to clipboard operation
CodenameOne copied to clipboard

(Android) PICKER_TYPE_DATE_AND_TIME Picker shows date - 1 day when editing

Open javieranton-zz opened this issue 5 years ago • 7 comments

This is a strange one. I could have sworn that this wasn't happening before, but I don't see any change affecting the Picker in the commits. The following minimal code reproduces the issue:

        Form hi = new Form("Test", new BorderLayout());
        Container center = new Container(BoxLayout.y());
        Picker startDateTimePicker = new Picker();
        startDateTimePicker.setType(Display.PICKER_TYPE_DATE_AND_TIME);
        startDateTimePicker.setDate(new Date());
        center.add(startDateTimePicker);
        hi.add(BorderLayout.CENTER, center);
        hi.show();

Using this code, when the picker displays the following example date: It then shows this other date when tapped and starting to edit:

From what I have seen, it doesn't always happen. I would say 90% of times. Race condition?

javieranton-zz avatar Dec 10 '20 16:12 javieranton-zz

I know we are in code freeze but it would be great if this very small issue could be fixed. I've pasted very detailed code on how to reproduce here. I tried fixing myself but unfortunately couldn't figure it out. Perhaps the person who first developed this one can help more, hopefully it will be an easy win

javieranton-zz avatar Feb 18 '21 11:02 javieranton-zz

As of today, this is still happening

javieranton-zz avatar Nov 22 '21 13:11 javieranton-zz

Is this only happening in Android, or can you reproduce it in the simulator?

shannah avatar Nov 22 '21 13:11 shannah

This also happens in the simulator

javieranton-zz avatar Nov 22 '21 14:11 javieranton-zz

Tried this test case today and couldn't reproduce it. I also stepped through the code and wasn't able to identify where this would be happening. Could be timezone related. Tried in a few different timezones with no results.

shannah avatar Nov 23 '21 14:11 shannah

https://user-images.githubusercontent.com/11293898/143045753-e460b720-5999-4d80-a624-bfa1be135566.mp4

Here is the result of the code above. As it can be seen, each time the picker is tapped, the edit panel switches to date - 1 day My time zone is UTC +00

javieranton-zz avatar Nov 23 '21 14:11 javieranton-zz

@shannah I was able to fix this in my own version of CN1. The problem is that the selected index of the spinner is calculated and then truncated, not rounded. Would be good to fix in CN1's code. The line here needs to be changed from

int out = (int)((currentValue - min) / DAY);

to

int out = (int) Math.round((new Long(this.currentValue).doubleValue() - new Long(this.min).doubleValue()) / new Long(DAY).doubleValue());

Testing this requires choosing dates in the future incrementally, one day at a time. Faulty results may not be obtained until 40+ days are tested, but it could be more. Because of how the value is truncated, depending on the time of day different results will be obtained too. But the above code fixes this teething issue

javieranton-zz avatar Oct 01 '22 14:10 javieranton-zz

Actually scrap this. The error still persists (now adding a day instead of subtracting)

javieranton-zz avatar Oct 05 '22 19:10 javieranton-zz

I managed to fix this but the solution is ugly and not worth sharing. Basically, I run an extra check to see if the date is as intended and if it isn't I move the date to +-1 day as needed before showing the dialog (in SpinnerDateModel)

javieranton-zz avatar Oct 10 '22 17:10 javieranton-zz