udacity / ud851-Sunshine

Apache License 2.0
2k stars 4.51k forks source link

[S11.02-Solution-TodayListItem] getFriendlyDateString() marks tomorrow with "Today" in error #183

Closed tatsuyafujisaki closed 3 years ago

tatsuyafujisaki commented 5 years ago

Hello,

I have found a minor issue in S11.02-Solution-TodayListItem on the student branch.

Running the project at noon on September 21 in Japan, September 22 is marked with "Today" in error.

screenshot

The error seems to originate from getFriendlyDateString() but I'm not 100% sure if the error is caused by time zone difference.

tatsuyafujisaki commented 5 years ago

I have found the root cause and the solution.

Inside getFriendlyDateString(...), changing

long localDate = getLocalMidnightFromNormalizedUtcDate(normalizedUtcMidnight);
long daysFromEpochToProvidedDate = elapsedDaysSinceEpoch(localDate);

to

long localDate = normalizedUtcMidnight;
long daysFromEpochToProvidedDate = elapsedDaysSinceEpoch(localDate);

will solve the problem. This is because elapsedDaysSinceEpoch(...) expects a UTC date rather than a local date as follows.

private static long elapsedDaysSinceEpoch(long utcDate) {
    return TimeUnit.MILLISECONDS.toDays(utcDate);
}
shakesgar commented 4 years ago

Great thanks