-
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.
* chore: change py in button * feat: getDiffText util function * refactor: button * feat: add env into gitignore
- Loading branch information
Showing
5 changed files
with
86 additions
and
9 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 |
---|---|---|
|
@@ -26,7 +26,8 @@ yarn-debug.log* | |
yarn-error.log* | ||
|
||
# local env files | ||
.env*.local | ||
.env | ||
.env.* | ||
|
||
# vercel | ||
.vercel | ||
|
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,33 @@ | ||
import { describe, it, expect } from 'vitest' | ||
|
||
import { DateOrderError, getDiffDateText } from '@/utils/date' | ||
|
||
describe('getDiffDateText', () => { | ||
it('should return 1 if the difference is less than a day', () => { | ||
const startDate = new Date('2024-05-04T23:40:00Z') | ||
const endDate = new Date('2024-05-04T23:59:00Z') | ||
|
||
expect(getDiffDateText(startDate, endDate)).toBe(1) | ||
}) | ||
|
||
it('should return 2 if day crosses midnight', () => { | ||
const startDate = new Date('2024-05-04T23:40:00Z') | ||
const endDate = new Date('2024-05-05T00:00:00Z') | ||
|
||
expect(getDiffDateText(startDate, endDate)).toBe(2) | ||
}) | ||
|
||
it('should return the correct number of days if the difference is more than a day', () => { | ||
const startDate = new Date('2024-05-04T00:00:00Z') | ||
const endDate = new Date('2024-05-06T00:00:00Z') | ||
|
||
expect(getDiffDateText(startDate, endDate)).toBe(3) | ||
}) | ||
|
||
it('should throw a DateOrderError if endDate is before startDate', () => { | ||
const startDate = new Date('2024-05-05T00:00:00Z') | ||
const endDate = new Date('2024-05-04T23:40:00Z') | ||
|
||
expect(() => getDiffDateText(startDate, endDate)).toThrow(DateOrderError) | ||
}) | ||
}) |
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,32 @@ | ||
export class DateOrderError extends Error { | ||
constructor() { | ||
super('인자의 순서가 잘못되었습니다.(endDate < startDate)') | ||
} | ||
} | ||
|
||
/** | ||
* 그리니치 천문대로부터 특정 날짜까지 몇 일이 떨어져 있는지 계산합니다. | ||
* @param targetDate - 계산할 대상 날짜 | ||
* @returns 그리니치 천문대 기준으로부터 떨어진 일 수 | ||
*/ | ||
const getDaysFromGreenwich = (targetDate: Date): number => { | ||
const greenwichDate = new Date('1970-01-01T00:00:00Z') | ||
const MS_PER_DAY = 1000 * 60 * 60 * 24 | ||
|
||
const diffInMs = targetDate.getTime() - greenwichDate.getTime() | ||
const diffInDays = Math.floor(diffInMs / MS_PER_DAY) | ||
|
||
return diffInDays | ||
} | ||
|
||
export const getDiffDateText = (startDate: Date, endDate: Date): number => { | ||
if (endDate.getTime() < startDate.getTime()) { | ||
throw new DateOrderError() | ||
} | ||
|
||
const startDay = getDaysFromGreenwich(startDate) | ||
const endDay = getDaysFromGreenwich(endDate) | ||
const diffDay = endDay - startDay | ||
|
||
return diffDay + 1 | ||
} |