move format time to time util
This commit is contained in:
parent
a5653763d0
commit
27104d0f7a
|
@ -1,7 +1,7 @@
|
||||||
import { Button, DialogActions, DialogContent, Stack } from '@mui/material';
|
import { Button, DialogActions, DialogContent, Stack } from '@mui/material';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { ExportStats } from 'types/export';
|
import { ExportStats } from 'types/export';
|
||||||
import { formatDateTime } from 'utils/file';
|
import { formatDateTime } from 'utils/time';
|
||||||
import constants from 'utils/strings/constants';
|
import constants from 'utils/strings/constants';
|
||||||
import { FlexWrapper, Label, Value } from './Container';
|
import { FlexWrapper, Label, Value } from './Container';
|
||||||
import { ComfySpan } from './ExportInProgress';
|
import { ComfySpan } from './ExportInProgress';
|
||||||
|
|
|
@ -4,9 +4,9 @@ import { EnteFile } from 'types/file';
|
||||||
import constants from 'utils/strings/constants';
|
import constants from 'utils/strings/constants';
|
||||||
import {
|
import {
|
||||||
changeFileCreationTime,
|
changeFileCreationTime,
|
||||||
formatDateTime,
|
|
||||||
updateExistingFilePubMetadata,
|
updateExistingFilePubMetadata,
|
||||||
} from 'utils/file';
|
} from 'utils/file';
|
||||||
|
import { formatDateTime } from 'utils/time';
|
||||||
import EditIcon from '@mui/icons-material/Edit';
|
import EditIcon from '@mui/icons-material/Edit';
|
||||||
import { Label, Row, Value } from 'components/Container';
|
import { Label, Row, Value } from 'components/Container';
|
||||||
import { logError } from 'utils/sentry';
|
import { logError } from 'utils/sentry';
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import React, { useContext } from 'react';
|
import React, { useContext } from 'react';
|
||||||
import constants from 'utils/strings/constants';
|
import constants from 'utils/strings/constants';
|
||||||
import { formatDateTime } from 'utils/file';
|
import { formatDateTime } from 'utils/time';
|
||||||
import { RenderFileName } from './RenderFileName';
|
import { RenderFileName } from './RenderFileName';
|
||||||
import { ExifData } from './ExifData';
|
import { ExifData } from './ExifData';
|
||||||
import { RenderCreationTime } from './RenderCreationTime';
|
import { RenderCreationTime } from './RenderCreationTime';
|
||||||
|
|
|
@ -194,44 +194,6 @@ export function formatDate(date: number | Date) {
|
||||||
return dateTimeFormat.format(date);
|
return dateTimeFormat.format(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatDateTime(date: number | Date) {
|
|
||||||
const dateTimeFormat = new Intl.DateTimeFormat('en-IN', {
|
|
||||||
weekday: 'short',
|
|
||||||
year: 'numeric',
|
|
||||||
month: 'long',
|
|
||||||
day: 'numeric',
|
|
||||||
});
|
|
||||||
const timeFormat = new Intl.DateTimeFormat('en-IN', {
|
|
||||||
timeStyle: 'short',
|
|
||||||
});
|
|
||||||
return `${dateTimeFormat.format(date)} ${timeFormat.format(date)}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function formatDateRelative(date: number) {
|
|
||||||
const units = {
|
|
||||||
year: 24 * 60 * 60 * 1000 * 365,
|
|
||||||
month: (24 * 60 * 60 * 1000 * 365) / 12,
|
|
||||||
day: 24 * 60 * 60 * 1000,
|
|
||||||
hour: 60 * 60 * 1000,
|
|
||||||
minute: 60 * 1000,
|
|
||||||
second: 1000,
|
|
||||||
};
|
|
||||||
const relativeDateFormat = new Intl.RelativeTimeFormat('en-IN', {
|
|
||||||
localeMatcher: 'best fit',
|
|
||||||
numeric: 'always',
|
|
||||||
style: 'long',
|
|
||||||
});
|
|
||||||
const elapsed = date - Date.now();
|
|
||||||
|
|
||||||
// "Math.abs" accounts for both "past" & "future" scenarios
|
|
||||||
for (const u in units)
|
|
||||||
if (Math.abs(elapsed) > units[u] || u === 'second')
|
|
||||||
return relativeDateFormat.format(
|
|
||||||
Math.round(elapsed / units[u]),
|
|
||||||
u as Intl.RelativeTimeFormatUnit
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function sortFiles(files: EnteFile[]) {
|
export function sortFiles(files: EnteFile[]) {
|
||||||
// sort according to modification time first
|
// sort according to modification time first
|
||||||
files = files.sort((a, b) => {
|
files = files.sort((a, b) => {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { ElectronFile } from 'types/upload';
|
import { ElectronFile } from 'types/upload';
|
||||||
import { convertBytesToHumanReadable } from 'utils/file';
|
import { convertBytesToHumanReadable } from 'utils/file';
|
||||||
import { formatDateTime } from 'utils/file';
|
import { formatDateTime } from 'utils/time';
|
||||||
import { saveLogLine, getLogs } from 'utils/storage';
|
import { saveLogLine, getLogs } from 'utils/storage';
|
||||||
|
|
||||||
export function addLogLine(log: string) {
|
export function addLogLine(log: string) {
|
||||||
|
|
|
@ -163,3 +163,39 @@ function getDateFromComponents(dateComponent: DateComponent<string>) {
|
||||||
? new Date(year, month, day, hour, minute, second)
|
? new Date(year, month, day, hour, minute, second)
|
||||||
: new Date(year, month, day);
|
: new Date(year, month, day);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function formatDateTime(date: number | Date) {
|
||||||
|
const dateTimeFormat = new Intl.DateTimeFormat('en-IN', {
|
||||||
|
weekday: 'short',
|
||||||
|
year: 'numeric',
|
||||||
|
month: 'long',
|
||||||
|
day: 'numeric',
|
||||||
|
});
|
||||||
|
const timeFormat = new Intl.DateTimeFormat('en-IN', {
|
||||||
|
timeStyle: 'short',
|
||||||
|
});
|
||||||
|
return `${dateTimeFormat.format(date)} ${timeFormat.format(date)}`;
|
||||||
|
}
|
||||||
|
export function formatDateRelative(date: number) {
|
||||||
|
const units = {
|
||||||
|
year: 24 * 60 * 60 * 1000 * 365,
|
||||||
|
month: (24 * 60 * 60 * 1000 * 365) / 12,
|
||||||
|
day: 24 * 60 * 60 * 1000,
|
||||||
|
hour: 60 * 60 * 1000,
|
||||||
|
minute: 60 * 1000,
|
||||||
|
second: 1000,
|
||||||
|
};
|
||||||
|
const relativeDateFormat = new Intl.RelativeTimeFormat('en-IN', {
|
||||||
|
localeMatcher: 'best fit',
|
||||||
|
numeric: 'always',
|
||||||
|
style: 'long',
|
||||||
|
});
|
||||||
|
const elapsed = date - Date.now(); // "Math.abs" accounts for both "past" & "future" scenarios
|
||||||
|
|
||||||
|
for (const u in units)
|
||||||
|
if (Math.abs(elapsed) > units[u] || u === 'second')
|
||||||
|
return relativeDateFormat.format(
|
||||||
|
Math.round(elapsed / units[u]),
|
||||||
|
u as Intl.RelativeTimeFormatUnit
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue