Skip to content

Commit

Permalink
[Add] copyWith
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewPiterov committed Jan 14, 2022
1 parent 747c5d6 commit 46f890b
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.6.0

* **[BREAKING CHANGE]** getters `now` of `Date` and `Time` into method `now()`
* **[BREAKING CHANGE]** constructors of `Date` and `Time` with required named parameters
* [Add] `copyWith()` for `Date` and `Time`

## 0.5.0

* [Add] getter `now` for `Date`
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ given('DateTime', () {
});
```

#### CopyWith

```dart
final date = Date(year: 2021, month: 3, day: 7);
print(date.copyWith(year: 2022)); // prints 07/03/2022
```

### DateRange

```dart
Expand All @@ -81,6 +88,14 @@ final isTime2After2 = time2.isAfter(time);
print('Is time2 after: $isTime2After');
```

#### CopyWith

```dart
final time = Time(hour: 6, minute: 30, second: 7);
print(time); // prints 06:30:07
print(time.copyWith(second: 0)); // prints 06:30:00
```

### Overflowed Time

to keep days
Expand Down
3 changes: 3 additions & 0 deletions example/dates.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ void main() {
// Get [Date] & [Time] from [DateTime]
print(DateTime.now().date);
print(DateTime.now().time);

final dateCopyWithYear = Date(year: 2021, month: 3, day: 7);
print(dateCopyWithYear.copyWith(year: 2022)); // prints 3/7/2022
}
13 changes: 13 additions & 0 deletions example/time.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// ignore_for_file: avoid_print, prefer_const_constructors

import 'package:date_time/date_time.dart';

void main() {
final dateTime = DateTime.now();
final date = dateTime.date;
final time = dateTime.time;

final time2 = Time(hour: 6, minute: 30, second: 7);
print(time2);
print(time2.copyWith(second: 0));
}
9 changes: 9 additions & 0 deletions lib/src/date.dart
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,13 @@ class Date {

@override
String toString() => DateFormat.yMd().format(asDateTime);

///
Date copyWith({int? year, int? month, int? day}) {
return Date(
year: year ?? this.year,
month: month ?? this.month,
day: day ?? this.day,
);
}
}
31 changes: 25 additions & 6 deletions lib/src/time.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ignore_for_file: constant_identifier_names, prefer_constructors_over_static_methods

import 'package:date_time/date_time.dart';
import 'package:quiver/core.dart';

import 'overflowed_time.dart';
Expand Down Expand Up @@ -42,6 +43,11 @@ class Time {
assert(second >= 0),
assert(second <= 60);

///
factory Time.from(DateTime dateTime) {
return dateTime.time;
}

///
factory Time.fromMinutes(int amount) {
return Time.fromSeconds(amount * 60);
Expand Down Expand Up @@ -100,19 +106,23 @@ class Time {

/// Tries to onvert a string to `Time`
///
/// [str] should be separated wit `:`, eg: '23:30:21' or `3:17`
static Time? fromStr(String? str) {
/// [str] should be separated with `:`, eg: '23:30:21' or `3:17`
static Time? fromStr(String? str, {String separator = ':'}) {
try {
if (str == null || str.isEmpty) {
return null;
}

final arr = str.split(':');
final arr = str.split(separator);
if (arr.isEmpty) {
return null;
}

final h = arr[0];
final String m = arr.length > 1 ? arr[1] : '0';
final hour = int.parse(arr.first);
final minute = int.parse(arr.length > 1 ? arr[1] : '0');
final second = int.parse(arr.length > 2 ? arr[2] : '0');

return Time(hour: int.parse(h), minute: int.parse(m));
return Time(hour: hour, minute: minute, second: second);
} catch (e) {
return null;
}
Expand Down Expand Up @@ -327,4 +337,13 @@ class Time {
minute.toString().padLeft(2, '0'),
second.toString().padLeft(2, '0'),
].join(defaultSeparator);

///
Time copyWith({int? hour, int? minute, int? second}) {
return Time(
hour: hour ?? this.hour,
minute: minute ?? this.minute,
second: second ?? this.second,
);
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: date_time
description: Package to work with Date & Time in separation and with its Ranges.
version: 0.5.0
version: 0.6.0
homepage: https://github.com/AndrewPiterov/date_time

environment:
Expand Down
18 changes: 17 additions & 1 deletion test/date_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: avoid_redundant_argument_values
// ignore_for_file: avoid_redundant_argument_values, prefer_const_constructors

import 'package:date_time/date_time.dart';
import 'package:given_when_then_unit_test/given_when_then_unit_test.dart';
Expand Down Expand Up @@ -682,4 +682,20 @@ void main() {
date.year.should.be(dateTime.year);
date.should.be(dateTime.date);
});

group('copyWith', () {
const date = Date(year: 2022, month: 5, day: 7);

test('year', () {
date.copyWith(year: 2025).should.be(Date(year: 2025, month: 5, day: 7));
});

test('month', () {
date.copyWith(month: 9).should.be(Date(year: 2022, month: 9, day: 7));
});

test('day', () {
date.copyWith(day: 13).should.be(Date(year: 2022, month: 5, day: 13));
});
});
}
43 changes: 43 additions & 0 deletions test/time_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ void main() {
time.formatAs(TimeStringFormat.Hm).should.be('4:5');
});
});

when('format is HH:mm', () {
then('description', () {
time.format('HH:mm').should.be('04:05');
});
});

when('format w/o parameter', () {
then('description', () {
time.format().should.be('04:05:07');
});
});
});

given('DateTime Now', () {
Expand Down Expand Up @@ -383,4 +395,35 @@ void main() {
res.minute.should.be(59);
res.second.should.be(59);
});

group('copyWith', () {
const time = Time(hour: 6, minute: 7, second: 8);

test('hour', () {
time.copyWith(hour: 22).should.be(Time(hour: 22, minute: 7, second: 8));
});

test('minute', () {
time.copyWith(minute: 22).should.be(Time(hour: 6, minute: 22, second: 8));
});

test('second', () {
time.copyWith(second: 22).should.be(Time(hour: 6, minute: 7, second: 22));
});
});

test('from', () {
final time = Time.from(DateTime(2022, 1, 15, 6, 32, 54));
time.should.be(Time(hour: 6, minute: 32, second: 54));
});

group('fromStr', () {
test('HH:mm', () {
Time.fromStr('20:33').should.be(Time(hour: 20, minute: 33));
});

test('HH:mm:ss', () {
Time.fromStr('20:33:7').should.be(Time(hour: 20, minute: 33, second: 7));
});
});
}

0 comments on commit 46f890b

Please sign in to comment.