mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Apply rounding in StringUtil.formatDurationApproximate(); remove similar method from DateFormatUtil (IDEA-CR-45112)
This commit is contained in:
+4
-3
@@ -1,4 +1,4 @@
|
||||
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.featureStatistics.actions;
|
||||
|
||||
import com.intellij.CommonBundle;
|
||||
@@ -11,6 +11,7 @@ import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.DialogWrapper;
|
||||
import com.intellij.openapi.ui.Splitter;
|
||||
import com.intellij.openapi.ui.VerticalFlowLayout;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.ui.ScrollPaneFactory;
|
||||
import com.intellij.ui.TableViewSpeedSearch;
|
||||
import com.intellij.ui.table.TableView;
|
||||
@@ -138,10 +139,10 @@ public class ShowFeatureUsageStatisticsDialog extends DialogWrapper {
|
||||
|
||||
final String uptimeS = FeatureStatisticsBundle.message("feature.statistics.application.uptime",
|
||||
ApplicationNamesInfo.getInstance().getFullProductName(),
|
||||
DateFormatUtil.formatDuration(uptime));
|
||||
StringUtil.formatDurationApproximate(uptime));
|
||||
|
||||
final String idleTimeS = FeatureStatisticsBundle.message("feature.statistics.application.idle.time",
|
||||
DateFormatUtil.formatDuration(idleTime));
|
||||
StringUtil.formatDurationApproximate(idleTime));
|
||||
|
||||
String labelText = uptimeS + ", " + idleTimeS;
|
||||
CompletionStatistics stats = ((FeatureUsageTrackerImpl)FeatureUsageTracker.getInstance()).getCompletionStatistics();
|
||||
|
||||
@@ -10,6 +10,8 @@ import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.util.*;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.text.*;
|
||||
import gnu.trove.TIntArrayList;
|
||||
import gnu.trove.TLongArrayList;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -1580,7 +1582,8 @@ public class StringUtil extends StringUtilRt {
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
private static String formatDuration(long duration, @NotNull String unitSeparator, int maxFragments) {
|
||||
List<String> fragments = new ArrayList<>();
|
||||
TLongArrayList unitValues = new TLongArrayList();
|
||||
TIntArrayList unitIndices = new TIntArrayList();
|
||||
|
||||
long count = duration;
|
||||
int i = 1;
|
||||
@@ -1589,21 +1592,37 @@ public class StringUtil extends StringUtilRt {
|
||||
if (count < multiplier) break;
|
||||
long remainder = count % multiplier;
|
||||
count /= multiplier;
|
||||
if (remainder != 0 || fragments.size() > 0) {
|
||||
fragments.add(0, remainder + unitSeparator + TIME_UNITS[i - 1]);
|
||||
if (remainder != 0 || unitValues.size() > 0) {
|
||||
unitValues.insert(0, remainder);
|
||||
unitIndices.insert(0, i - 1);
|
||||
}
|
||||
else {
|
||||
remainder = Math.round(remainder * 100 / (double)multiplier);
|
||||
count += remainder / 100;
|
||||
}
|
||||
}
|
||||
unitValues.insert(0, count);
|
||||
unitIndices.insert(0, i - 1);
|
||||
|
||||
fragments.add(0, count + unitSeparator + TIME_UNITS[i - 1]);
|
||||
if (fragments.size() > maxFragments) {
|
||||
fragments = fragments.subList(0, maxFragments);
|
||||
if (unitValues.size() > maxFragments) {
|
||||
int lastUnitIndex = unitIndices.get(maxFragments - 1);
|
||||
long lastMultiplier = TIME_MULTIPLIERS[lastUnitIndex];
|
||||
// Round up if needed
|
||||
if (unitValues.get(maxFragments) > lastMultiplier / 2) {
|
||||
long increment = lastMultiplier - unitValues.get(maxFragments);
|
||||
for (int unit = lastUnitIndex - 1; unit > 0; unit--) {
|
||||
increment *= TIME_MULTIPLIERS[unit];
|
||||
}
|
||||
return formatDuration(duration + increment, unitSeparator, maxFragments);
|
||||
}
|
||||
}
|
||||
|
||||
return join(fragments, " ");
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (i = 0; i < unitValues.size() && i < maxFragments; i++) {
|
||||
if (i > 0) result.append(" ");
|
||||
result.append(unitValues.get(i)).append(unitSeparator).append(TIME_UNITS[unitIndices.get(i)]);
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.util.text;
|
||||
|
||||
import com.intellij.CommonBundle;
|
||||
@@ -185,40 +185,6 @@ public class DateFormatUtil {
|
||||
return formatTime ? DATE_TIME_FORMAT.format(time) : DATE_FORMAT.format(time);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String formatDuration(long delta) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
for (int i = 0; i < DENOMINATORS.length; i++) {
|
||||
long denominator = DENOMINATORS[i];
|
||||
int n = (int)(delta / denominator);
|
||||
if (n != 0) {
|
||||
buf.append(composeDurationMessage(PERIODS[i], n));
|
||||
buf.append(' ');
|
||||
delta = delta % denominator;
|
||||
}
|
||||
}
|
||||
|
||||
if (buf.length() == 0) return CommonBundle.message("date.format.less.than.a.minute");
|
||||
return buf.toString().trim();
|
||||
}
|
||||
|
||||
private static String composeDurationMessage(final Period period, final int n) {
|
||||
switch (period) {
|
||||
case DAY:
|
||||
return CommonBundle.message("date.format.n.days", n);
|
||||
case MINUTE:
|
||||
return CommonBundle.message("date.format.n.minutes", n);
|
||||
case HOUR:
|
||||
return CommonBundle.message("date.format.n.hours", n);
|
||||
case MONTH:
|
||||
return CommonBundle.message("date.format.n.months", n);
|
||||
case WEEK:
|
||||
return CommonBundle.message("date.format.n.weeks", n);
|
||||
default:
|
||||
return CommonBundle.message("date.format.n.years", n);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String formatFrequency(long time) {
|
||||
return CommonBundle.message("date.frequency", formatBetweenDates(time, 0));
|
||||
|
||||
@@ -543,7 +543,11 @@ public class StringUtilTest {
|
||||
public void testFormatDurationApproximate() {
|
||||
assertEquals("2 m", StringUtil.formatDurationApproximate(120000));
|
||||
assertEquals("2 m 3 s", StringUtil.formatDurationApproximate(123000));
|
||||
assertEquals("2 m 4 s", StringUtil.formatDurationApproximate(123789));
|
||||
assertEquals("2 m 3 s", StringUtil.formatDurationApproximate(123456));
|
||||
assertEquals("1 h 1 m", StringUtil.formatDurationApproximate(3659009));
|
||||
assertEquals("2 h", StringUtil.formatDurationApproximate(7199000));
|
||||
assertEquals("1 d", StringUtil.formatDurationApproximate((23*60*60 + 59*60 + 59) * 1000L));
|
||||
assertEquals("1 yr 1 mo", StringUtil.formatDurationApproximate(33786061001L));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user