Skip to content

Commit

Permalink
Merge release/11.1 into cascading/11.1.0-11.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
otter-cascading[bot] authored Feb 12, 2025
2 parents c30690b + 8a68faa commit 31bb743
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
27 changes: 26 additions & 1 deletion packages/@ama-sdk/core/src/fwk/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ describe('DateTime', () => {
expect(dateUtils).toEqual(dateCompare);
});

it('should support daylight saving time switch', () => {
expect((new Date('1972-05-28T00:00:00')).getTimezoneOffset()).toBe(-120);
expect((new Date('1972-05-27T23:59:59')).getTimezoneOffset()).toBe(-60);
const dateUtils = new utils.DateTime('1972-05-28T00:00:00');
// At midnight in italy, for this date, time should be 1 am
expect(dateUtils.toJSON()).toEqual('1972-05-28T01:00:00.000');
});

it('should ignore timezone of datetime', () => {
const date1 = '2015-03-25T12:00:00-02:00';
const date2 = '2015-03-25T12:00:00+05:00';
Expand Down Expand Up @@ -104,8 +112,25 @@ describe('Date', () => {
it('should be converted to a Js DateTime', () => {
const date1 = '1988-06-07';

const dateCompare = (new utils.DateTime('1988-06-07T12:00:00Z'));
const dateCompare = (new utils.DateTime('1988-06-07T00:00:00Z'));

expect((new utils.Date(date1))).toEqual(dateCompare);
});

it('should be at midnight for any date but daylight switch at midnight', () => {
const date1 = new utils.Date('1988-06-07');

expect(date1.getHours()).toEqual(0);
expect(date1.getMinutes()).toEqual(0);
expect(date1.getSeconds()).toEqual(0);
});

it('should support daylight saving time switch', () => {
expect((new Date('1972-05-28T00:00:00')).getTimezoneOffset()).toBe(-120);
expect((new Date('1972-05-27T23:59:59')).getTimezoneOffset()).toBe(-60);
const dateUtils = new utils.Date('1972-05-28');
// The day should be correct and not 27
expect(dateUtils.toJSON()).toEqual('1972-05-28');
expect(dateUtils.getDate()).toBe(28);
});
});
20 changes: 13 additions & 7 deletions packages/@ama-sdk/core/src/fwk/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ export function pad(val: number, digits: number = 2): string {
return '0'.repeat(Math.max(0, digits - str.length)) + str;
}

export class CommonDate extends Date {
function getNewTimeZoneWithOffset(dateArgs: any[], timezoneOffset: number) {
const newDateArgs = [...dateArgs];
newDateArgs[0] += `${timezoneOffset < 0 ? '+' : '-'}${pad(Math.floor(Math.abs(timezoneOffset / 60)))}:${pad(Math.abs(timezoneOffset % 60))}`;
return newDateArgs;
}

export class CommonDate extends Date {
/**
* Removes timezone information from ISO8601 strings (Received from DAPI)
*/
Expand All @@ -34,10 +39,13 @@ export class CommonDate extends Date {
args[0] = args[0].substring(0, idx);
}

const TIME_ZONE_OFFSET = ((new (Date as any)(...args)) as Date).getTimezoneOffset();
const ORIGINAL_TIME_ZONE_OFFSET = ((new (Date as any)(...args)) as Date).getTimezoneOffset();

if (idxT > 0) {
args[0] += `${TIME_ZONE_OFFSET < 0 ? '+' : '-'}${pad(Math.floor(Math.abs(TIME_ZONE_OFFSET / 60)))}:${pad(Math.abs(TIME_ZONE_OFFSET % 60))}`;
const newDateArgs = getNewTimeZoneWithOffset(args, ORIGINAL_TIME_ZONE_OFFSET);
// Adjust timezone in case of a daylight saving change mid-offset
const NEW_DATE_TIME_ZONE_OFFSET = ((new (Date as any)(...newDateArgs)) as Date).getTimezoneOffset();
args = getNewTimeZoneWithOffset(args, NEW_DATE_TIME_ZONE_OFFSET);
}
} else if (args[0] instanceof CommonDate) {
args[0] = args[0];
Expand Down Expand Up @@ -75,11 +83,9 @@ export namespace utils {
constructor(year: number, month: number, date?: number)
constructor(...args: any[]) {
if (args && typeof args[0] === 'string' && args[0].lastIndexOf('T') < 0) {
// Set time to 12 to limit scenarios where the native date api is not able to correctly
// compute the offset (for example, italian daylight saving switch prior to 1980).
args[0] = `${args[0]}T12:00:00Z`;
args[0] = `${args[0]}T00:00:00Z`;
} else if (args[0] instanceof _NativeDateClass) {
args[0] = `${args[0].getFullYear()}-${pad(args[0].getMonth() + 1)}-${pad(args[0].getDate())}T12:00:00Z`;
args[0] = `${args[0].getFullYear()}-${pad(args[0].getMonth() + 1)}-${pad(args[0].getDate())}T00:00:00Z`;
}

super(...args);
Expand Down
5 changes: 5 additions & 0 deletions packages/@ama-sdk/core/testing/global-timezone-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Used to test edge case around daylight saving changes
const process = require('node:process');
module.exports = async () => {
process.env.TZ = 'Europe/Rome';
}
1 change: 1 addition & 0 deletions packages/@ama-sdk/core/testing/jest.config.ut.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
// TODO re-enable fake dates when issue fixed https://github.com/sinonjs/fake-timers/issues/437
doNotFake: ['Date']
},
globalSetup: '<rootDir>/testing/global-timezone-setup.js',
testPathIgnorePatterns: [
'<rootDir>/.*/templates/.*',
'<rootDir>/builders/.*',
Expand Down

0 comments on commit 31bb743

Please sign in to comment.