Skip to content

Commit

Permalink
feat : when you do not set a custom timeZone, the schedule will alway…
Browse files Browse the repository at this point in the history
…s be displayed the same. (#718)

* style: add prettier config

* fix: when you do not set a custom timeZone, the schedule will always be displayed the same

* style: update code style
  • Loading branch information
jungeun-cho authored Nov 18, 2020
1 parent 3db0214 commit 6d4d635
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"extends": "tui",
"plugins": ["prettier"],
"extends": ["tui", "plugin:prettier/recommended"],
"globals": {
"chance": true,
"moment": true
Expand Down
16 changes: 16 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"singleQuote": true,
"printWidth": 100,
"tabWidth": 4,
"useTabs": false,
"semi": true,
"quoteProps": "as-needed",
"jsxSingleQuote": false,
"trailingComma": "es5",
"arrowParens": "always",
"endOfLine": "lf",
"bracketSpacing": true,
"requirePragma": false,
"insertPragma": false,
"proseWrap": "preserve"
}
47 changes: 46 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
"devDependencies": {
"css-loader": "^3.2.0",
"eslint": "^4.19.1",
"eslint-config-prettier": "^6.15.0",
"eslint-config-tui": "^1.0.3",
"eslint-loader": "^2.0.0",
"eslint-plugin-jasmine": "^2.10.1",
"eslint-plugin-prettier": "^3.1.4",
"handlebars": "^4.7.6",
"handlebars-template-loader": "^1.0.0",
"istanbul-instrumenter-loader": "^3.0.1",
Expand All @@ -64,6 +66,7 @@
"mini-css-extract-plugin": "^0.4.0",
"optimize-css-assets-webpack-plugin": "^5.0.3",
"preprocess-loader": "^0.3.0",
"prettier": "^2.1.2",
"safe-umd-webpack-plugin": "^4.0.0",
"stylus": "^0.54.5",
"stylus-loader": "^3.0.2",
Expand Down
64 changes: 35 additions & 29 deletions src/js/common/timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var getterMethods = [
'getMilliseconds',
'getMinutes',
'getMonth',
'getSeconds'
'getSeconds',
];

var setterMethods = [
Expand All @@ -30,7 +30,7 @@ var setterMethods = [
'setMilliseconds',
'setMinutes',
'setMonth',
'setSeconds'
'setSeconds',
];

/**
Expand Down Expand Up @@ -65,10 +65,16 @@ function getCustomTimezoneOffset(timestamp) {
* @returns {number} local time
*/
function getLocalTime(time) {
var timezoneOffset = getTimezoneOffset(time);
var customTimezoneOffset = getCustomTimezoneOffset(time);
var timezoneOffsetDiff = customTimezoneOffset ? 0 : nativeOffsetMs - timezoneOffset;
var localTime = time - customTimezoneOffset + timezoneOffset + timezoneOffsetDiff;
var timezoneOffset, customTimezoneOffset, timezoneOffsetDiff, localTime;

if (!setByTimezoneOption) {
return time;
}

timezoneOffset = getTimezoneOffset(time);
customTimezoneOffset = getCustomTimezoneOffset(time);
timezoneOffsetDiff = customTimezoneOffset ? 0 : nativeOffsetMs - timezoneOffset;
localTime = time - customTimezoneOffset + timezoneOffset + timezoneOffsetDiff;

return localTime;
}
Expand All @@ -95,7 +101,7 @@ function createDateWithUTCTime(arg) {

if (arg instanceof TZDate) {
time = arg.getUTCTime();
} else if ((typeof arg) === 'number') {
} else if (typeof arg === 'number') {
time = arg;
} else if (arg === null) {
time = 0;
Expand All @@ -116,7 +122,7 @@ function createDateAsLocalTime(arg) {

if (arg instanceof Date) {
time = arg.getTime();
} else if ((typeof arg) === 'string') {
} else if (typeof arg === 'string') {
time = Date.parse(arg);
} else {
throw new Error('Invalid Type');
Expand All @@ -133,7 +139,7 @@ function createDateAsLocalTime(arg) {
* @returns {boolean}
*/
function useLocalTimeConverter(arg) {
return arg instanceof Date || (typeof arg) === 'string';
return arg instanceof Date || typeof arg === 'string';
}

/**
Expand Down Expand Up @@ -163,7 +169,7 @@ function TZDate(date) {
* Get milliseconds which is converted by timezone
* @returns {number} milliseconds
*/
TZDate.prototype.getTime = function() {
TZDate.prototype.getTime = function () {
var time = this._date.getTime();

return time + getCustomTimezoneOffset(time) - getTimezoneOffset(time);
Expand All @@ -173,50 +179,50 @@ TZDate.prototype.getTime = function() {
* Get UTC milliseconds
* @returns {number} milliseconds
*/
TZDate.prototype.getUTCTime = function() {
TZDate.prototype.getUTCTime = function () {
return this._date.getTime();
};

/**
* toUTCString
* @returns {string}
*/
TZDate.prototype.toUTCString = function() {
TZDate.prototype.toUTCString = function () {
return this._date.toUTCString();
};

/**
* to Date
* @returns {Date}
*/
TZDate.prototype.toDate = function() {
TZDate.prototype.toDate = function () {
return this._date;
};

TZDate.prototype.valueOf = function() {
TZDate.prototype.valueOf = function () {
return this.getTime();
};

TZDate.prototype.addDate = function(day) {
TZDate.prototype.addDate = function (day) {
this.setDate(this.getDate() + day);

return this;
};

TZDate.prototype.addMinutes = function(minutes) {
TZDate.prototype.addMinutes = function (minutes) {
this.setMinutes(this.getMinutes() + minutes);

return this;
};

TZDate.prototype.addMilliseconds = function(milliseconds) {
TZDate.prototype.addMilliseconds = function (milliseconds) {
this.setMilliseconds(this.getMilliseconds() + milliseconds);

return this;
};

/* eslint-disable max-params*/
TZDate.prototype.setWithRaw = function(y, M, d, h, m, s, ms) {
TZDate.prototype.setWithRaw = function (y, M, d, h, m, s, ms) {
this.setFullYear(y, M, d);
this.setHours(h, m, s, ms);

Expand All @@ -226,22 +232,22 @@ TZDate.prototype.setWithRaw = function(y, M, d, h, m, s, ms) {
/**
* @returns {TZDate} local time
*/
TZDate.prototype.toLocalTime = function() {
TZDate.prototype.toLocalTime = function () {
var time = this.getTime();
var utcTime = this.getUTCTime();
var diff = time - utcTime;

return new TZDate(utcTime - diff);
};

getterMethods.forEach(function(methodName) {
TZDate.prototype[methodName] = function() {
getterMethods.forEach(function (methodName) {
TZDate.prototype[methodName] = function () {
return this._date[methodName].apply(this._date, arguments);
};
});

setterMethods.forEach(function(methodName) {
TZDate.prototype[methodName] = function() {
setterMethods.forEach(function (methodName) {
TZDate.prototype[methodName] = function () {
this._date[methodName].apply(this._date, arguments);

return this.getTime();
Expand All @@ -255,15 +261,15 @@ module.exports = {
* Set offset
* @param {number} offset - timezone offset based on minutes
*/
setOffset: function(offset) {
setOffset: function (offset) {
customOffsetMs = offset * MIN_TO_MS;
},

/**
* Set offset
* @param {number} offset - timezone offset based on minutes
*/
setOffsetByTimezoneOption: function(offset) {
setOffsetByTimezoneOption: function (offset) {
this.setOffset(-offset);
setByTimezoneOption = true;
},
Expand All @@ -272,7 +278,7 @@ module.exports = {
* Get offset in case of `setByTimezoneOption`. Or return 0.
* @returns {number} timezone offset offset minutes
*/
getOffset: function() {
getOffset: function () {
if (setByTimezoneOption) {
return customOffsetMs / MIN_TO_MS;
}
Expand All @@ -284,15 +290,15 @@ module.exports = {
* Set a callback function to get timezone offset by timestamp
* @param {function} callback - callback function
*/
setOffsetCallback: function(callback) {
setOffsetCallback: function (callback) {
timezoneOffsetCallback = callback;
},

/**
* (Use this method only for testing)
* Reset system timezone and custom timezone
*/
restoreOffset: function() {
restoreOffset: function () {
customOffsetMs = getTimezoneOffset();
}
},
};

0 comments on commit 6d4d635

Please sign in to comment.