Skip to content

Commit e325d98

Browse files
fix: add duration formatter
1 parent 053ffa8 commit e325d98

File tree

8 files changed

+130
-9
lines changed

8 files changed

+130
-9
lines changed

src/containers/App/App.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import {registerLanguages} from '../../utils/monaco';
1414

1515
import './App.scss';
1616

17+
i18n.setLang(Lang.En);
18+
configureYdbUiComponents({lang: Lang.En});
19+
configureUiKit({lang: Lang.En});
20+
1721
registerLanguages();
1822

1923
class App extends React.Component {
@@ -24,13 +28,6 @@ class App extends React.Component {
2428
children: PropTypes.node,
2529
};
2630

27-
constructor(props) {
28-
super(props);
29-
i18n.setLang(Lang.En);
30-
configureYdbUiComponents({lang: Lang.En});
31-
configureUiKit({lang: Lang.En});
32-
}
33-
3431
componentDidMount() {
3532
const {isAuthenticated, getUser} = this.props;
3633
if (isAuthenticated) {

src/utils/constants.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ export const GIGABYTE = 1_000_000_000;
1414
export const TERABYTE = 1_000_000_000_000;
1515
export const GROUP = 'group';
1616

17-
export const HOUR_IN_SECONDS = 60 * 60;
17+
export const MINUTE_IN_SECONDS = 60;
18+
export const HOUR_IN_SECONDS = 60 * MINUTE_IN_SECONDS;
1819
export const DAY_IN_SECONDS = 24 * HOUR_IN_SECONDS;
1920

21+
export const NANOS_IN_MS = 1000000;
22+
2023
export const TABLET_STATES = {
2124
TABLET_VOLATILE_STATE_UNKNOWN: 'unknown',
2225
TABLET_VOLATILE_STATE_STOPPED: 'stopped',

src/utils/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import numeral from 'numeral';
22
import locales from 'numeral/locales'; // eslint-disable-line no-unused-vars
33

44
import {i18n} from './i18n';
5-
import {MEGABYTE, TERABYTE, DAY_IN_SECONDS, GIGABYTE} from './constants';
5+
import {MEGABYTE, TERABYTE, GIGABYTE, DAY_IN_SECONDS} from './constants';
66
import {isNumeric} from './utils';
77

88
numeral.locale(i18n.lang);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {formatDurationToShortTimeFormat} from '../formatDuration';
2+
import i18n from '../i18n';
3+
4+
describe('formatDurationToShortTimeFormat', () => {
5+
// 1 - ms
6+
const timeWithMs = 123;
7+
const formattedTimeWithMs = i18n('ms', {seconds: 0, ms: 123});
8+
9+
// 2 - seconds and ms
10+
const timeWithSecondsAndMsInMs = 12345;
11+
const formattedTimeWithSecondsAndMs = i18n('secMs', {seconds: 12, ms: 345});
12+
13+
// 3 - minutes
14+
const timeWithMinutesInMs = 754567;
15+
const formattedTimeWithMinutes = i18n('minSec', {minutes: 12, seconds: 34});
16+
17+
// 4 - hours
18+
const timeWithHoursInMs = 9245678;
19+
const formattedTimeWithHours = i18n('hoursMin', {hours: 2, minutes: 34});
20+
21+
// 5 - days
22+
const timeWithDaysInMs = 439234123;
23+
const formattedTimeWithDays = i18n('daysHours', {days: 5, hours: 2});
24+
25+
// 6 - zero
26+
const formattedZero = i18n('ms', {seconds: 0, ms: 0});
27+
28+
it('should return ms on values less than second', () => {
29+
expect(formatDurationToShortTimeFormat(timeWithMs)).toEqual(formattedTimeWithMs);
30+
});
31+
it('should return seconds and ms', () => {
32+
expect(formatDurationToShortTimeFormat(timeWithSecondsAndMsInMs)).toEqual(
33+
formattedTimeWithSecondsAndMs,
34+
);
35+
});
36+
it('should return minutes and seconds', () => {
37+
expect(formatDurationToShortTimeFormat(timeWithMinutesInMs)).toEqual(
38+
formattedTimeWithMinutes,
39+
);
40+
});
41+
it('should return hours and minutes', () => {
42+
expect(formatDurationToShortTimeFormat(timeWithHoursInMs)).toEqual(formattedTimeWithHours);
43+
});
44+
it('should return days and hours', () => {
45+
expect(formatDurationToShortTimeFormat(timeWithDaysInMs)).toEqual(formattedTimeWithDays);
46+
});
47+
it('should process zero values', () => {
48+
expect(formatDurationToShortTimeFormat(0)).toEqual(formattedZero);
49+
});
50+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {DAY_IN_SECONDS, HOUR_IN_SECONDS} from '../constants';
2+
3+
import i18n from './i18n';
4+
5+
/**
6+
* Process time difference in ms and returns formated time.
7+
* Only two major values are returned (days & hours, hours & minutes, minutes & seconds, etc.)
8+
*/
9+
export const formatDurationToShortTimeFormat = (value: number) => {
10+
const ms = value % 1000;
11+
let remain = Math.floor(value / 1000);
12+
13+
const days = Math.floor(remain / DAY_IN_SECONDS);
14+
remain = remain % DAY_IN_SECONDS;
15+
16+
const hours = Math.floor(remain / HOUR_IN_SECONDS);
17+
remain = remain % HOUR_IN_SECONDS;
18+
19+
const minutes = Math.floor(remain / 60);
20+
remain = remain % 60;
21+
22+
const seconds = remain;
23+
24+
const duration = {
25+
days,
26+
hours,
27+
minutes,
28+
seconds,
29+
ms,
30+
};
31+
32+
if (days > 0) {
33+
return i18n('daysHours', duration);
34+
}
35+
if (hours > 0) {
36+
return i18n('hoursMin', duration);
37+
}
38+
if (minutes > 0) {
39+
return i18n('minSec', duration);
40+
}
41+
if (seconds > 0) {
42+
return i18n('secMs', duration);
43+
}
44+
45+
return i18n('ms', duration);
46+
};

src/utils/timeParsers/i18n/en.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"daysHours": "{{days}}d {{hours}}h",
3+
"hoursMin": "{{hours}}h {{minutes}}m",
4+
"minSec": "{{minutes}}m {{seconds}}s",
5+
"secMs": "{{seconds}}s {{ms}}ms",
6+
"ms": "{{ms}}ms"
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {i18n, Lang} from '../../i18n';
2+
3+
import en from './en.json';
4+
import ru from './ru.json';
5+
6+
const COMPONENT = 'ydb-time-parsers';
7+
8+
i18n.registerKeyset(Lang.En, COMPONENT, en);
9+
i18n.registerKeyset(Lang.Ru, COMPONENT, ru);
10+
11+
export default i18n.keyset(COMPONENT);

src/utils/timeParsers/i18n/ru.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"daysHours": "{{days}}д {{hours}}ч",
3+
"hoursMin": "{{hours}}ч {{minutes}}м",
4+
"minSec": "{{minutes}}м {{seconds}}с",
5+
"secMs": "{{seconds}}с {{ms}}мс",
6+
"ms": "{{ms}}мс"
7+
}

0 commit comments

Comments
 (0)