-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e4e07c9
commit 346eafc
Showing
11 changed files
with
183 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
## Working on | ||
## v1.1.0 | ||
Responsive design | ||
Added local time | ||
|
||
## v1.0.1 | ||
Support for public paths on webpack build | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
function* counter() { | ||
let index = 0; | ||
while (index < Number.MAX_SAFE_INTEGER) { | ||
yield index++; | ||
} | ||
} | ||
|
||
class TimeUpdate { | ||
constructor () { | ||
this.counter = counter(); | ||
this.times = []; | ||
} | ||
|
||
addTime (date, callback) { | ||
let id = this.counter.next().value; | ||
this.times.push({ id, date, callback }); | ||
|
||
if (this.times.length === 1) { | ||
this.startTimer(); | ||
} | ||
|
||
return id; | ||
} | ||
|
||
removeTime (id) { | ||
let index = this.times.findIndex(time => time.id === id); | ||
if (index !== -1) { | ||
this.times.splice(index, 1); | ||
} | ||
|
||
if (this.times.length === 0) { | ||
this.stopTimer(); | ||
} | ||
} | ||
|
||
timer () { | ||
const now = Date.now(); | ||
|
||
this.times.forEach(time => { | ||
time.date.timeUpdate(now); | ||
time.callback(time.date); | ||
}) | ||
} | ||
|
||
startTimer () { | ||
this.interval = setInterval(this.timer.bind(this), 5000); | ||
} | ||
|
||
stopTimer () { | ||
clearInterval(this.interval); | ||
} | ||
} | ||
|
||
export default new TimeUpdate(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const BASE_URL = TIMEZONE_URL || 'http://localhost/timezone'; | ||
|
||
/* API Calls */ | ||
const search = async (url) => { | ||
let response = await fetch(url, { cache: 'force-cache' }); | ||
|
||
let json = await response.json(); | ||
|
||
return json; | ||
} | ||
|
||
/* Public API */ | ||
const searchTimezone = async (lat, lon) => { | ||
let url = `${BASE_URL}?lat=${lat}&lon=${lon}`; | ||
|
||
return await search(url); | ||
} | ||
|
||
export default { searchTimezone }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const twoDigits = (number) => number < 10 ? '0' + number : number; | ||
|
||
class TimezoneDate extends Date { | ||
constructor (timezone) { | ||
super(); | ||
|
||
this.diffTimezoneOffset = this.getTimezoneOffset() - timezone.offset; | ||
this.addMinutes(this.diffTimezoneOffset); | ||
|
||
this.timezone = timezone.timezone; | ||
this.timezoneOffset = timezone.offset; | ||
} | ||
|
||
timeUpdate (timestamp) { | ||
this.setTime(timestamp + this.diffTimezoneOffset * 60000); // 1000 * 60 | ||
} | ||
|
||
addMinutes(minutes) { | ||
if (minutes !== 0) { | ||
this.setTime(this.getTime() + minutes * 60000); // 1000 * 60 | ||
} | ||
} | ||
|
||
simpleTime() { | ||
let hours = twoDigits(this.getHours()); | ||
let minutes = twoDigits(this.getMinutes()); | ||
return `${hours}:${minutes}`; | ||
} | ||
|
||
getTimezoneOffset () { | ||
return this.timezoneOffset || super.getTimezoneOffset(); | ||
} | ||
} | ||
|
||
export default TimezoneDate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React, { PureComponent } from 'react'; | ||
import TimezoneAPI from '@/api/time/timezone-api.js'; | ||
import TimezoneDate from '@/api/time/timezone-date.js'; | ||
import TimeUpdate from '@/api/time/time-update.js'; | ||
|
||
class Time extends PureComponent { | ||
constructor (props) { | ||
super(props); | ||
this.searchTimezone(); | ||
} | ||
|
||
state = { | ||
hasTimezone: false | ||
} | ||
|
||
searchTimezone = () => { | ||
let { lat, lon } = this.props.location; | ||
if (!lat || !lon) return; | ||
|
||
TimezoneAPI.searchTimezone(lat, lon).then(timezone => { | ||
const date = new TimezoneDate(timezone); | ||
|
||
this.setState({ | ||
hasTimezone: true, | ||
date: date.simpleTime() | ||
}); | ||
|
||
this.timerId = TimeUpdate.addTime(date, (updatedDate) => { | ||
this.setState({ | ||
date: updatedDate.simpleTime() | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
componentWillUnmount() { | ||
TimeUpdate.removeTime(this.timerId); | ||
} | ||
|
||
render () { | ||
if (!this.state.hasTimezone) return (null); | ||
|
||
return (<div className={this.props.className}> { this.state.date } </div>) | ||
} | ||
} | ||
|
||
export default Time; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters