Skip to content

Commit

Permalink
Attempt to fix #972
Browse files Browse the repository at this point in the history
Remove code that causes #972. Most likely it will break something else,
but this is yet to figure.
  • Loading branch information
kossnocorp committed Dec 7, 2018
1 parent 721a722 commit 4ba1745
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 27 deletions.
11 changes: 11 additions & 0 deletions scripts/test/regression.sh
@@ -0,0 +1,11 @@
#!/bin/bash

# The script runs the regression tests.
#
# It's a part of the test process.

set -ex

export PATH="$(yarn bin):$PATH"

env TZ=America/Sao_Paulo babel-node ./test/regression/972.js
76 changes: 49 additions & 27 deletions src/toDate/index.js
Expand Up @@ -82,9 +82,11 @@ var patterns = {
* var result = toDate('+02014101', {additionalDigits: 1})
* //=> Fri Apr 11 2014 00:00:00
*/
export default function toDate (argument, dirtyOptions) {
export default function toDate(argument, dirtyOptions) {
if (arguments.length < 1) {
throw new TypeError('1 argument required, but only ' + arguments.length + ' present')
throw new TypeError(
'1 argument required, but only ' + arguments.length + ' present'
)
}

if (argument === null) {
Expand All @@ -93,20 +95,37 @@ export default function toDate (argument, dirtyOptions) {

var options = dirtyOptions || {}

var additionalDigits = options.additionalDigits == null ? DEFAULT_ADDITIONAL_DIGITS : toInteger(options.additionalDigits)
if (additionalDigits !== 2 && additionalDigits !== 1 && additionalDigits !== 0) {
var additionalDigits =
options.additionalDigits == null
? DEFAULT_ADDITIONAL_DIGITS
: toInteger(options.additionalDigits)
if (
additionalDigits !== 2 &&
additionalDigits !== 1 &&
additionalDigits !== 0
) {
throw new RangeError('additionalDigits must be 0, 1 or 2')
}

// Clone the date
if (argument instanceof Date ||
(typeof argument === 'object' && Object.prototype.toString.call(argument) === '[object Date]')
if (
argument instanceof Date ||
(typeof argument === 'object' &&
Object.prototype.toString.call(argument) === '[object Date]')
) {
// Prevent the date to lose the milliseconds when passed to new Date() in IE10
return new Date(argument.getTime())
} else if (typeof argument === 'number' || Object.prototype.toString.call(argument) === '[object Number]') {
} else if (
typeof argument === 'number' ||
Object.prototype.toString.call(argument) === '[object Number]'
) {
return new Date(argument)
} else if (!(typeof argument === 'string' || Object.prototype.toString.call(argument) === '[object String]')) {
} else if (
!(
typeof argument === 'string' ||
Object.prototype.toString.call(argument) === '[object String]'
)
) {
return new Date(NaN)
}

Expand Down Expand Up @@ -143,7 +162,6 @@ export default function toDate (argument, dirtyOptions) {
} else {
// get offset accurate to hour in timezones that change offset
offset = getTimezoneOffsetInMilliseconds(new Date(timestamp + time))
offset = getTimezoneOffsetInMilliseconds(new Date(timestamp + time + offset))
}

return new Date(timestamp + time + offset)
Expand All @@ -152,7 +170,7 @@ export default function toDate (argument, dirtyOptions) {
}
}

function splitDateString (dateString) {
function splitDateString(dateString) {
var dateStrings = {}
var array = dateString.split(patterns.dateTimeDelimeter)
var timeString
Expand Down Expand Up @@ -182,7 +200,7 @@ function splitDateString (dateString) {
return dateStrings
}

function parseYear (dateString, additionalDigits) {
function parseYear(dateString, additionalDigits) {
var patternYYY = patterns.YYY[additionalDigits]
var patternYYYYY = patterns.YYYYY[additionalDigits]

Expand Down Expand Up @@ -214,7 +232,7 @@ function parseYear (dateString, additionalDigits) {
}
}

function parseDate (dateString, year) {
function parseDate(dateString, year) {
// Invalid ISO-formatted year
if (year === null) {
return null
Expand Down Expand Up @@ -304,7 +322,7 @@ function parseDate (dateString, year) {
return null
}

function parseTime (timeString) {
function parseTime(timeString) {
var token
var hours
var minutes
Expand All @@ -331,8 +349,9 @@ function parseTime (timeString) {
return NaN
}

return (hours % 24) * MILLISECONDS_IN_HOUR +
minutes * MILLISECONDS_IN_MINUTE
return (
(hours % 24) * MILLISECONDS_IN_HOUR + minutes * MILLISECONDS_IN_MINUTE
)
}

// hh:mm:ss or hhmmss
Expand All @@ -346,16 +365,18 @@ function parseTime (timeString) {
return NaN
}

return (hours % 24) * MILLISECONDS_IN_HOUR +
return (
(hours % 24) * MILLISECONDS_IN_HOUR +
minutes * MILLISECONDS_IN_MINUTE +
seconds * 1000
)
}

// Invalid ISO-formatted time
return null
}

function parseTimezone (timezoneString) {
function parseTimezone(timezoneString) {
var token
var absoluteOffset

Expand All @@ -377,7 +398,7 @@ function parseTimezone (timezoneString) {
}

absoluteOffset = hours * MILLISECONDS_IN_HOUR
return (token[1] === '+') ? -absoluteOffset : absoluteOffset
return token[1] === '+' ? -absoluteOffset : absoluteOffset
}

// ±hh:mm or ±hhmm
Expand All @@ -390,14 +411,15 @@ function parseTimezone (timezoneString) {
return NaN
}

absoluteOffset = hours * MILLISECONDS_IN_HOUR + minutes * MILLISECONDS_IN_MINUTE
return (token[1] === '+') ? -absoluteOffset : absoluteOffset
absoluteOffset =
hours * MILLISECONDS_IN_HOUR + minutes * MILLISECONDS_IN_MINUTE
return token[1] === '+' ? -absoluteOffset : absoluteOffset
}

return 0
}

function dayOfISOWeekYear (isoWeekYear, week, day) {
function dayOfISOWeekYear(isoWeekYear, week, day) {
week = week || 0
day = day || 0
var date = new Date(0)
Expand All @@ -413,11 +435,11 @@ function dayOfISOWeekYear (isoWeekYear, week, day) {
var DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
var DAYS_IN_MONTH_LEAP_YEAR = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

function isLeapYearIndex (year) {
function isLeapYearIndex(year) {
return year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)
}

function validateDate (year, month, date) {
function validateDate(year, month, date) {
if (month < 0 || month > 11) {
return false
}
Expand All @@ -439,7 +461,7 @@ function validateDate (year, month, date) {
return true
}

function validateDayOfYearDate (year, dayOfYear) {
function validateDayOfYearDate(year, dayOfYear) {
if (dayOfYear < 1) {
return false
}
Expand All @@ -455,7 +477,7 @@ function validateDayOfYearDate (year, dayOfYear) {
return true
}

function validateWeekDate (year, week, day) {
function validateWeekDate(year, week, day) {
if (week < 0 || week > 52) {
return false
}
Expand All @@ -467,7 +489,7 @@ function validateWeekDate (year, week, day) {
return true
}

function validateTime (hours, minutes, seconds) {
function validateTime(hours, minutes, seconds) {
if (hours != null && (hours < 0 || hours >= 25)) {
return false
}
Expand All @@ -483,7 +505,7 @@ function validateTime (hours, minutes, seconds) {
return true
}

function validateTimezone (hours, minutes) {
function validateTimezone(hours, minutes) {
if (minutes != null && (minutes < 0 || minutes > 59)) {
return false
}
Expand Down
14 changes: 14 additions & 0 deletions test/regression/972.js
@@ -0,0 +1,14 @@
// This is a regression test for issue #972: https://github.com/date-fns/date-fns/issues/972

import format from '../../src/format'
import assert from 'assert'

if (process.env.TZ !== 'America/Sao_Paulo')
throw new Error('The test must be run with TZ=America/Sao_Paulo')

const result = format('2018-11-04', 'yyyy-MM-dd HH:mm')
const expectation = '2018-11-04 01:00'
assert(
result === expectation,
`The result should be equal ${expectation} but got ${result}`
)

0 comments on commit 4ba1745

Please sign in to comment.