Skip to content

Commit cd0c85a

Browse files
SmartTime to represent time without leading zeros
1 parent 9b28b83 commit cd0c85a

File tree

2 files changed

+219
-0
lines changed

2 files changed

+219
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2019 Philipp Salvisberg <philipp.salvisberg@trivadis.com>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.utplsql.sqldev.ui.runner
17+
18+
import java.text.DecimalFormat
19+
20+
class SmartTime {
21+
var Double seconds
22+
var boolean smart = false
23+
24+
new() {
25+
super()
26+
}
27+
28+
new(Double seconds, boolean smart) {
29+
super()
30+
this.seconds = seconds
31+
this.smart = smart
32+
}
33+
34+
def setMillis(Double seconds) {
35+
this.seconds = seconds
36+
}
37+
38+
def setSmart(boolean smart) {
39+
this.smart = smart
40+
}
41+
42+
def getSeconds() {
43+
return seconds
44+
}
45+
46+
override toString() {
47+
var String ret;
48+
if (seconds === null) {
49+
ret = null
50+
} else if (smart) {
51+
if (seconds >= 60*60) {
52+
val DecimalFormat formatter = new DecimalFormat("#0.00")
53+
ret = formatter.format(seconds / 60 / 60) + " h"
54+
} else if (seconds >= 60) {
55+
val DecimalFormat formatter = new DecimalFormat("#0.00")
56+
ret = formatter.format(seconds / 60) + " min"
57+
} else if (seconds >= 1) {
58+
val DecimalFormat formatter = new DecimalFormat("#0.000")
59+
ret = formatter.format(seconds) + " s"
60+
} else {
61+
val DecimalFormat formatter = new DecimalFormat("##0")
62+
ret = formatter.format(seconds * 1000) + " ms"
63+
}
64+
65+
} else {
66+
val DecimalFormat formatter = new DecimalFormat("##,##0.000")
67+
ret = formatter.format(seconds)
68+
}
69+
return ret
70+
}
71+
72+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Copyright 2019 Philipp Salvisberg <philipp.salvisberg@trivadis.com>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.utplsql.sqldev.test.runner
17+
18+
import org.junit.Assert
19+
import org.junit.Test
20+
import org.utplsql.sqldev.ui.runner.SmartTime
21+
22+
class SmartTimeTest {
23+
24+
@Test
25+
def null_default() {
26+
val effective = (new SmartTime(null, false)).toString
27+
Assert.assertEquals(null, effective);
28+
}
29+
30+
@Test
31+
def null_smart() {
32+
val effective = (new SmartTime(null, true)).toString
33+
Assert.assertEquals(null, effective);
34+
}
35+
36+
@Test
37+
def ms_0_default() {
38+
val effective = (new SmartTime(0.0, false)).toString
39+
Assert.assertEquals("0.000", effective);
40+
}
41+
42+
@Test
43+
def ms_0_smart() {
44+
val effective = (new SmartTime(0.0, true)).toString
45+
Assert.assertEquals("0 ms", effective);
46+
}
47+
48+
@Test
49+
def ms_999_default() {
50+
val effective = (new SmartTime(0.999, false)).toString
51+
Assert.assertEquals("0.999", effective);
52+
}
53+
54+
@Test
55+
def ms_999_smart() {
56+
val effective = (new SmartTime(0.999, true)).toString
57+
Assert.assertEquals("999 ms", effective);
58+
}
59+
60+
@Test
61+
def s_1_default() {
62+
val effective = (new SmartTime(1.0, false)).toString
63+
Assert.assertEquals("1.000", effective);
64+
}
65+
66+
@Test
67+
def s_1_smart() {
68+
val effective = (new SmartTime(1.0, true)).toString
69+
Assert.assertEquals("1.000 s", effective);
70+
}
71+
72+
@Test
73+
def s_59_default() {
74+
val effective = (new SmartTime(59.999, false)).toString
75+
Assert.assertEquals("59.999", effective);
76+
}
77+
78+
@Test
79+
def s_59_smart() {
80+
val effective = (new SmartTime(59.999, true)).toString
81+
Assert.assertEquals("59.999 s", effective);
82+
}
83+
84+
@Test
85+
def min_1_default() {
86+
val effective = (new SmartTime(60.0, false)).toString
87+
Assert.assertEquals("60.000", effective);
88+
}
89+
90+
@Test
91+
def min_1_smart() {
92+
val effective = (new SmartTime(60.0, true)).toString
93+
Assert.assertEquals("1.00 min", effective);
94+
}
95+
96+
@Test
97+
def min_59_default() {
98+
val effective = (new SmartTime(3599.999, false)).toString
99+
Assert.assertEquals("3,599.999", effective);
100+
}
101+
102+
@Test
103+
def min_59_smart_and_rounded() {
104+
val effective = (new SmartTime(3599.999, true)).toString
105+
Assert.assertEquals("60.00 min", effective);
106+
}
107+
108+
@Test
109+
def h_1_default() {
110+
val effective = (new SmartTime(3600.0, false)).toString
111+
Assert.assertEquals("3,600.000", effective);
112+
}
113+
114+
@Test
115+
def h_1_smart() {
116+
val effective = (new SmartTime(3600.0, true)).toString
117+
Assert.assertEquals("1.00 h", effective);
118+
}
119+
120+
@Test
121+
def h_max_default() {
122+
val effective = (new SmartTime(99999.999, false)).toString
123+
Assert.assertEquals("99,999.999", effective);
124+
}
125+
126+
@Test
127+
def h_max_smart() {
128+
val effective = (new SmartTime(99999.999, true)).toString
129+
Assert.assertEquals("27.78 h", effective);
130+
}
131+
132+
@Test
133+
def h_higher_than_max_default() {
134+
// larger than format mask
135+
// grouping separator applied, even if not specified in format mask
136+
val effective = (new SmartTime(100000000.0, false)).toString
137+
Assert.assertEquals("100,000,000.000", effective);
138+
}
139+
140+
@Test
141+
def h_higher_than_max_smart() {
142+
// larger than format mask
143+
// no grouping separator applied (that's ok)
144+
val effective = (new SmartTime(100000000.0, true)).toString
145+
Assert.assertEquals("27777.78 h", effective);
146+
}
147+
}

0 commit comments

Comments
 (0)