Skip to content

Commit

Permalink
Merge pull request #176 from smiclea/timezone-fix
Browse files Browse the repository at this point in the history
Fix ignoring daylight saving time
  • Loading branch information
Dorin Paslaru authored Mar 29, 2018
2 parents 4ab3f05 + e1bcc39 commit 650386f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 28 deletions.
15 changes: 9 additions & 6 deletions src/components/molecules/DatetimePicker/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type Props = {
}
type State = {
showPicker: boolean,
date: ?Date,
date: ?moment$Moment,
}
class DatetimePicker extends React.Component<Props, State> {
itemMouseDown: boolean
Expand All @@ -76,7 +76,9 @@ class DatetimePicker extends React.Component<Props, State> {
}

componentWillMount() {
this.setState({ date: this.props.value })
if (this.props.value) {
this.setState({ date: moment(this.props.value) })
}
}

componentDidMount() {
Expand All @@ -100,23 +102,24 @@ class DatetimePicker extends React.Component<Props, State> {

if (!this.itemMouseDown && !path.find(n => n.className === 'rdtPicker')) {
if (this.state.date && this.state.showPicker) {
this.props.onChange(this.state.date)
this.props.onChange(this.state.date.toDate())
}
this.setState({ showPicker: false })
}
}

handleDropdownClick() {
if (this.state.showPicker && this.state.date) {
this.props.onChange(this.state.date)
this.props.onChange(this.state.date.toDate())
}

this.setState({ showPicker: !this.state.showPicker })
}

handleChange(date: Date) {
handleChange(newDate: Date) {
let date = moment(newDate)
if (this.props.timezone === 'utc') {
date = DateUtils.getLocalTime(date)
date = DateUtils.getLocalTime(newDate)
}

this.setState({ date })
Expand Down
14 changes: 6 additions & 8 deletions src/components/molecules/ScheduleItem/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,8 @@ class ScheduleItem extends React.Component<Props> {
if (zeroBasedIndex) {
let value = this.props.item.schedule[fieldName]

if (fieldName === 'hour') {
if (this.props.timezone === 'local') {
value = DateUtils.getLocalHour(value)
}
if (fieldName === 'hour' && this.props.timezone === 'local') {
value = DateUtils.getLocalHour(value)
}

return items[value + 1]
Expand Down Expand Up @@ -304,12 +302,12 @@ class ScheduleItem extends React.Component<Props> {

renderExpirationValue() {
let date = this.props.item.expiration_date && moment(this.props.item.expiration_date)
let labelDate = date
if (this.props.timezone === 'utc' && date) {
labelDate = DateUtils.getUtcTime(date)
}

if (this.props.item.enabled) {
let labelDate = date
if (this.props.timezone === 'utc' && date) {
labelDate = DateUtils.getUtcTime(date)
}
return this.renderLabel({ label: (labelDate && labelDate.format('DD/MM/YYYY hh:mm A')) || '-' })
}

Expand Down
1 change: 1 addition & 0 deletions src/types/Execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ export type Execution = {
number: number,
status: string,
created_at: Date,
updated_at: Date,
tasks: Task[],
}
1 change: 1 addition & 0 deletions src/types/MainItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export type MainItem = {
status: string,
tasks: Task[],
created_at: Date,
updated_at: Date,
origin_endpoint_id: string,
destination_endpoint_id: string,
instances: string[],
Expand Down
1 change: 1 addition & 0 deletions src/types/Task.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type Task = {
id: string,
status: string,
created_at: Date,
updated_at: Date,
progress_updates: ProgressUpdate[],
task_type: string,
instance: string,
Expand Down
25 changes: 11 additions & 14 deletions src/utils/DateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,28 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

// @flow

import moment from 'moment'

class DateUtils {
static getLocalTime(rawDate) {
let offset = (new Date().getTimezoneOffset() / 60) * -1

return moment(rawDate).add(offset, 'hours')
static getLocalTime(rawDate: ?Date | ?moment$Moment): moment$Moment {
return moment(rawDate).add(-new Date().getTimezoneOffset(), 'minutes')
}

static getUtcTime(rawDate) {
let offset = (new Date().getTimezoneOffset() / 60)
return moment(rawDate).add(offset, 'hours')
static getUtcTime(rawDate: ?Date | ?moment$Moment): moment$Moment {
return moment(rawDate).add(new Date().getTimezoneOffset(), 'minutes')
}

static getLocalHour(hour) {
let hourDate = new Date(2017, 0, 1, hour)
return moment(hourDate).add(-hourDate.getTimezoneOffset(), 'minutes').get('hours')
static getLocalHour(hour: number): number {
return moment('00', 'HH').add(-new Date().getTimezoneOffset(), 'minutes').add(hour, 'hours').get('hours')
}

static getUtcHour(hour) {
let hourDate = new Date(2017, 0, 1, hour)
return moment(hourDate).add(hourDate.getTimezoneOffset(), 'minutes').get('hours')
static getUtcHour(hour: number): number {
return moment('00', 'HH').add(new Date().getTimezoneOffset(), 'minutes').add(hour, 'hours').get('hours')
}

static getOrdinalDay(number) {
static getOrdinalDay(number: number) {
switch (number) {
case 1:
case 21:
Expand Down

0 comments on commit 650386f

Please sign in to comment.