2019-09-25 02:26:31 +05:00
|
|
|
const getFullSeconds = ({ startedAt, total }) => {
|
|
|
|
|
if (startedAt) {
|
|
|
|
|
return Math.floor((new Date() - startedAt) / 1000) + total;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return total;
|
|
|
|
|
};
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
|
export const createTimer = ({ hours, minutes, seconds }) => ({
|
|
|
|
|
startedAt: null,
|
|
|
|
|
total: hours * 60 * 60 + minutes * 60 + seconds,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const updateTimer = ({ startedAt }, parts) => ({
|
|
|
|
|
...createTimer(parts),
|
|
|
|
|
startedAt: startedAt && new Date(),
|
|
|
|
|
});
|
|
|
|
|
|
2020-02-03 18:42:31 +05:00
|
|
|
export const startTimer = timer => ({
|
2019-08-31 04:07:25 +05:00
|
|
|
startedAt: new Date(),
|
|
|
|
|
total: timer ? timer.total : 0,
|
|
|
|
|
});
|
|
|
|
|
|
2020-02-03 18:42:31 +05:00
|
|
|
export const stopTimer = timer => ({
|
2019-08-31 04:07:25 +05:00
|
|
|
startedAt: null,
|
|
|
|
|
total: getFullSeconds(timer),
|
|
|
|
|
});
|
|
|
|
|
|
2020-02-03 18:42:31 +05:00
|
|
|
export const getTimerParts = timer => {
|
2019-08-31 04:07:25 +05:00
|
|
|
const fullSeconds = getFullSeconds(timer);
|
|
|
|
|
|
|
|
|
|
const hours = Math.floor(fullSeconds / 3600);
|
|
|
|
|
const minutes = Math.floor((fullSeconds - hours * 3600) / 60);
|
|
|
|
|
const seconds = fullSeconds - hours * 3600 - minutes * 60;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
hours,
|
|
|
|
|
minutes,
|
|
|
|
|
seconds,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-03 18:42:31 +05:00
|
|
|
export const formatTimer = timer => {
|
2019-08-31 04:07:25 +05:00
|
|
|
const { hours, minutes, seconds } = getTimerParts(timer);
|
|
|
|
|
|
2020-02-03 18:42:31 +05:00
|
|
|
return [hours, ...[minutes, seconds].map(part => (part < 10 ? `0${part}` : part))].join(':');
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|