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

Replace format_time_as_date with format_date

Oliver Güntherd0a3ce57ccb8
diff --git a/app/components/work_packages/reminder/modal_body_component.rb b/app/components/work_packages/reminder/modal_body_component.rb
index f58592786c8..5c0e1102a6a 100644
--- a/app/components/work_packages/reminder/modal_body_component.rb
+++ b/app/components/work_packages/reminder/modal_body_component.rb
@@ -120,7 +120,7 @@ module WorkPackages
end
def time_as_date(time)
- format_time_as_date(time, format: "%Y-%m-%d")
+ format_date(time, format: "%Y-%m-%d")
end
def attribute_blank?(attribute)
diff --git a/app/models/exports/concerns/csv.rb b/app/models/exports/concerns/csv.rb
index 5c04f1b64d9..2c4571787bf 100644
--- a/app/models/exports/concerns/csv.rb
+++ b/app/models/exports/concerns/csv.rb
@@ -84,7 +84,7 @@ module Exports
def csv_export_filename
sane_filename(
"#{Setting.app_title} #{title} \
- #{format_time_as_date(Time.zone.now, format: '%Y-%m-%d')}.csv"
+ #{format_date(Time.zone.now, format: '%Y-%m-%d')}.csv"
)
end
end
diff --git a/lib_static/redmine/i18n.rb b/lib_static/redmine/i18n.rb
index 53f3941f041..e123bb2d740 100644
--- a/lib_static/redmine/i18n.rb
+++ b/lib_static/redmine/i18n.rb
@@ -69,10 +69,27 @@ module Redmine
("%.2f" % hours.to_f)
end
- def format_date(date)
- return nil unless date
+ # Formats the given date or datetime as a date string according to the user's time zone
+ # and optional specified or system default format.
+ #
+ # @param date_or_time [Date|Time] The date or time object to format.
+ # @param time_zone [ActiveSupport::TimeZone] Use a different time zone than the current users's.
+ # If provided, will output the time zone identifier
+ # @param format [String, nil] The strftime format to use for the date. If nil, the default
+ # date format from `Setting.date_format` is used.
+ def format_date(date_or_time, time_zone: nil, format: Setting.date_format)
+ return nil unless date_or_time
+
+ local =
+ if time_zone
+ date_or_time.in_time_zone(time_zone).to_date
+ elsif date_or_time.instance_of?(Date) # Important not to use is_a? as it will match DateTime
+ date_or_time
+ else
+ in_user_zone(time).to_date
+ end
- Setting.date_format.blank? ? ::I18n.l(date.to_date) : date.strftime(Setting.date_format)
+ format.blank? ? ::I18n.l(local) : date.strftime(format)
end
##
@@ -118,25 +135,6 @@ module Redmine
/(\[(.+?)\]\((.+?)\))/
end
- # Formats the given time as a date string according to the user's time zone and
- # optional specified format.
- #
- # @param time [Time] The time to format.
- # @param format [String, nil] The strftime format to use for the date. If nil, the default
- # date format from `Setting.date_format` is used.
- # @return [String, nil] The formatted date string, or nil if the time is not provided.
- def format_time_as_date(time, format: nil)
- return nil unless time
-
- local_date = in_user_zone(time).to_date
-
- if format
- local_date.strftime(format)
- else
- format_date(local_date)
- end
- end
-
# Formats the given time as a time string according to the user's time zone
# and optional specified format.
#
diff --git a/modules/bim/lib/open_project/bim/bcf_xml/exporter.rb b/modules/bim/lib/open_project/bim/bcf_xml/exporter.rb
index dcb27d4b8d6..49384b1c7be 100644
--- a/modules/bim/lib/open_project/bim/bcf_xml/exporter.rb
+++ b/modules/bim/lib/open_project/bim/bcf_xml/exporter.rb
@@ -43,7 +43,7 @@ module OpenProject::Bim::BcfXml
sane_filename(
"#{Setting.app_title} #{I18n.t(:label_work_package_plural)} \
- #{format_time_as_date(Time.current, format: '%Y-%m-%d')}.bcf"
+ #{format_date(Time.current, format: '%Y-%m-%d')}.bcf"
)
end
diff --git a/modules/meeting/app/components/meetings/row_component.rb b/modules/meeting/app/components/meetings/row_component.rb
index cb6dc7dc4c8..e38e79c9b69 100644
--- a/modules/meeting/app/components/meetings/row_component.rb
+++ b/modules/meeting/app/components/meetings/row_component.rb
@@ -60,7 +60,7 @@ module Meetings
else
safe_join(
[
- helpers.format_time_as_date(model.start_time),
+ helpers.format_date(model.start_time),
helpers.format_time(model.start_time, include_date: false)
],
" "
diff --git a/modules/meeting/app/components/meetings/side_panel/details_component.html.erb b/modules/meeting/app/components/meetings/side_panel/details_component.html.erb
index db2ba4a8901..1d364351994 100644
--- a/modules/meeting/app/components/meetings/side_panel/details_component.html.erb
+++ b/modules/meeting/app/components/meetings/side_panel/details_component.html.erb
@@ -50,7 +50,7 @@
details.with_row do
render_meeting_attribute_row(:calendar) do
render(Primer::Beta::Text.new) do
- format_time_as_date(@meeting.start_time)
+ format_date(@meeting.start_time)
end
end
end
diff --git a/modules/meeting/app/components/meetings/side_panel/details_form_component.rb b/modules/meeting/app/components/meetings/side_panel/details_form_component.rb
index 3ba4b391928..0e72937d3d1 100644
--- a/modules/meeting/app/components/meetings/side_panel/details_form_component.rb
+++ b/modules/meeting/app/components/meetings/side_panel/details_form_component.rb
@@ -47,7 +47,7 @@ module Meetings
private
def start_date_initial_value
- format_time_as_date(@meeting.start_time, format: "%Y-%m-%d")
+ format_date(@meeting.start_time, format: "%Y-%m-%d")
end
def start_time_initial_value
diff --git a/modules/meeting/app/models/meeting/virtual_start_time.rb b/modules/meeting/app/models/meeting/virtual_start_time.rb
index 405fc8979cc..6e476af4c78 100644
--- a/modules/meeting/app/models/meeting/virtual_start_time.rb
+++ b/modules/meeting/app/models/meeting/virtual_start_time.rb
@@ -112,7 +112,7 @@ module Meeting::VirtualStartTime
end
def update_derived_fields
- @start_date = format_time_as_date(start_time, format: "%Y-%m-%d")
+ @start_date = format_date(start_time, format: "%Y-%m-%d")
@start_time_hour = format_time(start_time, include_date: false, format: "%H:%M")
end
diff --git a/modules/meeting/app/views/meeting_mailer/cancelled.html.erb b/modules/meeting/app/views/meeting_mailer/cancelled.html.erb
index 60b556e7820..5d9d73eb189 100644
--- a/modules/meeting/app/views/meeting_mailer/cancelled.html.erb
+++ b/modules/meeting/app/views/meeting_mailer/cancelled.html.erb
@@ -59,7 +59,7 @@ See COPYRIGHT and LICENSE files for more details.
</td>
<td style="<%= placeholder_text_styles %>">
<s>
- <%= format_time_as_date @meeting.start_time %> <%= format_time @meeting.start_time, include_date: false %>
+ <%= format_date @meeting.start_time %> <%= format_time @meeting.start_time, include_date: false %>
-
<%= format_time @meeting.end_time, include_date: false %> (<%= formatted_time_zone_offset %>)
</s>
diff --git a/modules/meeting/app/views/meeting_mailer/cancelled.text.erb b/modules/meeting/app/views/meeting_mailer/cancelled.text.erb
index 8e2a05f3f3e..62d38f5f8bf 100644
--- a/modules/meeting/app/views/meeting_mailer/cancelled.text.erb
+++ b/modules/meeting/app/views/meeting_mailer/cancelled.text.erb
@@ -44,4 +44,4 @@ See COPYRIGHT and LICENSE files for more details.
<%= @meeting.project.name %>: <%= @meeting.title %>
<%= @meeting.author %>
-<%= t :label_meeting_date_time %>: <%= format_time_as_date @meeting.start_time %> <%= format_time @meeting.start_time, include_date: false %>-<%= format_time @meeting.end_time, include_date: false %> (<%= formatted_time_zone_offset %>)
+<%= t :label_meeting_date_time %>: <%= format_date @meeting.start_time %> <%= format_time @meeting.start_time, include_date: false %>-<%= format_time @meeting.end_time, include_date: false %> (<%= formatted_time_zone_offset %>)
diff --git a/modules/meeting/app/views/meeting_mailer/icalendar_notification.html.erb b/modules/meeting/app/views/meeting_mailer/icalendar_notification.html.erb
index bcc30100146..31171b2b29b 100644
--- a/modules/meeting/app/views/meeting_mailer/icalendar_notification.html.erb
+++ b/modules/meeting/app/views/meeting_mailer/icalendar_notification.html.erb
@@ -4,7 +4,7 @@
<p><%= t(:text_notificiation_invited) %></p>
<ul>
-<li><%= t :label_meeting_date_time %>: <%= format_time_as_date @meeting.start_time %> <%= format_time @meeting.start_time, include_date: false %>-<%= format_time @meeting.end_time, include_date: false %> (<%= formatted_time_zone_offset %>)</li>
+<li><%= t :label_meeting_date_time %>: <%= format_date @meeting.start_time %> <%= format_time @meeting.start_time, include_date: false %>-<%= format_time @meeting.end_time, include_date: false %> (<%= formatted_time_zone_offset %>)</li>
<li><%= Meeting.human_attribute_name(:location) %>: <%= @meeting.location %></li>
<li><%= Meeting.human_attribute_name(:participants_invited) %>: <%= @meeting.participants.invited.sort.join("; ") %></li>
<li><%= Meeting.human_attribute_name(:participants_attended) %>: <%= @meeting.participants.attended.sort.join("; ") %></li>
diff --git a/modules/meeting/app/views/meeting_mailer/icalendar_notification.text.erb b/modules/meeting/app/views/meeting_mailer/icalendar_notification.text.erb
index 077cc974d31..16dd800f8ce 100644
--- a/modules/meeting/app/views/meeting_mailer/icalendar_notification.text.erb
+++ b/modules/meeting/app/views/meeting_mailer/icalendar_notification.text.erb
@@ -3,7 +3,7 @@
<%= t(:text_notificiation_invited) %>
-<%=t :label_meeting_date_time %>: <%= format_time_as_date @meeting.start_time %> <%= format_time @meeting.start_time, include_date: false %>-<%= format_time @meeting.end_time, include_date: false %> (<%= formatted_time_zone_offset %>)
+<%=t :label_meeting_date_time %>: <%= format_date @meeting.start_time %> <%= format_time @meeting.start_time, include_date: false %>-<%= format_time @meeting.end_time, include_date: false %> (<%= formatted_time_zone_offset %>)
<%=Meeting.human_attribute_name(:location) %>: <%= @meeting.location %>
<%=Meeting.human_attribute_name(:participants_invited) %>: <%= @meeting.participants.invited.sort.join("; ") %>
<%=Meeting.human_attribute_name(:participants_attended) %>: <%= @meeting.participants.attended.sort.join("; ") %>
diff --git a/modules/meeting/app/views/meeting_mailer/invited.html.erb b/modules/meeting/app/views/meeting_mailer/invited.html.erb
index c21ecf63708..b0196ea6f34 100644
--- a/modules/meeting/app/views/meeting_mailer/invited.html.erb
+++ b/modules/meeting/app/views/meeting_mailer/invited.html.erb
@@ -45,7 +45,7 @@ See COPYRIGHT and LICENSE files for more details.
<%= I18n.t(:label_meeting_date_time) %>
</td>
<td style="<%= placeholder_text_styles %>">
- <%= format_time_as_date @meeting.start_time %> <%= format_time @meeting.start_time, include_date: false %>
+ <%= format_date @meeting.start_time %> <%= format_time @meeting.start_time, include_date: false %>
-
<%= format_time @meeting.end_time, include_date: false %> (<%= formatted_time_zone_offset %>)
</td>
diff --git a/modules/meeting/app/views/meeting_mailer/invited.text.erb b/modules/meeting/app/views/meeting_mailer/invited.text.erb
index 021ad77f12e..a80b24d651d 100644
--- a/modules/meeting/app/views/meeting_mailer/invited.text.erb
+++ b/modules/meeting/app/views/meeting_mailer/invited.text.erb
@@ -32,7 +32,7 @@ See COPYRIGHT and LICENSE files for more details.
<%= @meeting.project.name %>: <%= @meeting.title %> (<%= meeting_url(@meeting) %>)
<%= @meeting.author %>
-<%=t :label_meeting_date_time %>: <%= format_time_as_date @meeting.start_time %> <%= format_time @meeting.start_time, include_date: false %>-<%= format_time @meeting.end_time, include_date: false %> (<%= formatted_time_zone_offset %>)
+<%=t :label_meeting_date_time %>: <%= format_date @meeting.start_time %> <%= format_time @meeting.start_time, include_date: false %>-<%= format_time @meeting.end_time, include_date: false %> (<%= formatted_time_zone_offset %>)
<%= Meeting.human_attribute_name(:location) %>: <%= @meeting.location %>
<%= Meeting.human_attribute_name(:participants_invited) %>: <%= @meeting.participants.invited.sort.join("; ") %>
<%= Meeting.human_attribute_name(:participants_attended) %>: <%= @meeting.participants.attended.sort.join("; ") %>
diff --git a/modules/meeting/app/views/meeting_mailer/rescheduled.html.erb b/modules/meeting/app/views/meeting_mailer/rescheduled.html.erb
index 9a03fe20e30..0806594e5e0 100644
--- a/modules/meeting/app/views/meeting_mailer/rescheduled.html.erb
+++ b/modules/meeting/app/views/meeting_mailer/rescheduled.html.erb
@@ -46,7 +46,7 @@ See COPYRIGHT and LICENSE files for more details.
</td>
<td style="<%= placeholder_text_styles %>">
<s>
- <%= format_time_as_date @changes[:old_start] %> <%= format_time @changes[:old_start], include_date: false %>
+ <%= format_date @changes[:old_start] %> <%= format_time @changes[:old_start], include_date: false %>
-
<%= format_time (@changes[:old_start] + @changes[:old_duration].hours), include_date: false %> (<%= formatted_time_zone_offset %>)
</s>
@@ -57,7 +57,7 @@ See COPYRIGHT and LICENSE files for more details.
<%= t("meeting.email.rescheduled.new_date_time") %>
</td>
<td style="<%= placeholder_text_styles("font-weight": "bold") %>">
- <%= format_time_as_date @changes[:new_start] %> <%= format_time @changes[:new_start], include_date: false %>
+ <%= format_date @changes[:new_start] %> <%= format_time @changes[:new_start], include_date: false %>
-
<%= format_time (@changes[:new_start] + @changes[:new_duration].hours), include_date: false %> (<%= formatted_time_zone_offset %>)
</td>
diff --git a/modules/meeting/app/views/meeting_mailer/rescheduled.text.erb b/modules/meeting/app/views/meeting_mailer/rescheduled.text.erb
index 93810d371c3..d85d3159197 100644
--- a/modules/meeting/app/views/meeting_mailer/rescheduled.text.erb
+++ b/modules/meeting/app/views/meeting_mailer/rescheduled.text.erb
@@ -37,7 +37,7 @@ See COPYRIGHT and LICENSE files for more details.
) %>
<%= t("meeting.email.rescheduled.old_date_time") %>:
-<%= format_time_as_date @changes[:old_start] %> <%= format_time @changes[:old_start], include_date: false %> - <%= format_time (@changes[:old_start] + @changes[:old_duration]), include_date: false %> (<%= formatted_time_zone_offset %>}
+<%= format_date @changes[:old_start] %> <%= format_time @changes[:old_start], include_date: false %> - <%= format_time (@changes[:old_start] + @changes[:old_duration]), include_date: false %> (<%= formatted_time_zone_offset %>}
<%= t("meeting.email.rescheduled.new_date_time") %>:
-<%= format_time_as_date @changes[:new_start] %> <%= format_time @changes[:new_start], include_date: false %> - <%= format_time (@changes[:new_start] + @changes[:new_duration]), include_date: false %> (<%= formatted_time_zone_offset %>}
+<%= format_date @changes[:new_start] %> <%= format_time @changes[:new_start], include_date: false %> - <%= format_time (@changes[:new_start] + @changes[:new_duration]), include_date: false %> (<%= formatted_time_zone_offset %>}
diff --git a/modules/meeting/app/views/meeting_series_mailer/template_completed.html.erb b/modules/meeting/app/views/meeting_series_mailer/template_completed.html.erb
index 7d0a08b97cd..2ed78d30a3a 100644
--- a/modules/meeting/app/views/meeting_series_mailer/template_completed.html.erb
+++ b/modules/meeting/app/views/meeting_series_mailer/template_completed.html.erb
@@ -54,7 +54,7 @@ See COPYRIGHT and LICENSE files for more details.
<%= I18n.t(:label_recurring_meeting_next_occurrence) %>
</td>
<td style="<%= placeholder_text_styles %>">
- <%= format_time_as_date @next_occurrence %> <%= format_time @next_occurrence, include_date: false %>
+ <%= format_date @next_occurrence %> <%= format_time @next_occurrence, include_date: false %>
(<%= formatted_time_zone_offset %>)
</td>
</tr>
diff --git a/modules/xls_export/app/models/xls_export/concerns/spreadsheet_builder.rb b/modules/xls_export/app/models/xls_export/concerns/spreadsheet_builder.rb
index 1c2cc22ccb0..e8cf3b55dbc 100644
--- a/modules/xls_export/app/models/xls_export/concerns/spreadsheet_builder.rb
+++ b/modules/xls_export/app/models/xls_export/concerns/spreadsheet_builder.rb
@@ -79,7 +79,7 @@ module XlsExport
def xls_export_filename
sane_filename(
"#{Setting.app_title} #{spreadsheet_title} \
- #{format_time_as_date(Time.zone.now, format: '%Y-%m-%d')}.xls"
+ #{format_date(Time.zone.now, format: '%Y-%m-%d')}.xls"
)
end
end
diff --git a/spec/lib/redmine/i18n_spec.rb b/spec/lib/redmine/i18n_spec.rb
index 90bfd8a1b16..7ac5b709218 100644
--- a/spec/lib/redmine/i18n_spec.rb
+++ b/spec/lib/redmine/i18n_spec.rb
@@ -37,7 +37,7 @@ module OpenProject
let(:format) { "%d/%m/%Y" }
let(:user) { build_stubbed(:user) }
- describe "#format_time_as_date" do
+ describe "#format_date with time" do
current_user { build_stubbed(:user, preferences: { time_zone: user_time_zone }) }
describe "with user time zone" do
@@ -45,12 +45,12 @@ module OpenProject
it "returns a date string in the user timezone for a utc timestamp" do
time = ActiveSupport::TimeZone["UTC"].local(2013, 6, 30, 23, 59)
- expect(format_time_as_date(time, format:)).to eq "01/07/2013"
+ expect(format_date(time, format:)).to eq "01/07/2013"
end
it "returns a date string in the user timezone for a non-utc timestamp" do
time = ActiveSupport::TimeZone["Berlin"].local(2013, 6, 30, 23, 59)
- expect(format_time_as_date(time, format:)).to eq "01/07/2013"
+ expect(format_date(time, format:)).to eq "01/07/2013"
end
end
@@ -59,12 +59,12 @@ module OpenProject
it "returns a date string in the utc timezone for a utc timestamp" do
time = ActiveSupport::TimeZone["UTC"].local(2013, 6, 30, 23, 59)
- expect(format_time_as_date(time, format:)).to eq "30/06/2013"
+ expect(format_date(time, format:)).to eq "30/06/2013"
end
it "returns a date string in the utc timezone for a non-utc timestamp" do
time = ActiveSupport::TimeZone["Berlin"].local(2013, 6, 30, 23, 59)
- expect(format_time_as_date(time, format:)).to eq "30/06/2013"
+ expect(format_date(time, format:)).to eq "30/06/2013"
end
end
end