This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 974
/
clock.js
61 lines (57 loc) · 1.87 KB
/
clock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
const React = require('react')
class Clock extends React.Component {
constructor () {
super()
this.dateTimeFormat = new Intl.DateTimeFormat([], {hour: '2-digit', minute: '2-digit'})
this.state = this.getClockState(new Date())
}
get formattedTime () {
return this.state.currentTime.map(component => {
if (component.type === 'literal') {
// wrap ':' in a span with a class, so it can be centered
if (component.value === ':') {
return <span className='timeSeparator'>{component.value}</span>
} else if (component.value.trim() === '') {
// hide blank strings
return null
}
} else if (component.type === 'dayperiod') {
// hide day-period (AM / PM), it's rendered in a separate component
return null
}
return component.value
})
}
get formattedTimePeriod () {
const time = this.state.currentTime
const period = time.find(component => component.type === 'dayperiod')
return period ? period.value : ''
}
getMinutes (date) {
return Math.floor(date / 1000 / 60)
}
maybeUpdateClock () {
const now = new Date()
if (this.getMinutes(this.state.date) !== this.getMinutes(now)) {
this.setState(this.getClockState(now))
}
}
getClockState (now) {
return {
date: now,
currentTime: this.dateTimeFormat.formatToParts(now)
}
}
componentDidMount () {
window.setInterval(this.maybeUpdateClock.bind(this), 2000)
}
render () {
return <div className='clock'>
<span className='time'>{this.formattedTime}</span><span className='timePeriod'>{this.formattedTimePeriod}</span>
</div>
}
}
module.exports = Clock