From 4a818ac0048bbae7dad9a305f642bda550e50b04 Mon Sep 17 00:00:00 2001 From: Jama Mohamed Date: Sun, 12 May 2024 12:54:16 +0300 Subject: [PATCH] [ISSUE-273] Use floating points to calculate relative time --- lib/src/display.dart | 12 ++++++------ test/src/display_test.dart | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/src/display.dart b/lib/src/display.dart index 24153d2..1853e76 100644 --- a/lib/src/display.dart +++ b/lib/src/display.dart @@ -45,13 +45,13 @@ class Display { } final seconds = - diff(firstDateTime, secondDateTime, Unit.second, false).abs(); + diff(firstDateTime, secondDateTime, Unit.second, true).abs(); final minutes = - diff(firstDateTime, secondDateTime, Unit.minute, false).abs(); - final hours = diff(firstDateTime, secondDateTime, Unit.hour, false).abs(); - final days = diff(firstDateTime, secondDateTime, Unit.day, false).abs(); - final months = diff(firstDateTime, secondDateTime, Unit.month, false).abs(); - final years = diff(firstDateTime, secondDateTime, Unit.year, false).abs(); + diff(firstDateTime, secondDateTime, Unit.minute, true).abs(); + final hours = diff(firstDateTime, secondDateTime, Unit.hour, true).abs(); + final days = diff(firstDateTime, secondDateTime, Unit.day, true).abs(); + final months = diff(firstDateTime, secondDateTime, Unit.month, true).abs(); + final years = diff(firstDateTime, secondDateTime, Unit.year, true).abs(); String result; diff --git a/test/src/display_test.dart b/test/src/display_test.dart index 0515196..055070f 100644 --- a/test/src/display_test.dart +++ b/test/src/display_test.dart @@ -1,3 +1,4 @@ +import 'package:jiffy/jiffy.dart'; import 'package:jiffy/src/display.dart'; import 'package:jiffy/src/enums/unit.dart'; import 'package:jiffy/src/getter.dart'; @@ -323,6 +324,16 @@ List> toAsRelativeDateTimeTestData() { 'secondDateTime': DateTime(1997, 10, 23, 12, 11, 45), 'expectedToAsRelativeDateTime': 'in a minute' }, + { + 'firstDateTime': DateTime(2024, 1, 1, 2, 0, 0), + 'secondDateTime': DateTime(2024, 1, 1, 1, 58, 5), + 'expectedToAsRelativeDateTime': '2 minutes ago' + }, + { + 'firstDateTime': DateTime(2024, 1, 1, 2, 0, 0), + 'secondDateTime': DateTime(2024, 1, 1, 1, 58, 31), + 'expectedToAsRelativeDateTime': 'a minute ago' + }, { 'firstDateTime': DateTime(1997, 10, 23, 12, 11, 23), 'secondDateTime': DateTime(1997, 10, 23, 12, 11, 22), @@ -513,6 +524,16 @@ List> fromAsRelativeDateTimeTestData() { 'secondDateTime': DateTime(1997, 10, 23, 12, 11, 0), 'expectedFromAsRelativeDateTime': 'in a minute' }, + { + 'firstDateTime': DateTime(2024, 1, 1, 2, 0, 0), + 'secondDateTime': DateTime(2024, 1, 1, 1, 58, 5), + 'expectedFromAsRelativeDateTime': 'in 2 minutes' + }, + { + 'firstDateTime': DateTime(2024, 1, 1, 2, 0, 0), + 'secondDateTime': DateTime(2024, 1, 1, 1, 58, 31), + 'expectedFromAsRelativeDateTime': 'in a minute' + }, { 'firstDateTime': DateTime(1997, 10, 23, 12, 11, 0), 'secondDateTime': DateTime(1997, 10, 23, 12, 11, 45),