Skip to content

Commit

Permalink
Merge pull request #982 from OneUptime/layer-preview
Browse files Browse the repository at this point in the history
Add useState hook to Layer component and pass
  • Loading branch information
simlarsen authored Nov 30, 2023
2 parents f4f96d2 + 11daa0b commit 24b71c4
Show file tree
Hide file tree
Showing 8 changed files with 586 additions and 147 deletions.
115 changes: 115 additions & 0 deletions Common/Types/Date.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import InBetween from './Database/InBetween';
import DayOfWeek from './Day/DayOfWeek';
import BadDataException from './Exception/BadDataException';
import { JSONObject, ObjectType } from './JSON';
import PositiveNumber from './PositiveNumber';
Expand All @@ -23,6 +24,19 @@ export default class OneUptimeDate {
return moment();
}

public static keepTimeButMoveDay(keepTimeFor: Date, moveDayTo: Date): Date {
keepTimeFor = this.fromString(keepTimeFor);
moveDayTo = this.fromString(moveDayTo);
return moment(moveDayTo)
.set({
hour: keepTimeFor.getHours(),
minute: keepTimeFor.getMinutes(),
second: keepTimeFor.getSeconds(),
millisecond: keepTimeFor.getMilliseconds(),
})
.toDate();
}

public static getOneMinAgo(): Date {
return this.getSomeMinutesAgo(new PositiveNumber(1));
}
Expand Down Expand Up @@ -87,6 +101,48 @@ export default class OneUptimeDate {
};
}

public static areOnTheSameDay(date1: Date, date2: Date): boolean {
date1 = this.fromString(date1);
date2 = this.fromString(date2);
return moment(date1).isSame(date2, 'day');
}

public static areOnTheSameMonth(date1: Date, date2: Date): boolean {
date1 = this.fromString(date1);
date2 = this.fromString(date2);
return moment(date1).isSame(date2, 'month');
}

public static areOnTheSameYear(date1: Date, date2: Date): boolean {
date1 = this.fromString(date1);
date2 = this.fromString(date2);
return moment(date1).isSame(date2, 'year');
}

public static areOnTheSameHour(date1: Date, date2: Date): boolean {
date1 = this.fromString(date1);
date2 = this.fromString(date2);
return moment(date1).isSame(date2, 'hour');
}

public static areOnTheSameMinute(date1: Date, date2: Date): boolean {
date1 = this.fromString(date1);
date2 = this.fromString(date2);
return moment(date1).isSame(date2, 'minute');
}

public static areOnTheSameSecond(date1: Date, date2: Date): boolean {
date1 = this.fromString(date1);
date2 = this.fromString(date2);
return moment(date1).isSame(date2, 'second');
}

public static areOnTheSameWeek(date1: Date, date2: Date): boolean {
date1 = this.fromString(date1);
date2 = this.fromString(date2);
return moment(date1).isSame(date2, 'week');
}

public static addRemoveMinutes(date: Date, minutes: number): Date {
date = this.fromString(date);
return moment(date).add(minutes, 'minutes').toDate();
Expand All @@ -97,6 +153,26 @@ export default class OneUptimeDate {
return moment(date).add(days, 'days').toDate();
}

public static addRemoveHours(date: Date, hours: number): Date {
date = this.fromString(date);
return moment(date).add(hours, 'hours').toDate();
}

public static addRemoveYears(date: Date, years: number): Date {
date = this.fromString(date);
return moment(date).add(years, 'years').toDate();
}

public static addRemoveMonths(date: Date, months: number): Date {
date = this.fromString(date);
return moment(date).add(months, 'months').toDate();
}

public static addRemoveWeeks(date: Date, weeks: number): Date {
date = this.fromString(date);
return moment(date).add(weeks, 'weeks').toDate();
}

public static addRemoveSeconds(date: Date, seconds: number): Date {
date = this.fromString(date);
return moment(date).add(seconds, 'seconds').toDate();
Expand Down Expand Up @@ -366,6 +442,45 @@ export default class OneUptimeDate {
return moment(date).isAfter(startDate);
}

public static isOnOrAfter(date: Date, startDate: Date): boolean {
date = this.fromString(date);
startDate = this.fromString(startDate);
return moment(date).isSameOrAfter(startDate);
}

public static getDayOfWeek(date: Date): DayOfWeek {
const dayOfWeek: number = this.geyDayOfWeekAsNumber(date);

if (dayOfWeek === 1) {
return DayOfWeek.Monday;
} else if (dayOfWeek === 2) {
return DayOfWeek.Tuesday;
} else if (dayOfWeek === 3) {
return DayOfWeek.Wednesday;
} else if (dayOfWeek === 4) {
return DayOfWeek.Thursday;
} else if (dayOfWeek === 5) {
return DayOfWeek.Friday;
} else if (dayOfWeek === 6) {
return DayOfWeek.Saturday;
} else if (dayOfWeek === 7) {
return DayOfWeek.Sunday;
}

throw new BadDataException('Invalid day of week');
}

public static geyDayOfWeekAsNumber(date: Date): number {
date = this.fromString(date);
return moment(date).isoWeekday();
}

public static isOnOrBefore(date: Date, endDate: Date): boolean {
date = this.fromString(date);
endDate = this.fromString(endDate);
return moment(date).isSameOrBefore(endDate);
}

public static isEqualBySeconds(date: Date, startDate: Date): boolean {
date = this.fromString(date);
startDate = this.fromString(startDate);
Expand Down
1 change: 1 addition & 0 deletions Common/Types/Events/EventInterval.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
enum EventInterval {
Hour = 'Hour',
Day = 'Day',
Week = 'Week',
Month = 'Month',
Expand Down
Loading

0 comments on commit 24b71c4

Please sign in to comment.