dotnet-timezone

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

.NET Timezone

.NET 时区处理

Resolve timezone questions for .NET and C# code with production-safe guidance and copy-paste-ready snippets.
借助生产环境可用的指导方案和可直接复制粘贴的代码片段,解决.NET和C#代码中的时区问题。

Start With The Right Path

选择正确的处理路径

Identify the request type first:
  • Address or location lookup
  • Timezone ID lookup
  • UTC/local conversion
  • Cross-platform timezone compatibility
  • Scheduling or DST handling
  • API or persistence design
If the library is unclear, default to
TimeZoneConverter
for cross-platform work. If the scenario involves recurring schedules or strict DST rules, prefer
NodaTime
.
首先确定请求类型:
  • 地址或位置查询
  • 时区ID查询
  • UTC/本地时间转换
  • 跨平台时区兼容性处理
  • 调度或夏令时(DST)处理
  • API或持久化设计
若不确定使用哪个库,跨平台场景默认选择
TimeZoneConverter
。若涉及重复调度或严格的夏令时规则,优先使用
NodaTime

Resolve Addresses And Locations

解析地址与位置

If the user provides an address, city, region, country, or document containing place names:
  1. Extract each location from the input.
  2. Read
    references/timezone-index.md
    for common Windows and IANA mappings.
  3. If the exact location is not listed, infer the correct IANA zone from geography, then map it to the Windows ID.
  4. Return both IDs and a ready-to-use C# example.
For each resolved location, provide:
text
Location: <resolved place>
Windows ID: <windows id>
IANA ID: <iana id>
UTC offset: <standard offset and DST offset when relevant>
DST: <yes/no>
Then include a cross-platform snippet like:
csharp
using TimeZoneConverter;

TimeZoneInfo tz = TZConvert.GetTimeZoneInfo("Asia/Colombo");
DateTime local = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tz);
If multiple locations are present, include one block per location and then a combined multi-timezone snippet.
If a location is ambiguous, list the possible timezone matches and ask the user to choose the correct one.
如果用户提供地址、城市、地区、国家或包含地点名称的文档:
  1. 从输入中提取每个地点信息。
  2. 查阅
    references/timezone-index.md
    获取常见Windows与IANA时区映射关系。
  3. 若未列出精确地点,根据地理位置推断正确的IANA时区,再映射为Windows ID。
  4. 返回两种ID及可直接使用的C#示例。
每个解析后的地点需提供:
text
地点:<解析后的地点>
Windows ID:<windows id>
IANA ID:<iana id>
UTC偏移量:<标准偏移量及相关夏令时偏移量>
夏令时:<是/否>
随后提供跨平台代码片段,例如:
csharp
using TimeZoneConverter;

TimeZoneInfo tz = TZConvert.GetTimeZoneInfo("Asia/Colombo");
DateTime local = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tz);
若存在多个地点,每个地点对应一个信息块,再提供合并的多时区代码片段。
若地点存在歧义,列出可能的时区匹配项并请用户选择正确的一项。

Look Up Timezone IDs

查询时区ID

Use
references/timezone-index.md
for Windows to IANA mappings.
Always provide both formats:
  • Windows ID for
    TimeZoneInfo.FindSystemTimeZoneById()
    on Windows
  • IANA ID for Linux, containers,
    NodaTime
    , and
    TimeZoneConverter
使用
references/timezone-index.md
获取Windows与IANA时区的映射关系。
需始终提供两种格式:
  • Windows ID:用于Windows平台的
    TimeZoneInfo.FindSystemTimeZoneById()
    方法
  • IANA ID:用于Linux、容器、
    NodaTime
    TimeZoneConverter

Generate Code

生成代码

Use
references/code-patterns.md
and pick the smallest pattern that fits:
  • Pattern 1:
    TimeZoneInfo
    for Windows-only code
  • Pattern 2:
    TimeZoneConverter
    for cross-platform conversion
  • Pattern 3:
    NodaTime
    for strict timezone arithmetic and DST-sensitive scheduling
  • Pattern 4:
    DateTimeOffset
    for APIs and data transfer
  • Pattern 5: ASP.NET Core persistence and presentation
  • Pattern 6: recurring jobs and schedulers
  • Pattern 7: ambiguous and invalid DST timestamps
Always include package guidance when recommending third-party libraries.
参考
references/code-patterns.md
,选择最贴合需求的最小代码模式:
  • 模式1:仅Windows环境下使用
    TimeZoneInfo
  • 模式2:跨平台转换使用
    TimeZoneConverter
  • 模式3:严格时区运算和夏令时敏感调度使用
    NodaTime
  • 模式4:API和数据传输使用
    DateTimeOffset
  • 模式5:ASP.NET Core持久化与展示
  • 模式6:定时任务与调度器
  • 模式7:模糊或无效的夏令时时间戳
推荐第三方库时,需始终说明包的安装要求。

Warn About Common Pitfalls

常见陷阱警告

Mention the relevant warning when applicable:
  • TimeZoneInfo.FindSystemTimeZoneById()
    is platform-specific for timezone IDs.
  • Avoid storing
    DateTime.Now
    in databases; store UTC instead.
  • Treat
    DateTimeKind.Unspecified
    as a bug risk unless it is deliberate input.
  • DST transitions can skip or repeat local times.
  • Azure Windows and Azure Linux environments may expect different timezone ID formats.
适用时提及相关警告:
  • TimeZoneInfo.FindSystemTimeZoneById()
    的时区ID具有平台特异性。
  • 避免在数据库中存储
    DateTime.Now
    ;应存储UTC时间。
  • 除非是刻意输入,否则将
    DateTimeKind.Unspecified
    视为潜在bug风险。
  • 夏令时转换可能会跳过或重复本地时间。
  • Azure Windows和Azure Linux环境可能要求不同格式的时区ID。

Response Shape

响应格式

For address and location requests:
  1. Return the resolved timezone block for each location.
  2. State the recommended implementation in one sentence.
  3. Include a copy-paste-ready C# snippet.
For code and architecture requests:
  1. State the recommended approach in one sentence.
  2. Provide the timezone IDs if relevant.
  3. Include the minimal working code snippet.
  4. Mention the package requirement if needed.
  5. Add one pitfall warning if it matters.
Keep responses concise and code-first.
针对地址与位置请求:
  1. 返回每个地点的解析时区信息块。
  2. 用一句话说明推荐的实现方案。
  3. 包含可直接复制粘贴的C#代码片段。
针对代码与架构请求:
  1. 用一句话说明推荐的处理方式。
  2. 若相关则提供时区ID。
  3. 包含最小可运行代码片段。
  4. 若需要则说明包的依赖要求。
  5. 添加一个相关的陷阱警告(若适用)。
保持响应简洁,以代码为核心。

References

参考资料

  • references/timezone-index.md
    : common Windows and IANA timezone mappings
  • references/code-patterns.md
    : ready-to-use .NET timezone patterns
  • references/timezone-index.md
    :常见Windows与IANA时区映射关系
  • references/code-patterns.md
    :可直接使用的.NET时区代码模式