Skip to content

Commit

Permalink
Add new date time utility function to receive difference for a date i…
Browse files Browse the repository at this point in the history
…n days.
  • Loading branch information
linuspahl committed May 8, 2024
1 parent 8a41b7f commit bb70b95
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
15 changes: 11 additions & 4 deletions graylog2-web-interface/src/util/DateTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
import moment from 'moment-timezone';

import {
relativeDifference,
formatAsBrowserTime,
adjustFormat,
toUTCFromTz,
DATE_TIME_FORMATS,
adjustFormat,
formatAsBrowserTime,
getBrowserTimezone,
parseFromIsoString,
relativeDifference,
relativeDifferenceDays,
toDateObject,
toUTCFromTz,
} from 'util/DateTime';

const mockRootTimeZone = 'America/Chicago';
Expand Down Expand Up @@ -170,6 +171,12 @@ describe('DateTime utils', () => {
});
});

describe('relativeDifferenceDays', () => {
it('should return relative difference for time in days', () => {
expect(relativeDifferenceDays('2019-01-01T10:00:00.000Z')).toBe(364);
});
});

describe('toUTCFromTz', () => {
it('should transform time to UTC based on defined tz', () => {
expect(toUTCFromTz('2020-01-01T10:00:00.000', moscowTZ).toISOString()).toEqual('2020-01-01T07:00:00.000Z');
Expand Down
10 changes: 10 additions & 0 deletions graylog2-web-interface/src/util/DateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ export const relativeDifference = (dateTime: DateTime) => {
return validateDateTime(dateObject, dateTime).fromNow();
};

/**
* Returns the time, relative to the provided date time, in days.
*/
export const relativeDifferenceDays = (dateTime: DateTime) => {
const eventDateObject = toDateObject(dateTime);
const todayDateObject = toDateObject(new Date());

return todayDateObject.diff(eventDateObject, 'days');
};

/**
* Validate if the provided time has a supported format.
*/
Expand Down

0 comments on commit bb70b95

Please sign in to comment.