Skip to content

Commit

Permalink
Support for other variants of num (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
jogboms authored Oct 29, 2019
2 parents 2a26dbd + 0300e03 commit 494a9cc
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 22 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final DateTime fourHoursFromNow = DateTime.now() + Duration(hours: 4);
## 🎖 Installation
```yaml
dependencies:
time: "^1.0.0"
time: "^1.1.0"
```
### ⚡ Import
Expand All @@ -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:
Expand All @@ -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;
```

Expand Down
12 changes: 10 additions & 2 deletions example/time_example.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
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);
print(DateTime.now() - 7.days);

// Duration Extensions
print(7.days.inWeeks);
print(7.days.later);
print(7.days.fromNow);
print(7.days.ago);
}
25 changes: 14 additions & 11 deletions lib/src/extensions.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
extension IntTimeExtension on int {
extension NumTimeExtension<T extends num> 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 {
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>

Expand Down
52 changes: 47 additions & 5 deletions test/time_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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', () {
Expand All @@ -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) =>
dateTime.millisecondsSinceEpoch - expected.millisecondsSinceEpoch < 1);
Matcher _isAbout(DateTime expected) =>
predicate<DateTime>((dateTime) => dateTime.millisecondsSinceEpoch - expected.millisecondsSinceEpoch < 1);

0 comments on commit 494a9cc

Please sign in to comment.