Created
April 20, 2020 14:51
-
-
Save kkonyshev/f70828cd5afd027a1466a9bef5beefab to your computer and use it in GitHub Desktop.
This file contains hidden or 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
1. sudo sh -c '(echo "#!/usr/bin/env sh" && curl -L https://github.com/lihaoyi/Ammonite/releases/download/2.0.4/2.13-2.0.4) > /usr/local/bin/amm && chmod +x /usr/local/bin/amm' && amm | |
2. create file: | |
val scalaLangVersion = "2.11" | |
val scalaLibVersion = "2.11.8" | |
import $ivy.`org.scala-lang:scala-compiler:2.11.8` | |
import $ivy.`org.scalaj:scalaj-time_2.11:0.8` | |
import scala.util.{Failure, Success, Try} | |
import org.joda.time._ | |
object DateTimeOps { | |
/** | |
* Helper for evaluating joda DateTime from strings | |
* Uses https://github.com/jorgeortiz85/scala-time wrapper around joda date-time library | |
* Some examples: | |
* - now: "DateTime.now" | |
* - relative date: "DateTime.now - 2.days" | |
* - exact date (uses DateTime.parse): "2020-01-01T00:00:00" | |
* | |
* @param dateTimeString | |
*/ | |
implicit class DateTimeStringOps(dateTimeString: String) { | |
import scala.reflect.runtime.universe | |
import scala.tools.reflect.ToolBox | |
val toolBox = universe.runtimeMirror(getClass.getClassLoader).mkToolBox() | |
private def evalSheet(expression: String): String = | |
s""" | |
import org.scala_tools.time.Imports._ | |
$expression | |
""" | |
def asDateTime: DateTime = Try(DateTime.parse(dateTimeString)) match { | |
case Success(value) => value | |
case Failure(_) => toolBox.eval(toolBox.parse(evalSheet(dateTimeString))).asInstanceOf[org.joda.time.DateTime] | |
} | |
} | |
} | |
import DateTimeOps._ | |
println("DateTime.now - 2.days".asDateTime) | |
3. run: amm scala-date-time-dsl.sc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment