Skip to content

Commit

Permalink
feat(date-time): Added the getDefaultTimeZone function to the `Stor…
Browse files Browse the repository at this point in the history
…mDateTime` class
  • Loading branch information
sullivanpj committed Oct 18, 2024
1 parent 8aba20e commit f2fac83
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 53 deletions.
54 changes: 32 additions & 22 deletions packages/date-time/src/storm-date-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ export function deserializeStormDateTime(utcString: JsonValue): StormDateTime {
*/
@Serializable()
export class StormDateTime extends Date {
/**
* A helper function to get the default time zone
*
* @returns The default time zone
*/
public static getDefaultTimeZone(): string {
return (
Temporal.Now.timeZoneId() ||
process.env.STORM_TIMEZONE ||
process.env.DEFAULT_TIMEZONE ||
process.env.TZ ||
"UTC"
);
}

/**
* Type-check to determine if `obj` is a `DateTime` object
*
Expand Down Expand Up @@ -159,19 +174,16 @@ export class StormDateTime extends Date {
*/
public static create = (
dateTime?: DateTimeInput,
options?: DateTimeOptions
options: DateTimeOptions = {}
) =>
new StormDateTime(dateTime, {
timeZone:
(StormDateTime.isDateTime(dateTime)
? dateTime.timeZoneId
: options?.timeZone) ??
process.env.STORM_TIMEZONE ??
Temporal.Now.timeZoneId(),
...options,
timeZone: StormDateTime.isDateTime(dateTime)
? dateTime.timeZoneId
: options?.timeZone,
calendar: StormDateTime.isDateTime(dateTime)
? dateTime.calendarId
: (options?.calendar ??
new Intl.DateTimeFormat().resolvedOptions().calendar)
: options?.calendar
});

/**
Expand Down Expand Up @@ -254,7 +266,7 @@ export class StormDateTime extends Date {
*/
#zonedDateTime: Temporal.ZonedDateTime = Temporal.Now.zonedDateTime(
new Intl.DateTimeFormat().resolvedOptions().calendar,
process.env.STORM_TIMEZONE ?? Temporal.Now.timeZoneId()
StormDateTime.getDefaultTimeZone()
);

/**
Expand All @@ -267,9 +279,12 @@ export class StormDateTime extends Date {
*/
#options: DateTimeOptions;

public constructor(dateTime?: DateTimeInput, options?: DateTimeOptions) {
public constructor(dateTime?: DateTimeInput, options: DateTimeOptions = {}) {
let _dateTime = dateTime;

options.timeZone ??= StormDateTime.getDefaultTimeZone();
options.calendar ??= new Intl.DateTimeFormat().resolvedOptions().calendar;

const input = dateTime;
if (!_dateTime && !options?.skipDefaulting) {
_dateTime = Temporal.Now.instant();
Expand All @@ -293,21 +308,16 @@ export class StormDateTime extends Date {
if (instant && StormDateTime.validate(_dateTime, options) === null) {
this.#instant = instant;

const timeZone =
options?.timeZone ||
process.env.STORM_TIMEZONE ||
process.env.TZ ||
Temporal.Now.timeZoneId();
this.#zonedDateTime = options?.calendar
? this.#instant.toZonedDateTime({
timeZone,
timeZone: options.timeZone,
calendar: options.calendar
})
: this.#instant.toZonedDateTimeISO(timeZone);
: this.#instant.toZonedDateTimeISO(options.timeZone);
}

this.#input = input;
this.#options = options ?? {};
this.#options = options;
}

/**
Expand Down Expand Up @@ -356,7 +366,7 @@ export class StormDateTime extends Date {
* An accessor that returns the `timeZoneId` string of the DateTime object
*/
public get timeZoneId(): string {
return this.#zonedDateTime.timeZoneId;
return this.#zonedDateTime.timeZoneId || StormDateTime.getDefaultTimeZone();
}

/**
Expand Down Expand Up @@ -840,7 +850,7 @@ export class StormDateTime extends Date {
public getPlainDate(): StormDateTime {
return StormDateTime.create(
this.#zonedDateTime.toPlainDate().toZonedDateTime({
timeZone: Temporal.Now.timeZoneId(),
timeZone: StormDateTime.getDefaultTimeZone(),
plainTime: undefined
}).epochMilliseconds,
{
Expand All @@ -858,7 +868,7 @@ export class StormDateTime extends Date {
public getPlainTime(): StormDateTime {
return StormDateTime.create(
this.#zonedDateTime.toPlainTime().toZonedDateTime({
timeZone: Temporal.Now.timeZoneId(),
timeZone: StormDateTime.getDefaultTimeZone(),
plainDate: Temporal.PlainDate.from({
year: 1970,
month: 0,
Expand Down
20 changes: 10 additions & 10 deletions packages/date-time/src/storm-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,24 +218,24 @@ export class StormDate extends StormDateTime {
*/
public static override create = (
date?: DateTimeInput,
options?: DateTimeOptions
options: DateTimeOptions = {}
) =>
new StormDate(date, {
timeZone:
(StormDateTime.isDateTime(date)
? date.timeZoneId
: options?.timeZone) ?? Temporal.Now.timeZoneId(),
calendar: StormDateTime.isDateTime(date)
? date.calendarId
: options?.calendar
...options,
timeZone: StormDateTime.isDateTime(date) ? date.timeZoneId : undefined,
calendar: StormDateTime.isDateTime(date) ? date.calendarId : undefined
});

public constructor(dateTime?: DateTimeInput, options?: DateTimeOptions) {
public constructor(dateTime?: DateTimeInput, options: DateTimeOptions = {}) {
super(dateTime, options);

const stormDateTime = StormDateTime.create(dateTime, options);
this.instant = stormDateTime.instant
.toZonedDateTimeISO("UTC")
.toZonedDateTimeISO(
options.timeZone ||
stormDateTime.timeZoneId ||
StormDateTime.getDefaultTimeZone()
)
.with({
hour: 0,
minute: 0,
Expand Down
20 changes: 10 additions & 10 deletions packages/date-time/src/storm-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,12 @@ export class StormTime extends StormDateTime {
*/
public static override create = (
time?: DateTimeInput,
options?: DateTimeOptions
options: DateTimeOptions = {}
) =>
new StormTime(time, {
timeZone:
(StormDateTime.isDateTime(time)
? time.timeZoneId
: options?.timeZone) ?? Temporal.Now.timeZoneId(),
calendar: StormDateTime.isDateTime(time)
? time.calendarId
: options?.calendar
...options,
timeZone: StormDateTime.isDateTime(time) ? time.timeZoneId : undefined,
calendar: StormDateTime.isDateTime(time) ? time.calendarId : undefined
});

/**
Expand Down Expand Up @@ -160,12 +156,16 @@ export class StormTime extends StormDateTime {
return null;
}

public constructor(dateTime?: DateTimeInput, options?: DateTimeOptions) {
public constructor(dateTime?: DateTimeInput, options: DateTimeOptions = {}) {
super(dateTime, options);

const stormDateTime = StormDateTime.create(dateTime, options);
this.instant = stormDateTime.instant
.toZonedDateTimeISO("UTC")
.toZonedDateTimeISO(
options.timeZone ||
stormDateTime.timeZoneId ||
StormDateTime.getDefaultTimeZone()
)
.with({
year: 1970,
month: 1,
Expand Down
28 changes: 19 additions & 9 deletions packages/date-time/src/utilities/format-since.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import type { Temporal } from "@js-temporal/polyfill";
import { StormError } from "@storm-stack/errors";
import { MessageType } from "@storm-stack/types";
import { DateTimeErrorCode } from "../errors";
import { StormDateTime } from "../storm-date-time";
import { isDateTime } from "./is-date-time";
Expand All @@ -27,9 +28,12 @@ const SECOND_ROUNDING_EPSILON = 0.000_000_1;

const parseMilliseconds = (milliseconds: number) => {
if (!Number.isFinite(milliseconds)) {
throw new StormError(DateTimeErrorCode.ms_format, {
message: "Method `parseMilliseconds` expected a finite number"
});
throw StormError.createValidation(
{ code: DateTimeErrorCode.ms_format, type: MessageType.ERROR },
{
message: "Method `parseMilliseconds` expected a finite number"
}
);
}

return {
Expand Down Expand Up @@ -130,9 +134,12 @@ export const formatSince = (
: dateTimeOrDuration.milliseconds;

if (!Number.isFinite(milliseconds)) {
throw new StormError(DateTimeErrorCode.ms_format, {
message: "Method `formatSince` expected a finite number"
});
throw StormError.createValidation(
{ code: DateTimeErrorCode.ms_format, type: MessageType.ERROR },
{
message: "Method `formatSince` expected a finite number"
}
);
}

// Adjust the milliseconds to be positive
Expand Down Expand Up @@ -249,9 +256,12 @@ export const formatSince = (

if (compact) {
if (!result[0]) {
throw new StormError(DateTimeErrorCode.formatting_failure, {
message: "Unexpected empty result"
});
throw StormError.createValidation(
{ code: DateTimeErrorCode.formatting_failure, type: MessageType.ERROR },
{
message: "Unexpected empty result"
}
);
}

return result[0];
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/utility-types/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ export type MessageDetails<
code: string;
message?: string;
type: TMessageType;
params?: string[];
params?: Record<string, any>;
}
| {
code?: string;
message: string;
type: TMessageType;
params?: string[];
params?: Record<string, any>;
};

export type HelpMessageDetails = MessageDetails<typeof MessageType.HELP>;
Expand Down

0 comments on commit f2fac83

Please sign in to comment.