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 for cross-platform work. If the scenario involves recurring schedules or strict DST rules, prefer .
TimeZoneConverterNodaTime首先确定请求类型:
- 地址或位置查询
- 时区ID查询
- UTC/本地时间转换
- 跨平台时区兼容性处理
- 调度或夏令时(DST)处理
- API或持久化设计
若不确定使用哪个库,跨平台场景默认选择。若涉及重复调度或严格的夏令时规则,优先使用。
TimeZoneConverterNodaTimeResolve Addresses And Locations
解析地址与位置
If the user provides an address, city, region, country, or document containing place names:
- Extract each location from the input.
- Read for common Windows and IANA mappings.
references/timezone-index.md - If the exact location is not listed, infer the correct IANA zone from geography, then map it to the Windows ID.
- 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.
如果用户提供地址、城市、地区、国家或包含地点名称的文档:
- 从输入中提取每个地点信息。
- 查阅获取常见Windows与IANA时区映射关系。
references/timezone-index.md - 若未列出精确地点,根据地理位置推断正确的IANA时区,再映射为Windows ID。
- 返回两种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 for Windows to IANA mappings.
references/timezone-index.mdAlways provide both formats:
- Windows ID for on Windows
TimeZoneInfo.FindSystemTimeZoneById() - IANA ID for Linux, containers, , and
NodaTimeTimeZoneConverter
使用获取Windows与IANA时区的映射关系。
references/timezone-index.md需始终提供两种格式:
- Windows ID:用于Windows平台的方法
TimeZoneInfo.FindSystemTimeZoneById() - IANA ID:用于Linux、容器、和
NodaTimeTimeZoneConverter
Generate Code
生成代码
Use and pick the smallest pattern that fits:
references/code-patterns.md- Pattern 1: for Windows-only code
TimeZoneInfo - Pattern 2: for cross-platform conversion
TimeZoneConverter - Pattern 3: for strict timezone arithmetic and DST-sensitive scheduling
NodaTime - Pattern 4: for APIs and data transfer
DateTimeOffset - 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:
- is platform-specific for timezone IDs.
TimeZoneInfo.FindSystemTimeZoneById() - Avoid storing in databases; store UTC instead.
DateTime.Now - Treat as a bug risk unless it is deliberate input.
DateTimeKind.Unspecified - DST transitions can skip or repeat local times.
- Azure Windows and Azure Linux environments may expect different timezone ID formats.
适用时提及相关警告:
- 的时区ID具有平台特异性。
TimeZoneInfo.FindSystemTimeZoneById() - 避免在数据库中存储;应存储UTC时间。
DateTime.Now - 除非是刻意输入,否则将视为潜在bug风险。
DateTimeKind.Unspecified - 夏令时转换可能会跳过或重复本地时间。
- Azure Windows和Azure Linux环境可能要求不同格式的时区ID。
Response Shape
响应格式
For address and location requests:
- Return the resolved timezone block for each location.
- State the recommended implementation in one sentence.
- Include a copy-paste-ready C# snippet.
For code and architecture requests:
- State the recommended approach in one sentence.
- Provide the timezone IDs if relevant.
- Include the minimal working code snippet.
- Mention the package requirement if needed.
- Add one pitfall warning if it matters.
Keep responses concise and code-first.
针对地址与位置请求:
- 返回每个地点的解析时区信息块。
- 用一句话说明推荐的实现方案。
- 包含可直接复制粘贴的C#代码片段。
针对代码与架构请求:
- 用一句话说明推荐的处理方式。
- 若相关则提供时区ID。
- 包含最小可运行代码片段。
- 若需要则说明包的依赖要求。
- 添加一个相关的陷阱警告(若适用)。
保持响应简洁,以代码为核心。
References
参考资料
- : common Windows and IANA timezone mappings
references/timezone-index.md - : ready-to-use .NET timezone patterns
references/code-patterns.md
- :常见Windows与IANA时区映射关系
references/timezone-index.md - :可直接使用的.NET时区代码模式
references/code-patterns.md