Skip to content

fix(datetime): strip timezone tokens from time format across locales#3360

Open
Ivy233 wants to merge 1 commit into
linuxdeepin:masterfrom
Ivy233:fix/datetime-timezone-token-strip
Open

fix(datetime): strip timezone tokens from time format across locales#3360
Ivy233 wants to merge 1 commit into
linuxdeepin:masterfrom
Ivy233:fix/datetime-timezone-token-strip

Conversation

@Ivy233

@Ivy233 Ivy233 commented Jul 20, 2026

Copy link
Copy Markdown
Contributor
  1. Replace simple regex with a comprehensive pattern that handles timezone tokens wrapped in half-width/full-width parentheses, square brackets, quoted literals with a trailing period, and bare tokens with separators
  2. Require an outer period before stripping the quoted-literal segment so that quoted literals modifying time fields (e.g. fr_CA "ss 's'") are not removed by mistake
  3. Add a trailing dangling-punctuation cleanup to drop leftover commas, semicolons or periods after token stripping

Log: Fix getCurrentTime to strip timezone tokens cleanly across locales

fix(datetime): 去除时间格式中各语言环境下的时区 token

  1. 用更完整的正则替换简单匹配,覆盖半角/全角括号、方括号包裹的时区, 引号字面量带句点的时区,以及带分隔符的裸时区
  2. 对引号字面量段要求外层有句点,避免误删修饰时间字段的引号字面量 (如 fr_CA 中的 "ss 's'")
  3. 增加末尾悬空标点清理,去除剥离后残留的逗号、分号、句点等

Log: 修复 getCurrentTime 在各语言环境下无法干净去除时区 token 的问题
PMS: BUG-370729

Summary by Sourcery

Bug Fixes:

  • Ensure getCurrentTime reliably removes timezone tokens and associated delimiters across locales without accidentally stripping non-timezone literals.

@deepin-ci-robot

Copy link
Copy Markdown

[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.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@Ivy233
Ivy233 force-pushed the fix/datetime-timezone-token-strip branch from 6f9b59b to 766d627 Compare July 20, 2026 10:11
@sourcery-ai

sourcery-ai Bot commented Jul 20, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refines 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

Change Details Files
Strengthened timezone-token stripping logic in getCurrentTime to handle multiple locale-specific patterns and added a final cleanup pass for dangling punctuation.
  • Replaced the previous simple QRegularExpression that matched bare t/tttt tokens and bracketed variants with a composite pattern that removes timezone tokens wrapped in half-width/full-width parentheses, square brackets, and prefixed separators.
  • Extended the regex to remove timezone tokens embedded in quoted literals that are followed by an external period, ensuring only timezone-related quoted segments are removed while preserving literals that modify time fields (e.g., "ss 's'").
  • Added a second regex pass that removes trailing dangling punctuation characters (commas, semicolons, periods and their localized variants) at the end of the time format after timezone token stripping.
  • Updated and expanded inline comments above the new regex logic to document the handled locale-specific patterns and rationale in both Chinese and English.
src/plugin-datetime/operation/datetimemodel.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/plugin-datetime/operation/datetimemodel.cpp Outdated
@Ivy233
Ivy233 force-pushed the fix/datetime-timezone-token-strip branch 3 times, most recently from e1ed286 to 20b26e8 Compare July 21, 2026 09:34
@BLumia BLumia closed this Jul 21, 2026
@BLumia BLumia reopened this Jul 21, 2026
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
@Ivy233
Ivy233 force-pushed the fix/datetime-timezone-token-strip branch from 20b26e8 to 6df4d0b Compare July 21, 2026 10:34
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:95分

■ 【总体评价】

代码实现了对时间格式中时区 token 的精准剥离,逻辑严谨且性能优化到位
逻辑正确,正则处理边界清晰,无安全漏洞,因头文件缺末尾换行符扣5分

■ 【详细分析】

  • 1.语法逻辑(基本正确)✓

代码在 DCCLocale::stripTimezoneFromTimeFormat 中使用两个正则表达式分别处理时区 token 和尾部分隔符,逻辑清晰。静态局部变量初始化在 C++11 及以上版本是线程安全的,符合规范。
潜在问题:正则表达式 [\s,,;;]*[tT]+ 在遇到连续多个 t 且夹杂其他字符时可能存在非预期匹配,但在标准 Qt 时间格式中风险极低。
建议:保持现状,可补充单元测试覆盖各种语言环境的时间格式。

  • 2.代码质量(良好)✓

代码注释详尽,解释了正则表达式的各个分支和排除特定字符的原因,头文件也补充了 Doxygen 风格的注释。提取为公共方法提高了代码复用性。
潜在问题:dcclocale.h 文件末尾缺少换行符,不符合部分编码规范。
建议:在 dcclocale.h 文件末尾添加一个空行。

  • 3.代码性能(高效)✓

通过 static const QRegularExpression 缓存正则表达式对象,避免了每次调用 stripTimezoneFromTimeFormat 时重新编译正则,性能表现优秀。
潜在问题:无
建议:保持现状。

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
总体风险描述:代码处理的是系统内部的时间格式字符串,不存在外部输入注入风险,正则使用安全,无内存越界等问题。

  • 建议:保持现状。

■ 【改进建议代码示例】

// 文件:src/shared-utils/dcclocale.h
// ... 前面代码不变 ...
    static QString stripTimezoneFromTimeFormat(const QString &timeFormat);
};

@BLumia BLumia left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议优先考虑试试 icu 的 DateFormat::createTimeInstance(DateFormat::kMedium, icuLocale); 能否满足需求,这个是只显示时分秒。

@deepin-bot

deepin-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown

TAG Bot

New tag: 6.1.101
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #3368

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants