1
---
2
name: dotnet-timezone
3
description: '.NET timezone handling guidance for C# applications. Use when working with TimeZoneInfo, DateTimeOffset, NodaTime, UTC conversion, daylight saving time, scheduling across timezones, cross-platform Windows/IANA timezone IDs, or when a .NET user needs the timezone for a city, address, region, or country and copy-paste-ready C# code.'
4
---
5
6
# .NET Timezone
7
8
Resolve timezone questions for .NET and C# code with production-safe guidance and copy-paste-ready snippets.
9
10
## Start With The Right Path
11
12
Identify the request type first:
13
14
- Address or location lookup
15
- Timezone ID lookup
16
- UTC/local conversion
17
- Cross-platform timezone compatibility
18
- Scheduling or DST handling
19
- API or persistence design
20
21
If the library is unclear, default to `TimeZoneConverter` for cross-platform work. If the scenario involves recurring schedules or strict DST rules, prefer `NodaTime`.
22
23
## Resolve Addresses And Locations
24
25
If the user provides an address, city, region, country, or document containing place names:
26
27
1. Extract each location from the input.
28
2. Read `references/timezone-index.md` for common Windows and IANA mappings.
29
3. If the exact location is not listed, infer the correct IANA zone from geography, then map it to the Windows ID.
30
4. Return both IDs and a ready-to-use C# example.
31
32
For each resolved location, provide:
33
34
```text
35
Location: <resolved place>
36
Windows ID: <windows id>
37
IANA ID: <iana id>
38
UTC offset: <standard offset and DST offset when relevant>
39
DST: <yes/no>
40
```
41
42
Then include a cross-platform snippet like:
43
44
```csharp
45
using TimeZoneConverter;
46
47
TimeZoneInfo tz = TZConvert.GetTimeZoneInfo("Asia/Colombo");
48
DateTime local = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tz);
49
```
50
51
If multiple locations are present, include one block per location and then a combined multi-timezone snippet.
52
53
If a location is ambiguous, list the possible timezone matches and ask the user to choose the correct one.
54
55
## Look Up Timezone IDs
56
57
Use `references/timezone-index.md` for Windows to IANA mappings.
58
59
Always provide both formats:
60
61
- Windows ID for `TimeZoneInfo.FindSystemTimeZoneById()` on Windows
62
- IANA ID for Linux, containers, `NodaTime`, and `TimeZoneConverter`
63
64
## Generate Code
65
66
Use `references/code-patterns.md` and pick the smallest pattern that fits:
67
68
- Pattern 1: `TimeZoneInfo` for Windows-only code
69
- Pattern 2: `TimeZoneConverter` for cross-platform conversion
70
- Pattern 3: `NodaTime` for strict timezone arithmetic and DST-sensitive scheduling
71
- Pattern 4: `DateTimeOffset` for APIs and data transfer
72
- Pattern 5: ASP.NET Core persistence and presentation
73
- Pattern 6: recurring jobs and schedulers
74
- Pattern 7: ambiguous and invalid DST timestamps
75
76
Always include package guidance when recommending third-party libraries.
77
78
## Warn About Common Pitfalls
79
80
Mention the relevant warning when applicable:
81
82
- `TimeZoneInfo.FindSystemTimeZoneById()` is platform-specific for timezone IDs.
83
- Avoid storing `DateTime.Now` in databases; store UTC instead.
84
- Treat `DateTimeKind.Unspecified` as a bug risk unless it is deliberate input.
85
- DST transitions can skip or repeat local times.
86
- Azure Windows and Azure Linux environments may expect different timezone ID formats.
87
88
## Response Shape
89
90
For address and location requests:
91
92
1. Return the resolved timezone block for each location.
93
2. State the recommended implementation in one sentence.
94
3. Include a copy-paste-ready C# snippet.
95
96
For code and architecture requests:
97
98
1. State the recommended approach in one sentence.
99
2. Provide the timezone IDs if relevant.
100
3. Include the minimal working code snippet.
101
4. Mention the package requirement if needed.
102
5. Add one pitfall warning if it matters.
103
104
Keep responses concise and code-first.
105
106
## References
107
108
- `references/timezone-index.md`: common Windows and IANA timezone mappings
109
- `references/code-patterns.md`: ready-to-use .NET timezone patterns
110