Class: Commons::Lang::Time::DurationFormatUtils
- Inherits:
-
Object
- Object
- Commons::Lang::Time::DurationFormatUtils
- Defined in:
- lib/commons/lang/time/duration_format_utils.rb
Defined Under Namespace
Classes: Token
Constant Summary collapse
- ISO_EXTENDED_FORMAT_PATTERN =
"'P'yyyy'Y'M'M'd'DT'H'H'm'M's.S'S'"
- TOKEN_y =
:y
- TOKEN_M =
:M
- TOKEN_d =
:d
- TOKEN_H =
:H
- TOKEN_m =
:m
- TOKEN_s =
:s
- TOKEN_S =
:S
Class Method Summary collapse
- .format(tokens, years, months, days, hours, minutes, seconds, milliseconds, pad_with_zeros) ⇒ Object
- .format_duration(duration_millis, format, pad_with_zeros = true) ⇒ Object
- .format_duration_hms(duration_millis) ⇒ Object
-
.lexx(format) ⇒ Object
Parses a classic date format string into Tokens.
Instance Method Summary collapse
-
#initialize ⇒ DurationFormatUtils
constructor
A new instance of DurationFormatUtils.
Constructor Details
#initialize ⇒ DurationFormatUtils
Returns a new instance of DurationFormatUtils.
52 53 54 |
# File 'lib/commons/lang/time/duration_format_utils.rb', line 52 def initialize super() end |
Class Method Details
.format(tokens, years, months, days, hours, minutes, seconds, milliseconds, pad_with_zeros) ⇒ Object
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 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/commons/lang/time/duration_format_utils.rb', line 96 def self.format( tokens, years, months, days, hours, minutes, seconds, milliseconds, pad_with_zeros) buffer = String.new('') last_output_seconds = false tokens.each {|token| value = token.value count = token.count if value.kind_of?(String) buffer.concat(value.to_s) else if value.equal?(TOKEN_y) buffer.concat( pad_with_zeros ? years.to_s.rjust(count, '0') : years.to_s) last_output_seconds = false elsif value.equal?(TOKEN_M) buffer.concat( pad_with_zeros ? months.to_s.rjust(count, '0') : months.to_s) last_output_seconds = false elsif value.equal?(TOKEN_d) buffer.concat( pad_with_zeros ? days.to_s.rjust(count, '0') : days.to_s) last_output_seconds = false elsif value.equal?(TOKEN_H) buffer.concat( pad_with_zeros ? hours.to_s.rjust(count, '0') : hours.to_s) last_output_seconds = false elsif value.equal?(TOKEN_m) buffer.concat( pad_with_zeros ? minutes.to_s.rjust(count, '0') : minutes.to_s) last_output_seconds = false elsif value.equal?(TOKEN_s) buffer.concat( pad_with_zeros ? seconds.to_s.rjust(count, '0') : seconds.to_s) last_output_seconds = true elsif value.equal?(TOKEN_S) if last_output_seconds milliseconds += 1000 str = pad_with_zeros \ ? milliseconds.to_s.rjust(count, '0') : milliseconds.to_s buffer.concat(str[1..-1]) else buffer.concat(pad_with_zeros \ ? milliseconds.to_s.rjust(count, '0') : milliseconds.to_s) end last_output_seconds = false end end } return buffer end |
.format_duration(duration_millis, format, pad_with_zeros = true) ⇒ Object
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 |
# File 'lib/commons/lang/time/duration_format_utils.rb', line 62 def self.format_duration(duration_millis, format, pad_with_zeros = true) tokens = lexx(format) days = 0 hours = 0 minutes = 0 seconds = 0 milliseconds = 0 if Token.contains_token_with_value?(tokens, TOKEN_d) days = duration_millis.div(DateUtils::MILLIS_PER_DAY) duration_millis = duration_millis - (days * DateUtils::MILLIS_PER_DAY) end if Token.contains_token_with_value?(tokens, TOKEN_H) hours = duration_millis.div(DateUtils::MILLIS_PER_HOUR) duration_millis = duration_millis - (hours * DateUtils::MILLIS_PER_HOUR) end if Token.contains_token_with_value?(tokens, TOKEN_m) minutes = duration_millis.div(DateUtils::MILLIS_PER_MINUTE) duration_millis = duration_millis - (minutes * DateUtils::MILLIS_PER_MINUTE) end if Token.contains_token_with_value?(tokens, TOKEN_s) seconds = duration_millis.div(DateUtils::MILLIS_PER_SECOND) duration_millis = duration_millis - (seconds * DateUtils::MILLIS_PER_SECOND) end if Token.contains_token_with_value?(tokens, TOKEN_S) milliseconds = duration_millis end return format( tokens, 0, 0, days, hours, minutes, seconds, milliseconds, pad_with_zeros) end |
.format_duration_hms(duration_millis) ⇒ Object
57 58 59 |
# File 'lib/commons/lang/time/duration_format_utils.rb', line 57 def self.format_duration_hms(duration_millis) return format_duration(duration_millis, 'H:mm:ss.SSS') end |
.lexx(format) ⇒ Object
Parses a classic date format string into Tokens
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/commons/lang/time/duration_format_utils.rb', line 153 def self.lexx(format) chars = format.split(//) tokens = Array.new in_literal = false buffer = nil previous = nil chars.each {|char| if in_literal && char != "'" buffer.concat(char) next end value = nil case char # TODO: Need to handle escaping of ' when "'" if in_literal buffer = nil in_literal = false else buffer = String.new('') tokens.push(Token.new(buffer)) in_literal = true end when 'y' then value = TOKEN_y when 'M' then value = TOKEN_M when 'd' then value = TOKEN_d when 'H' then value = TOKEN_H when 'm' then value = TOKEN_m when 's' then value = TOKEN_s when 'S' then value = TOKEN_S else if buffer == nil buffer = String.new('') tokens.push(Token.new(buffer)) end buffer.concat(char) end if value != nil if previous != nil && previous.value.equal?(value) previous.increment else token = Token.new(value) tokens.push(token) previous = token end buffer = nil end } return tokens end |