/*
 * Copyright (C) 2009-2014 Johan Nilsson <http://markupartist.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.markupartist.sthlmtraveling.utils;

import android.content.res.Resources;

import com.markupartist.sthlmtraveling.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Date & Time utils
 */
public class DateTimeUtil {
    public static final long SECOND_IN_MILLIS = 1000;
    public static final long MINUTE_IN_MILLIS = SECOND_IN_MILLIS * 60;
    public static final long HOUR_IN_MILLIS = MINUTE_IN_MILLIS * 60;
    public static final long DAY_IN_MILLIS = HOUR_IN_MILLIS * 24;
    public static final long WEEK_IN_MILLIS = DAY_IN_MILLIS * 7;

    /**
     * Constructs a Date from the provided date and time.
     *
     * @param dateString In the yy.MM.dd format
     * @param timeString In the HH:mm format
     * @return A Date or null if failed to process the provided strings.
     */
    public static Date fromSlDateTime(final String dateString, final String timeString) {
        String dateTime = String.format("%s %s", dateString, timeString);
        Date date = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yy HH:mm");
        try {
            date = simpleDateFormat.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * Return given duration in a human-friendly format. For example, "4
     * minutes" or "1 second". Returns only largest meaningful unit of time,
     * from seconds up to hours.
     * <p/>
     * From android.text.format.DateUtils
     */
    public static CharSequence formatDuration(final Resources res, final long millis) {
        if (millis >= HOUR_IN_MILLIS) {
            final int hours = (int) ((millis + 1800000) / HOUR_IN_MILLIS);
            return res.getQuantityString(R.plurals.duration_hours, hours, hours);
        } else if (millis >= MINUTE_IN_MILLIS) {
            final int minutes = (int) ((millis + 30000) / MINUTE_IN_MILLIS);
            return res.getQuantityString(R.plurals.duration_minutes, minutes, minutes);
        } else {
            final int seconds = (int) ((millis + 500) / SECOND_IN_MILLIS);
            return res.getQuantityString(R.plurals.duration_seconds, seconds, seconds);
        }
    }
}
@@ -712,16 +712,59 @@
 sources;
+%0Aimport android.support.annotation.NonNull;
 %0A%0Aimport
@@ -890,16 +890,54 @@
 il.Date;
+%0Aimport java.util.concurrent.TimeUnit;
 %0A%0A/**%0A *
@@ -2956,10 +2956,1246 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * Format duration without rounding. Seconds are still rounded though.%0A     *%0A     * @param resources a resource%0A     * @param millis    duration in millis%0A     * @return A string representing the duration%0A     */%0A    public static CharSequence formatDetailedDuration(@NonNull final Resources resources, long millis) %7B%0A        int days = 0, hours = 0, minutes = 0;%0A        if (millis %3E 0) %7B%0A            days = (int) TimeUnit.MILLISECONDS.toDays(millis);%0A            millis -= TimeUnit.DAYS.toMillis(days);%0A            hours = (int) TimeUnit.MILLISECONDS.toHours(millis);%0A            millis -= TimeUnit.HOURS.toMillis(hours);%0A            minutes = (int) TimeUnit.MILLISECONDS.toMinutes(millis);%0A        %7D%0A        if (days %3E 0) %7B%0A            return resources.getQuantityString(R.plurals.duration_days, days, days);%0A        %7D%0A        if (hours %3E 0) %7B%0A            if (minutes %3E 0) %7B%0A                return resources.getString(R.string.duration_short_hours_minutes, hours, minutes);%0A            %7D else %7B%0A                return resources.getQuantityString(R.plurals.duration_short_hours, hours, hours);%0A            %7D%0A        %7D%0A        return resources.getQuantityString(R.plurals.duration_short_minutes, minutes, minutes);%0A    %7D
 %0A%7D

Add helper for getting duration