objectbox / objectbox-dart

Flutter database for super-fast Dart object persistence
https://docs.objectbox.io/getting-started
Apache License 2.0
927 stars 115 forks source link

Datetime field greaterThan method doesn't work as expected #531

Closed ebadta81 closed 1 year ago

ebadta81 commented 1 year ago

Basic info:

Steps to reproduce

Run the test code.

Expected behavior

TestClass_.time.greaterThan(time) should not include "2020-01-01" TestClass entry in the result set.

Code

import 'package:datetime_test/objectbox.g.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

@Entity()
class TestClass {
  @Id()
  int id;
  DateTime time;

  TestClass(this.id, this.time);
}

void main() async {
    WidgetsFlutterBinding.ensureInitialized();
    final dir = await getApplicationSupportDirectory();
    String obDirPath = '${dir.path}/objectbox';

    Store MBStore = Store(getObjectBoxModel(),
      directory: obDirPath, queriesCaseSensitiveDefault: false);
    final tbox = MBStore.box<TestClass>();
    tbox.put(TestClass(0, DateTime.parse("2020-01-01")));
    tbox.put(TestClass(0, DateTime.parse("2023-04-20")));

    final time = DateTime.now().subtract(const Duration(days: 90)).millisecondsSinceEpoch;
    var q1 = TestClass_.id.greaterThan(0);
    q1.and(TestClass_.time.greaterThan(time));

    final list = tbox.query(q1).build().find();
    for (final item in list) {
      print("id: ${item.id} time: ${item.time}");
    }

  runApp(const MyApp());
}

Logs, stack traces

flutter: id: 1 time: 2020-01-01 00:00:00.000
flutter: id: 2 time: 2023-04-20 00:00:00.000
ebadta81 commented 1 year ago

My bad. Forgot to give the additional condition to q1.