Skip to content

Instantly share code, notes, and snippets.

@xhanin
Created February 13, 2013 22:20
Show Gist options
  • Save xhanin/4948901 to your computer and use it in GitHub Desktop.
Save xhanin/4948901 to your computer and use it in GitHub Desktop.
ISO DateTime / Epoch timestamp converter plugin for IntellijEval
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.editor.SelectionModel
import org.joda.time.*
import static intellijeval.PluginUtil.*
// add-to-classpath $HOME/.m2/repository/joda-time/joda-time/2.1/joda-time-2.1.jar
registerAction("TransformDateISOEpoch", "ctrl alt shift T") { AnActionEvent event ->
def editor = currentEditorIn(event.project)
runDocumentWriteAction(event.project) {
SelectionModel selectionModel = editor.selectionModel
String selectedText = selectionModel.selectedText
if (selectedText == null) {
// try to auto expand selection to full iso date or epoch timestamp
def offset = editor.caretModel.offset
def start = offset
while (start > 0 && editor.document.text[start - 1] ==~ /[\dT\-\.\+:]/) {
start--
}
def end = offset
while (end < editor.document.text.length()
&& editor.document.text[end] ==~ /[\dT\-\.\+:]/) {
end++
}
selectionModel.setSelection(start, end)
}
transformSelectionIn(editor, { String s ->
try {
if (s ==~ /\d+/) {
// this is probably an epoch timestamp
return new DateTime(Long.valueOf(s)).toString()
} else {
// it's supposed to be an ISO date
return String.valueOf(DateTime.parse(s).millis)
}
} catch (Exception e) {
// if called with unsupported selection
show("unsupported selection $s : $e.message")
return s
}
}
)
}
}
show("Loaded 'TransformDateISOEpoch'
Select text in editor (either "
+ "an epoch timestamp or an ISO date) and press ctrl+shift+alt+T to run it")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment