apache / apache/lucenenet

DateTools.DateToString TimeZoneInfo Parameter Doesn't Have An Effect

Open
#1,218 5 comments 1 reaction 1 assignee Claimed by @NightOwl888 View on GitHub
is:bug pri:high
Dominant language
C#
Stars
2.4k
Forks
658
Avg merge
3d 5h
Merged PRs (30d)
9

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

Lucene.Net `DateTools.DateToString` has a parameter for TimeZoneInfo. But this parameter has ne effect.
```csharp
public static string DateToString(DateTime date, TimeZoneInfo timeZone, DateResolution resolution)
```

### Expected Behavior

This parameter should change the returned string according to the TimeZoneInfo

### Steps To Reproduce

- Create a C# project
- Add reference to Lucene.Net nuget package
- Add this code to the `Main` method
```csharp
using System;
using Lucene.Net.Documents;
internal class Program
{
public static void Main()
{
var hawaiianTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Hawaiian Standard Time");

Test("Local DateTime", new DateTime(2025, 11, 13, 0, 0, 0, DateTimeKind.Local), hawaiianTimeZone);
Test("Unspecified DateTime", new DateTime(2025, 11, 13, 0, 0, 0, DateTimeKind.Unspecified), hawaiianTimeZone);
Test("UTC DateTime", new DateTime(2025, 11, 13, 0, 0, 0, DateTimeKind.Utc), hawaiianTimeZone);
}

private static void Test(string label, DateTime dt, TimeZoneInfo customTimeZoneInfo)
{
Console.WriteLine($"\n=== {label} ===");
var local = DateTools.DateToString(dt, TimeZoneInfo.Local, DateResolution.SECOND);
var utc = DateTools.DateToString(dt, TimeZoneInfo.Utc, DateResolution.SECOND);
var custom = DateTools.DateToString(dt, customTimeZoneInfo, DateResolution.SECOND);
Console.WriteLine($"Local: {local}");
Console.WriteLine($"UTC: {utc}");
Console.WriteLine($"Hawaiian: {custom}");
if (local == utc && utc == custom)
{
Console.WriteLine("!!! BUG: All time zones produce identical output!");
}
else
{
Console.WriteLine("✓✓✓ Time zones produce different results");
}
}
}
```
- The output will be:
```
=== Local DateTime ===
Local: 20251112230000
UTC: 20251112230000
Hawaiian: 20251112230000
!!! BUG: All time zones produce identical output!

=== Unspecified DateTime ===
Local: 20251112230000
UTC: 20251112230000
Hawaiian: 20251112230000
!!! BUG: All time zones produce identical output!

=== UTC DateTime ===
Local: 20251113000000
UTC: 20251113000000
Hawaiian: 20251113000000
!!! BUG: All time zones produce identical output!
```

### Exceptions (if any)

_No response_

### Lucene.NET Version

4.8.0-beta00017

### .NET Version

9.0.300

### Operating System

Windows

### Anything else?

When we look at Lucene.Net => DateTools.cs file in master branch, the DateToString method implementation shows the problem

```csharp
public static string DateToString(DateTime date, TimeZoneInfo timeZone, DateResolution resolution)
{
if (timeZone is null)
throw new ArgumentNullException(nameof(timeZone));

return DateToStringInternal(date, timeZone, resolution);
}
private static string DateToStringInternal(DateTime date, TimeZoneInfo? timeZone, DateResolution resolution)
{
string? format = ToDateFormat(resolution);
if (format is null)
throw new ArgumentException($"Unknown resolution {resolution}.");

DateTimeOffset timeZoneAdjusted = new DateTimeOffset(date.ToUniversalTime(), TimeSpan.Zero);
if (timeZone is not null && !TimeZoneInfo.Utc.Equals(timeZone))
{
timeZoneAdjusted = TimeZoneInfo.ConvertTime(timeZoneAdjusted, timeZone);
}

DateTime d = Round(timeZoneAdjusted.UtcDateTime, resolution); // time zone conversion is undone
return d.ToString(format, CultureInfo.InvariantCulture);
}
```
`timeZoneAdjusted.UtcDateTime` returns the UTC time, the timezone conversion is effectively undone.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.