55
66public class DurationUtils {
77
8- private static char [] TIME_UNITS = { 'w' , 'd' , 'h' , 'm' , 's' };
9- private static int [] TIME_UNIT_MULTIPLIERS = { 60 * 60 * 24 * 7 , 60 * 60 * 24 , 60 * 60 , 60 , 1 };
10-
11- private static Pattern durationPattern = Pattern .compile ("(\\ s*(\\ d+)([a-z]))" );
8+ private static final String [] TIME_UNITS = { "mo" , "w" , "d" , "h" , "m" , "s" };
9+ private static final int [] TIME_UNIT_MULTIPLIERS = { 60 * 60 * 24 * 30 , 60 * 60 * 24 * 7 , 60 * 60 * 24 , 60 * 60 , 60 , 1 };
10+ private static Pattern durationPattern = Pattern .compile ("(\\ s*(\\ d+)(mo|[wdhms]))" );
1211
1312 /**
1413 * Create a human readable duration string from seconds.
@@ -18,15 +17,33 @@ public class DurationUtils {
1817 */
1918 public static final String toString (int durationSeconds ) {
2019
21- int weeks = durationSeconds / TIME_UNIT_MULTIPLIERS [0 ];
22- int days = (durationSeconds - weeks * TIME_UNIT_MULTIPLIERS [0 ]) / TIME_UNIT_MULTIPLIERS [1 ];
23- int seconds = durationSeconds - (weeks * TIME_UNIT_MULTIPLIERS [0 ]) - (days * TIME_UNIT_MULTIPLIERS [1 ]);
20+ int months = durationSeconds / TIME_UNIT_MULTIPLIERS [0 ];
21+ int weeks = (durationSeconds - months * TIME_UNIT_MULTIPLIERS [0 ]) / TIME_UNIT_MULTIPLIERS [1 ];
22+ int days = (durationSeconds - months * TIME_UNIT_MULTIPLIERS [0 ] - weeks * TIME_UNIT_MULTIPLIERS [1 ]) / TIME_UNIT_MULTIPLIERS [2 ];
23+ int seconds = durationSeconds - (months * TIME_UNIT_MULTIPLIERS [0 ]) - (weeks * TIME_UNIT_MULTIPLIERS [1 ]) - (days * TIME_UNIT_MULTIPLIERS [2 ]);
2424 int hours = seconds / 3600 ;
2525 int minutes = (seconds % 3600 ) / 60 ;
2626 seconds = seconds % 60 ;
2727
2828 StringBuilder buf = new StringBuilder ();
29- if (weeks > 0 ) {
29+ if (months > 0 ) {
30+
31+ buf .append (months ).append ("mo" );
32+ if (weeks > 0 ) {
33+ buf .append (weeks ).append ('w' );
34+ }
35+
36+ if (seconds > 0 ) {
37+ buf .append (days ).append ('d' ).append (hours ).append ('h' ).append (minutes ).append ('m' ).append (seconds ).append ('s' );
38+ } else if (minutes > 0 ) {
39+ buf .append (days ).append ('d' ).append (hours ).append ('h' ).append (minutes ).append ('m' );
40+ } else if (hours > 0 ) {
41+ buf .append (days ).append ('d' ).append (hours ).append ('h' );
42+ } else if (days > 0 ) {
43+ buf .append (days ).append ('d' );
44+ }
45+
46+ } else if (weeks > 0 ) {
3047
3148 buf .append (weeks ).append ('w' );
3249 if (seconds > 0 ) {
@@ -93,7 +110,7 @@ public static final int parse(String durationString) {
93110 int numGroups = matcher .groupCount ();
94111 if (numGroups == 3 ) {
95112
96- char unit = matcher .group (3 ). charAt ( 0 );
113+ String unit = matcher .group (3 );
97114 int nextUnitIndex = getUnitIndex (unit );
98115 if (nextUnitIndex > currentUnitIndex ) {
99116
@@ -119,10 +136,10 @@ public static final int parse(String durationString) {
119136 return (seconds );
120137 }
121138
122- private static final int getUnitIndex (char unit ) {
139+ private static final int getUnitIndex (String unit ) {
123140
124141 for (int i = 0 ; i < TIME_UNITS .length ; i ++) {
125- if (unit == TIME_UNITS [i ])
142+ if (unit . equals ( TIME_UNITS [i ]) )
126143 return (i );
127144 }
128145
0 commit comments