Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

From Date to Temporal #316

Open
steida opened this issue Jan 10, 2024 · 1 comment
Open

From Date to Temporal #316

steida opened this issue Jan 10, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@steida
Copy link
Contributor

steida commented Jan 10, 2024

  • Allow to inject Temporal Now for tests
  • For all Temporal types, add branded string variant, e.g. PlainDateString. It will be iso compatible with SQLite.
  • update cast helper

https://github.com/fullcalendar/temporal-polyfill
https://tc39.es/proposal-temporal/docs/plaindate.html

@steida steida added enhancement New feature or request good first issue Good for newcomers labels Jan 10, 2024
@steida steida changed the title Allow to inject getDate to EvoluProvider From Date to Temporal Jan 22, 2024
@steida steida removed the good first issue Good for newcomers label Jan 22, 2024
@steida
Copy link
Contributor Author

steida commented Feb 10, 2024

Proposal:

import * as S from "@effect/schema/Schema";
import { Temporal } from "temporal-polyfill";

// https://www.sqlite.org/lang_datefunc.html

/**
 * UTC DateTime is stored in ISO 8601 format to ensure datetimes can be sorted
 * and compared lexicographically.
 */
export const SqliteDateTime = S.string.pipe(S.brand("SqliteDateTime"));
export type SqliteDateTime = S.Schema.To<typeof SqliteDateTime>;

/** YYYY-MM-DD. There is no UTC. We can't say without a time. */
export const SqliteDate = S.string.pipe(S.brand("SqliteDate"));
export type SqliteDate = S.Schema.To<typeof SqliteDate>;

/** HH:MM. There is no UTC. We can't say without a date. */
export const SqliteTime = S.string.pipe(S.brand("SqliteTime"));
export type SqliteTime = S.Schema.To<typeof SqliteTime>;

/** Cast plain Temporal values to UTC strings and vice-versa. */
export function castTemporal(
  timeZoneId: string,
  value: SqliteDateTime,
): Temporal.PlainDateTime;
export function castTemporal(
  timeZoneId: string,
  value: SqliteDate,
): Temporal.PlainDate;
export function castTemporal(
  timeZoneId: string,
  value: SqliteTime,
): Temporal.PlainTime;
export function castTemporal(
  timeZoneId: string,
  value: Temporal.PlainDateTime,
): SqliteDateTime;
export function castTemporal(
  timeZoneId: string,
  value: Temporal.PlainDate,
): SqliteDate;
export function castTemporal(
  timeZoneId: string,
  value: Temporal.PlainTime,
): SqliteTime;
export function castTemporal(
  timeZoneId: string,
  value:
    | SqliteDateTime
    | SqliteDate
    | SqliteTime
    | Temporal.PlainDateTime
    | Temporal.PlainDate
    | Temporal.PlainTime,
):
  | SqliteDateTime
  | SqliteDate
  | SqliteTime
  | Temporal.PlainDateTime
  | Temporal.PlainDate
  | Temporal.PlainTime {
  if (typeof value === "string") {
    switch (value.length) {
      case 10:
        return Temporal.PlainDate.from(value);
      case 5:
        return Temporal.PlainTime.from(value);
      default:
        return Temporal.Instant.from(value)
          .toZonedDateTimeISO(timeZoneId)
          .toPlainDateTime();
    }
  }
  if (value instanceof Temporal.PlainDateTime)
    return value
      .toZonedDateTime(timeZoneId)
      .toInstant()
      .toString() as SqliteDateTime;
  if (value instanceof Temporal.PlainDate)
    return value.toString() as SqliteDate;
  return value.toString().slice(0, 5) as SqliteTime;
}
``

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant