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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Update the Timestamp duration? logic to handle all valid duration strings
Dombi Attila6e14446abf33
diff --git a/app/models/timestamp.rb b/app/models/timestamp.rbindex 65609730208..12d0e03c494 100644--- a/app/models/timestamp.rb+++ b/app/models/timestamp.rb@@ -34,6 +34,8 @@ class Timestampclass Exception < StandardError; endclass TimestampParser+ DURATION_REGEX = /[+-]?P/ # ISO8601 "Period"+DATE_KEYWORD_REGEX =%r{^(?:#{ALLOWED_DATE_KEYWORDS.join("|")}) # match the relative date keyword@@ -49,7 +51,7 @@ class Timestamp@timestamp_string = self.class.substitute_special_shortcut_values(@original_string)case @timestamp_string- when /[+-]?P/ # ISO8601 "Period"+ when DURATION_REGEXActiveSupport::Duration.parse(@timestamp_string).iso8601when DATE_KEYWORD_REGEX # Built in date keywords@timestamp_string@@ -119,11 +121,11 @@ class Timestampenddef duration?- to_s.first == "P" # ISO8601 "Period"+ to_s.match? TimestampParser::DURATION_REGEXenddef relative_date_keyword?- TimestampParser::DATE_KEYWORD_REGEX.match?(to_s)+ to_s.match? TimestampParser::DATE_KEYWORD_REGEXenddef to_sdiff --git a/spec/models/timestamp_spec.rb b/spec/models/timestamp_spec.rbindex 827b8c06751..e9f1ab7e914 100644--- a/spec/models/timestamp_spec.rb+++ b/spec/models/timestamp_spec.rb@@ -100,17 +100,26 @@ describe Timestamp do{'PT1H' => 'PT1H',- 'PT0001H' => 'PT0001H',- 'PT0009H' => 'PT0009H',+ 'PT0001H' => 'PT1H',+ 'PT0009H' => 'PT9H','PT-1H' => 'PT-1H',- '+PT1H' => '+PT1H',- '-PT1H' => '-PT1H',- '-PT-1H' => '-PT-1H',+ '+PT1H' => 'PT1H',+ '-PT1H' => 'PT-1H',+ '-PT-1H' => 'PT1H',' PT1H ' => 'PT1H',- '-P1M-1DT1H-02M' => '-P1M-1DT1H-02M'+ '-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)+ context "with the duration #{input.inspect}" do+ subject { described_class.parse(input) }++ it "keeps the original duration string intact" do+ expect(subject).to eq(input.strip)+ end++ it "parses into #{expected.inspect}" do+ expect(subject).to be_duration+ expect(subject.to_duration.iso8601).to eq(expected)+ endendendend