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 07a607a..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,10 +24,11 @@ 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; -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 +53,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..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); @@ -17,6 +25,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..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,11 +33,14 @@ 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 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/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 diff --git a/test/time_test.dart b/test/time_test.dart index 4d74491..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,12 +88,20 @@ 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); }); - 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,8 +111,8 @@ 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) => - dateTime.millisecondsSinceEpoch - expected.millisecondsSinceEpoch < 1); +Matcher _isAbout(DateTime expected) => + predicate((dateTime) => dateTime.millisecondsSinceEpoch - expected.millisecondsSinceEpoch < 1);