Fix incorrect time parsing

For some reason, string-interpolating the result of chrono.parseDate
can set the time of day-only dates to noon, instead of midnight, which
is much more useful as a baseline time.

To get midnight, prepend "00:00" to all day-only date strings before
parsing them with chrono.parseDate.
This commit is contained in:
Andrew Ferrazzutti 2021-06-05 23:56:54 -04:00
parent 6583815301
commit 712a256dee
1 changed files with 7 additions and 6 deletions

View File

@ -136,17 +136,18 @@ class MautrixController {
} }
/** /**
* Parse a date separator (mws-relative-timestamp) * Parse a date separator.
* *
* @param {string} text - The text in the mws-relative-timestamp element. * @param {string} text - The text in the date saparator.
* @return {?Date} - The value in the date separator. * @return {Promise<?Date>} - The value of the date separator.
* @private * @private
*/ */
async _tryParseDayDate(text) { async _tryParseDateSeparator(text) {
if (!text) { if (!text) {
return null return null
} }
text = text.replace(/\. /, "/") // Must prefix with midnight to prevent getting noon
text = "00:00 " + text.replace(/\. /, "/")
const now = new Date() const now = new Date()
let newDate = await this._tryParseDate(text) let newDate = await this._tryParseDate(text)
if (!newDate || newDate > now) { if (!newDate || newDate > now) {
@ -345,7 +346,7 @@ class MautrixController {
let refDate = null let refDate = null
for (const child of msgList) { for (const child of msgList) {
if (child.classList.contains("mdRGT10Date")) { if (child.classList.contains("mdRGT10Date")) {
refDate = await this._tryParseDayDate(child.firstElementChild.innerText) refDate = await this._tryParseDateSeparator(child.firstElementChild.innerText)
} else if (child.classList.contains("MdRGT07Cont")) { } else if (child.classList.contains("MdRGT07Cont")) {
// TODO :not(.MdNonDisp) to exclude not-yet-posted messages, // TODO :not(.MdNonDisp) to exclude not-yet-posted messages,
// but that is unlikely to be a problem here. // but that is unlikely to be a problem here.