Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions LDK/resources/web/LDK/ConvertUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Ext4.ns('LDK');
* Static helpers designed to help with type conversion in JS.
*/
LDK.ConvertUtils = new function(){
var verboseLogging = false;

var DATEFORMATS = LABKEY.Utils.getDateAltFormats().split('|');
DATEFORMATS.push('Y/m/d H:i:s');
DATEFORMATS.push('n-j-Y');
Expand All @@ -16,7 +18,7 @@ LDK.ConvertUtils = new function(){
//@private

//adapted from Ext.field.Date. will parse a date in the given format, returning null if it does not match
function safeParseDate(value, format, useStrict){
function safeParseDate(value, format, useStrict, verboseLogging){
var result = null,
parsedDate;

Expand All @@ -26,28 +28,42 @@ LDK.ConvertUtils = new function(){

useStrict = Ext4.isDefined(useStrict) ? useStrict : false;

// The core of the parsing problem comes from JS Date.parse() treating ISO 8601 short differently from other date formats:
// https://www.w3.org/TR/NOTE-datetime
// Example:
// new Date('2024-01-01')
// Sun Dec 31 2023 16:00:00 GMT-0800 (Pacific Standard Time)
// new Date('1/1/2024')
// Mon Jan 01 2024 00:00:00 GMT-0800 (Pacific Standard Time)
// Therefore special case this format and append the browser's time zone:
if (format === 'c' && value && value.length === 10) {
if (verboseLogging) {
console.log('switching from c to Y-m-d format')
}

format = 'Y-m-d';
}

if (Ext4.Date.formatContainsHourInfo(format)) {
// if parse format contains hour information, no DST adjustment is necessary
result = Ext4.Date.parse(value, format, useStrict);
} else {
// The core of the parsing problem comes from JS Date.parse() treating ISO 8601 short differently from other date formats:
// https://www.w3.org/TR/NOTE-datetime
// Example:
// new Date('2024-01-01')
// Sun Dec 31 2023 16:00:00 GMT-0800 (Pacific Standard Time)
// new Date('1/1/2024')
// Mon Jan 01 2024 00:00:00 GMT-0800 (Pacific Standard Time)

// Therefore special case this format and append the browser's time zone:
if (format === 'c' && value.length === 10) {
format = 'Y-m-d';
}

parsedDate = Ext4.Date.parse(value, format, useStrict);
if (parsedDate) {
result = Ext4.Date.clearTime(parsedDate);
}
}

// TODO: added for insight into TeamCity test failures. Ultimately remove this.
if (verboseLogging && result) {
var msg = 'Parsing, raw value: ' + value + ', format: ' + format + ', result: ' + result;
console.log(msg);

LDK.Utils.logToServer({
level: 'INFO',
message: msg
})
}
return result;
}

Expand Down Expand Up @@ -86,7 +102,7 @@ LDK.ConvertUtils = new function(){

var val;
for (var i=0; i < formats.length; ++i) {
val = safeParseDate(value, formats[i]);
val = safeParseDate(value, formats[i], true, verboseLogging);
if (val) {
break;
}
Expand All @@ -113,6 +129,10 @@ LDK.ConvertUtils = new function(){
}, this);
}
}, this);
},

setVerboseLogging: function(val) {
verboseLogging = val;
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@
package org.labkey.test.tests.external.labModules;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.labkey.api.util.DateUtil;
import org.labkey.remoteapi.CommandException;
import org.labkey.remoteapi.Connection;
import org.labkey.remoteapi.collections.CaseInsensitiveHashMap;
Expand Down Expand Up @@ -1499,6 +1496,14 @@ private void samplesTableTest() throws Exception
new Window.WindowFinder(getDriver()).withTitle("Mark Removed").waitFor();
Ext4FieldRef.getForLabel(this, "Date Removed").setValue("2017-01-02");
Ext4FieldRef.getForLabel(this, "Comment").setValue("I removed these samples");

// TODO: for debugging date. ultimately remove
BaseWebDriverTest.getCurrentTest().getArtifactCollector().dumpPageSnapshot("LabModulesTestDate1");

// Debug date parsing
String clientFormattedString = (String)executeScript("return Ext4.Date.format(LDK.ConvertUtils.parseDate('2017-01-02'), 'Y-m-d');");
assertEquals("Incorrect date parsing", clientFormattedString, "2017-01-02");

waitAndClickAndWait(Ext4Helper.Locators.ext4Button("Submit"));

dr = new DataRegionTable.DataRegionFinder(getDriver()).withName("query").find();
Expand All @@ -1515,7 +1520,7 @@ private void samplesTableTest() throws Exception
{
Assert.assertEquals("I removed these samples", row.get("remove_comment"));
Assert.assertEquals(getUserId(), row.get("removedby"));
Assert.assertEquals("2017-01-02", dateFormat.format(row.get("dateremoved")));
Assert.assertEquals("Incorrect date, raw value: " + row.get("dateremoved"), "2017-01-02", dateFormat.format(row.get("dateremoved")));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public void addRecordsToAssayTemplate(String[][] data, List<String> expectedColu
for (String[] row : data)
{
sb.append(StringUtils.join(row, '\t'));
sb.append(System.getProperty("line.separator"));
sb.append(System.lineSeparator());
}

_test.waitForText("Sample Information");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ Ext4.define('Laboratory.ext.AssaySpreadsheetImportWindow', {
return;
}

// TODO: debugging, ultimately remove this
LDK.ConvertUtils.setVerboseLogging(true);

var models = LDK.StoreUtils.getModelsFromText({
store: win.targetGrid.store,
text: text
Expand All @@ -77,6 +80,9 @@ Ext4.define('Laboratory.ext.AssaySpreadsheetImportWindow', {

win.targetGrid.store.add(toAdd);

// TODO: debugging, ultimately remove this
LDK.ConvertUtils.setVerboseLogging(false);

win.close();
}
},{
Expand Down