mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
added core.util.Time to help parsing time to seconds
and tested core.util.Time
This commit is contained in:
parent
b641adf98c
commit
977d8f7432
2 changed files with 219 additions and 0 deletions
70
jsprit-core/src/main/java/jsprit/core/util/Time.java
Normal file
70
jsprit-core/src/main/java/jsprit/core/util/Time.java
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
package jsprit.core.util;
|
||||
|
||||
public class Time {
|
||||
|
||||
/**
|
||||
* Parse time to seconds.
|
||||
*
|
||||
* <p>If you add PM or AM to timeString, it considers english-time, otherwise not.
|
||||
*
|
||||
* <p>timeString must be hour:min:sec
|
||||
* <p>example: 12 AM returns 12*3600. sec
|
||||
* 6:30 AM --> 6*3600. + 30*60.
|
||||
* 0:30:20 AM --> 30*3600. + 20.
|
||||
* 6:00 PM --> 6*3600. + 12.*3600.
|
||||
* 6:00:12 --> 6*3600. + 12.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static double parseTimeToSeconds(String timeString){
|
||||
if(timeString.substring(0, 1).matches("\\D")) throw new IllegalArgumentException("timeString must start with digit [0-9]");
|
||||
double dayTime = 0.;
|
||||
if(timeString.toLowerCase().contains("pm")){
|
||||
dayTime = 12.*3600.;
|
||||
}
|
||||
String[] tokens = timeString.split(":");
|
||||
|
||||
if(tokens.length == 1){ //1 AM or 01 AM
|
||||
return getHourInSeconds(tokens[0]) + dayTime;
|
||||
}
|
||||
else if(tokens.length == 2){
|
||||
return getHourInSeconds(tokens[0]) + getMinInSeconds(tokens[1]) + dayTime;
|
||||
}
|
||||
else if(tokens.length == 3){
|
||||
return getHourInSeconds(tokens[0]) + getMinInSeconds(tokens[1]) + getSecondsInSeconds(tokens[2]) + dayTime;
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("wrong timeString");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static double getSecondsInSeconds(String secString) {
|
||||
return getDigit(secString);
|
||||
}
|
||||
|
||||
private static double getMinInSeconds(String minString) {
|
||||
return getDigit(minString)*60.;
|
||||
}
|
||||
|
||||
private static double getHourInSeconds(String hourString) {
|
||||
return getDigit(hourString)*3600.;
|
||||
}
|
||||
|
||||
private static double getDigit(String digitString) {
|
||||
if(digitString.length() == 1){
|
||||
return Double.parseDouble(digitString);
|
||||
}
|
||||
if(digitString.substring(1, 2).matches("\\D")){
|
||||
return Double.parseDouble(digitString.substring(0, 1));
|
||||
}
|
||||
else{
|
||||
if(digitString.startsWith("0")){
|
||||
return Double.parseDouble(digitString.substring(1, 2));
|
||||
}
|
||||
else{
|
||||
return Double.parseDouble(digitString.substring(0, 2));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
149
jsprit-core/src/test/java/jsprit/core/util/TimeTest.java
Normal file
149
jsprit-core/src/test/java/jsprit/core/util/TimeTest.java
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
package jsprit.core.util;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class TimeTest {
|
||||
|
||||
@Test
|
||||
public void sixAM_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("6AM");
|
||||
assertEquals(6.*3600.,sec,0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sixAMWithWhiteSpace_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("6 AM");
|
||||
assertEquals(6.*3600.,sec,0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sixaMWithWhiteSpace_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("6 aM");
|
||||
assertEquals(6.*3600.,sec,0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sixamWithWhiteSpace_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("6 am");
|
||||
assertEquals(6.*3600.,sec,0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sixAmWithWhiteSpace_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("6 Am");
|
||||
assertEquals(6.*3600.,sec,0.01);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void sixPM_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("6PM");
|
||||
assertEquals(6.*3600.+12.*3600.,sec,0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sixPMWithWhiteSpace_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("6 PM");
|
||||
assertEquals(6.*3600.+12.*3600.,sec,0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sixpMWithWhiteSpace_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("6 pM");
|
||||
assertEquals(6.*3600.+12.*3600.,sec,0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sixpmWithWhiteSpace_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("6 pm");
|
||||
assertEquals(6.*3600.+12.*3600.,sec,0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sixPmWithWhiteSpace_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("6 Pm");
|
||||
assertEquals(6.*3600.+12.*3600,sec,0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sixAMWithLeadingZero_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("06AM");
|
||||
assertEquals(6.*3600.,sec,0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sixHour_twelveMin_AM_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("6:12AM");
|
||||
assertEquals(6.*3600.+12.*60.,sec,0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sixHour_sixMin_AM_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("6:6AM");
|
||||
assertEquals(6.*3600.+6.*60.,sec,0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sixHour_sixMinWithLeadingZero_AM_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("6:06AM");
|
||||
assertEquals(6.*3600.+6.*60.,sec,0.01);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void sixHour_twelveMin_PM_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("6:12PM");
|
||||
assertEquals(6.*3600.+12.*60.+12.*3600.,sec,0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sixHour_sixMin_PM_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("6:6PM");
|
||||
assertEquals(6.*3600.+6.*60.+12.*3600.,sec,0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sixHour_sixMinWithLeadingZero_PM_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("6:06PM");
|
||||
assertEquals(6.*3600.+6.*60.+12.*3600.,sec,0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sixHour_sixMinWithLeadingZero_twelveSec_PM_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("6:06:12PM");
|
||||
assertEquals(6.*3600.+6.*60.+12.*3600.+12.,sec,0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sixHour_sixMinWithLeadingZero_twelveSec_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("6:06:12");
|
||||
assertEquals(6.*3600.+6.*60.+12.,sec,0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sixHour_sixMinWithLeadingZero_sixSecWithLeadingZero_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("6:06:06");
|
||||
assertEquals(6.*3600.+6.*60.+6.,sec,0.01);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void whenTimeStringHasNoDigit_itThrowsException(){
|
||||
Time.parseTimeToSeconds("PM");
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void whenTimeStringHasMilliSeconds_itThrowsException(){
|
||||
Time.parseTimeToSeconds("01:00:12:01PM");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zeroHour_zeroMinWithLeadingZero_oneSecWithLeadingZero_shouldBeParsedCorrectly(){
|
||||
double sec = Time.parseTimeToSeconds("0:00:01");
|
||||
assertEquals(1.,sec,0.01);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue