/** * Indicates a date or string is not a valid representation of a datetime * according to the atproto * {@link https://atproto.com/specs/lexicon#datetime specification}. */ export declare class InvalidDatetimeError extends Error { } /** * A subset of {@link DatetimeString} that represent valid datetime strings with * the format: `YYYY-MM-DDTHH:mm:ss.sssZ`, as returned by `Date.toISOString() * for dates between the years 0000 and 9999. * * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString} */ export type ISODatetimeString = `${string}-${string}-${string}T${string}:${string}:${string}.${string}Z`; /** * Represents a {@link Date} that can be safely stringified into a valid atproto * {@link DatetimeString} using the {@link Date.toISOString toISOString()} * method. */ export interface AtprotoDate extends Date { toISOString(): ISODatetimeString; } /** * @see {@link AtprotoDate} */ export declare function assertAtprotoDate(date: Date): asserts date is AtprotoDate; /** * @see {@link AtprotoDate} */ export declare function asAtprotoDate(date: Date): AtprotoDate; /** * @see {@link AtprotoDate} */ export declare function isAtprotoDate(date: Date): date is AtprotoDate; /** * @see {@link AtprotoDate} */ export declare function ifAtprotoDate(date: Date): AtprotoDate | undefined; /** * Datetime strings in atproto data structures and API calls should meet the * {@link https://ijmacd.github.io/rfc3339-iso8601/ intersecting} requirements * of the RFC 3339, ISO 8601, and WHATWG HTML datetime standards. * * @note This literal template type is not accurate enough to ensure that a * string is a valid atproto datetime. The {@link DatetimeString} validation * functions ({@link assertDatetimeString}, {@link isDatetimeString}, etc) * should be used to validate that a string meets the atproto datetime * requirements, and the {@link toDatetimeString} function should be used to * convert a {@link Date} object into a valid {@link DatetimeString} string. * * @example "2024-01-15T12:30:00Z" * @example "2024-01-15T12:30:00.000Z" * @example "2024-01-15T12:30:00+00:00" * @example "2024-01-15T11:30:00-01:00" * @see {@link https://atproto.com/specs/lexicon#datetime atproto Lexicon datetime format} * @see {@link https://www.rfc-editor.org/rfc/rfc3339 RFC 3339} * @see {@link https://www.iso.org/iso-8601-date-and-time-format.html ISO 8601} */ export type DatetimeString = `${string}-${string}-${string}T${string}:${string}:${string}Z` | `${string}-${string}-${string}T${string}:${string}:${string}${'+' | '-'}${string}:${string}`; /** * Validates that a string is a valid {@link DatetimeString} format string, * throwing an error if it is not. * * @throws InvalidDatetimeError if the input string does not meet the atproto 'datetime' format requirements. * @see {@link DatetimeString} */ export declare function assertDatetimeString(input: I): asserts input is I & DatetimeString; /** * Casts a string to a {@link DatetimeString} if it is a valid datetime format * string, throwing an error if it is not. * * @throws InvalidDatetimeError if the input string does not meet the atproto 'datetime' format requirements. * @see {@link DatetimeString} */ export declare function asDatetimeString(input: I): I & DatetimeString; /** * Checks if a string is a valid {@link DatetimeString} format string. * * @see {@link DatetimeString} */ export declare function isDatetimeString(input: I): input is I & DatetimeString; /** * Returns the input if it is a valid {@link DatetimeString} format string, or * `undefined` if it is not. * * @see {@link DatetimeString} */ export declare function ifDatetimeString(input: I): undefined | (I & DatetimeString); /** * Returns the current date and time as a {@link DatetimeString}. * * @see {@link DatetimeString} */ export declare function currentDatetimeString(): DatetimeString; /** * Converts any {@link Date} into a {@link DatetimeString} if possible, throwing * an error if the date is not a valid atproto datetime. * * This is short-hand for `asAtprotoDate(date).toISOString()`. * * @throws InvalidDatetimeError if the input date is not a valid atproto datetime (eg, it is too far in the future or past, or it normalizes to a negative year). * @see {@link DatetimeString} */ export declare function toDatetimeString(date: Date): DatetimeString; /** * Takes a flexible datetime string and normalizes its representation. * * This function will work with any valid value that can be parsed as a date. It * *additionally* is more flexible about accepting datetimes that are missing * timezone information, and normalizing them to a valid atproto datetime. * * One use-case is a consistent, sortable string. Another is to work with older * invalid createdAt datetimes. * * @note This function might return different normalized strings for the same * input depending on the timezone of the machine it is run on, since it will * attempt to parse the input "as is" if it fails to parse with an explicit * timezone. * * @returns ISODatetimeString - a valid atproto datetime with millisecond precision (3 sub-second digits) and UTC timezone with trailing 'Z' syntax. * @throws InvalidDatetimeError - if the input string could not be parsed as a datetime, even with permissive parsing. */ export declare function normalizeDatetime(dtStr: string): ISODatetimeString; /** * Variant of {@link normalizeDatetime} which always returns a valid datetime * string. * * If a {@link InvalidDatetimeError} is encountered, returns the UNIX epoch time * as a UTC datetime (`1970-01-01T00:00:00.000Z`). * * @see {@link normalizeDatetime} */ export declare function normalizeDatetimeAlways(dtStr: string): ISODatetimeString; export { assertDatetimeString as ensureValidDatetime, isDatetimeString as isValidDatetime, }; //# sourceMappingURL=datetime.d.ts.map