@talend/utils
Advanced tools
Comparing version 2.1.1 to 2.2.0
# @talend/utils | ||
## 2.2.0 | ||
### Minor Changes | ||
- 9f87b591a: return wrong date when calling “convertToTimeZone” function with specific timezone and summer time | ||
## 2.1.1 | ||
@@ -4,0 +10,0 @@ |
@@ -13,2 +13,3 @@ declare type DateFnsFormatInput = Date | number | string; | ||
* @param {string} timeZone Timezone IANA name | ||
* @param {Date} date (optional) When specified, will be instead of default current time | ||
* @returns {Number} | ||
@@ -18,3 +19,3 @@ * | ||
*/ | ||
export declare function getUTCOffset(timeZone: string): number; | ||
export declare function getUTCOffset(timeZone: string, date?: Date): number; | ||
/** | ||
@@ -21,0 +22,0 @@ * Format a human-readable UTC offset |
@@ -26,2 +26,3 @@ "use strict"; | ||
* @param {string} timeZone Timezone IANA name | ||
* @param {Date} date (optional) When specified, will be instead of default current time | ||
* @returns {Number} | ||
@@ -31,3 +32,3 @@ * | ||
*/ | ||
function getUTCOffset(timeZone) { | ||
function getUTCOffset(timeZone, date) { | ||
// Build localized formats for UTC and the target timezone | ||
@@ -49,5 +50,5 @@ const formatOptions = { | ||
const date = new Date(); | ||
const utcDate = new Date(utcFormat.format(date)); | ||
const timezoneDate = new Date(timezoneFormat.format(date)); // Compute delta between dates | ||
const dateObj = date || new Date(); | ||
const utcDate = new Date(utcFormat.format(dateObj)); | ||
const timezoneDate = new Date(timezoneFormat.format(dateObj)); // Compute delta between dates | ||
@@ -134,7 +135,7 @@ return (timezoneDate.getTime() - utcDate.getTime()) / (1000 * 60); | ||
const parsedDate = (0, _parse.default)(date); | ||
let offset = getUTCOffset(timeZone) + parsedDate.getTimezoneOffset(); | ||
let offset = getUTCOffset(timeZone, parsedDate) + parsedDate.getTimezoneOffset(); | ||
if (sourceTimeZone) { | ||
offset -= new Date().getTimezoneOffset(); | ||
offset -= getUTCOffset(sourceTimeZone); | ||
offset -= parsedDate.getTimezoneOffset(); | ||
offset -= getUTCOffset(sourceTimeZone, parsedDate); | ||
} | ||
@@ -159,5 +160,3 @@ | ||
const dateFnsFormatWithTimeZoneValue = formatTimeZoneTokens(formatString, options.timeZone); | ||
return (0, _format.default)(dateConvertedToTimezone, dateFnsFormatWithTimeZoneValue, { | ||
locale: options.locale | ||
}); | ||
return (0, _format.default)(dateConvertedToTimezone, dateFnsFormatWithTimeZoneValue, options); | ||
} | ||
@@ -245,3 +244,2 @@ /** | ||
; | ||
var _default = { | ||
@@ -248,0 +246,0 @@ convertToLocalTime, |
{ | ||
"name": "@talend/utils", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "Various utilities", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -113,3 +113,3 @@ import dateFnsFormat from 'date-fns/format'; | ||
describe('formatToTimeZone', () => { | ||
it('should format a locale date to a given timezone in a specifc format', () => { | ||
it('should format a locale date to a given timezone in a specific format', () => { | ||
// given | ||
@@ -141,3 +141,3 @@ const dateObj = new Date('2020-05-13, 20:00'); | ||
// given | ||
const mockLocal = { format: () => {}}; | ||
const mockLocal = { format: () => {} }; | ||
const dateObj = new Date('2020-12-20, 20:00'); | ||
@@ -162,2 +162,23 @@ const formatString = 'ddd YYYY-MM-DD HH:mm:ss'; | ||
}); | ||
it('should be formatted to a correct winter time and summer time for specific timezone', () => { | ||
// given | ||
const formatString = 'ddd YYYY-MM-DD HH:mm:ss'; | ||
const timeZone = 'Europe/Berlin'; | ||
const winterTimestamp = 1643095932000; // 2022-01-25 08:32:12 | ||
const expectedWinterTime = 'Tue 2022-01-25 08:32:12'; | ||
const summerTimestamp = 1654068732000; // 2022-06-01 08:32:12 | ||
const expectedSummerTime = 'Wed 2022-06-01 09:32:12'; | ||
// when for winter time | ||
const winterTime = formatToTimeZone(winterTimestamp, formatString, { timeZone }); | ||
// then | ||
expect(winterTime).toEqual(expectedWinterTime); | ||
// when for summer time | ||
const summerTime = formatToTimeZone(summerTimestamp, formatString, { timeZone }); | ||
// then | ||
expect(summerTime).toEqual(expectedSummerTime); | ||
}); | ||
}); | ||
@@ -185,2 +206,13 @@ | ||
}); | ||
test.each([ | ||
['Africa/Bamako', new Date('2022-01-25 08:32:12'), 0], | ||
['Asia/Seoul', new Date('2022-06-14 08:32:12'), 540], | ||
['Europe/Berlin', new Date('2022-01-25 08:32:12'), 60], // winter time | ||
['Europe/Berlin', new Date('2022-06-1 08:32:12'), 120], // summer time | ||
])( | ||
'it should get %s timezone offset with specific date', | ||
(timezone: string, date: Date, expectedOffset: number) => { | ||
expect(getUTCOffset(timezone, date)).toEqual(expectedOffset); | ||
}, | ||
); | ||
}); | ||
@@ -187,0 +219,0 @@ |
@@ -7,9 +7,9 @@ import dateFnsFormat from 'date-fns/format'; | ||
interface ConversionOptions { | ||
timeZone: string, | ||
sourceTimeZone?: string, | ||
locale?: Object, | ||
timeZone: string; | ||
sourceTimeZone?: string; | ||
locale?: Object; | ||
} | ||
export interface DateFormatOptions { | ||
[key: string]: Intl.DateTimeFormatOptions, | ||
[key: string]: Intl.DateTimeFormatOptions; | ||
} | ||
@@ -20,2 +20,3 @@ | ||
* @param {string} timeZone Timezone IANA name | ||
* @param {Date} date (optional) When specified, will be instead of default current time | ||
* @returns {Number} | ||
@@ -25,3 +26,3 @@ * | ||
*/ | ||
export function getUTCOffset(timeZone: string): number { | ||
export function getUTCOffset(timeZone: string, date?: Date): number { | ||
// Build localized formats for UTC and the target timezone | ||
@@ -41,5 +42,5 @@ const formatOptions: Intl.DateTimeFormatOptions = { | ||
// Create the same date in UTC timezone and the target timezone | ||
const date = new Date(); | ||
const utcDate = new Date(utcFormat.format(date)); | ||
const timezoneDate = new Date(timezoneFormat.format(date)); | ||
const dateObj = date || new Date(); | ||
const utcDate = new Date(utcFormat.format(dateObj)); | ||
const timezoneDate = new Date(timezoneFormat.format(dateObj)); | ||
@@ -125,7 +126,7 @@ // Compute delta between dates | ||
let offset = getUTCOffset(timeZone) + parsedDate.getTimezoneOffset(); | ||
let offset = getUTCOffset(timeZone, parsedDate) + parsedDate.getTimezoneOffset(); | ||
if (sourceTimeZone) { | ||
offset -= new Date().getTimezoneOffset(); | ||
offset -= getUTCOffset(sourceTimeZone); | ||
offset -= parsedDate.getTimezoneOffset(); | ||
offset -= getUTCOffset(sourceTimeZone, parsedDate); | ||
} | ||
@@ -145,3 +146,7 @@ | ||
*/ | ||
export function formatToTimeZone(date: DateFnsFormatInput, formatString: string, options: ConversionOptions): string { | ||
export function formatToTimeZone( | ||
date: DateFnsFormatInput, | ||
formatString: string, | ||
options: ConversionOptions, | ||
): string { | ||
const dateConvertedToTimezone = convertToTimeZone(date, options); | ||
@@ -152,5 +157,3 @@ | ||
return dateFnsFormat(dateConvertedToTimezone, dateFnsFormatWithTimeZoneValue, { | ||
locale: options.locale, | ||
}); | ||
return dateFnsFormat(dateConvertedToTimezone, dateFnsFormatWithTimeZoneValue, options); | ||
} | ||
@@ -223,3 +226,3 @@ | ||
return new Intl.DateTimeFormat(lang, options[dateOption]).format(parse(date)); | ||
}; | ||
} | ||
@@ -226,0 +229,0 @@ export default { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
64651
1111