UNPKG

@web5/credentials

Version:
115 lines 5.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isValidRFC3339Timestamp = exports.isValidXmlSchema112Timestamp = exports.getFutureXmlSchema112Timestamp = exports.getXmlSchema112Timestamp = exports.getCurrentXmlSchema112Timestamp = void 0; /** * Retrieves the current timestamp in XML Schema 1.1.2 date-time format. * * This function omits the milliseconds part from the ISO 8601 timestamp, returning a date-time * string in the format "yyyy-MM-ddTHH:mm:ssZ". * * @example * ```ts * const currentTimestamp = getCurrentXmlSchema112Timestamp(); // "2023-08-23T12:34:56Z" * ``` * * @returns The current timestamp in XML Schema 1.1.2 format. */ function getCurrentXmlSchema112Timestamp() { // Omit the milliseconds part from toISOString() output return new Date().toISOString().replace(/\.\d+Z$/, 'Z'); } exports.getCurrentXmlSchema112Timestamp = getCurrentXmlSchema112Timestamp; /** * Converts a UNIX timestamp to an XML Schema 1.1.2 compliant date-time string, omitting milliseconds. * * This function takes a UNIX timestamp (number of seconds since the UNIX epoch) as input and converts it * to a date-time string formatted according to XML Schema 1.1.2 specifications, specifically omitting * the milliseconds component from the standard ISO 8601 format. This is useful for generating * timestamps for verifiable credentials and other applications requiring precision to the second * without the need for millisecond granularity. * * @param timestampInSeconds The UNIX timestamp to convert, measured in seconds. * @example * ```ts * const issuanceDate = getXmlSchema112Timestamp(1633036800); // "2021-10-01T00:00:00Z" * ``` * * @returns A date-time string in the format "yyyy-MM-ddTHH:mm:ssZ", compliant with XML Schema 1.1.2, based on the provided UNIX timestamp. */ function getXmlSchema112Timestamp(timestampInSeconds) { var date = new Date(timestampInSeconds * 1000); // Format the date to an ISO string and then remove milliseconds return date.toISOString().replace(/\.\d{3}/, ''); } exports.getXmlSchema112Timestamp = getXmlSchema112Timestamp; /** * Calculates a future timestamp in XML Schema 1.1.2 date-time format based on a given number of * seconds. * * This function takes a number of seconds and adds it to the current timestamp, returning a * date-time string in the format "yyyy-MM-ddTHH:mm:ssZ" without milliseconds. * * @example * ```ts * const futureTimestamp = getFutureXmlSchema112Timestamp(60); // "2023-08-23T12:35:56Z" * ``` * * @param secondsInFuture - The number of seconds to project into the future. * @returns The future timestamp in XML Schema 1.1.2 format. */ function getFutureXmlSchema112Timestamp(secondsInFuture) { var futureDate = new Date(Date.now() + secondsInFuture * 1000); return futureDate.toISOString().replace(/\.\d+Z$/, 'Z'); } exports.getFutureXmlSchema112Timestamp = getFutureXmlSchema112Timestamp; /** * Validates a timestamp string against the XML Schema 1.1.2 date-time format. * * This function checks whether the provided timestamp string conforms to the * format "yyyy-MM-ddTHH:mm:ssZ", without milliseconds, as defined in XML Schema 1.1.2. * * @example * ```ts * const isValid = isValidXmlSchema112Timestamp('2023-08-23T12:34:56Z'); // true * ``` * * @param timestamp - The timestamp string to validate. * @returns `true` if the timestamp is valid, `false` otherwise. */ function isValidXmlSchema112Timestamp(timestamp) { // Format: yyyy-MM-ddTHH:mm:ssZ var regex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/; if (!regex.test(timestamp)) { return false; } var date = new Date(timestamp); return !isNaN(date.getTime()); } exports.isValidXmlSchema112Timestamp = isValidXmlSchema112Timestamp; /** * Validates a timestamp string against the RFC 3339 format. * * This function checks whether the provided timestamp string conforms to the * RFC 3339 standard, which includes full date and time representations with * optional fractional seconds and a timezone offset. The format allows for * both 'Z' (indicating UTC) and numeric timezone offsets (e.g., "-07:00", "+05:30"). * This validation ensures that the timestamp is not only correctly formatted * but also represents a valid date and time. * * @param timestamp - The timestamp string to validate. * @returns `true` if the timestamp is valid and conforms to RFC 3339, `false` otherwise. */ function isValidRFC3339Timestamp(timestamp) { // RFC 3339 format: yyyy-MM-ddTHH:mm:ss[.fractional-seconds]Z or yyyy-MM-ddTHH:mm:ss[.fractional-seconds]±HH:mm // This regex matches both 'Z' for UTC and timezone offsets like '-07:00' var regex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/; if (!regex.test(timestamp)) { return false; } // Parsing the timestamp to a Date object to check validity var date = new Date(timestamp); // Checking if the date is an actual date return !isNaN(date.getTime()); } exports.isValidRFC3339Timestamp = isValidRFC3339Timestamp; //# sourceMappingURL=utils.js.map