Skip to content
This repository was archived by the owner on Sep 27, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/custom-functions/bcode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ test('isV3Call', () => {
expect(isV3Call('', '')).toBeFalsy()
expect(isV3Call('2020Q1', 'net_sales')).toBeTruthy()
expect(isV3Call('LYLQ', 'net_sales')).toBeTruthy()
expect(isV3Call('2020-09-06', 'market_capital')).toBeTruthy()
})
2 changes: 1 addition & 1 deletion src/fiscal-periods/date-param.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('isLatest', () => {
})

test('parse', () => {
expect(DateParam.parse('2020-09-06')).toEqual(new DateParam(new Date(2020, 9, 6)))
expect(DateParam.parse('2020-09-06')).toEqual(new DateParam(new Date('2020-09-06')))
expect(DateParam.parse('latest')).toEqual(new DateParam('latest'))
expect(DateParam.parse('Latest')).toEqual(new DateParam('latest'))
expect(() => DateParam.parse('foo')).toThrow(ParseError)
Expand Down
5 changes: 1 addition & 4 deletions src/fiscal-periods/date-param.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ export class DateParam {
throw new ParseError(`Invalid date format: ${str}`)
}

const year = parseInt(matches[1], 10)
const month = parseInt(matches[2], 10)
const day = parseInt(matches[3], 10)
return new DateParam(new Date(year, month, day))
return new DateParam(new Date(str))
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

バグ修正の本体です。2つのバグがありました。

  • new Date() の引数は new Date(year, month, day) ではなく new Date(year, monthIndex, day) なので、 指定月-1 を渡すのが正しいのだが、そのまま月を渡していた
  • new Date(year, monthIndex, day) 形式で Date を生成すると自動的にタイムゾーンが GMT+0900 (Japan Standard Time) として解釈されるものの、 toISOString() 時に T15:00:00.000Z とUTCに自動的に変換されてしまうために、ロケールが日本の場合には-1日されて解釈されてしまう (?)

上記の問題はどちらも new Date('YYYY-MM-DD') のように文字列型で渡すことで解決したので、そのようにしています。

参考: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Date/Date

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

文字列渡すほうがむしろ安全っていうのがちょい残念だけど、なるほど。
真ん中が monthIndex だったの、見るたびに「あーそうだった」ってなるな・・・

}
}
10 changes: 5 additions & 5 deletions src/fiscal-periods/date-range.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DateRange } from '~/fiscal-periods/date-range'

test('diff', () => {
expect(new DateRange(new Date(2020, 9, 6), new Date(2021, 9, 6)).diff()).toEqual(365)
expect(new DateRange(new Date(2020, 9, 6), new Date(2020, 9, 7)).diff()).toEqual(1)
expect(new DateRange(new Date(2020, 9, 6), new Date(2020, 9, 6)).diff()).toEqual(0)
expect(new DateRange(new Date(2020, 9, 6), new Date(2020, 9, 5)).diff()).toEqual(-1)
expect(new DateRange(new Date(2020, 9, 6), new Date(2019, 9, 6)).diff()).toEqual(-366)
expect(new DateRange(new Date('2020-09-06'), new Date('2021-09-06')).diff()).toEqual(365)
expect(new DateRange(new Date('2020-09-06'), new Date('2020-09-07')).diff()).toEqual(1)
expect(new DateRange(new Date('2020-09-06'), new Date('2020-09-06')).diff()).toEqual(0)
expect(new DateRange(new Date('2020-09-06'), new Date('2020-09-05')).diff()).toEqual(-1)
expect(new DateRange(new Date('2020-09-06'), new Date('2019-09-06')).diff()).toEqual(-366)
})
2 changes: 1 addition & 1 deletion src/fiscal-periods/period-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test('parse', () => {
expect(PeriodParser.parse('LY-1Q4')).toEqual(new YearQuarterParam(new LyWithOffset(-1), 4))
expect(PeriodParser.parse('2020LQ-1')).toEqual(new YearQuarterParam(2020, new LqWithOffset(-1)))
expect(PeriodParser.parse('LY-1LQ-1')).toEqual(new YearQuarterParam(new LyWithOffset(-1), new LqWithOffset(-1)))
expect(PeriodParser.parse('2020-09-06')).toEqual(new DateParam(new Date(2020, 9, 6)))
expect(PeriodParser.parse('2020-09-06')).toEqual(new DateParam(new Date('2020-09-06')))
expect(() => PeriodParser.parse('foo')).toThrow(ParseError)
expect(() => PeriodParser.parse('2020/09/06')).toThrow(ParseError)
expect(() => PeriodParser.parse('latest')).toThrow(ParseError)
Expand Down