-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: set custom timezone options from New York os time to Seoul custom timezone * feat: resolve custom timezone spec * refactor: refactor timezone diff offset logic * test: resolve broken TC * test: add initUtil TC * refactor: support breaking changes for timezoneOffset property * style: revert eslint style * refactor: remove unnecessary code * test: resolve broken TC * refactor: refactor duration code * refactor: remove console log * fix: recalculate detail popup time * fix: resolve creation popup for using dst timezone * refactor: apply PR feedbacks * refactor: add type declaration for timezone option and refactor timezone example * test: add intl polyfill for initUtil TC * test: add weakmap polyfill for intl.dateTimeFormat polyfill * test: add getCanonical polyfill * test: add Set polyfill * test: resolve TC * test: resolve broken TC * test: add require locale data * test: add intl.numberFormat * test: add intl.locale polyfill * test: add numberformat local data * test: change polyfill * test: add locale data polyfill * test: resolve broken TC * test: remove numberformat locale-data polyfill * test: resolve broken TC (locae data) * chore: remove unnecessary dev dependency * test: change intl polyfill * test: add comment * refactor: remove unnecessary title text
- Loading branch information
1 parent
03112f3
commit b2870fe
Showing
18 changed files
with
1,091 additions
and
276 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
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,113 @@ | ||
'use strict'; | ||
|
||
var util = require('tui-code-snippet'); | ||
var intlFormatter = {}; | ||
var intlUtil; | ||
|
||
var typeToPos = { | ||
year: 0, | ||
month: 1, | ||
day: 2, | ||
hour: 3, | ||
minute: 4, | ||
second: 5 | ||
}; | ||
|
||
/** | ||
* Extract date tokens (y, M, d, h, m, s) using the formatToParts() method. | ||
* @param {Intl.DateTimeFormat} dtf - Intl.DateTimeFormat instance | ||
* @param {Date} date - date object | ||
* @returns {Array.<number>} An array of objects only containing the formatted date | ||
*/ | ||
function parseOffset(dtf, date) { | ||
var formatted = dtf.formatToParts(date); | ||
var filled = []; | ||
var formattedLength = formatted.length; | ||
var i, pos; | ||
|
||
for (i = 0; i < formattedLength; i += 1) { | ||
pos = typeToPos[formatted[i].type]; | ||
|
||
if (!util.isUndefined(pos)) { | ||
filled[pos] = parseInt(formatted[i].value, 10); | ||
} | ||
} | ||
|
||
return filled; | ||
} | ||
|
||
/** | ||
* The time zone offset is calculated from the difference between the current time and the time in a specific time zone. | ||
* @param {Array.<number>} parts - An array of objects only containing the formatted date (e.g. [2020, 12, 14, 10, 15, 19]) | ||
* @param {Date} date - date object | ||
* @returns {number} offset | ||
*/ | ||
function calculateOffset(parts, date) { | ||
var y = parts[0]; | ||
var M = parts[1]; | ||
var d = parts[2]; | ||
var h = parts[3]; | ||
var m = parts[4]; | ||
var s = parts[5]; | ||
|
||
var utc = new Date(Date.UTC(y, M - 1, d, h, m, s)); | ||
var offset = (utc - date) / 60 / 1000; | ||
|
||
return Math.round(offset); | ||
} | ||
|
||
/** | ||
* Check if browser supports Intl.DateTimeFormat.prototype.formatToParts API | ||
* @returns {boolean} supported | ||
*/ | ||
function supportIntl() { | ||
/** | ||
* IE9 and IE10 do not support Intl.DateTimeFormat | ||
* IE11 does not support IANA timezone names | ||
* http://kangax.github.io/compat-table/esintl/#test-DateTimeFormat_accepts_IANA_timezone_names | ||
*/ | ||
return global.Intl && global.Intl.DateTimeFormat && | ||
util.isFunction(Intl.DateTimeFormat.prototype.formatToParts); | ||
} | ||
|
||
/** | ||
* Return DateTimeFormat instance by timezone | ||
* @param {string} timezoneName - timezone | ||
* @returns {DateTimeFormat} Intl.DateTimeFormat instance | ||
*/ | ||
function getIntlFormatter(timezoneName) { | ||
if (!intlFormatter[timezoneName]) { | ||
intlFormatter[timezoneName] = new Intl.DateTimeFormat('en-US', { | ||
hourCycle: 'h23', | ||
year: 'numeric', | ||
month: 'numeric', | ||
day: 'numeric', | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
second: 'numeric', | ||
timeZone: timezoneName | ||
}); | ||
} | ||
|
||
return intlFormatter[timezoneName]; | ||
} | ||
|
||
/** | ||
* Get offset of the time by timezone | ||
* @param {string} timezoneName - recognize the time zone names of the IANA time zone database, such as 'Asia/Seoul', 'America/New_York' | ||
* @param {number} timestamp - timestamp | ||
* @returns {number} offset | ||
*/ | ||
function offsetCalculator(timezoneName, timestamp) { | ||
var formatter = getIntlFormatter(timezoneName); | ||
var date = new Date(timestamp); | ||
|
||
return -calculateOffset(parseOffset(formatter, date), date); | ||
} | ||
|
||
intlUtil = { | ||
supportIntl: supportIntl, | ||
offsetCalculator: offsetCalculator | ||
}; | ||
|
||
module.exports = intlUtil; |
Oops, something went wrong.