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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to