humanize
humanize copied to clipboard
NaturalDay
Hello, your natural day function is a bit wrong.
public static String naturalDay(int style, Date then) {
Date today = new Date();
long delta = then.getTime() - today.getTime();
long days = delta / 82800000L;
return days == 0L?((DefaultContext)context.get()).getMessage("today"):(days == 1L?((DefaultContext)context.get()).getMessage("tomorrow"):(days == -1L?((DefaultContext)context.get()).getMessage("yesterday"):formatDate(style, then)));
}
Imagin to be at 5:00 AM, an action that happens 10 hours before would result in: "Today". Here is how i reach the goal if you want to update it:
private String naturalDayMod(Date then) {
Calendar midnight = Calendar.getInstance();
midnight.set(Calendar.HOUR_OF_DAY, 0);
midnight.set(Calendar.MINUTE, 0);
midnight.set(Calendar.SECOND, 0);
long midnightStamp = midnight.getTimeInMillis();
long delta = then.getTime() - midnightStamp;
return delta > 0L ? getContext().getString(R.string.today) :
(then.getTime() > (midnightStamp - (1000 * 60 * 60 * 24)) ? getContext().getString(R.string.yesterday) :
Humanize.formatDate(3, then));
}
Yes, you're right, thank you for pointing it out.
But, leaving aside timezone concerns, it can be fixed avoiding the Calendar thing :smiley_cat:
Consider this snippet to solve the issue:
Date now = new Date();
long nowMillis = now.getTime();
long startOfDayMillis = nowMillis - (nowMillis % (24 * 60 * 60 * 1000));
long delta = then.getTime() - startOfDayMillis;
Hope this helps!
Nice!!! thanks!