Skip to content

Commit

Permalink
Introduce support for other variants of num
Browse files Browse the repository at this point in the history
  • Loading branch information
jogboms committed Oct 29, 2019
1 parent 568695a commit 0cfb4fb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 13 deletions.
10 changes: 9 additions & 1 deletion example/time_example.dart
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
23 changes: 13 additions & 10 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,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;
Expand Down
46 changes: 44 additions & 2 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,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);
});
Expand All @@ -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) =>
dateTime.millisecondsSinceEpoch - expected.millisecondsSinceEpoch < 1);
Matcher _isAbout(DateTime expected) =>
predicate<DateTime>((dateTime) => dateTime.millisecondsSinceEpoch - expected.millisecondsSinceEpoch < 1);

0 comments on commit 0cfb4fb

Please sign in to comment.