/
DurationFormatting.scala
136 lines (115 loc) · 4.52 KB
/
DurationFormatting.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* Licensed to Big Data Genomics (BDG) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The BDG licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bdgenomics.utils.instrumentation
import java.text.DecimalFormat
import java.util.concurrent.TimeUnit
import scala.concurrent.duration.Duration
/**
* Functions for formatting durations. The following rules are applied:
* - Durations greater than one hour are formatted as "X hours X mins X secs"
* - Durations greater than one minute, but less than one hour are formatted as "X mins X secs"
* - Durations less than one minute are formatted with two digits after the decimal point. Zeros are suppressed.
*/
@deprecated("to be removed in version 0.3.0")
object DurationFormatting {
private val TwoDigitNumberFormatter = new DecimalFormat(".0#")
/**
* Formats the passed-in value as a duration. Non-integer values will be rounded
* before they are formatted.
*/
def formatMillisecondDuration(number: Number): String = {
val conversion: (Long) => Long = TimeUnit.MILLISECONDS.toNanos
formatNumber(number, conversion)
}
/**
* Formats the passed-in value as a duration. Non-integer values will be rounded
* before they are formatted.
*/
def formatNanosecondDuration(number: Number): String = {
val conversion: (Long) => Long = TimeUnit.NANOSECONDS.toNanos
formatNumber(number, conversion)
}
def formatDuration(duration: Duration): String = {
val builder = new StringBuilder()
var nanoDuration = duration.toNanos
if (nanoDuration < 0) {
builder.append("-")
nanoDuration = Math.abs(nanoDuration)
}
val hours = TimeUnit.NANOSECONDS.toHours(nanoDuration)
nanoDuration -= TimeUnit.HOURS.toNanos(hours)
val minutes = TimeUnit.NANOSECONDS.toMinutes(nanoDuration)
nanoDuration -= TimeUnit.MINUTES.toNanos(minutes)
val seconds = TimeUnit.NANOSECONDS.toSeconds(nanoDuration)
nanoDuration -= TimeUnit.SECONDS.toNanos(seconds)
val millis = TimeUnit.NANOSECONDS.toMillis(nanoDuration)
nanoDuration -= TimeUnit.MILLISECONDS.toNanos(millis)
val micros = TimeUnit.NANOSECONDS.toMicros(nanoDuration)
nanoDuration -= TimeUnit.MICROSECONDS.toNanos(micros)
val nanos = nanoDuration
if (hours > 0) {
builder.append(hours)
builder.append(" hour").append(if (hours != 1) "s" else "")
}
if (minutes > 0 || hours > 0) {
if (hours > 0) {
builder.append(" ")
}
builder.append(minutes)
builder.append(" min").append(if (minutes != 1) "s" else "")
builder.append(" ")
builder.append(seconds)
builder.append(" sec").append(if (seconds != 1) "s" else "")
} else {
if (seconds > 0) {
formatValue(seconds, millis, builder)
builder.append(" secs")
} else {
if (millis > 0) {
formatValue(millis, micros, builder)
builder.append(" ms")
} else {
if (micros > 0) {
formatValue(micros, nanos, builder)
builder.append(" µs")
} else if (nanos > 0) {
builder.append(nanos)
builder.append(" ns")
} else {
builder.append("0")
}
}
}
}
builder.toString()
}
private def formatValue(largeValue: Long, smallValue: Long, builder: StringBuilder) {
val totalValue = largeValue + (smallValue / 1000d)
val stringValue = TwoDigitNumberFormatter.format(totalValue)
// DecimalFormat doesn't seem to be able to do this for us, even if we specify
// # as the first digit after the decimal place
if (stringValue.endsWith(".0")) {
builder.append(stringValue.substring(0, stringValue.length - ".0".length))
} else {
builder.append(stringValue)
}
}
private def formatNumber(number: Number, conversionFunction: (Long) => Long): String = {
formatDuration(Duration.fromNanos(conversionFunction.apply(number.longValue())))
}
}