From 80ad95819e4222199a9be4909ba2d4a9bcb6cde7 Mon Sep 17 00:00:00 2001 From: Juan Pablo Orsay Date: Tue, 15 Oct 2019 17:24:07 -0300 Subject: [PATCH 1/3] Rename later to fromNow to clarify the origin time --- README.md | 4 ++-- example/time_example.dart | 2 +- lib/src/extensions.dart | 2 +- test/time_test.dart | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 07a607a..622f34f 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ final Duration tenMinutes = 10.minutes; final DateTime afterTenMinutes = DateTime.now() + 10.minutes; final Duration tenMinutesAndSome = 10.minutes + 15.seconds; final int tenMinutesInSeconds = 10.minutes.inSeconds; -final DateTime tenMinutesLater = 10.minutes.later; +final DateTime tenMinutesFromNow = 10.minutes.fromNow; ``` You can perform all basic arithmetic operations on `Duration` as you always have been: @@ -52,7 +52,7 @@ final int twoMinutesInSeconds = 2.minutes.inSeconds; You can also convert `Duration` to `DateTime`, if needed: ```dart -final DateTime timeInFuture = 5.minutes.later; +final DateTime timeInFuture = 5.minutes.fromNow; final DateTime timeInPast = 5.minutes.ago; ``` diff --git a/example/time_example.dart b/example/time_example.dart index 76058a3..5b26574 100644 --- a/example/time_example.dart +++ b/example/time_example.dart @@ -17,6 +17,6 @@ void main() { // Duration Extensions print(7.days.inWeeks); - print(7.days.later); + print(7.days.fromNow); print(7.days.ago); } diff --git a/lib/src/extensions.dart b/lib/src/extensions.dart index b4d22c5..3aefc16 100644 --- a/lib/src/extensions.dart +++ b/lib/src/extensions.dart @@ -37,7 +37,7 @@ extension DurationTimeExtension on Duration { int get inWeeks => (inDays / 7).ceil(); /// Adds the Duration to the current DateTime and returns a DateTime in the future - DateTime get later => DateTime.now() + this; + DateTime get fromNow => DateTime.now() + this; /// Subtracts the Duration from the current DateTime and returns a DateTime in the past DateTime get ago => DateTime.now() - this; diff --git a/test/time_test.dart b/test/time_test.dart index 4d74491..1fea037 100644 --- a/test/time_test.dart +++ b/test/time_test.dart @@ -58,8 +58,8 @@ void main() { expect(7.days.inWeeks, 1); }); - test('can be converted into a later DateTime', () { - expect(7.days.later, _isAbout(DateTime.now() + 7.days)); + test('can be converted into a future DateTime', () { + expect(7.days.fromNow, _isAbout(DateTime.now() + 7.days)); }); test('can be converted into a previous DateTime', () { @@ -69,7 +69,7 @@ void main() { }); } -// Checks if the two times returned a *just* about equal. Since `later` and +// Checks if the two times returned a *just* about equal. Since `fromNow` and // `ago` use DateTime.now(), we can't create an expected condition that is // exactly equal. Matcher _isAbout(DateTime expected) => predicate((dateTime) => From 0cfb4fbdfb6841ce09f33736a369876604219167 Mon Sep 17 00:00:00 2001 From: Jogboms Date: Tue, 29 Oct 2019 08:03:43 +0100 Subject: [PATCH 2/3] Introduce support for other variants of num --- example/time_example.dart | 10 ++++++++- lib/src/extensions.dart | 23 +++++++++++--------- test/time_test.dart | 46 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 13 deletions(-) diff --git a/example/time_example.dart b/example/time_example.dart index 5b26574..665e707 100644 --- a/example/time_example.dart +++ b/example/time_example.dart @@ -1,15 +1,23 @@ import 'package:time/time.dart'; void main() { - // Integer Extensions + // Num Extensions print(1.weeks); + print(1.5.weeks); print(7.days); + print(7.5.days); print(22.hours); + print(22.5.hours); print(45.minutes); + print(45.5.minutes); print(30.seconds); + print(30.5.seconds); print(15.milliseconds); + print(15.5.milliseconds); print(10.microseconds); + print(10.5.microseconds); print(5.nanoseconds); + print(5.5.nanoseconds); // DateTime Extensions print(DateTime.now() + 7.days); diff --git a/lib/src/extensions.dart b/lib/src/extensions.dart index 3aefc16..e3b9136 100644 --- a/lib/src/extensions.dart +++ b/lib/src/extensions.dart @@ -1,27 +1,27 @@ -extension IntTimeExtension on int { +extension NumTimeExtension on T { /// Returns a Duration represented in weeks - Duration get weeks => Duration(days: this * 7); + Duration get weeks => days * DurationTimeExtension.daysPerWeek; /// Returns a Duration represented in days - Duration get days => Duration(days: this); + Duration get days => milliseconds * Duration.millisecondsPerDay; /// Returns a Duration represented in hours - Duration get hours => Duration(hours: this); + Duration get hours => milliseconds * Duration.millisecondsPerHour; /// Returns a Duration represented in minutes - Duration get minutes => Duration(minutes: this); + Duration get minutes => milliseconds * Duration.millisecondsPerMinute; /// Returns a Duration represented in seconds - Duration get seconds => Duration(seconds: this); + Duration get seconds => milliseconds * Duration.millisecondsPerSecond; /// Returns a Duration represented in milliseconds - Duration get milliseconds => Duration(milliseconds: this); + Duration get milliseconds => Duration(microseconds: (this * Duration.microsecondsPerMillisecond).toInt()); /// Returns a Duration represented in microseconds - Duration get microseconds => Duration(microseconds: this); + Duration get microseconds => milliseconds ~/ Duration.microsecondsPerMillisecond; /// Returns a Duration represented in nanoseconds - Duration get nanoseconds => Duration(microseconds: this ~/ 1000); + Duration get nanoseconds => microseconds ~/ DurationTimeExtension.nanosecondsPerMicrosecond; } extension DateTimeTimeExtension on DateTime { @@ -33,8 +33,11 @@ extension DateTimeTimeExtension on DateTime { } extension DurationTimeExtension on Duration { + static const int daysPerWeek = 7; + static const int nanosecondsPerMicrosecond = 1000; + /// Returns the representation in weeks - int get inWeeks => (inDays / 7).ceil(); + int get inWeeks => (inDays / daysPerWeek).ceil(); /// Adds the Duration to the current DateTime and returns a DateTime in the future DateTime get fromNow => DateTime.now() + this; diff --git a/test/time_test.dart b/test/time_test.dart index 1fea037..d8acc02 100644 --- a/test/time_test.dart +++ b/test/time_test.dart @@ -37,6 +37,40 @@ void main() { }); }); + group('Double', () { + test('can be converted into weeks', () { + expect(1.5.weeks, Duration(days: 10, hours: 12)); + }); + + test('can be converted into days', () { + expect(5.5.days, Duration(days: 5, hours: 12)); + }); + + test('can be converted into hours', () { + expect(22.5.hours, Duration(hours: 22, minutes: 30)); + }); + + test('can be converted into minutes', () { + expect(45.5.minutes, Duration(minutes: 45, seconds: 30)); + }); + + test('can be converted into seconds', () { + expect(30.5.seconds, Duration(seconds: 30, milliseconds: 500)); + }); + + test('can be converted into milliseconds', () { + expect(15.5.milliseconds, Duration(milliseconds: 15, microseconds: 500)); + }); + + test('can be converted into microseconds', () { + expect(10.5.microseconds, Duration(microseconds: 10)); + }); + + test('can be converted into nanoseconds', () { + expect(5.5.nanoseconds, Duration(microseconds: 5 ~/ 1000)); + }); + }); + group('DateTime', () { test('can subtract Durations', () { expect( @@ -54,6 +88,14 @@ void main() { }); group('Duration', () { + test('has correct days-to-week static value', () { + expect(DurationTimeExtension.daysPerWeek, 7); + }); + + test('has correct nanosecond-to-microsecond static value', () { + expect(DurationTimeExtension.nanosecondsPerMicrosecond, 1000); + }); + test('can be converted to weeks', () { expect(7.days.inWeeks, 1); }); @@ -72,5 +114,5 @@ void main() { // Checks if the two times returned a *just* about equal. Since `fromNow` and // `ago` use DateTime.now(), we can't create an expected condition that is // exactly equal. -Matcher _isAbout(DateTime expected) => predicate((dateTime) => - dateTime.millisecondsSinceEpoch - expected.millisecondsSinceEpoch < 1); +Matcher _isAbout(DateTime expected) => + predicate((dateTime) => dateTime.millisecondsSinceEpoch - expected.millisecondsSinceEpoch < 1); From 0300e037cc719f506d8d71ffa3256ab58a10a0c5 Mon Sep 17 00:00:00 2001 From: Jogboms Date: Tue, 29 Oct 2019 08:15:29 +0100 Subject: [PATCH 3/3] v1.1.0 --- CHANGELOG.md | 5 +++++ README.md | 3 ++- pubspec.yaml | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1495bb5..f4516e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.1.0 + +- Breaking Change: renamed `later` to `fromNow` to align with other ecosystems +- Introduced support for other variants of `num` i.e `double` + ## 1.0.0 - Named extensions to allow discoverability diff --git a/README.md b/README.md index 622f34f..ea578b3 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ final DateTime fourHoursFromNow = DateTime.now() + Duration(hours: 4); ## 🎖 Installation ```yaml dependencies: - time: "^1.0.0" + time: "^1.1.0" ``` ### ⚡ Import @@ -24,6 +24,7 @@ import 'package:time/time.dart'; ```dart final Duration tenMinutes = 10.minutes; +final Duration oneHourThirtyMinutes = 1.5.hours; final DateTime afterTenMinutes = DateTime.now() + 10.minutes; final Duration tenMinutesAndSome = 10.minutes + 15.seconds; final int tenMinutesInSeconds = 10.minutes.inSeconds; diff --git a/pubspec.yaml b/pubspec.yaml index a00e09e..a8c5562 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: time description: Type-safe DateTime and Duration calculations, powered by extensions. -version: 1.0.0 +version: 1.1.0 homepage: https://github.com/jogboms/time.dart author: Jogboms