Public
Edited
Aug 17, 2022
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
board = await BoardUrl.create(
"https://ijlyttle.github.io/pins-three-ways/pins/"
)
Insert cell
Insert cell
board.pinList()
Insert cell
Insert cell
timeArquero = await board
.pinFetch("time-arrow")
.then((response) => response.arrayBuffer())
.then(aq.fromArrow)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
datetime = timeArquero.column("datetime")
Insert cell
Insert cell
datetime.type
Insert cell
Insert cell
Insert cell
// Table -> [{column: String, unit: Number, timezone: String}]
aqTimestampMeta = function (table) {
// https://stackoverflow.com/a/7390612
const toType = function (obj) {
return {}.toString.call(obj).match(/\s([a-zA-Z]+)/)[1];
};

return table
.columnNames()
.map((x) => ({
column: x,
data: table.column(x)
}))
.filter((x) => toType(x.data) == "TimestampVector")
.map((x) => {
x.unit = x.data.type.unit;
x.timezone = x.data.type.timezone;
delete x.data;
return x;
});
}
Insert cell
Insert cell
timeArqueroMeta = aqTimestampMeta(timeArquero)
Insert cell
Insert cell
Insert cell
Insert cell
timestamp = timeArquero.array("datetime")[0]
Insert cell
Insert cell
aq.op.parse_date(timestamp)
Insert cell
Insert cell
toZonedDateTime(timestamp, "America/Denver").toString() // using .toString() only for display
Insert cell
Insert cell
toFakeUTC(timestamp, "America/Denver")
Insert cell
Insert cell
// Number, String -> ZonedDateTime
toZonedDateTime = function (num, tz) {
const date = aq.op.parse_date(num);
return date.toTemporalInstant().toZonedDateTimeISO(tz);
}
Insert cell
// Number, String -> Date
toFakeUTC = function (num, tz) {
const zdt = toZonedDateTime(num, tz);
return new Date(
Date.UTC(
zdt.year,
zdt.month - 1, // 😲 Temporal months start at 1
zdt.day,
zdt.hour,
zdt.minute,
zdt.second,
zdt.millisecond
)
);
}
Insert cell
Insert cell
Insert cell
Inputs.table(aqArrowTime(timeArquero, (x) => x))
Insert cell
Insert cell
Inputs.table(aqArrowTime(timeArquero, aq.op.parse_date))
Insert cell
Insert cell
Inputs.table(aqArrowTime(timeArquero, toZonedDateTime))
Insert cell
Insert cell
Inputs.table(aqArrowTime(timeArquero, toFakeUTC))
Insert cell
Insert cell
// Table, (Number, String -> *) -> Table
aqArrowTime = function (table, fn) {
const meta = aqTimestampMeta(table);

// operate on the columns named in meta
return meta.reduce(
(tbl, obj) =>
tbl.params(obj).derive({
[obj.column]: aq.escape((d, $) => fn(d[$.column], $.timezone))
}),
table
);
}
Insert cell
Insert cell
import { BoardUrl } from "@ijlyttle/using-pins-with-javascript";
Insert cell
arrow = require("apache-arrow@9.0.0/Arrow.es2015.min.js")
Insert cell
import { aq } from "@uwdata/arquero"
Insert cell
// Load the Temporal API using a Polyfill
// https://observablehq.com/@observablehq/discovering-date-patterns#Temporal
Temporal = {
const TemporalLib = await require("@js-temporal/polyfill@0.4.2");
return TemporalLib.Temporal;
}
Insert cell
toTemporalInstant = {
const TemporalLib = await require("@js-temporal/polyfill@0.4.2");
return TemporalLib.toTemporalInstant;
}
Insert cell
(Date.prototype.toTemporalInstant = toTemporalInstant)
Insert cell
Insert cell
<style> [role="mark"] { background: #EEEEEE; } </style>
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more