Skip to content

Commit

Permalink
Added the SADL datetime format
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Cuddihy committed Jan 27, 2021
1 parent 56e88b2 commit 3f08698
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,20 @@ public abstract class Utility {

public static ArrayList<DateTimeFormatter> DATE_FORMATTERS = new ArrayList<DateTimeFormatter>();
public static ArrayList<DateTimeFormatter> DATETIME_FORMATTERS = new ArrayList<DateTimeFormatter>();
public static ArrayList<DateTimeFormatter> ZONED_FORMATTERS = new ArrayList<DateTimeFormatter>();


public static final DateTimeFormatter ZONED_FORMATTER_SADL = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy"); // SADL: Wed Mar 22 20:00:00 EDT 2017

public static final DateTimeFormatter DATETIME_FORMATTER_yyyyMMddHHmmss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // e.g. 2014-12-01 00:00:00
public static final DateTimeFormatter DATETIME_FORMATTER_yyyyMMddHHmmssSSS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); // e.g. 2014-12-01 00:00:00.000
public static final DateTimeFormatter DATETIME_FORMATTER_MMddyyyyhmmssa = DateTimeFormatter.ofPattern("MM/dd/yyyy h:mm:ss a"); // e.g. 02/02/2018 4:00:00 AM
public static final DateTimeFormatter DATETIME_FORMATTER_ISO8601 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); // e.g. 2017-09-16T13:37:04Z

public static final DateTimeFormatter DATE_FORMATTER_yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd"); // e.g. 20141031



private static StrSubstitutor envSubstitutor = new StrSubstitutor(new CaseInsensitiveMap(System.getenv()));
public static final Boolean ENV_TEST = ! envSubstitutor.replace("${HOST_IP}").equals("${HOST_IP}");
public static final String ENV_TEST_EXCEPTION_STRING = "Can't find environment variable $HOST_IP, suggesting a testing setup problem.";
Expand Down Expand Up @@ -141,6 +147,8 @@ public abstract class Utility {
builder.parseCaseInsensitive().appendPattern("dd-MMM-yyyy HH:mm:ss");
dateFormat = builder.toFormatter();
DATETIME_FORMATTERS.add(dateFormat);

ZONED_FORMATTERS.add(ZONED_FORMATTER_SADL);
}

/**
Expand Down Expand Up @@ -423,7 +431,7 @@ public static String getStringFromFilePath(String path) throws Exception {
*/
public static String getSPARQLDateTimeString(String s) throws Exception{

// ISO_OFFSET_DATE_TIME is the only valid format with timezone
// ISO_OFFSET_DATE_TIME
// Try it first
// If it succeeds then return as-is, since it is also valid SPARQL
try{
Expand All @@ -433,6 +441,16 @@ public static String getSPARQLDateTimeString(String s) throws Exception{
// move on
}

// try all zoned formatters until find one that works
for (DateTimeFormatter formatter : ZONED_FORMATTERS){
try{
ZonedDateTime zoned = ZonedDateTime.parse(s, formatter);
return zoned.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
}catch (Exception e) {
// try the next one
}
}

// try all formatters until find one that works
for (DateTimeFormatter formatter : DATETIME_FORMATTERS){
try{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,27 @@ public class UtilityTest {
@Test
public void getSPARQLDateString() throws Exception{

assertEquals(Utility.getSPARQLDateString("12/03/2011"),"2011-12-03");
assertEquals(Utility.getSPARQLDateString("12-03-2011"),"2011-12-03");
assertEquals(Utility.getSPARQLDateString("2011-12-03"),"2011-12-03");
assertEquals(Utility.getSPARQLDateString("12-Jun-2008"),"2008-06-12");
assertEquals(Utility.getSPARQLDateString("12-MAY-2008"),"2008-05-12");
assertEquals("2011-12-03T10:15:30+01:00", Utility.getSPARQLDateTimeString("2011-12-03T10:15:30+01:00"));
assertEquals("1979-03-05T12:00:00-04:00", Utility.getSPARQLDateTimeString("1979-03-05T12:00:00-04:00"));

assertEquals(Utility.getSPARQLDateTimeString("12/03/2011 20:15:30"),"2011-12-03T20:15:30");
assertEquals(Utility.getSPARQLDateTimeString("2011-12-03T10:15:30"),"2011-12-03T10:15:30");
assertEquals(Utility.getSPARQLDateTimeString("2011-12-03T10:15:30+01:00"),"2011-12-03T10:15:30+01:00");
assertEquals(Utility.getSPARQLDateTimeString("1979-03-05T12:00:00-04:00"),"1979-03-05T12:00:00-04:00");
assertEquals(Utility.getSPARQLDateTimeString("12/03/2011"),"2011-12-03T00:00:00");
assertEquals(Utility.getSPARQLDateTimeString("12-03-2011"),"2011-12-03T00:00:00");
assertEquals(Utility.getSPARQLDateTimeString("2011-12-03"),"2011-12-03T00:00:00");
assertEquals(Utility.getSPARQLDateTimeString("12-Jun-2008"),"2008-06-12T00:00:00");
assertEquals(Utility.getSPARQLDateTimeString("12-MAY-2008"),"2008-05-12T00:00:00");
assertEquals(Utility.getSPARQLDateTimeString("12-Jun-2008 05:00:00"),"2008-06-12T05:00:00");
assertEquals(Utility.getSPARQLDateTimeString("12-MAY-2008 05:00:00"),"2008-05-12T05:00:00");
assertEquals("2017-03-22T20:00:00-04:00", Utility.getSPARQLDateTimeString("Wed Mar 22 20:00:00 EST 2017"));

assertEquals("2011-12-03T20:15:30", Utility.getSPARQLDateTimeString("12/03/2011 20:15:30"));
assertEquals("2011-12-03T10:15:30", Utility.getSPARQLDateTimeString("2011-12-03T10:15:30"));

assertEquals("2008-06-12T05:00:00", Utility.getSPARQLDateTimeString("12-Jun-2008 05:00:00"));
assertEquals("2008-05-12T05:00:00", Utility.getSPARQLDateTimeString("12-MAY-2008 05:00:00"));
assertEquals("2011-12-03T00:00:00", Utility.getSPARQLDateTimeString("12/03/2011"));
assertEquals("2011-12-03T00:00:00", Utility.getSPARQLDateTimeString("12-03-2011"));
assertEquals("2011-12-03T00:00:00", Utility.getSPARQLDateTimeString("2011-12-03"));
assertEquals("2008-06-12T00:00:00", Utility.getSPARQLDateTimeString("12-Jun-2008"));
assertEquals("2008-05-12T00:00:00", Utility.getSPARQLDateTimeString("12-MAY-2008"));

assertEquals("2011-12-03", Utility.getSPARQLDateString("12/03/2011"));
assertEquals("2011-12-03", Utility.getSPARQLDateString("12-03-2011"));
assertEquals("2011-12-03", Utility.getSPARQLDateString("2011-12-03"));
assertEquals("2008-06-12", Utility.getSPARQLDateString("12-Jun-2008"));
assertEquals("2008-05-12", Utility.getSPARQLDateString("12-MAY-2008"));

}

Expand Down

0 comments on commit 3f08698

Please sign in to comment.