Skip to content

Commit

Permalink
Add settings.storeUtcDates options to store datetime-local in UTC for…
Browse files Browse the repository at this point in the history
…mat (toISOString) fix #8542 (#8543)
  • Loading branch information
andrewtelnov authored Jul 11, 2024
1 parent 9c1fcbb commit 7099f3e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
17 changes: 12 additions & 5 deletions src/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1542,10 +1542,19 @@ export class Question extends SurveyElement<Question>
public getFilteredValue(): any { return this.value; }
public getFilteredName(): any { return this.getValueName(); }
public get valueForSurvey(): any {
return this.valueForSurveyCore(this.value);
}
protected valueForSurveyCore(val: any): any {
if (!!this.valueToDataCallback) {
return this.valueToDataCallback(this.value);
return this.valueToDataCallback(val);
}
return this.value;
return val;
}
protected valueFromDataCore(val: any): any {
if (!!this.valueFromDataCallback) {
return this.valueFromDataCallback(val);
}
return val;
}
/**
* Sets the question's `value` and `comment` properties to `undefined`.
Expand Down Expand Up @@ -2337,9 +2346,7 @@ export class Question extends SurveyElement<Question>
//IQuestion
updateValueFromSurvey(newValue: any, clearData: boolean = false): void {
newValue = this.getUnbindValue(newValue);
if (!!this.valueFromDataCallback) {
newValue = this.valueFromDataCallback(newValue);
}
newValue = this.valueFromDataCore(newValue);
if (!this.checkIsValueCorrect(newValue)) return;
const isEmpty = this.isValueEmpty(newValue);
if(!isEmpty && this.defaultValueExpression) {
Expand Down
29 changes: 24 additions & 5 deletions src/question_text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,24 @@ export class QuestionTextModel extends QuestionTextBase {
this._inputValue = this.maskInstance.getMaskedValue(this.value);
}
}

private hasToConvertToUTC(val: any): boolean {
return settings.storeUtcDates && this.isDateTimeLocaleType() && !!val;
}
protected valueForSurveyCore(val: any): any {
if(this.hasToConvertToUTC(val)) {
val = new Date(val).toISOString();
}
return super.valueForSurveyCore(val);
}
protected valueFromDataCore(val: any): any {
if(this.hasToConvertToUTC(val)) {
const d = new Date(val);
const locale_d = new Date(d.getTime() - d.getTimezoneOffset() * 60 * 1000);
let res = locale_d.toISOString();
val = res.substring(0, res.length - 2);
}
return super.valueFromDataCore(val);
}
protected onCheckForErrors(
errors: Array<SurveyError>,
isOnValueChanged: boolean
Expand Down Expand Up @@ -431,7 +448,10 @@ export class QuestionTextModel extends QuestionTextBase {
);
}
private get isDateInputType(): boolean {
return this.inputType === "date" || this.inputType === "datetime-local";
return this.inputType === "date" || this.isDateTimeLocaleType();
}
private isDateTimeLocaleType(): boolean {
return this.inputType === "datetime-local";
}
private getCalculatedMinMax(minMax: any): any {
if (this.isValueEmpty(minMax)) return minMax;
Expand Down Expand Up @@ -488,11 +508,10 @@ export class QuestionTextModel extends QuestionTextBase {
return this.maskTypeIsEmpty ? super.getIsInputTextUpdate() : false;
}
supportGoNextPageAutomatic(): boolean {
return !this.getIsInputTextUpdate() &&
["date", "datetime-local"].indexOf(this.inputType) < 0;
return !this.getIsInputTextUpdate() && !this.isDateInputType;
}
public supportGoNextPageError(): boolean {
return ["date", "datetime-local"].indexOf(this.inputType) < 0;
return !this.isDateInputType;
}
/**
* An array of predefined options from which users can select. This property configures an HTML [`<datalist>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/datalist) element and associates it with the underlying `input` element.
Expand Down
3 changes: 2 additions & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,5 +770,6 @@ export var settings = {
"a": /[a-zA-Z]/,
"#": /[a-zA-Z0-9]/
}
}
},
storeUtcDates: false
};
27 changes: 26 additions & 1 deletion tests/question_texttests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,4 +432,29 @@ QUnit.test("Test maxLength & getMaxLength", function (assert) {
q.inputType = "password";
assert.equal(q.isTextInput, true, "isTextInput - password");
assert.equal(q.getMaxLength(), 10, "getMaxLength() - password");
});
});
QUnit.test("settings.storeUtcDates = true, #8542", function(assert) {
const survey = new SurveyModel({
"elements": [
{
"name": "q1",
"type": "text",
"inputType": "datetime-local"
}
]
});
const q1 = <QuestionTextModel>survey.getQuestionByName("q1");
settings.storeUtcDates = true;
const locD = new Date(Date.now());
const utcD = new Date(locD.toISOString());
q1.value = locD;
assert.deepEqual(survey.data, { q1: utcD.toISOString() }, "#1");
let str = survey.data.q1;
assert.equal(str.indexOf("Z"), str.length - 1, "Has z symbol");
q1.clearValue();
assert.deepEqual(survey.data, { }, "#2");
survey.data = { q1: utcD.toISOString() };
assert.equal(new Date(q1.value).toTimeString(), locD.toTimeString(), "#3");
assert.equal(q1.value.indexOf("Z"), -1, "Has no z symbol");
settings.storeUtcDates = false;
});

0 comments on commit 7099f3e

Please sign in to comment.