Skip to content

Instant ​

Instant representa un punto absoluto en la línea temporal con segundos con signo y un resto de nanosegundos no negativo.

Cuando Tyrus serializa un Instant como JSON, usa la cadena ISO 8601 en UTC devuelta por instantToString, también en respuestas HTTP. OpenAPI describe este valor como type: "string" con format: "date-time", igual que valores como 2026-09-05T18:32:52.54345Z.

API ​

  • instantNow(): Instant lee el reloj nativo actual.
  • instantFrom(value: string): Result<Instant, TemporalError> analiza un instante ISO.
  • instantFromEpochMilliseconds(milliseconds: i64): Result<Instant, TemporalError> crea un instante a partir de milisegundos desde Unix Epoch.
  • instantToEpochMilliseconds(value: Instant): Result<i64, TemporalError> convierte un instante a milisegundos desde el epoch.
  • instantToString(value: Instant): Result<string, TemporalError> formatea un instante como texto ISO UTC.
  • instantAddDuration y instantSubtractDuration devuelven Result<Instant, TemporalError> y aplican duraciones de tamaño fijo; las unidades de calendario se rechazan.
  • instantToZonedDateTime(value: Instant, timeZone: TimeZone): ZonedDateTime asocia un instante con una zona IANA versionada en el repositorio.
tyrus
import { Duration, Instant, TemporalError, TimeZone, ZonedDateTime, instantAddDuration, instantFrom, instantFromEpochMilliseconds, instantNow, instantToEpochMilliseconds, instantToString, instantToZonedDateTime, timeZone } from "tyrus:temporal";

export const example = (): void => {
    const now: Instant = instantNow();
    const parsed: Instant = match instantFrom("2026-07-09T15:30:00Z") { Ok(value) => value, Err(error) => { printError(error.code); return; } };
    const epoch: Instant = match instantFromEpochMilliseconds(0) { Ok(value) => value, Err(error) => { printError(error.code); return; } };
    const milliseconds: i64 = match instantToEpochMilliseconds(epoch) { Ok(value) => value, Err(error) => { printError(error.code); return; } }; // 0
    const text: string = match instantToString(epoch) { Ok(value) => value, Err(error) => { printError(error.code); return; } }; // "1970-01-01T00:00:00Z"
    const second: Duration = { years: 0, months: 0, weeks: 0, days: 0, hours: 0, minutes: 0, seconds: 1, milliseconds: 0, microseconds: 0, nanoseconds: 0 };
    const later: Instant = match instantAddDuration(epoch, second) { Ok(value) => value, Err(error) => { printError(error.code); return; } };
    const paris: TimeZone = match timeZone("Europe/Paris") { Ok(value) => value, Err(error) => { printError(error.code); return; } };
    const zoned: ZonedDateTime = instantToZonedDateTime(parsed, paris);
};