You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
876 B
JavaScript
35 lines
876 B
JavaScript
"use strict";
|
|
|
|
const { Temporal } = require("@js-temporal/polyfill");
|
|
const asExpression = require("as-expression");
|
|
const unreachable = require("@joepie91/unreachable")("zapdb");
|
|
|
|
module.exports = function fromTemporal(object, withTimezone) {
|
|
let zoned = asExpression(() => {
|
|
if (object.constructor.name === "Instant") {
|
|
return object.toZonedDateTimeISO("UTC");
|
|
} else if (object.constructor.name === "ZonedDateTime") {
|
|
return object;
|
|
} else {
|
|
unreachable("Invalid Temporal type");
|
|
}
|
|
});
|
|
|
|
let normalized = (withTimezone)
|
|
? zoned
|
|
: zoned.withTimeZone("UTC");
|
|
|
|
return {
|
|
millisecond: normalized.millisecond,
|
|
second: normalized.second,
|
|
minute: normalized.minute,
|
|
hour: normalized.hour,
|
|
day: normalized.day,
|
|
month: normalized.month,
|
|
year: normalized.year,
|
|
timezone: (withTimezone)
|
|
? normalized.timeZone.toString()
|
|
: undefined
|
|
};
|
|
};
|