Skip to content

Commit fc5dc3b

Browse files
committed
epoch parser + pretty time print from milliseconds added
1 parent 0487ee1 commit fc5dc3b

File tree

7 files changed

+237
-2
lines changed

7 files changed

+237
-2
lines changed

SmartVehicleAnalytics/.classpath

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
33
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="src" path="test"/>
45
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
6+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
57
<classpathentry kind="output" path="bin"/>
68
</classpath>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package smart.vehicle.analytics.exceptions;
2+
3+
public class InvalidInputTypeException extends Exception {
4+
5+
public InvalidInputTypeException() {
6+
super("error.invalid.input.type");
7+
}
8+
9+
}

SmartVehicleAnalytics/src/smart/vehicle/analytics/main/Main.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
public class Main {
44

55
public static void main(String[] args) {
6-
System.out.println("Our smart vehicle analytics");
7-
System.out.println("Our smart vehicle analytics from subhadip");
6+
System.out.println("Our smart vehicle analytics - subhadip & debayan");
87
}
98

109
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package smart.vehicle.analytics.timer;
2+
3+
import smart.vehicle.analytics.exceptions.InvalidInputTypeException;
4+
5+
public class EpochParser {
6+
7+
private static final int SECOND = 1000;
8+
private static final int MINUTE = 60 * SECOND;
9+
private static final int HOUR = 60 * MINUTE;
10+
private static final int DAY = 24 * HOUR;
11+
12+
public TimeUnit convert(String millisecondsInString) throws InvalidInputTypeException {
13+
long milliseconds = 0;
14+
try {
15+
milliseconds = Long.valueOf(millisecondsInString);
16+
}
17+
catch (NumberFormatException e) {
18+
throw new InvalidInputTypeException();
19+
}
20+
21+
TimeUnit timeUnit = new TimeUnit();
22+
23+
if(milliseconds > DAY) {
24+
timeUnit.setDays(milliseconds / DAY);
25+
milliseconds %= DAY;
26+
}
27+
if(milliseconds > HOUR) {
28+
timeUnit.setHours(milliseconds / HOUR);
29+
milliseconds %= HOUR;
30+
}
31+
if(milliseconds > MINUTE) {
32+
timeUnit.setMinutes(milliseconds / MINUTE);
33+
milliseconds %= MINUTE;
34+
}
35+
if(milliseconds > SECOND) {
36+
timeUnit.setSeconds(milliseconds / SECOND);
37+
milliseconds %= SECOND;
38+
}
39+
timeUnit.setMillis(milliseconds);
40+
41+
return timeUnit;
42+
}
43+
44+
45+
46+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package smart.vehicle.analytics.timer;
2+
3+
public class TimeUnit {
4+
5+
private long days;
6+
private long hours;
7+
private long minutes;
8+
private long seconds;
9+
private long millis;
10+
private TimeUnitDecorator timeUnitDecorator;
11+
12+
public TimeUnit() {
13+
timeUnitDecorator = new TimeUnitDecorator(this);
14+
}
15+
16+
public long getDays() {
17+
return days;
18+
}
19+
public void setDays(long days) {
20+
this.days = days;
21+
}
22+
public long getHours() {
23+
return hours;
24+
}
25+
public void setHours(long hours) {
26+
this.hours = hours;
27+
}
28+
public long getMinutes() {
29+
return minutes;
30+
}
31+
public void setMinutes(long minutes) {
32+
this.minutes = minutes;
33+
}
34+
public long getSeconds() {
35+
return seconds;
36+
}
37+
public void setSeconds(long seconds) {
38+
this.seconds = seconds;
39+
}
40+
public long getMillis() {
41+
return millis;
42+
}
43+
public void setMillis(long millis) {
44+
this.millis = millis;
45+
}
46+
47+
public String toPrettyString() {
48+
return timeUnitDecorator.toPrettyTimestampString();
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return null;
54+
}
55+
56+
57+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package smart.vehicle.analytics.timer;
2+
3+
public class TimeUnitDecorator {
4+
5+
private TimeUnit theTimeUnit;
6+
7+
public TimeUnitDecorator(TimeUnit timeUnit) {
8+
this.theTimeUnit = timeUnit;
9+
}
10+
11+
public String getDayString() {
12+
String day = "";
13+
switch (Long.valueOf(theTimeUnit.getDays()).intValue()) {
14+
case 0:
15+
day = "1st day";
16+
break;
17+
case 1:
18+
day = "2nd day";
19+
break;
20+
case 2:
21+
day = "3rd day";
22+
break;
23+
case 3:
24+
day = "4th day";
25+
break;
26+
case 4:
27+
day = "5th day";
28+
break;
29+
case 5:
30+
day = "6th day";
31+
break;
32+
default:
33+
break;
34+
}
35+
return day;
36+
}
37+
38+
public String getTimeString() {
39+
StringBuffer time = new StringBuffer("");
40+
boolean beforeNoon = true;
41+
42+
// Hour of the time of day
43+
if(theTimeUnit.getHours() < 13) {
44+
if(theTimeUnit.getHours() == 0) {
45+
time.append("12");
46+
}
47+
else {
48+
time.append(theTimeUnit.getHours());
49+
}
50+
beforeNoon = true;
51+
}
52+
else {
53+
time.append(theTimeUnit.getHours() % 12);
54+
beforeNoon = false;
55+
}
56+
// Hour - end
57+
time.append(":");
58+
// Minute of the time of day
59+
if(theTimeUnit.getMinutes() < 10) {
60+
time.append("0");
61+
}
62+
time.append(theTimeUnit.getMinutes());
63+
// Minute - end
64+
time.append(":");
65+
// Second of the time of day
66+
if(theTimeUnit.getSeconds() < 10) {
67+
time.append("0");
68+
}
69+
time.append(theTimeUnit.getSeconds());
70+
// Second - end
71+
time.append(" ");
72+
73+
if(beforeNoon) {
74+
time.append("AM");
75+
}
76+
else {
77+
time.append("PM");
78+
}
79+
80+
return time.toString();
81+
}
82+
83+
public String toPrettyTimestampString() {
84+
return "[" + getDayString() + " - " + getTimeString() + "]";
85+
}
86+
87+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package smart.vehicle.analytics.timer;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
import smart.vehicle.analytics.exceptions.InvalidInputTypeException;
10+
11+
public class EpochParserTest {
12+
13+
private EpochParser epochParser;
14+
15+
@Before
16+
public void setUp() throws Exception {
17+
epochParser = new EpochParser();
18+
}
19+
20+
@After
21+
public void tearDown() throws Exception {
22+
}
23+
24+
@Test
25+
public void testTimeUnitDetails() throws InvalidInputTypeException {
26+
TimeUnit atime = epochParser.convert("269123");
27+
assertEquals(0, atime.getDays());
28+
assertEquals(0, atime.getHours());
29+
assertEquals(4, atime.getMinutes());
30+
assertEquals(29, atime.getSeconds());
31+
assertEquals(123, atime.getMillis());
32+
assertEquals("[1st day - 12:04:29 AM]", atime.toPrettyString());
33+
}
34+
35+
}

0 commit comments

Comments
 (0)