UNPKG

@storm-stack/date-time

Version:

This package includes a DateTime class, various utility functions for working with dates and times, and a number of formatting options.

218 lines (217 loc) 6.18 kB
import { Temporal } from "@js-temporal/polyfill"; import { Serializable } from "@storm-stack/serialization"; import { isBigInt, isDate, isNumber, isSetString, MessageType } from "@storm-stack/types"; import { DATE_TIME_INVALID_DATE, RFC_3339_DATE_REGEX, RFC_3339_DATE_TIME_REGEX } from "./constants.mjs"; import { DateTimeErrorCode } from "./errors.mjs"; import { StormDateTime } from "./storm-date-time.mjs"; import { isInstant } from "./utilities/is-instant.mjs"; import { validateDayOfMonth } from "./utilities/validate-day-of-month.mjs"; export function serializeStormDate(date) { return date.instant.toJSON(); } export function deserializeStormDate(utcString) { return isSetString(utcString) ? StormDate.create(utcString) : StormDate.create(); } @Serializable() class StormDate extends StormDateTime { /** * The current function returns a new DateTime object with the current date and time * @returns A new instance of DateTime with the current date and time. */ static now() { return StormDate.current().epochMilliseconds; } /** * The current function returns a new DateTime object with the current date and time * @returns A new instance of DateTime with the current date and time. */ static current() { return StormDate.create(Temporal.Now.instant()); } /** * The maximum function returns a new StormDateTime object with the maximum date and time * * @returns A new instance of StormDateTime with the maximum date and time. */ static minimum() { return StormDate.create(/* @__PURE__ */ new Date(-864e13)); } /** * The maximum function returns a new StormDateTime object with the maximum date and time * * @returns A new instance of StormDateTime with the maximum date and time. */ static maximum() { return StormDate.create(/* @__PURE__ */ new Date(864e13)); } /** * Validate the input date value * * @param dateTime - The date value to validate * @returns A boolean representing whether the value is a valid *date-time* */ static validate(value) { if ((isDate(value) || StormDateTime.isDateTime(value)) && value.toString() === DATE_TIME_INVALID_DATE) { return { code: DateTimeErrorCode.rfc_3339_format, type: MessageType.ERROR }; } if (StormDateTime.isDateTime(value)) { return value.validate(); } if (isInstant(value)) { if (value.epochMilliseconds) { return null; } return { code: DateTimeErrorCode.invalid_instant, type: MessageType.ERROR }; } let datetime; if (isDate(value) || isNumber(value) || isBigInt(value)) { const date = isNumber(value) || isBigInt(value) ? new Date(Number(value)) : value; if (Number.isNaN(date.getTime())) { return { code: DateTimeErrorCode.invalid_time, type: MessageType.ERROR }; } datetime = date.toISOString(); } else { datetime = value === null || value === void 0 ? void 0 : value.toUpperCase(); } if (!datetime) { return { code: DateTimeErrorCode.invalid_value, type: MessageType.ERROR }; } if (!RFC_3339_DATE_REGEX.test(datetime) && !RFC_3339_DATE_TIME_REGEX.test(datetime)) { return { code: DateTimeErrorCode.rfc_3339_format, type: MessageType.ERROR }; } return validateDayOfMonth(StormDate.create(value)); } /** * Creates a new StormDate object with the given date and time * * @param date - The date to use * @param options - The options to use * @returns A new instance of DateTime with the given date and time. */ static create(date, options = {}) { return new StormDate(date, { ...options, timeZone: StormDateTime.isDateTime(date) ? date.timeZoneId : void 0, calendar: StormDateTime.isDateTime(date) ? date.calendarId : void 0 }); } constructor(dateTime, options = {}) { super(dateTime, options); const stormDateTime = StormDateTime.create(dateTime, options); this.instant = stormDateTime.instant.toZonedDateTimeISO( options.timeZone || stormDateTime.timeZoneId || StormDateTime.getDefaultTimeZone() ).with({ hour: 0, minute: 0, second: 0, millisecond: 0, microsecond: 0, nanosecond: 0 }).toInstant(); this.zonedDateTime = stormDateTime.zonedDateTime.with({ hour: 0, minute: 0, second: 0, millisecond: 0, microsecond: 0, nanosecond: 0 }); } /** * A function that validates the current Date object * * @returns A ValidationDetails object if the Date object is invalid, otherwise null */ validate() { return StormDate.validate(this.zonedDateTime.epochMilliseconds); } /** * Gets the hours in a date, using local time. */ getHours() { return 0; } /** * Gets the hours value in a Date object using Universal Coordinated Time (UTC). */ getUTCHours() { return 0; } /** * Gets the minutes of a Date object, using local time. */ getMinutes() { return 0; } /** * Gets the minutes of a Date object using Universal Coordinated Time (UTC). */ getUTCMinutes() { return 0; } /** * Gets the seconds of a Date object, using local time. */ getSeconds() { return 0; } /** * Gets the seconds of a Date object using Universal Coordinated Time (UTC). */ getUTCSeconds() { return 0; } /** * Gets the milliseconds of a Date, using local time. */ getMilliseconds() { return 0; } /** * Gets the milliseconds of a Date object using Universal Coordinated Time (UTC). */ getUTCMilliseconds() { return 0; } /** * Gets the difference in minutes between the time on the local computer and Universal Coordinated Time (UTC). */ getTimezoneOffset() { return 0; } /** * It returns the duration between two dates. * * @param dateTimeTo - DateTime = DateTime.current * @returns A duration object. */ getDuration(dateTimeTo = StormDate.current()) { return this.instant.since(dateTimeTo.instant); } } export { StormDate };