Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Fix parsing of negative iso8601 duration

Christophe Bliarde207523ed276
diff --git a/app/models/timestamp.rb b/app/models/timestamp.rb
index 328ef864360..1ca124177ab 100644
--- a/app/models/timestamp.rb
+++ b/app/models/timestamp.rb
@@ -42,9 +42,9 @@ class Timestamp
def self.parse(iso8601_string)
return iso8601_string if iso8601_string.is_a?(Timestamp)
- iso8601_string.strip!
+ iso8601_string = iso8601_string.strip
iso8601_string = substitute_special_shortcut_values(iso8601_string)
- if iso8601_string.start_with? "P" # ISO8601 "Period"
+ if iso8601_string.start_with? /[+-]?P/ # ISO8601 "Period"
iso8601_string = ActiveSupport::Duration.parse(iso8601_string).iso8601
elsif (time = Time.zone.parse(iso8601_string)).present?
iso8601_string = time.iso8601
diff --git a/spec/models/timestamp_spec.rb b/spec/models/timestamp_spec.rb
index 63891fa3fa8..b6fe7ca9129 100644
--- a/spec/models/timestamp_spec.rb
+++ b/spec/models/timestamp_spec.rb
@@ -97,6 +97,22 @@ describe Timestamp do
expect(subject.to_duration).to eq ActiveSupport::Duration.build(10)
expect(subject.relative?).to be true
end
+
+ {
+ 'PT1H' => 'PT1H',
+ 'PT0001H' => 'PT1H',
+ 'PT0009H' => 'PT9H',
+ 'PT-1H' => 'PT-1H',
+ '+PT1H' => 'PT1H',
+ '-PT1H' => 'PT-1H',
+ '-PT-1H' => 'PT1H',
+ ' PT1H ' => 'PT1H',
+ '-P1M-1DT1H-02M' => 'P-1M1DT-1H2M'
+ }.each do |input, expected|
+ it "parses #{input.inspect} into #{expected.inspect}" do
+ expect(described_class.parse(input).to_s).to eq(expected)
+ end
+ end
end
describe "when providing a valid ISO8601 time" do