Skip to content

Commit

Permalink
Support various formats as time input
Browse files Browse the repository at this point in the history
  • Loading branch information
markusweigelt committed Feb 19, 2024
1 parent e7c7f08 commit 77d296e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.kitodo.production.helper.metadata.MediaPartialHelper.addMediaPartialToMediaSelection;
import static org.kitodo.production.helper.metadata.MediaPartialHelper.calculateExtentAndSortMediaPartials;
import static org.kitodo.production.helper.metadata.MediaPartialHelper.convertFormattedTimeToMilliseconds;
import static org.kitodo.production.helper.metadata.MediaPartialHelper.convertTimeToFormattedTime;

import java.io.Serializable;
import java.util.Map;
Expand Down Expand Up @@ -166,6 +167,7 @@ protected boolean valid() {
validationError = "mediaPartialFormStartEmpty";
return false;
}
setBegin(convertTimeToFormattedTime(getBegin()));
if (!MediaPartialHelper.FORMATTED_TIME_PATTERN.matcher(getBegin()).matches()) {
validationError = "mediaPartialFormStartWrongTimeFormat";
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@
package org.kitodo.production.helper.metadata;

import java.net.URI;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalTime;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.kitodo.api.dataformat.LogicalDivision;
import org.kitodo.api.dataformat.MediaPartial;
Expand All @@ -47,6 +52,34 @@ public static Long convertFormattedTimeToMilliseconds(String formattedTime) {
return Duration.between(LocalTime.MIN, LocalTime.parse(formattedTime)).toMillis();
}

/**
* Convert various time input formats to formatted time.
*
* @param time
* The time input.
* @return The formatted time
*/
public static String convertTimeToFormattedTime(String time) {
if (FORMATTED_TIME_PATTERN.matcher(time).matches()) {
return time;
}

String[] separatedMilliseconds = time.split("\\.");
Date date;
try {
date = DateUtils.parseDate(separatedMilliseconds[0], "HH:mm:ss", "mm:ss", "ss");
} catch (ParseException e) {
return time;
}

if (separatedMilliseconds.length == 2) {
String filledMilliseconds = StringUtils.rightPad(separatedMilliseconds[1], 3, "0");
date = new Date(date.getTime() + Integer.parseInt(filledMilliseconds));
}

return new SimpleDateFormat("HH:mm:ss.SSS").format(date);
}

/**
* Convert milliseconds to formatted time.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.kitodo.api.dataformat.PhysicalDivision;
import org.kitodo.api.dataformat.View;
import org.kitodo.api.dataformat.Workpiece;
import org.kitodo.production.helper.metadata.MediaPartialHelper;
import org.kitodo.production.metadata.MetadataEditor;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
Expand Down Expand Up @@ -179,6 +180,19 @@ public void testValidation() throws NoSuchFieldException, IllegalAccessException
assertTrue(mediaPartialForm.valid());
}

/**
* Test time to formatted time conversion.
*/
@Test
public void testTimeToFormattedTimeConversion() {
assertEquals("00:00:01.000", MediaPartialHelper.convertTimeToFormattedTime("1"));
assertEquals("00:01:01.000", MediaPartialHelper.convertTimeToFormattedTime("1:1"));
assertEquals("01:01:01.000", MediaPartialHelper.convertTimeToFormattedTime("1:1:1"));
assertEquals("00:00:01.100", MediaPartialHelper.convertTimeToFormattedTime("1.1"));
assertEquals("00:01:01.110", MediaPartialHelper.convertTimeToFormattedTime("1:1.11"));
assertEquals("01:01:01.111", MediaPartialHelper.convertTimeToFormattedTime("1:1:1.111"));
}

private static String getValidationError(MediaPartialForm mediaPartialForm)
throws NoSuchFieldException, IllegalAccessException {
Field validationErrorField = mediaPartialForm.getClass().getDeclaredField("validationError");
Expand Down

0 comments on commit 77d296e

Please sign in to comment.