/
StringFormatHelper.groovy
50 lines (40 loc) · 1.46 KB
/
StringFormatHelper.groovy
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
package com.athaydes.spockframework.report.internal
import groovy.time.TimeDuration
import java.text.DecimalFormat
/**
*
* User: Renato
*/
class StringFormatHelper {
private final MINUTE = 60 * 1000
private final HOUR = 60 * MINUTE
static final ds = new DecimalFormat().decimalFormatSymbols.decimalSeparator
String toTimeDuration( timeInMs ) {
long t = timeInMs?.toLong() ?: 0L
int hours = ( t / HOUR ).toInteger()
int mins = ( ( t - HOUR * hours ) / MINUTE ).toInteger()
int secs = ( ( t - HOUR * hours - mins * MINUTE ) / 1000 ).toInteger()
int millis = ( t % 1000 ).toInteger()
internationalizeTimeDuration( new TimeDuration( hours, mins, secs, millis ) )
}
private String internationalizeTimeDuration( TimeDuration timeDuration ) {
( ds == '.' ) ? timeDuration.toString() : timeDuration.toString().replace( '.', ds.toString() )
}
String toPercentage( double rate ) {
String.format( '%.2f%%', rate * 100 ).replace( "${ds}00", "${ds}0" )
}
String formatToHtml( String text ) {
text.replaceAll( /[\t\n]/, '<br/>' )
}
String toDateString( Date date ) {
date.toString()
}
String escapeXml( String str ) {
str
.replaceAll( '&', '&' )
.replaceAll( '<', '<' )
.replaceAll( '>', '>' )
.replaceAll( '"', '"' )
.replaceAll( '\'', ''' )
}
}