Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow date below year 1000 for fromDateWithTimeZone #336

Merged
merged 1 commit into from
Oct 23, 2024
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
4 changes: 3 additions & 1 deletion src/CalendarDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ export class CalendarDate {
* returns a CalendarDate instance for the supplied Date, using the supplied time zone string
*/
static fromDateWithTimeZone(date: Date, timeZone: string): CalendarDate {
return new CalendarDate(CalendarDate.getIntlDateTimeFormatter(timeZone).format(date));
return new CalendarDate(
CalendarDate.getIntlDateTimeFormatter(timeZone).format(date).padStart(10, '0'),
);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions test/CalendarDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,21 @@ describe('CalendarDate', () => {
});
});

describe('Test of fromDateWithTimeZone', () => {
test('should return correct date for a year < 1000', () => {
// Arrange
const date = new Date(50, 0, 1);

// Act
const calendarDate = CalendarDate.fromDateWithTimeZone(date, 'America/New_York');

// Assert
expect(calendarDate.toFormat('d.M.yyyy')).toBe(
date.toLocaleDateString('de-DE', { timeZone: 'America/New_York' }),
);
});
});

describe('Test of parseString', () => {
test('Should throw error if input is not a valid iso string', () => {
fc.assert(
Expand Down