humanize icon indicating copy to clipboard operation
humanize copied to clipboard

NaturalDay

Open iGio90 opened this issue 8 years ago • 2 comments

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));
        }

iGio90 avatar Jun 23 '17 08:06 iGio90

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!

mfornos avatar Jun 27 '17 18:06 mfornos

Nice!!! thanks!

iGio90 avatar Jun 27 '17 19:06 iGio90