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

Preserve the timezone component of the provided Timestamp string while parsing

Dombi Attila2ad99470ab0d
diff --git a/app/models/timestamp.rb b/app/models/timestamp.rb
index 3ee962c36cd..fece7777dae 100644
--- a/app/models/timestamp.rb
+++ b/app/models/timestamp.rb
@@ -54,7 +54,7 @@ class Timestamp
when DATE_KEYWORD_REGEX # Built in date keywords
@timestamp_string
else
- Time.zone.iso8601(@timestamp_string).iso8601
+ DateTime.iso8601(@timestamp_string).iso8601
end
rescue ArgumentError => e
raise e.class, "The string \"#{@original_string}\" cannot be parsed to a Timestamp."
diff --git a/spec/models/timestamp_spec.rb b/spec/models/timestamp_spec.rb
index 54627318517..e79908a816d 100644
--- a/spec/models/timestamp_spec.rb
+++ b/spec/models/timestamp_spec.rb
@@ -121,10 +121,34 @@ describe Timestamp do
it "returns a described_class representing that absolute time" do
expect(subject).to be_a described_class
expect(subject).to be_valid
- expect(subject.to_s).to eq "2022-10-29T21:55:58Z"
- expect(subject.to_time).to eq Time.zone.parse("2022-10-29T21:55:58Z")
+ expect(subject.to_s).to eq "2022-10-29T21:55:58+00:00"
+ expect(subject.to_time).to eq Time.iso8601("2022-10-29T21:55:58+00:00")
expect(subject).not_to be_relative
end
+
+ context 'with a non-UTC time' do
+ subject { described_class.parse("2022-10-29T21:55:58+03:00") }
+
+ it "returns a described_class representing that absolute time and preserve the timezone component" do
+ expect(subject).to be_a described_class
+ expect(subject).to be_valid
+ expect(subject.to_s).to eq "2022-10-29T21:55:58+03:00"
+ expect(subject.to_time).to eq Time.iso8601("2022-10-29T21:55:58+03:00")
+ expect(subject).not_to be_relative
+ end
+ end
+
+ context 'without the seconds designator' do
+ subject { described_class.parse("2022-10-29T21:55Z") }
+
+ it "returns a described_class representing that absolute time" do
+ expect(subject).to be_a described_class
+ expect(subject).to be_valid
+ expect(subject.to_s).to eq "2022-10-29T21:55:00+00:00"
+ expect(subject.to_time).to eq Time.iso8601("2022-10-29T21:55:00Z")
+ expect(subject).not_to be_relative
+ end
+ end
end
describe "when providing a special shortcut value" do
@@ -169,7 +193,7 @@ describe Timestamp do
it "returns a Timestamp representing that absolute time" do
expect(subject).to be_a described_class
expect(subject).to be_valid
- expect(subject.to_s).to eq "2022-01-01T00:00:00Z"
+ expect(subject.to_s).to eq "2022-01-01T00:00:00+00:00"
expect(subject.to_time).to eq Time.zone.parse("2022-01-01T00:00:00Z")
expect(subject).not_to be_relative
end