Permalink
Browse files

Refactor da conversao de string para milisegundos.

  • Loading branch information...
1 parent b5c4bbf commit 45f599eb5b33b19e372ea46916f2d3a92ec9cd07 @viniciostorres viniciostorres committed May 10, 2012
View
36 utils/src/main/java/br/com/ibnetwork/xingu/utils/TimeUtils.java
@@ -17,8 +17,15 @@ public static long toMillis(String value)
return -1;
}
- long factor = 0;
String number = number(value);
+ long factor = getFactor(value);
+ long v = Long.parseLong(number);
+ return v * factor;
+ }
+
+ private static long getFactor(String value)
+ {
+ long factor = 0;
if (value.endsWith("ms"))
{
factor = 1;
@@ -37,10 +44,9 @@ else if (value.endsWith("h"))
}
else if (value.endsWith("d"))
{
- factor = 1000 * 60 * 60 * 30;
+ factor = 1000 * 60 * 60 * 24;
}
- long v = Long.parseLong(number);
- return v * factor;
+ return factor;
}
private static String number(String value)
@@ -77,27 +83,7 @@ public static String toDays(long millis)
public static String toString(long millis, String unit)
{
- double factor = 0;
- if (unit.endsWith("ms"))
- {
- return millis + "ms";
- }
- else if (unit.endsWith("s"))
- {
- factor = 1000.0;
- }
- else if (unit.endsWith("m"))
- {
- factor = 1000.0 * 60;
- }
- else if (unit.endsWith("h"))
- {
- factor = 1000.0 * 60 * 60;
- }
- else if (unit.endsWith("d"))
- {
- factor = 1000.0 * 60 * 60 * 24;
- }
+ double factor = getFactor(unit);
return ((double) millis) / factor + unit;
}
View
2 utils/src/test/java/br/com/ibnetwork/xingu/utils/TimeUtilsTest.java
@@ -17,6 +17,6 @@ public void testToMillis()
assertEquals(2000, TimeUtils.toMillis("2s"));
assertEquals(60000, TimeUtils.toMillis("1m"));
assertEquals(3600000, TimeUtils.toMillis("1h"));
- assertEquals(108000000, TimeUtils.toMillis("1d"));
+ assertEquals(86400000, TimeUtils.toMillis("1d"));
}
}

0 comments on commit 45f599e

Please sign in to comment.