Skip to content

DateTools.DateToString TimeZoneInfo Parameter Doesn't Have An Effect #1218

@sahin52

Description

@sahin52

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Lucene.Net DateTools.DateToString has a parameter for TimeZoneInfo. But this parameter has ne effect.

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
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

        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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions