Search in sources :

Example 1 with KSString

use of com.kickstarter.libs.KSString in project android-oss by kickstarter.

the class DateTimeUtilsTest method testRelative_withAbbreviated.

@Test
public void testRelative_withAbbreviated() {
    final Context context = context();
    final KSString ksString = ksString();
    final DateTime dateTime = DateTime.parse("2015-12-17T18:35:05Z");
    final RelativeDateTimeOptions.Builder builder = RelativeDateTimeOptions.builder().abbreviated(true);
    assertEquals("4 hrs ago", DateTimeUtils.relative(context, ksString, dateTime, builder.relativeToDateTime(DateTime.parse("2015-12-17T22:35:05Z")).build()));
    assertEquals("in 4 hrs", DateTimeUtils.relative(context, ksString, dateTime, builder.relativeToDateTime(DateTime.parse("2015-12-17T14:35:05Z")).build()));
}
Also used : Context(android.content.Context) RelativeDateTimeOptions(com.kickstarter.libs.RelativeDateTimeOptions) KSString(com.kickstarter.libs.KSString) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 2 with KSString

use of com.kickstarter.libs.KSString in project android-oss by kickstarter.

the class DateTimeUtilsTest method testRelative_withThreshold.

@Test
public void testRelative_withThreshold() {
    final Context context = context();
    final KSString ksString = ksString();
    final DateTime dateTime = DateTime.parse("2015-12-17T18:35:05Z");
    // Ten days
    final int threshold = 864_000;
    final RelativeDateTimeOptions.Builder builder = RelativeDateTimeOptions.builder().threshold(threshold);
    assertEquals("9 days ago", DateTimeUtils.relative(context, ksString, dateTime, builder.relativeToDateTime(DateTime.parse("2015-12-26T18:35:05Z")).build()));
    assertEquals("in 9 days", DateTimeUtils.relative(context, ksString, dateTime, builder.relativeToDateTime(DateTime.parse("2015-12-08T18:35:05Z")).build()));
    assertEquals("Dec 17, 2015", DateTimeUtils.relative(context, ksString, dateTime, builder.relativeToDateTime(DateTime.parse("2015-12-28T18:35:05Z")).build()));
    assertEquals("Dec 17, 2015", DateTimeUtils.relative(context, ksString, dateTime, builder.relativeToDateTime(DateTime.parse("2015-12-06T18:35:05Z")).build()));
}
Also used : Context(android.content.Context) RelativeDateTimeOptions(com.kickstarter.libs.RelativeDateTimeOptions) KSString(com.kickstarter.libs.KSString) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 3 with KSString

use of com.kickstarter.libs.KSString in project android-oss by kickstarter.

the class DateTimeUtilsTest method testRelative_withAbsolute.

@Test
public void testRelative_withAbsolute() {
    final Context context = context();
    final KSString ksString = ksString();
    final DateTime dateTime = DateTime.parse("2015-12-17T18:35:05Z");
    final RelativeDateTimeOptions.Builder builder = RelativeDateTimeOptions.builder().absolute(true);
    assertEquals("4 hours", DateTimeUtils.relative(context, ksString, dateTime, builder.relativeToDateTime(DateTime.parse("2015-12-17T22:35:05Z")).build()));
    assertEquals("4 hours", DateTimeUtils.relative(context, ksString, dateTime, builder.relativeToDateTime(DateTime.parse("2015-12-17T14:35:05Z")).build()));
}
Also used : Context(android.content.Context) RelativeDateTimeOptions(com.kickstarter.libs.RelativeDateTimeOptions) KSString(com.kickstarter.libs.KSString) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 4 with KSString

use of com.kickstarter.libs.KSString in project android-oss by kickstarter.

the class DateTimeUtilsTest method testRelative_withLocale.

@Test
@Config(qualifiers = "de")
public void testRelative_withLocale() {
    final Context context = context();
    final KSString ksString = ksString();
    final DateTime dateTime = DateTime.parse("2015-12-17T18:35:05Z");
    final RelativeDateTimeOptions.Builder builder = RelativeDateTimeOptions.builder();
    assertEquals("vor 2 Minuten", DateTimeUtils.relative(context, ksString, dateTime, builder.relativeToDateTime(DateTime.parse("2015-12-17T18:37:05Z")).build()));
    assertEquals("in 2 Minuten", DateTimeUtils.relative(context, ksString, dateTime, builder.relativeToDateTime(DateTime.parse("2015-12-17T18:33:05Z")).build()));
}
Also used : Context(android.content.Context) RelativeDateTimeOptions(com.kickstarter.libs.RelativeDateTimeOptions) KSString(com.kickstarter.libs.KSString) DateTime(org.joda.time.DateTime) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 5 with KSString

use of com.kickstarter.libs.KSString in project android-oss by kickstarter.

the class DateTimeUtils method relative.

/**
   * Returns a string indicating the distance between {@link DateTime}s. Defaults to comparing the input {@link DateTime} to
   * the current time.
   */
@NonNull
public static String relative(@NonNull final Context context, @NonNull final KSString ksString, @NonNull final DateTime dateTime, @NonNull final RelativeDateTimeOptions options) {
    final DateTime relativeToDateTime = ObjectUtils.coalesce(options.relativeToDateTime(), DateTime.now());
    final Seconds seconds = Seconds.secondsBetween(dateTime, relativeToDateTime);
    final int secondsDifference = seconds.getSeconds();
    if (secondsDifference >= 0.0 && secondsDifference <= 60.0) {
        return context.getString(R.string.dates_just_now);
    } else if (secondsDifference >= -60.0 && secondsDifference <= 0.0) {
        return context.getString(R.string.dates_right_now);
    }
    final Pair<String, Integer> unitAndDifference = unitAndDifference(secondsDifference, options.threshold());
    if (unitAndDifference == null) {
        // Couldn't find a good match, just render the date.
        return mediumDate(dateTime);
    }
    final String unit = unitAndDifference.first;
    final int difference = unitAndDifference.second;
    boolean willHappenIn = false;
    boolean happenedAgo = false;
    if (!options.absolute()) {
        if (secondsDifference < 0) {
            willHappenIn = true;
        } else if (secondsDifference > 0) {
            happenedAgo = true;
        }
    }
    if (happenedAgo && "days".equals(unit) && difference == 1) {
        return context.getString(R.string.dates_yesterday);
    }
    final StringBuilder baseKeyPath = new StringBuilder();
    if (willHappenIn) {
        baseKeyPath.append(String.format("dates_time_in_%s", unit));
    } else if (happenedAgo) {
        baseKeyPath.append(String.format("dates_time_%s_ago", unit));
    } else {
        baseKeyPath.append(String.format("dates_time_%s", unit));
    }
    if (options.abbreviated()) {
        baseKeyPath.append("_abbreviated");
    }
    return ksString.format(baseKeyPath.toString(), difference, "time_count", NumberUtils.format(difference, NumberOptions.builder().build()));
}
Also used : Seconds(org.joda.time.Seconds) KSString(com.kickstarter.libs.KSString) DateTime(org.joda.time.DateTime) NonNull(android.support.annotation.NonNull)

Aggregations

KSString (com.kickstarter.libs.KSString)7 DateTime (org.joda.time.DateTime)6 Context (android.content.Context)5 RelativeDateTimeOptions (com.kickstarter.libs.RelativeDateTimeOptions)5 Test (org.junit.Test)5 NonNull (android.support.annotation.NonNull)2 Seconds (org.joda.time.Seconds)1 Config (org.robolectric.annotation.Config)1