-
-
Notifications
You must be signed in to change notification settings - Fork 828
Force chat export to use 24-hour timestamp format #9724
Force chat export to use 24-hour timestamp format #9724
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this! Please do have a read through https://github.com/vector-im/element-web/blob/develop/CONTRIBUTING.md and make sure that you've done everything it asks.
Also note that the CI is currently failing for a number of different reasons. Please make sure you address the issues it has flagged up.
let date_time_string = new Date(event.getTs()).toLocaleString(); | ||
|
||
// regex for dissecting the 12-hour timestamp | ||
const TIMESTAMP_REGEX = /(\d*\/\d*\/[0-9]*), ([0-9]*:[0-9]*:[0-9]*) (AM|PM|am|pm|a\.m\.|p\.m\.)/; | ||
const timestampRegexResult = TIMESTAMP_REGEX.match(date_time_string); | ||
|
||
// ignore if the format happens to be in the 24-hour format | ||
if (timestampRegexResult) { | ||
const current_date = timestampRegexResult[1]; | ||
const timestamp = timestampRegexResult[2]; | ||
const am_or_pm = timestampRegexResult[3]; | ||
|
||
if (am_or_pm === "am" || am_or_pm === "AM" || am_or_pm === "a.m.") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are easier ways of splitting up a Date than using a regexp; see for example Date.getHours
.
// regex for dissecting the 12-hour timestamp | ||
const TIMESTAMP_REGEX = /(\d*\/\d*\/[0-9]*), ([0-9]*:[0-9]*:[0-9]*) (AM|PM|am|pm|a\.m\.|p\.m\.)/; | ||
const timestampRegexResult = TIMESTAMP_REGEX.match(date_time_string); | ||
|
||
// ignore if the format happens to be in the 24-hour format | ||
if (timestampRegexResult) { | ||
const current_date = timestampRegexResult[1]; | ||
const timestamp = timestampRegexResult[2]; | ||
const am_or_pm = timestampRegexResult[3]; | ||
|
||
if (am_or_pm === "am" || am_or_pm === "AM" || am_or_pm === "a.m.") { | ||
if (timestamp.substring(0,2) === "12") { | ||
date_time_string = `${current_date}, 00${timestamp.substring(2)}`; | ||
} else if (timestamp.length == 7) { | ||
date_time_string = `${current_date}, 0${timestamp}`; | ||
} else { | ||
date_time_string = `${current_date}, ${timestamp}`; | ||
} | ||
} else { | ||
let hour = timestamp.substring(0,2); | ||
|
||
if (hour === "12") { | ||
date_time_string = `${current_date}, ${timestamp}`; | ||
} else { | ||
|
||
let substring_start_index = 2; | ||
if (hour[1] === ":") { | ||
hour = timestamp[0]; | ||
substring_start_index = 1; | ||
} | ||
|
||
let new_hour = (+hour + 12).toString(); | ||
|
||
date_time_string = `${current_date}, ${new_hour}${timestamp.substring(substring_start_index)}`; | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please could you factor the Date
processing out to a separate function, and add some tests for it.
Why wouldn't we want the chat export to match your own preference of 12/24hr instead of ignoring it? |
Thanks for your contribution but this is not desirable, the export should match your user preference for time format as per element-hq/element-web#23838 |
Checklist
Notes: First open-source contribution and initial PR, addressing issue 23838 in the Element repo. Working on checklist.
Here's what your changelog entry will look like:
✨ Features