You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucenenet.apache.org by "sahin52 (via GitHub)" <gi...@apache.org> on 2025/11/12 11:24:44 UTC
[I] DateTools.DateToString TimeZoneInfo Parameter Doesn't Have An Effect [lucenenet]
sahin52 opened a new issue, #1218:
URL: https://github.com/apache/lucenenet/issues/1218
### 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");
}
}
}
```
### 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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] DateTools.DateToString TimeZoneInfo Parameter Doesn't Have An Effect [lucenenet]
Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin commented on issue #1218:
URL: https://github.com/apache/lucenenet/issues/1218#issuecomment-6256104978
@NightOwl888 This issue is aging quite a bit; can you provide an update here? Thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] DateTools.DateToString TimeZoneInfo Parameter Doesn't Have An Effect [lucenenet]
Posted by "NightOwl888 (via GitHub)" <gi...@apache.org>.
NightOwl888 commented on issue #1218:
URL: https://github.com/apache/lucenenet/issues/1218#issuecomment-3741158083
@paulirwin @sauravk58
I am assigning this to myself, since I have already fixed it on a local branch. However, I didn't move forward with the fix because it is still up in the air whether to use the default behavior of Java (which is to use UTC for unknown dates) or the default behavior of .NET (which is to assume local time zone for unknown dates).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] DateTools.DateToString TimeZoneInfo Parameter Doesn't Have An Effect [lucenenet]
Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin commented on issue #1218:
URL: https://github.com/apache/lucenenet/issues/1218#issuecomment-3741021077
@sauravk58 Assigned to you. Please perform an analysis of this bug to confirm it, as well as to reconcile against upstream Java Lucene behavior. Sometimes there are bugs in our conversion from Java, other times those bugs exist upstream.
If the bug exists upstream and still exists in the latest version of Lucene, we should upstream a fix for it there as well. If it has either been fixed upstream or the code removed to where it is no longer relevant, we can fix on our end without an upstream fix.
Thanks for your help in looking into this!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] DateTools.DateToString TimeZoneInfo Parameter Doesn't Have An Effect [lucenenet]
Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin commented on issue #1218:
URL: https://github.com/apache/lucenenet/issues/1218#issuecomment-4247426414
> [@paulirwin](https://github.com/paulirwin) [@sauravk58](https://github.com/sauravk58)
>
> I am assigning this to myself, since I have already fixed it on a local branch. However, I didn't move forward with the fix because it is still up in the air whether to use the default behavior of Java (which is to use UTC for unknown dates) or the default behavior of .NET (which is to assume local time zone for unknown dates).
My $0.02: if you're passing in an unspecified DateTime and wanting to convert it into another time zone, I think it's best to assume UTC like Java. Rationale being, time zones represent an offset from UTC (even if that offset is zero) for that particular date, so it seems reasonable to choose the Java behavior and assume UTC if unspecified. We should document this as well.
Further, IMO, any other methods in the code base that take DateTime parameters/properties (if there are any) should assume UTC for unspecified as well. Allowing this discrepancy from Java has likely created latent bugs like this throughout the codebase. Fortunately, there are not many places where DateTime is used in our code (only 131 references including many in tests), so it should be pretty easy to audit.
The only risk is that it could be a breaking change to the stored data in the index for any beta users that were not particularly careful about which time zone their DateTimes were in when building their indexes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] DateTools.DateToString TimeZoneInfo Parameter Doesn't Have An Effect [lucenenet]
Posted by "sahin52 (via GitHub)" <gi...@apache.org>.
sahin52 commented on issue #1218:
URL: https://github.com/apache/lucenenet/issues/1218#issuecomment-3741211038
@NightOwl888 Can you please add a lot of tests, especially tests of the actual result? This method has been changing constantly, because of the lack of tests. The tests aren't testing the actual behavior, they generally test stringifying and reverting behavior.
I wanted to write tests, but I wasn't sure about the behaviour either. The Java version isn't that related to .Net version.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] DateTools.DateToString TimeZoneInfo Parameter Doesn't Have An Effect [lucenenet]
Posted by "sauravk58 (via GitHub)" <gi...@apache.org>.
sauravk58 commented on issue #1218:
URL: https://github.com/apache/lucenenet/issues/1218#issuecomment-3733399528
@sahin52 assign me
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org