@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.
189 lines (188 loc) • 5.18 kB
JavaScript
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_TIME_REGEX,
RFC_3339_TIME_REGEX
} from "./constants.mjs";
import { DateTimeErrorCode } from "./errors.mjs";
import { StormDateTime } from "./storm-date-time.mjs";
import { isInstant } from "./utilities/is-instant.mjs";
export function serializeStormTime(date) {
return date.instant.toJSON();
}
export function deserializeStormTime(utcString) {
return isSetString(utcString) ? StormTime.create(utcString) : StormTime.create();
}
@Serializable()
class StormTime 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 StormTime.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 StormTime.create(Temporal.Now.instant());
}
/**
* Validate the input time 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_TIME_REGEX.test(datetime) && !RFC_3339_DATE_TIME_REGEX.test(datetime)) {
return {
code: DateTimeErrorCode.rfc_3339_format,
type: MessageType.ERROR
};
}
return null;
}
/**
* Creates a new instance of DateTime from a string with a specified format.
*
* @param time - The input value used to determine the current time
* @param options - The options to use
* @returns A new instance of StormTime with the time provided in the time parameter.
*/
static create(time, options = {}) {
return new StormTime(time, {
...options,
timeZone: StormDateTime.isDateTime(time) ? time.timeZoneId : void 0,
calendar: StormDateTime.isDateTime(time) ? time.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({
year: 1970,
month: 0,
day: 1
}).toInstant();
this.zonedDateTime = stormDateTime.zonedDateTime.with({
year: 1970,
month: 0,
day: 1
});
}
/**
* A function that validates the current Time object
*
* @returns A ValidationDetails object if the Time object is invalid, otherwise null
*/
validate() {
return StormTime.validate(this.zonedDateTime.epochMilliseconds);
}
/**
* Gets the year, using local time.
*/
getFullYear() {
return 1970;
}
/**
* Gets the year using Universal Coordinated Time (UTC).
*/
getUTCFullYear() {
return 1970;
}
/**
* Gets the month, using local time.
*/
getMonth() {
return 0;
}
/**
* Gets the month of a Date object using Universal Coordinated Time (UTC).
*/
getUTCMonth() {
return 0;
}
/**
* Gets the day-of-the-month, using local time.
*/
getDate() {
return 1;
}
/**
* Gets the day-of-the-month, using Universal Coordinated Time (UTC).
*/
getUTCDate() {
return 1;
}
/**
* Gets the day of the week, using local time.
*/
getDay() {
return 1;
}
/**
* Gets the day of the week using Universal Coordinated Time (UTC).
*/
getUTCDay() {
return 1;
}
/**
* It returns the duration between two dates.
*
* @param dateTimeTo - DateTime = DateTime.current
* @returns A duration object.
*/
getDuration(dateTimeTo = StormTime.current()) {
return this.instant.since(dateTimeTo.instant);
}
}
export { StormTime };