Skip to content

Commit 9be322b

Browse files
Copilotkendaleiv
andcommitted
Add browser IANA-to-Windows time zone conversion tests and docs
Co-authored-by: kendaleiv <1012917+kendaleiv@users.noreply.github.com>
1 parent f49c4ce commit 9be322b

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

AtTimeZone.Tests/AtTimeZoneTests.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,61 @@ public async Task ConvertUtcToIndiaStandardTime_IanaTimeZone()
228228
Assert.Equal(new DateTimeOffset(2024, 1, 15, 17, 30, 0, TimeSpan.FromHours(5.5)), result);
229229
}
230230

231+
[SkippableFact]
232+
public async Task BrowserIanaTimeZone_ConvertedToWindowsTimeZone_ProducesSameResult()
233+
{
234+
SkipIfIanaNotSupported();
235+
236+
// Browsers provide IANA time zones via the Intl API:
237+
// const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
238+
// For example, a user in Chicago would send "America/Chicago".
239+
var ianaTimeZone = "America/Chicago";
240+
241+
Assert.True(
242+
TimeZoneInfo.TryConvertIanaIdToWindowsId(ianaTimeZone, out var windowsTimeZone),
243+
$"Failed to convert IANA time zone '{ianaTimeZone}' to a Windows time zone.");
244+
245+
var sql = $"""
246+
SELECT
247+
CAST('2024-01-15 12:00:00 +00:00' AS datetimeoffset) AT TIME ZONE '{windowsTimeZone}' AS Windows,
248+
CAST('2024-01-15 12:00:00 +00:00' AS datetimeoffset) AT TIME ZONE '{ianaTimeZone}' AS Iana
249+
""";
250+
251+
await using var connection = new SqlConnection(db.ConnectionString);
252+
await connection.OpenAsync();
253+
await using var command = new SqlCommand(sql, connection);
254+
await using var reader = await command.ExecuteReaderAsync();
255+
await reader.ReadAsync();
256+
257+
var windowsResult = reader.GetDateTimeOffset(0);
258+
var ianaResult = reader.GetDateTimeOffset(1);
259+
260+
Assert.Equal(windowsResult, ianaResult);
261+
}
262+
263+
[Theory]
264+
[InlineData("America/New_York", "Eastern Standard Time")]
265+
[InlineData("America/Chicago", "Central Standard Time")]
266+
[InlineData("America/Denver", "Mountain Standard Time")]
267+
[InlineData("America/Los_Angeles", "Pacific Standard Time")]
268+
[InlineData("Europe/London", "GMT Standard Time")]
269+
[InlineData("Europe/Berlin", "W. Europe Standard Time")]
270+
[InlineData("Asia/Tokyo", "Tokyo Standard Time")]
271+
[InlineData("Australia/Sydney", "AUS Eastern Standard Time")]
272+
public void IanaToWindowsTimeZoneConversion_ReturnsExpectedWindowsId(
273+
string ianaId, string expectedWindowsId)
274+
{
275+
// Browsers send IANA time zones via:
276+
// const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
277+
//
278+
// .NET can convert these to Windows time zone IDs for SQL Server AT TIME ZONE:
279+
Assert.True(
280+
TimeZoneInfo.TryConvertIanaIdToWindowsId(ianaId, out var windowsId),
281+
$"Failed to convert IANA time zone '{ianaId}' to a Windows time zone.");
282+
283+
Assert.Equal(expectedWindowsId, windowsId);
284+
}
285+
231286
[Fact]
232287
public async Task InvalidTimeZoneName_ThrowsSqlException()
233288
{

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,31 @@ A SQL Server 2022 container is started automatically via Testcontainers.
3030
| Windows and IANA produce same result | `Eastern Standard Time` | `America/New_York` |
3131
| Invalid time zone name — throws error | _(invalid name)_ | |
3232
| IANA time zone when unsupported — throws error | | `America/New_York` |
33+
| Browser IANA to Windows conversion — same result | _(converted from IANA)_ | `America/Chicago` |
34+
| IANA to Windows ID mapping (C# only, no SQL) | Multiple | Multiple |
3335

3436
IANA time zone tests are skipped automatically when the SQL Server instance does not support IANA time zone names.
3537

3638
## PostgreSQL Comparison
3739

3840
PostgreSQL supports **only IANA time zone names** (e.g. `America/New_York`, `Europe/Berlin`) in its `AT TIME ZONE` clause — Windows time zone names like `Eastern Standard Time` are **not recognized** and will produce an error. You can query `pg_timezone_names` to see supported names.
41+
42+
## Browser Time Zone to SQL Server
43+
44+
Browsers expose the user's IANA time zone via the [Intl API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/resolvedOptions):
45+
46+
```js
47+
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
48+
// e.g. "America/Chicago"
49+
```
50+
51+
In C#, convert the IANA time zone to a Windows time zone ID for use with SQL Server `AT TIME ZONE`:
52+
53+
```csharp
54+
if (TimeZoneInfo.TryConvertIanaIdToWindowsId(ianaTimeZone, out var windowsTimeZone))
55+
{
56+
// Use windowsTimeZone with AT TIME ZONE in SQL Server
57+
}
58+
```
59+
60+
This allows applications to accept IANA time zones from the browser and convert them for SQL Server instances that only support Windows time zone names. SQL Server 2022+ may also accept IANA names directly.

0 commit comments

Comments
 (0)