@talend/utils
Advanced tools
Comparing version 1.2.0 to 1.3.0
declare type DateFnsFormatInput = Date | number | string; | ||
interface ConversionOptions { | ||
timeZone: string; | ||
sourceTimeZone?: string; | ||
} | ||
@@ -29,3 +30,4 @@ /** | ||
/** | ||
* Converts the given date from the local time to the given time zone and returns a new Date object. | ||
* Converts the given date from the local time (or from specified source timezone) to | ||
* the given timezone and returns a new Date object. | ||
* @param {DateFnsFormatInput} date Date to format | ||
@@ -42,4 +44,3 @@ * @param {ConversionOptions} options | ||
* @param {string} formatString Output format (see date-fns supported formats) | ||
* @param {Object} options | ||
* @param {string} options.timeZone Target timezone | ||
* @param {ConversionOptions} options | ||
* @returns {string} | ||
@@ -46,0 +47,0 @@ * |
@@ -82,3 +82,3 @@ "use strict"; | ||
function formatTimeZoneTokens(dateFormat, timeZone) { | ||
return dateFormat.replace(/z|ZZ?/g, function (match) { | ||
return dateFormat.replace(/(?<!\[)(z|ZZ?)/g, function (match) { | ||
var offset = getUTCOffset(timeZone); | ||
@@ -104,3 +104,4 @@ var separator = match === 'Z' ? ':' : ''; | ||
/** | ||
* Converts the given date from the local time to the given time zone and returns a new Date object. | ||
* Converts the given date from the local time (or from specified source timezone) to | ||
* the given timezone and returns a new Date object. | ||
* @param {DateFnsFormatInput} date Date to format | ||
@@ -113,4 +114,9 @@ * @param {ConversionOptions} options | ||
function convertToTimeZone(date, options) { | ||
var timeZone = options.timeZone, sourceTimeZone = options.sourceTimeZone; | ||
var parsedDate = parse_1.default(date); | ||
var offset = getUTCOffset(options.timeZone) + parsedDate.getTimezoneOffset(); | ||
var offset = getUTCOffset(timeZone) + parsedDate.getTimezoneOffset(); | ||
if (sourceTimeZone) { | ||
offset -= new Date().getTimezoneOffset(); | ||
offset -= getUTCOffset(sourceTimeZone); | ||
} | ||
return new Date(parsedDate.getTime() + offset * 60 * 1000); | ||
@@ -123,4 +129,3 @@ } | ||
* @param {string} formatString Output format (see date-fns supported formats) | ||
* @param {Object} options | ||
* @param {string} options.timeZone Target timezone | ||
* @param {ConversionOptions} options | ||
* @returns {string} | ||
@@ -127,0 +132,0 @@ * |
{ | ||
"name": "@talend/utils", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Various utilities", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -15,4 +15,6 @@ import { | ||
const timeZones = { | ||
'GMT+5': 'Asia/Oral', | ||
'GMT-3': 'America/Sao_Paulo', | ||
'UTC+5': 'Asia/Oral', | ||
'UTC+1': 'Europe/London', | ||
'UTC-3': 'America/Sao_Paulo', | ||
'UTC-6': 'America/Belize', | ||
}; | ||
@@ -24,3 +26,3 @@ | ||
const dateObj = new Date('2020-05-13, 20:00'); | ||
const options = { timeZone: timeZones['GMT+5'] }; | ||
const options = { timeZone: timeZones['UTC+5'] }; | ||
@@ -37,3 +39,3 @@ // when | ||
const dateObj = '2020-05-13T20:00:00'; | ||
const options = { timeZone: timeZones['GMT-3'] }; | ||
const options = { timeZone: timeZones['UTC-3'] }; | ||
@@ -52,3 +54,3 @@ // when | ||
const dateObj = new Date('2020-05-13, 20:00'); | ||
const options = { timeZone: timeZones['GMT+5'] }; | ||
const options = { timeZone: timeZones['UTC+5'] }; | ||
@@ -65,3 +67,3 @@ // when | ||
const dateObj = '2020-05-13T20:00:00'; | ||
const options = { timeZone: timeZones['GMT-3'] }; | ||
const options = { timeZone: timeZones['UTC-3'] }; | ||
@@ -74,2 +76,17 @@ // when | ||
}); | ||
it('should convert a date from a specific timezone to the target timezone time', () => { | ||
// given | ||
const dateObj = '2020-05-13T20:00:00'; | ||
const options = { | ||
sourceTimeZone: timeZones['UTC+1'], | ||
timeZone: timeZones['UTC-6'], | ||
}; | ||
// when | ||
const localDate = convertToTimeZone(dateObj, options); | ||
// then | ||
expect(localDate).toEqual(new Date('2020-05-13, 13:00')); | ||
}); | ||
}); | ||
@@ -97,3 +114,3 @@ | ||
const formatString = 'YYYY-MM-DD[T]HH:mm:ssZZ'; | ||
const options = { timeZone: timeZones['GMT+5'] }; | ||
const options = { timeZone: timeZones['UTC+5'] }; | ||
@@ -106,2 +123,15 @@ // when | ||
}); | ||
it('should not change timezone tokens that are wrapped in hooks', () => { | ||
// given | ||
const dateObj = new Date('2020-05-13, 20:00'); | ||
const formatString = 'YYYY-MM-DD[T]HH:mm:ss[Z]'; | ||
const options = { timeZone: timeZones['UTC+5'] }; | ||
// when | ||
const localDate = formatToTimeZone(dateObj, formatString, options); | ||
// then | ||
expect(localDate).toEqual('2020-05-13T23:00:00Z'); | ||
}); | ||
}); | ||
@@ -108,0 +138,0 @@ |
@@ -8,2 +8,3 @@ import format from 'date-fns/format'; | ||
timeZone: string, | ||
sourceTimeZone?: string, | ||
} | ||
@@ -65,3 +66,3 @@ | ||
*/ | ||
export function formatReadableUTCOffset(offset: number): string { | ||
export function formatReadableUTCOffset(offset: number): string { | ||
return formatUTCOffset(offset, ':'); | ||
@@ -81,3 +82,3 @@ } | ||
function formatTimeZoneTokens(dateFormat: string, timeZone: string): string { | ||
return dateFormat.replace(/z|ZZ?/g, match => { | ||
return dateFormat.replace(/(?<!\[)(z|ZZ?)/g, match => { | ||
const offset = getUTCOffset(timeZone); | ||
@@ -105,3 +106,4 @@ const separator = match === 'Z' ? ':' : ''; | ||
/** | ||
* Converts the given date from the local time to the given time zone and returns a new Date object. | ||
* Converts the given date from the local time (or from specified source timezone) to | ||
* the given timezone and returns a new Date object. | ||
* @param {DateFnsFormatInput} date Date to format | ||
@@ -114,5 +116,13 @@ * @param {ConversionOptions} options | ||
export function convertToTimeZone(date: DateFnsFormatInput, options: ConversionOptions): Date { | ||
const { timeZone, sourceTimeZone } = options; | ||
const parsedDate = parse(date); | ||
const offset = getUTCOffset(options.timeZone) + parsedDate.getTimezoneOffset(); | ||
let offset = getUTCOffset(timeZone) + parsedDate.getTimezoneOffset(); | ||
if (sourceTimeZone) { | ||
offset -= new Date().getTimezoneOffset(); | ||
offset -= getUTCOffset(sourceTimeZone); | ||
} | ||
return new Date(parsedDate.getTime() + offset * 60 * 1000); | ||
@@ -125,4 +135,3 @@ } | ||
* @param {string} formatString Output format (see date-fns supported formats) | ||
* @param {Object} options | ||
* @param {string} options.timeZone Target timezone | ||
* @param {ConversionOptions} options | ||
* @returns {string} | ||
@@ -129,0 +138,0 @@ * |
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
43629
971