Skip to content

Instantly share code, notes, and snippets.

@gabrielfeo
Last active April 4, 2023 18:01
Show Gist options
  • Save gabrielfeo/eb64564d6f88be86631eb3bebf86edd0 to your computer and use it in GitHub Desktop.
Save gabrielfeo/eb64564d6f88be86631eb3bebf86edd0 to your computer and use it in GitHub Desktop.
Kotlin ggdsl errors with datetime APIs. Don't use java.time. Don't use kotlinx.time LocalDate or LocalDateTime. Do use kotlinx.datetime.Instant.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"id": "bee18954",
"metadata": {},
"outputs": [
{
"data": {
"application/plot+json": {
"output": {
"data": {
"dates": [
1680631227760,
1680631228760
],
"values": [
0,
1
]
},
"data_meta": {
"series_annotations": [
{
"column": "dates",
"type": "datetime"
}
]
},
"kind": "plot",
"layers": [
{
"geom": "line",
"mapping": {
"x": "dates",
"y": "values"
},
"position": "identity",
"sampling": "none",
"stat": "identity"
}
],
"mapping": {},
"scales": [
{
"aesthetic": "x",
"breaks": [
1680631227760,
1680631228760
],
"datetime": true,
"format": "%H-%M-%S"
},
{
"aesthetic": "y"
}
]
},
"output_type": "lets_plot_spec"
},
"text/html": [
" <div id=\"OtZOLe\"></div>\n",
" <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
" (function() {\n",
" var plotSpec={\n",
"\"mapping\":{\n",
"},\n",
"\"data\":{\n",
"\"values\":[0.0,1.0],\n",
"\"dates\":[1.68063122776E12,1.68063122876E12]\n",
"},\n",
"\"kind\":\"plot\",\n",
"\"scales\":[{\n",
"\"aesthetic\":\"x\",\n",
"\"datetime\":true,\n",
"\"breaks\":[1680631227760,1680631228760],\n",
"\"format\":\"%H-%M-%S\"\n",
"},{\n",
"\"aesthetic\":\"y\"\n",
"}],\n",
"\"layers\":[{\n",
"\"mapping\":{\n",
"\"x\":\"dates\",\n",
"\"y\":\"values\"\n",
"},\n",
"\"stat\":\"identity\",\n",
"\"sampling\":\"none\",\n",
"\"position\":\"identity\",\n",
"\"geom\":\"line\",\n",
"\"data\":{\n",
"}\n",
"}],\n",
"\"data_meta\":{\n",
"\"series_annotations\":[{\n",
"\"column\":\"dates\",\n",
"\"type\":\"datetime\"\n",
"}]\n",
"}\n",
"};\n",
" var plotContainer = document.getElementById(\"OtZOLe\");\n",
" window.letsPlotCall(function() {{\n",
" LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
" }});\n",
" })(); \n",
" </script>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%useLatestDescriptors\n",
"%use ggdsl\n",
"\n",
"import kotlinx.datetime.*\n",
"\n",
"val now = java.time.Instant.now()\n",
"val before = now.minusSeconds(1)\n",
"val table = mapOf(\n",
" \"dates\" to listOf(before.toKotlinInstant(), now.toKotlinInstant()),\n",
" \"values\" to listOf(0, 1)\n",
")\n",
"\n",
"plot(table) {\n",
" line {\n",
" val scale = continuousPos<Instant>()\n",
" x(\"dates\"<Instant>().scaled(scale)).with {\n",
" axis {\n",
" breaks(table[\"dates\"] as List<Instant>, \"%H-%M-%S\")\n",
" }\n",
" }\n",
" y(\"values\"<Int>())\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "176a0253",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Kotlin",
"language": "kotlin",
"name": "kotlin"
},
"language_info": {
"codemirror_mode": "text/x-kotlin",
"file_extension": ".kt",
"mimetype": "text/x-kotlin",
"name": "kotlin",
"nbconvert_exporter": "",
"pygments_lexer": "kotlin",
"version": "1.8.20-Beta"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment