Skip to content

Using Temporal.Instant for dates #162

@mgood

Description

@mgood

This is mostly just informative rather than a feature request at the moment, could be interesting to consider how to offer representing dateTime with the proposed Temporal.Instant instead of Date.

I had started using a Temporal polyfill mainly for working with Durations, and found it useful to represent the dates as an Instant.

So, I tried this quick-and-dirty mapping to substitute the ldkit handling for xsd.dateTime:

import { registerDataHandler } from 'ldkit';
import { xsd } from 'ldkit/namespaces';
import { Temporal } from '@js-temporal/polyfill';

declare module 'ldkit' {
	interface CustomDataTypes {
		[xsd.dateTime]: Temporal.Instant;
	}
}

registerDataHandler(
	xsd.dateTime,
	(literalValue: string) => {
		const m = literalValue.match(/^"([^"]+)"\^\^/);
		if (m) return Temporal.Instant.from(m[1]);
		return Temporal.Instant.from(literalValue);
	},
	(value: Temporal.Instant) => {
		return `"${value.toString()}"^^${xsd.dateTime}`;
	}
);

I saw ldkit has some helpers internally for converting the value^^type format, but they didn't seem to be available through the exported APIs. Do you have a suggestion for a better way to handle the string encoding & decoding?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions