Last active
August 22, 2018 14:06
-
-
Save ov7a/f3ec3bf72e544611efbc010f2842500f to your computer and use it in GitHub Desktop.
Fun with Durations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1s: | |
java.time.period -> java.time.format.DateTimeParseException: Text cannot be parsed to a Period | |
java.time -> PT1S | |
log4j -> PT1S | |
javafx.util -> 1000.0 ms | |
joda -> PT1S | |
joda.period -> PT1S | |
javax.xml -> java.lang.IllegalArgumentException: 1s | |
typesafe -> PT1S | |
scala -> 1 second | |
2s: | |
java.time.period -> java.time.format.DateTimeParseException: Text cannot be parsed to a Period | |
java.time -> PT2S | |
log4j -> PT2S | |
javafx.util -> 2000.0 ms | |
joda -> PT2S | |
joda.period -> PT2S | |
javax.xml -> java.lang.IllegalArgumentException: 2s | |
typesafe -> PT2S | |
scala -> 2 seconds | |
2m: | |
java.time.period -> java.time.format.DateTimeParseException: Text cannot be parsed to a Period | |
java.time -> PT2M | |
log4j -> PT2M | |
javafx.util -> 120000.0 ms | |
joda -> java.lang.IllegalArgumentException: Invalid format: "PT2M" | |
joda.period -> PT2M | |
javax.xml -> java.lang.IllegalArgumentException: 2m | |
typesafe -> PT2M | |
scala -> java.lang.NumberFormatException: format error 2m | |
2h: | |
java.time.period -> java.time.format.DateTimeParseException: Text cannot be parsed to a Period | |
java.time -> PT2H | |
log4j -> PT2H | |
javafx.util -> 7200000.0 ms | |
joda -> java.lang.IllegalArgumentException: Invalid format: "PT2H" | |
joda.period -> PT2H | |
javax.xml -> java.lang.IllegalArgumentException: 2h | |
typesafe -> PT2H | |
scala -> 2 hours | |
2d: | |
java.time.period -> P2D | |
java.time -> PT48H | |
log4j -> P2D | |
javafx.util -> java.lang.IllegalArgumentException: The time parameter must have a suffix of [ms|s|m|h] | |
joda -> java.lang.IllegalArgumentException: Invalid format: "P2D" | |
joda.period -> P2D | |
javax.xml -> java.lang.IllegalArgumentException: 2d | |
typesafe -> PT48H | |
scala -> 2 days | |
2y: | |
java.time.period -> P2Y | |
java.time -> java.time.format.DateTimeParseException: Text cannot be parsed to a Duration | |
log4j -> java.lang.IllegalArgumentException: Text cannot be parsed to a Duration: 2y | |
javafx.util -> java.lang.IllegalArgumentException: The time parameter must have a suffix of [ms|s|m|h] | |
joda -> java.lang.IllegalArgumentException: Invalid format: "P2Y" | |
joda.period -> P2Y | |
javax.xml -> java.lang.IllegalArgumentException: 2y | |
typesafe -> com.typesafe.config.ConfigException$BadValue: String: 1: Invalid value at 'val': Could not parse time unit 'y' (try ns, us, ms, s, m, h, d) | |
scala -> java.lang.NumberFormatException: format error 2y | |
1h30m: | |
java.time.period -> java.time.format.DateTimeParseException: Text cannot be parsed to a Period | |
java.time -> PT1H30M | |
log4j -> PT1H30M | |
javafx.util -> java.lang.IllegalArgumentException: The time parameter must have a suffix of [ms|s|m|h] | |
joda -> java.lang.IllegalArgumentException: Invalid format: "PT1H30M" | |
joda.period -> PT1H30M | |
javax.xml -> java.lang.IllegalArgumentException: 1h30m | |
typesafe -> com.typesafe.config.ConfigException$BadValue: String: 1: Invalid value at 'val': Could not parse duration number '1h30' | |
scala -> java.lang.NumberFormatException: format error 1h30m | |
86000000ms: | |
java.time.period -> java.time.format.DateTimeParseException: Text cannot be parsed to a Period | |
java.time -> java.time.format.DateTimeParseException: Text cannot be parsed to a Duration | |
log4j -> P59722DT5H20M | |
javafx.util -> 8.6E7 ms | |
joda -> java.lang.IllegalArgumentException: Invalid format: "PT86000000MS" | |
joda.period -> java.lang.IllegalArgumentException: Invalid format: "PT86000000MS" is malformed at "S" | |
javax.xml -> java.lang.IllegalArgumentException: 86000000ms | |
typesafe -> PT23H53M20S | |
scala -> 86000 seconds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.typesafe.config.ConfigFactory | |
import scala.util.Try | |
object TestDuration extends App { | |
val strings = Seq("1s", "2s", "2m", "2h", "2d", "2y", "1h30m", "86000000ms") | |
def getISOPrefix(duration: String): String = if (Set('y', 'd').contains(duration.last)) "P" else "PT" | |
val parseAndProcessDuration: Map[String, String => String] = Map( | |
"java.time" -> (duration => java.time.Duration.parse(s"${getISOPrefix(duration)}${duration.toUpperCase}").toString), | |
"java.time.period" -> (duration => java.time.Period.parse(s"${getISOPrefix(duration)}${duration.toUpperCase}").toString), | |
"javafx.util" -> (duration => javafx.util.Duration.valueOf(duration).toString), | |
"javax.xml" -> (duration => javax.xml.datatype.DatatypeFactory.newInstance().newDuration(duration).toString), | |
"log4j" -> (duration => org.apache.logging.log4j.core.appender.rolling.action.Duration.parse(duration).toString), | |
"joda" -> (duration => org.joda.time.Duration.parse(s"${getISOPrefix(duration)}${duration.toUpperCase}").toString), | |
"joda.period" -> (duration => org.joda.time.Period.parse(s"${getISOPrefix(duration)}${duration.toUpperCase}").toString), | |
"scala" -> (duration => scala.concurrent.duration.Duration.apply(duration).toString), | |
"typesafe" -> (duration => ConfigFactory.parseString(s"val = $duration").getDuration("val").toString) | |
) | |
strings.foreach { str => | |
println(s"\n$str:") | |
println(parseAndProcessDuration.mapValues(transform => Try(transform(str)).recover { case fail: Throwable => fail.toString }.get).mkString("\n")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment