refactor(luxon): Plugin CircleCI - Swapped over from using DayJS to Luxon (+ humanize-duration)

Signed-off-by: Harry Hogg <hhogg@spotify.com>
This commit is contained in:
Harry Hogg
2021-09-16 12:59:31 +01:00
parent 14ebe91454
commit 47e21d8d69
4 changed files with 38 additions and 20 deletions
+3 -1
View File
@@ -41,8 +41,9 @@
"@material-ui/icons": "^4.9.1",
"@material-ui/lab": "4.0.0-alpha.57",
"circleci-api": "^4.0.0",
"dayjs": "^1.9.4",
"humanize-duration": "^3.27.0",
"lodash": "^4.17.15",
"luxon": "^2.0.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-lazylog": "^4.5.2",
@@ -58,6 +59,7 @@
"@testing-library/jest-dom": "^5.10.1",
"@testing-library/react": "^11.2.5",
"@testing-library/user-event": "^13.1.8",
"@types/humanize-duration": "^3.25.1",
"@types/jest": "^26.0.7",
"@types/node": "^14.14.32",
"@types/react-lazylog": "^4.5.0",
+23 -17
View File
@@ -13,25 +13,31 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { DateTime, Interval } from 'luxon';
import humanizeDuration from 'humanize-duration';
import dayjs from 'dayjs';
import durationPlugin from 'dayjs/plugin/duration';
import relativeTimePlugin from 'dayjs/plugin/relativeTime';
dayjs.extend(durationPlugin);
dayjs.extend(relativeTimePlugin);
type DateTimeObject = Date | string | number | undefined;
export function relativeTimeTo(time: DateTimeObject, withoutSuffix = false) {
return dayjs().to(dayjs(time), withoutSuffix);
export function relativeTimeTo(dateTimeISOString?: string) {
return dateTimeISOString
? DateTime.fromISO(dateTimeISOString).toRelative()
: '';
}
export function durationHumanized(
startTime: DateTimeObject,
endTime: DateTimeObject,
) {
return dayjs
.duration(dayjs(startTime).diff(dayjs(endTime || new Date())))
.humanize();
startDateTimeISOString?: string,
endDateTimeISOString?: string,
): string {
if (!startDateTimeISOString || !endDateTimeISOString) {
return '';
}
const startDateTime = DateTime.fromISO(startDateTimeISOString);
const endDateTime = DateTime.fromISO(endDateTimeISOString);
const duration = Interval.fromDateTimes(
startDateTime,
endDateTime,
).toDuration();
return humanizeDuration(duration.as('milliseconds'), {
largest: 1,
});
}