Skip to content

Commit 2261d37

Browse files
SamSaffronpento
andauthored
FEATURE: allow customizing default timezone for email. (#32964)
1. Changes default email format to llll (eg: Tue, May 8, 2018 2:00 AM) from 2018-05-08T00:00:00Z UTC which is not nice on the eyes 2. Adds `discourse_local_dates_email_timezone` which allows configuring default timezone in emails 3. Improved help text on site settings (format / timezone) --------- Co-authored-by: Gary Pendergast <gary@pento.net>
1 parent 0142d35 commit 2261d37

File tree

6 files changed

+58
-5
lines changed

6 files changed

+58
-5
lines changed

plugins/discourse-local-dates/assets/javascripts/lib/discourse-markdown/discourse-local-dates.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ function addLocalDate(attributes, state, buffer, applyDataAttributes) {
3030

3131
const emailFormat =
3232
state.md.options.discourse.datesEmailFormat || moment.defaultFormat;
33+
const emailTimezone =
34+
state.md.options.discourse.datesEmailTimezone || "Etc/UTC";
3335

34-
attributes.emailPreview = `${dateTime.utc().format(emailFormat)} UTC`;
36+
attributes.emailPreview = `${dateTime.utc().tz(emailTimezone).format(emailFormat)}`;
3537

3638
let token = new state.Token("span_open", "span", 1);
3739
token.attrs = [["class", "discourse-local-date"]];
@@ -112,6 +114,7 @@ export function setup(helper) {
112114

113115
helper.registerOptions((opts, siteSettings) => {
114116
opts.datesEmailFormat = siteSettings.discourse_local_dates_email_format;
117+
opts.datesEmailTimezone = siteSettings.discourse_local_dates_email_timezone;
115118

116119
opts.features["discourse-local-dates"] =
117120
!!siteSettings.discourse_local_dates_enabled;

plugins/discourse-local-dates/config/locales/server.en.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ en:
33
discourse_local_dates_enabled: "Enable the discourse-local-dates feature. This will add support to local timezone aware dates in posts using the [date] element"
44
discourse_local_dates_default_formats: "Frequently used date time formats, see: <a target='_blank' rel='noopener' href='https://momentjs.com/docs/#/parsing/string-format/'>momentjs string format</a>"
55
discourse_local_dates_default_timezones: "Default list of timezones, must be a valid <a target='_blank' rel='noopener' href='https://en.wikipedia.org/wiki/List_of_tz_database_time_zones'>TZ</a>"
6-
discourse_local_dates_email_format: "Format used to display a date in emails"
6+
discourse_local_dates_email_format: "Format used to display a date in emails, examples: 'LLLL', 'llll' or 'YYYY-MM-DDTHH:mm:ss[Z]'. see: <a target='_blank' rel='noopener' href='https://momentjs.com/docs/#/parsing/string-format/'>momentjs string format</a>"
7+
discourse_local_dates_email_timezone: "Timezone used to display dates in emails, must be a valid <a target='_blank' rel='noopener' href='https://en.wikipedia.org/wiki/List_of_tz_database_time_zones'>TZ</a>"

plugins/discourse-local-dates/config/settings.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
plugins:
2+
discourse_local_dates_email_timezone:
3+
default: "Etc/UTC"
4+
client: true
5+
area: "localization"
26
discourse_local_dates_email_format:
3-
default: "YYYY-MM-DDTHH:mm:ss[Z]"
7+
default: "llll z"
48
client: true
59
discourse_local_dates_enabled:
610
default: true
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
class AddUtcToSetting < ActiveRecord::Migration[7.2]
3+
def up
4+
# we changed the setting so UTC is no longer appended, we append it now in the format
5+
execute <<~SQL
6+
UPDATE site_settings
7+
SET value = value || ' z'
8+
WHERE name = 'discourse_local_dates_email_format'
9+
SQL
10+
end
11+
12+
def down
13+
execute <<~SQL
14+
UPDATE site_settings
15+
SET value = REPLACE(value, ' z', '')
16+
WHERE name = 'discourse_local_dates_email_format'
17+
SQL
18+
end
19+
end

plugins/discourse-local-dates/spec/integration/local_dates_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# frozen_string_literal: true
22

33
RSpec.describe "Local Dates" do
4-
before { freeze_time DateTime.parse("2018-11-10 12:00") }
4+
before do
5+
SiteSetting.discourse_local_dates_email_format = "YYYY-MM-DDTHH:mm:ss[Z] z"
6+
freeze_time DateTime.parse("2018-11-10 12:00")
7+
end
58

69
it "should work without timezone" do
710
post = Fabricate(:post, raw: <<~MD)

plugins/discourse-local-dates/spec/lib/pretty_text_spec.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def generate_html(text, opts = {})
1919

2020
describe "emails simplified rendering" do
2121
it "works with default markup" do
22+
SiteSetting.discourse_local_dates_email_format = "YYYY-MM-DDTHH:mm:ss[Z] z"
2223
cooked = PrettyText.cook("[date=2018-05-08]")
2324
cooked_mail =
2425
generate_html(
@@ -31,6 +32,7 @@ def generate_html(text, opts = {})
3132
end
3233

3334
it "works with time" do
35+
SiteSetting.discourse_local_dates_email_format = "YYYY-MM-DDTHH:mm:ss[Z] UTC"
3436
cooked = PrettyText.cook("[date=2018-05-08 time=20:00:00]")
3537
cooked_mail =
3638
generate_html(
@@ -44,6 +46,7 @@ def generate_html(text, opts = {})
4446
end
4547

4648
it "works with multiple timezones" do
49+
SiteSetting.discourse_local_dates_email_format = "YYYY-MM-DDTHH:mm:ss[Z] UTC"
4750
cooked =
4851
PrettyText.cook(
4952
'[date=2023-05-08 timezone="Europe/Paris" timezones="America/Los_Angeles|Pacific/Auckland"]',
@@ -60,8 +63,28 @@ def generate_html(text, opts = {})
6063
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
6164
end
6265

66+
describe "discourse_local_dates_email_timezone" do
67+
before do
68+
SiteSetting.discourse_local_dates_email_timezone = "Europe/Paris"
69+
SiteSetting.discourse_local_dates_email_format = "llll"
70+
end
71+
72+
it "uses the site setting" do
73+
cooked = PrettyText.cook("[date=2018-05-08]")
74+
75+
cooked_mail =
76+
generate_html(
77+
"Tue, May 8, 2018 2:00 AM",
78+
date: "2018-05-08",
79+
email_preview: "Tue, May 8, 2018 2:00 AM",
80+
)
81+
82+
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
83+
end
84+
end
85+
6386
describe "discourse_local_dates_email_format" do
64-
before { SiteSetting.discourse_local_dates_email_format = "DD/MM" }
87+
before { SiteSetting.discourse_local_dates_email_format = "DD/MM UTC" }
6588

6689
it "uses the site setting" do
6790
cooked = PrettyText.cook("[date=2018-05-08]")

0 commit comments

Comments
 (0)