fix(datetime): strip timezone tokens from time format across locales#3360
fix(datetime): strip timezone tokens from time format across locales#3360Ivy233 wants to merge 1 commit into
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: Ivy233 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
6f9b59b to
766d627
Compare
Reviewer's guide (collapsed on small PRs)Reviewer's GuideRefines getCurrentTime’s locale-specific time format sanitization to robustly remove timezone tokens (t/tttt variants) along with surrounding punctuation and wrappers, while preserving non-timezone quoted literals and cleaning trailing punctuation artifacts. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- Consider extracting the timezone-stripping QRegularExpression into a named
static const(or helper function) so the complex pattern is compiled once and its intent can be referenced and maintained more easily. - The final cleanup regex
[,,;;.]\s*$will drop any trailing period/comma even if no timezone token was present; if that's not desired for all locales, you might want to guard this cleanup so it only runs when a timezone token was actually removed.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider extracting the timezone-stripping QRegularExpression into a named `static const` (or helper function) so the complex pattern is compiled once and its intent can be referenced and maintained more easily.
- The final cleanup regex `[,,;;.]\s*$` will drop any trailing period/comma even if no timezone token was present; if that's not desired for all locales, you might want to guard this cleanup so it only runs when a timezone token was actually removed.
## Individual Comments
### Comment 1
<location path="src/plugin-datetime/operation/datetimemodel.cpp" line_range="1001" />
<code_context>
+ // 最后再扫一遍末尾悬空的标点(逗号/分号/句点等)收尾。
+ timeFormat.remove(QRegularExpression(
+ "(\\([tT]+\\)|([tT]+)|\\[[tT]+\\])"
+ "|[\\s,,;;]*'[^']*'\\.+\\s*[tT]+"
+ "|[\\s,,;;]*[tT]+"
+ ));
</code_context>
<issue_to_address>
**suggestion:** The quoted-literal pattern doesn’t handle escaped single quotes inside Qt format literals.
In Qt format strings, a literal single quote is written as `''` inside a quoted section. With the current pattern `'[^']*'`, the match stops at the first quote, so inputs like `''O''Clock'' tttt` can break the match and mis-detect the timezone segment. Consider updating the pattern to handle doubled quotes, e.g. `'(?:[^']|'')*'`, so escaped quotes inside literals don’t affect detection/removal.
```suggestion
"|[\\s,,;;]*'(?:[^']|'')*'\\.+\\s*[tT]+"
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
e1ed286 to
20b26e8
Compare
1. Add DCCLocale::stripTimezoneFromTimeFormat in src/shared-utils/dcclocale and call it from DatetimeModel::getCurrentTime, so the tz-strip logic is reusable and unit-testable by other plugins 2. Strip wrapped timezone tokens (half-width/full-width parentheses, square brackets) and bare tokens with their preceding separators, covering formats like es_ES "H:mm:ss (tttt)", zh_HK "Aph:mm:ss [tttt]", zh_CN "tttt HH:mm:ss" and cs_CZ "H:mm:ss, tttt" 3. Leave quoted literals untouched; bg_BG "H:mm:ss 'ch'. tttt" keeps the 'ch'. (o'clock abbreviation) which Qt renders itself, and fr_CA "HH 'h' mm 'min' ss 's' tttt" keeps the literals modifying time fields 4. Clean up trailing dangling separators (spaces/commas/semicolons) but not periods, so abbreviation-ending formats like bg_BG 'ch'. are preserved 5. Cache the two compiled QRegularExpression objects in function-local statics to avoid recompiling on every call Log: Add DCCLocale::stripTimezoneFromTimeFormat and use it in getCurrentTime fix(datetime): 通过 DCCLocale 辅助函数统一剥离各语言环境时区 token 1. 在 src/shared-utils/dcclocale 中新增 DCCLocale::stripTimezoneFromTimeFormat 并由 DatetimeModel::getCurrentTime 调用,便于其他插件复用和单元测试 2. 剥离半角/全角括号、方括号包裹的时区 token,以及带前导分隔符的裸时区 token,覆盖 es_ES "H:mm:ss (tttt)"、zh_HK "Aph:mm:ss [tttt]"、 zh_CN "tttt HH:mm:ss"、cs_CZ "H:mm:ss, tttt" 等格式 3. 不触碰引号字面量;bg_BG "H:mm:ss 'ч'. tttt" 保留 'ч'. (o'clock 缩写) 由 Qt 自行渲染,fr_CA "HH 'h' mm 'min' ss 's' tttt" 保留修饰时间字段 的引号字面量 4. 末尾清理只去除悬空的空格/逗号/分号,不吞句点,避免误伤 bg_BG 'ч'. 这类以缩写句点结尾的格式 5. 两条 QRegularExpression 缓存到函数局部 static 对象,避免每次调用 重新编译 Log: 新增 DCCLocale::stripTimezoneFromTimeFormat 并在 getCurrentTime 中使用 PMS: BUG-370729
20b26e8 to
6df4d0b
Compare
deepin pr auto review★ 总体评分:95分■ 【总体评价】
■ 【详细分析】
■ 【改进建议代码示例】 // 文件:src/shared-utils/dcclocale.h
// ... 前面代码不变 ...
static QString stripTimezoneFromTimeFormat(const QString &timeFormat);
};
|
BLumia
left a comment
There was a problem hiding this comment.
建议优先考虑试试 icu 的 DateFormat::createTimeInstance(DateFormat::kMedium, icuLocale); 能否满足需求,这个是只显示时分秒。
|
TAG Bot New tag: 6.1.101 |
Log: Fix getCurrentTime to strip timezone tokens cleanly across locales
fix(datetime): 去除时间格式中各语言环境下的时区 token
Log: 修复 getCurrentTime 在各语言环境下无法干净去除时区 token 的问题
PMS: BUG-370729
Summary by Sourcery
Bug Fixes: