diff --git a/app/assets/javascripts/initializers/initializeCommentsPage.js.erb b/app/assets/javascripts/initializers/initializeCommentsPage.js.erb index bfd985520..4210f3aa1 100644 --- a/app/assets/javascripts/initializers/initializeCommentsPage.js.erb +++ b/app/assets/javascripts/initializers/initializeCommentsPage.js.erb @@ -300,6 +300,7 @@ function handleCommentSubmit(event) { updateCommentsCount(); initializeCommentsPage(); initializeCommentDate(); + initializeDateHelpers(); activateRunkitTags(); }) } else { diff --git a/app/assets/javascripts/initializers/initializeDateTimeHelpers.js b/app/assets/javascripts/initializers/initializeDateTimeHelpers.js index b72e1a908..ebfd5edb3 100644 --- a/app/assets/javascripts/initializers/initializeDateTimeHelpers.js +++ b/app/assets/javascripts/initializers/initializeDateTimeHelpers.js @@ -15,4 +15,11 @@ function initializeDateHelpers() { month: 'short', day: 'numeric', }); + + // Date with short year: Jul 12 '20 + localizeTimeElements(document.querySelectorAll('time.date-short-year'), { + year: '2-digit', + month: 'short', + day: 'numeric', + }); } diff --git a/app/assets/javascripts/utilities/buildCommentHTML.js.erb b/app/assets/javascripts/utilities/buildCommentHTML.js.erb index 8aa2bdd3f..0b677caee 100644 --- a/app/assets/javascripts/utilities/buildCommentHTML.js.erb +++ b/app/assets/javascripts/utilities/buildCommentHTML.js.erb @@ -57,7 +57,7 @@ function buildCommentHTML(comment) { - diff --git a/app/assets/javascripts/utilities/localDateTime.js b/app/assets/javascripts/utilities/localDateTime.js index 6449d99f2..c571d21fb 100644 --- a/app/assets/javascripts/utilities/localDateTime.js +++ b/app/assets/javascripts/utilities/localDateTime.js @@ -19,7 +19,13 @@ function timestampToLocalDateTime(timestamp, locale, options) { try { var time = new Date(timestamp); - return new Intl.DateTimeFormat(locale || 'default', options).format(time); + let formattedTime = new Intl.DateTimeFormat( + locale || 'default', + options, + ).format(time); + return options.year === '2-digit' + ? formattedTime.replace(', ', " '") + : formattedTime; } catch (e) { return ''; } diff --git a/app/lib/middlewares/set_time_zone.rb b/app/lib/middlewares/set_time_zone.rb index fcdd51da0..b1ac65bed 100644 --- a/app/lib/middlewares/set_time_zone.rb +++ b/app/lib/middlewares/set_time_zone.rb @@ -5,7 +5,7 @@ module Middlewares end def call(env) - Time.zone = ActiveSupport::TimeZone.new(ENV["TZ"]) if ENV["TZ"] + Time.zone = ActiveSupport::TimeZone.new(ENV["TZ"]) if ENV["TZ"] && ENV["DESYNC_TIMEZONE"].nil? @app.call(env) end diff --git a/app/views/comments/_comment_date.erb b/app/views/comments/_comment_date.erb index a16ce71dc..cb7159af6 100644 --- a/app/views/comments/_comment_date.erb +++ b/app/views/comments/_comment_date.erb @@ -1,13 +1,13 @@ - diff --git a/spec/system/comments/user_views_a_comment_spec.rb b/spec/system/comments/user_views_a_comment_spec.rb index 45acc6b3c..e5840ba32 100644 --- a/spec/system/comments/user_views_a_comment_spec.rb +++ b/spec/system/comments/user_views_a_comment_spec.rb @@ -7,21 +7,63 @@ RSpec.describe "Viewing a comment", type: :system, js: true do before do Timecop.freeze - sign_in user - visit comment.path + ENV["DESYNC_TIMEZONE"] = "true" + mock_user_tz = ActiveSupport::TimeZone[Zonebie.random_timezone].tzinfo.name + ENV["TZ"] = mock_user_tz end after do + ENV["TZ"] = Time.zone.tzinfo.name + ENV["DESYNC_TIMEZONE"] = nil + Capybara.current_session.quit Timecop.return end - context "when showing the date" do - it "shows all published data" do - comment_date = comment.readable_publish_date.gsub(" ", " ") + context "when viewing the comment date" do + it "contains a time tag with the correct value for the datetime attribute" do + sign_in user + visit comment.path timestamp = comment.decorate.published_timestamp - expect(page).to have_selector(".comment-date time", text: comment_date) expect(page).to have_selector(".comment-date time[datetime='#{timestamp}']") end + + it "shows the published date in the user's local time zone" do + sign_in user + visit comment.path + date = comment.created_at.getlocal.strftime("%b %-d") + + expect(page).to have_selector(".comment-date time", text: date) + end + end + + context "when a year has passed" do + before do + comment + Timecop.freeze(1.year.from_now) + end + + it "shows the published date in the correct format" do + sign_in user + visit comment.path + date = comment.created_at.getlocal.strftime("%b %-d '%y") + + expect(page).to have_selector(".comment-date time", text: date) + end + end + + context "when the comment is edited and a year has passed" do + before do + comment.update(body_markdown: "This message is edited.", edited_at: 1.day.from_now) + Timecop.freeze(1.year.from_now) + end + + it "shows the edited date in the correct format" do + sign_in user + visit comment.path + date = comment.edited_at.getlocal.strftime("%b %-d") + + expect(page).to have_content("Edited on #{date}") + end end end