Sieve icon indicating copy to clipboard operation
Sieve copied to clipboard

UTC DateTime conversion is wrong

Open havotto opened this issue 6 years ago • 2 comments

I am using this great library. One thing that causes problems is that UTC formatted times in filter are parsed as Local DateTimes. The following sample reproduced this behavior:

    class Program
    {
        static void Main(string[] args)
        {
            SieveProcessor processor = new SieveProcessor(new SieveOptionsAccessor());

            var createdAtUtc = DateTime.UtcNow;

            IQueryable<Entity> entities = new[]{
                new Entity { CratedAtUtc = createdAtUtc }
            }.AsQueryable();

            var model = new SieveModel()
            {
                Filters = $"CratedAtUtc=={createdAtUtc.ToString("o")}"
            };

            var result = processor.Apply(model, entities).ToList();

            //should print 1, but prints 0
            Console.WriteLine($"Count: {result.Count}");
        }
    }

    class Entity
    {
        [Sieve(CanFilter = true, CanSort = true)]
        public DateTime CratedAtUtc { get; set; }
    }

    public class SieveOptionsAccessor : IOptions<SieveOptions>
    {
        public SieveOptions Value { get; }

        public SieveOptionsAccessor()
        {
            Value = new SieveOptions()
            {
                ThrowExceptions = true
            };
        }
    }

With Local DateTimeKind it works properly.

havotto avatar Aug 15 '18 11:08 havotto

I know I am resurrecting this one but the behaviour is still exists. It is the default DateTimeConverter causing this. It is creating a DateTimeKind.Local instance when converting from string if the string data has time zone data as you say, DateTimeKind.Unspecified instance when has not. The filter data could be manipulated before going into Sieve but the compare against data could be any kind of DateTime. So it is very counter productive to say "All DateTime values are UTC" from a library point of view.

BUT, from this a feature development could be considered. There could be a global behavioral setting for handling DateTime values, or there could be a mechanism to specify some kind of attribute or an attribute list to filter data value(s), I don't know. This type of feature is widely open to a discussion I think.

hasanmanzak avatar Jan 07 '21 18:01 hasanmanzak

I know I am resurrecting this one, but the problem still exists. I don't think the problem is in default DateTimeConverter. Yes, it is creating a DateTimeKind.Local, but why this matters? Because from my point of view 2022-12-20T10:00:00Z and 2022-12-20T11:00:00+1 are equal values, but somehow they are not equal in SieveProcessor.

maskalek avatar Dec 20 '22 10:12 maskalek