2019-10-20 12:39:01 +00:00
|
|
|
import React from 'react';
|
2020-08-10 18:49:46 +00:00
|
|
|
import Analog from 'react-clock';
|
2019-10-20 12:39:01 +00:00
|
|
|
|
2020-08-10 18:49:46 +00:00
|
|
|
export default class Clock extends React.PureComponent {
|
2019-10-20 12:39:01 +00:00
|
|
|
constructor(...args) {
|
|
|
|
super(...args);
|
2020-07-17 12:37:33 +00:00
|
|
|
|
|
|
|
this.timer = undefined;
|
2019-10-20 12:39:01 +00:00
|
|
|
this.state = {
|
2020-08-10 18:49:46 +00:00
|
|
|
time: '',
|
2020-07-17 12:37:33 +00:00
|
|
|
ampm: ''
|
2019-10-20 12:39:01 +00:00
|
|
|
};
|
|
|
|
}
|
2020-07-20 21:39:31 +00:00
|
|
|
|
2020-08-10 18:49:46 +00:00
|
|
|
startTime(time = localStorage.getItem('seconds') === 'true' || localStorage.getItem('analog') === 'true' ? (1000 - Date.now() % 1000) : (60000 - Date.now() % 60000)) {
|
2020-07-16 16:40:24 +00:00
|
|
|
this.timer = setTimeout(() => {
|
2020-07-16 19:02:26 +00:00
|
|
|
const now = new Date();
|
2020-07-20 21:39:31 +00:00
|
|
|
|
2020-08-10 18:49:46 +00:00
|
|
|
// Analog clock
|
2020-10-11 15:54:07 +00:00
|
|
|
if (localStorage.getItem('analog') === 'true') this.setState({ time: now });
|
|
|
|
else {
|
2020-08-10 18:49:46 +00:00
|
|
|
let sec = '';
|
2019-10-20 12:39:01 +00:00
|
|
|
|
2020-08-10 18:49:46 +00:00
|
|
|
// Extra 0
|
|
|
|
const zero = localStorage.getItem('zero');
|
|
|
|
|
|
|
|
if (localStorage.getItem('seconds') === 'true') {
|
|
|
|
if (zero === 'false') sec = `:${now.getSeconds()}`;
|
|
|
|
else sec = `:${('00' + now.getSeconds()).slice(-2)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (localStorage.getItem('24hour') === 'true') {
|
|
|
|
let time = '';
|
|
|
|
if (zero === 'false') time = `${now.getHours()}:${now.getMinutes()}${sec}`;
|
|
|
|
else time = `${('00' + now.getHours()).slice(-2)}:${('00' + now.getMinutes()).slice(-2)}${sec}`;
|
2020-10-11 15:54:07 +00:00
|
|
|
|
|
|
|
this.setState({ time: time });
|
2020-08-10 18:49:46 +00:00
|
|
|
} else {
|
|
|
|
// 12 hour support
|
|
|
|
let hours = now.getHours();
|
|
|
|
if (hours > 12) hours -= 12;
|
|
|
|
|
|
|
|
// Toggle AM/PM
|
|
|
|
let ampm = now.getHours() > 11 ? 'PM' : 'AM';
|
|
|
|
if (localStorage.getItem('ampm') === 'false') ampm = '';
|
|
|
|
|
|
|
|
let time = '';
|
|
|
|
if (zero === 'false') time = `${hours}:${now.getMinutes()}${sec}`;
|
|
|
|
else time = `${('00' + hours).slice(-2)}:${('00' + now.getMinutes()).slice(-2)}${sec}`;
|
2020-09-21 21:23:04 +00:00
|
|
|
|
2020-08-10 18:49:46 +00:00
|
|
|
this.setState({
|
|
|
|
time: time,
|
|
|
|
ampm: ampm
|
|
|
|
});
|
|
|
|
}
|
2020-07-16 19:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.startTime();
|
|
|
|
}, time);
|
2019-10-20 12:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-08-10 18:49:46 +00:00
|
|
|
if (localStorage.getItem('time') === 'false') return;
|
2020-07-16 16:40:24 +00:00
|
|
|
this.startTime(0);
|
2019-10-20 12:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-08-10 18:49:46 +00:00
|
|
|
let clockHTML = <h1 className='clock'>{this.state.time}<span className='ampm'>{this.state.ampm}</span> </h1>;
|
2020-08-26 13:32:52 +00:00
|
|
|
if (localStorage.getItem('analog') === 'true') clockHTML = <Analog className='analogclock' value={this.state.time} renderHourMarks={false} renderMinuteMarks={false} />;
|
2020-08-10 18:49:46 +00:00
|
|
|
return clockHTML;
|
2019-10-20 12:39:01 +00:00
|
|
|
}
|
|
|
|
}
|