Skip to content

Commit 8ca3324

Browse files
committed
update useTime to use only seconds state
1 parent 28083dc commit 8ca3324

File tree

2 files changed

+34
-39
lines changed

2 files changed

+34
-39
lines changed

src/useTime.js

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,33 @@
11
import { useState, useEffect, useRef } from 'react';
2+
import { Time } from './utils';
23

34
export default function useTime(settings) {
45
const { format } = settings || {};
56

6-
const [seconds, setSeconds] = useState(0);
7-
const [minutes, setMinutes] = useState(0);
8-
const [hours, setHours] = useState(0);
9-
const [ampm, setAmPm] = useState('');
7+
const [seconds, setSeconds] = useState(Time.getSecondsFromTimeNow());
108
const intervalRef = useRef();
119

12-
function formatHours(hoursValue) {
13-
if (format === '12-hour') {
14-
const ampmValue = hoursValue >= 12 ? 'pm' : 'am';
15-
let formattedHours = hoursValue % 12;
16-
formattedHours = formattedHours || 12;
17-
return { hoursValue: formattedHours, ampmValue };
10+
function clearIntervalRef() {
11+
if (intervalRef.current) {
12+
clearInterval(intervalRef.current);
13+
intervalRef.current = undefined;
1814
}
19-
return { hoursValue, ampmValue: '' };
2015
}
2116

22-
function setCurrentTime() {
23-
const now = new Date();
24-
const secondsValue = now.getSeconds();
25-
const minutesValue = now.getMinutes();
26-
const { hoursValue, ampmValue } = formatHours(now.getHours());
27-
28-
setSeconds(secondsValue);
29-
setMinutes(minutesValue);
30-
setHours(hoursValue);
31-
setAmPm(ampmValue);
32-
}
33-
34-
3517
function start() {
3618
if (!intervalRef.current) {
37-
setCurrentTime();
38-
intervalRef.current = setInterval(() => setCurrentTime(), 1000);
39-
}
40-
}
41-
42-
function reset() {
43-
if (intervalRef.current) {
44-
clearInterval(intervalRef.current);
45-
intervalRef.current = undefined;
19+
intervalRef.current = setInterval(() => setSeconds(Time.getSecondsFromTimeNow()), 1000);
4620
}
47-
setSeconds(0);
48-
setMinutes(0);
49-
setHours(0);
50-
setAmPm('');
5121
}
5222

5323
// didMount effect
5424
useEffect(() => {
5525
start();
56-
return reset;
26+
return clearIntervalRef;
5727
}, []);
5828

5929

6030
return {
61-
seconds, minutes, hours, ampm,
31+
...Time.getFormattedTimeFromSeconds(seconds, format),
6232
};
6333
}

src/utils/Time.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,29 @@ export default class Time {
2121
}
2222
return 0;
2323
}
24+
25+
static getSecondsFromTimeNow() {
26+
const now = new Date();
27+
const currentTimestamp = now.getTime();
28+
const offset = (now.getTimezoneOffset() * 60);
29+
return (currentTimestamp / 1000) - offset;
30+
}
31+
32+
static getFormattedTimeFromSeconds(totalSeconds, format) {
33+
const { seconds: secondsValue, minutes, hours } = Time.getTimeFromSeconds(totalSeconds);
34+
let ampm = '';
35+
let hoursValue = hours;
36+
37+
if (format === '12-hour') {
38+
ampm = hours >= 12 ? 'pm' : 'am';
39+
hoursValue = hours % 12;
40+
}
41+
42+
return {
43+
seconds: secondsValue,
44+
minutes,
45+
hours: hoursValue,
46+
ampm,
47+
};
48+
}
2449
}

0 commit comments

Comments
 (0)