Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong LocalTime while converting from one timezone to another #191

Closed
zezekalo opened this issue Nov 7, 2017 · 9 comments
Closed

Wrong LocalTime while converting from one timezone to another #191

zezekalo opened this issue Nov 7, 2017 · 9 comments

Comments

@zezekalo
Copy link

zezekalo commented Nov 7, 2017

I used sample application and have just changed sampleLocalDate method:

`private void sampleLocalDate() {
        List<String> text = new ArrayList<String>();
        LocalDate now = LocalDate.now();
        text.add("Now: " + now);
        long startDayMillis = now.toDateTimeAtStartOfDay().getMillis();
        text.add("LocalDate in millis: " + startDayMillis);

        long startDayMillisInUTC = now.toDateTimeAtStartOfDay().withZone(DateTimeZone.UTC).getMillis();
        text.add("startDayMillisInUTC: " + startDayMillisInUTC);

        text.add("LocalDate after converting: " + new LocalDate(startDayMillis));
        text.add("Now + 2 days: " + now.plusDays(2));
        text.add("Now + 3 months: " + now.plusMonths(3));

        text.add("----------------------------------------------");

        LocalTime time = new LocalTime(DateTimeZone.UTC);
        text.add("LocalTime: " + time);
        long timeNowMillis = time.getMillisOfDay();
        text.add("LocalTime in millis - timeNowMillis: " + timeNowMillis);
        long millis = time.getMillisOfDay();
        LocalTime localTime = new LocalTime(millis, DateTimeZone.UTC);
        text.add("LocalTime, getMillisOfDay: " + millis + "; local time (UTC) - " + localTime);
        TimeZone timeZone = TimeZone.getDefault();
        text.add("Timezone: rawOffset: " + timeZone.getRawOffset()/(60*60*1000) + "; timezone - " + timeZone.toString());
        text.add("LocalTime, local time (device timezone) - "
                + new LocalTime(millis, DateTimeZone.getDefault())
//                + new DateTime(millis, DateTimeZone.forTimeZone(timeZone))
        );
        text.add("difference between timeNowMillis and startDayMillis: " + new LocalTime(timeNowMillis - startDayMillis, DateTimeZone.UTC));

        addSample("LocalDate", text);`

My idea is - I have LocalTime in UTC and save it as long in database. So, I can restore long timestamp and convert it in LocalTime in UTC. This works well. But I 'd like to convert LocalTime(UTC) to LocalTime(TimeZoneOfMyDevice).
I tried converting but I had an issue - converted LocalTime differ from current time

device-2017-11-07-184846

What could be wrong in my case?
Device: Xiaomi Redmi Note 4X, MTK

@dlew
Copy link
Owner

dlew commented Nov 7, 2017

I'm not exactly sure what you're trying to do. If you're using LocalTime that means that you don't care about timezone at all, so to throw timezones into the mix confuses me.

@zezekalo
Copy link
Author

zezekalo commented Nov 8, 2017

Hmmm, but LocalTime-class has the constructor which includes DateTimeZone parameter. If it means that LocalTime don't care about timezone then why it has DateTimeZone argument?
My idea is having normalized time, which doesn't depend on timezones and has no date. But I'd like to convert this normalized time to time of my device timezone.

P.S. After a little investigation I tend to think that my case is an issue of MIUI 8 of Xiaomi. And it happens when I switch on 'Automatic time zone' in settings.

@dlew
Copy link
Owner

dlew commented Nov 8, 2017

Sure, you can construct a LocalTime from a DateTimeZone, but afterwards you are essentially losing that data. That's useful when you, say, have a time picker that includes timezone: the user picks the time they want (11 AM in zone X), you convert that to LocalTime (11 AM anywhere), and the timezone doesn't matter anymore.

If you want to convert LocalTime back to DateTime, maybe you should use LocalTime.toDateTimeToday()?

@zezekalo
Copy link
Author

zezekalo commented Nov 8, 2017

Yeah, thanks for your help, I think that this is what I want. But when I changed code to your advice then the time difference on my device is still present((

text.add("LocalTime, local time (device timezone) - "
                  + new LocalTime(millis, DateTimeZone.getDefault()).toDateTimeToday());

device-2017-11-08-163159

@dlew
Copy link
Owner

dlew commented Nov 8, 2017

What timezone is your phone in, and how old is your phone? I'm beginning to suspect the problem may be that the zone data on your phone is out-of-date compared to joda-time. That could result in the output being wrong between the system clock and your app's clock.

@zezekalo
Copy link
Author

zezekalo commented Nov 8, 2017

At the screenshot above I switched on 'Automatic time zone' and I gave 'timezone' from my internet provider.
Yes, my real timezone is "Europe/Kyiv"

But when I switched off 'Automatic time zone' and selected manually 'Europe/Kyev' I found such result:
device-2017-11-08-163825

Bucarest? )) Chinese is such Chinese))

@zezekalo
Copy link
Author

zezekalo commented Nov 8, 2017

Hmmm, why do other time-applications work well in such case?))

@dlew
Copy link
Owner

dlew commented Nov 9, 2017

I really don't know. I could look more into this if you provide a brief snippet that demonstrates the exact problem (and not just underlined areas of the sample app). Otherwise I don't really have time to chase this down; I'm guessing it's probably not the lib's fault, but some misunderstanding of the timezones involved.

@dlew dlew closed this as completed May 6, 2018
@dlew
Copy link
Owner

dlew commented May 6, 2018

Closing due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants