Skip to content

Commit 8def0ca

Browse files
authored
Merge pull request #4153 from gchq/gh-4152_date_time_formatter
#4152 Fix date time formatter always showing `Z` when it should show …
2 parents ffa38b1 + a99c8a0 commit 8def0ca

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

Diff for: stroom-core-client/src/main/java/stroom/preferences/client/DateTimeFormatter.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,14 @@ public String format(final Long ms) {
3636
}
3737

3838
Use use = Use.UTC;
39-
String pattern = "YYYY-MM-DDTHH:mm:ss.SSS[Z]";
39+
String pattern = "YYYY-MM-DD[T]HH:mm:ss.SSSZ";
4040
int offsetMinutes = 0;
4141
String zoneId = "UTC";
4242

4343
final UserPreferences userPreferences = userPreferencesManager.getCurrentUserPreferences();
4444
if (userPreferences != null) {
45-
if (userPreferences.getDateTimePattern() != null
46-
&& userPreferences.getDateTimePattern().trim().length() > 0) {
47-
pattern = userPreferences.getDateTimePattern();
48-
pattern = convertJavaDateTimePattern(pattern);
45+
if (userPreferences.getDateTimePattern() != null &&
46+
userPreferences.getDateTimePattern().trim().length() > 0) {
4947

5048
final TimeZone timeZone = userPreferences.getTimeZone();
5149
if (timeZone != null) {
@@ -62,9 +60,20 @@ public String format(final Long ms) {
6260

6361
zoneId = timeZone.getId();
6462
}
63+
64+
pattern = userPreferences.getDateTimePattern();
65+
pattern = convertJavaDateTimePattern(pattern);
6566
}
6667
}
6768

69+
// If UTC then just display the `Z` suffix.
70+
if (Use.UTC.equals(use)) {
71+
pattern = pattern.replaceAll("Z", "[Z]");
72+
}
73+
// Ensure we haven't doubled up square brackets.
74+
pattern = pattern.replaceAll("\\[+", "[");
75+
pattern = pattern.replaceAll("]+", "]");
76+
6877
return nativeToDateString(ms, use.getDisplayValue(), pattern, zoneId, offsetMinutes);
6978
}
7079

@@ -73,7 +82,8 @@ String convertJavaDateTimePattern(final String pattern) {
7382
converted = converted.replace('y', 'Y');
7483
converted = converted.replace('d', 'D');
7584
converted = converted.replaceAll("'", "");
76-
converted = converted.replaceAll("SSSXX", "SSS[Z]");
85+
converted = converted.replaceAll("SSSXX", "SSSZ");
86+
converted = converted.replaceAll("T", "[T]");
7787
converted = converted.replaceAll("xxx", "Z");
7888
converted = converted.replaceAll("xx", "z");
7989
converted = converted.replaceAll("VV", "ZZ");

Diff for: stroom-core-client/src/test/java/stroom/preferences/client/TestDateTimeFormatter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ void test() {
1010
final DateTimeFormatter dateTimeFormatter = new DateTimeFormatter(null);
1111
assertThat(dateTimeFormatter
1212
.convertJavaDateTimePattern("yyyy-MM-dd'T'HH:mm:ss.SSSXX"))
13-
.isEqualTo("YYYY-MM-DDTHH:mm:ss.SSS[Z]");
13+
.isEqualTo("YYYY-MM-DD[T]HH:mm:ss.SSSZ");
1414
assertThat(dateTimeFormatter
1515
.convertJavaDateTimePattern("yyyy-MM-dd'T'HH'#'mm'#'ss,SSSXX"))
16-
.isEqualTo("YYYY-MM-DDTHH#mm#ss,SSS[Z]");
16+
.isEqualTo("YYYY-MM-DD[T]HH#mm#ss,SSSZ");
1717
assertThat(dateTimeFormatter
1818
.convertJavaDateTimePattern("E, dd MMM yyyy HH:mm:ss Z"))
1919
.isEqualTo("ddd, DD MMM YYYY HH:mm:ss Z");

Diff for: unreleased_changes/20240308_095510_520__4152.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
* Issue **#4152** : Fix date time formatter always showing `Z` when it should show actual timezone.
2+
3+
4+
```sh
5+
# ********************************************************************************
6+
# Issue title: When displaying date/times as non UTC, Stroom incorrectly showing `Z` suffix as opposed to actual offset
7+
# Issue link: https://github.com/gchq/stroom/issues/4152
8+
# ********************************************************************************
9+
10+
# ONLY the top line will be included as a change entry in the CHANGELOG.
11+
# The entry should be in GitHub flavour markdown and should be written on a SINGLE
12+
# line with no hard breaks. You can have multiple change files for a single GitHub issue.
13+
# The entry should be written in the imperative mood, i.e. 'Fix nasty bug' rather than
14+
# 'Fixed nasty bug'.
15+
#
16+
# Examples of acceptable entries are:
17+
#
18+
#
19+
# * Issue **123** : Fix bug with an associated GitHub issue in this repository
20+
#
21+
# * Issue **namespace/other-repo#456** : Fix bug with an associated GitHub issue in another repository
22+
#
23+
# * Fix bug with no associated GitHub issue.
24+
```

0 commit comments

Comments
 (0)