Skip to content

Commit 2afb2bf

Browse files
authored
Handle UTC flag during copyWith #45
2 parents 10e0753 + c3122db commit 2afb2bf

File tree

3 files changed

+49
-30
lines changed

3 files changed

+49
-30
lines changed

lib/src/extensions.dart

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,27 @@ extension DateTimeTimeExtension on DateTime {
180180
int? millisecond,
181181
int? microsecond,
182182
}) {
183-
return DateTime(
184-
year ?? this.year,
185-
month ?? this.month,
186-
day ?? this.day,
187-
hour ?? this.hour,
188-
minute ?? this.minute,
189-
second ?? this.second,
190-
millisecond ?? this.millisecond,
191-
microsecond ?? this.microsecond,
192-
);
183+
return isUtc
184+
? DateTime.utc(
185+
year ?? this.year,
186+
month ?? this.month,
187+
day ?? this.day,
188+
hour ?? this.hour,
189+
minute ?? this.minute,
190+
second ?? this.second,
191+
millisecond ?? this.millisecond,
192+
microsecond ?? this.microsecond,
193+
)
194+
: DateTime(
195+
year ?? this.year,
196+
month ?? this.month,
197+
day ?? this.day,
198+
hour ?? this.hour,
199+
minute ?? this.minute,
200+
second ?? this.second,
201+
millisecond ?? this.millisecond,
202+
microsecond ?? this.microsecond,
203+
);
193204
}
194205
}
195206

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ dev_dependencies:
1111
test: ^1.16.5
1212
test_coverage:
1313
git:
14-
url: https://github.com/jogboms/test-coverage.git
14+
url: https://github.com/timsneath/test-coverage.git

test/time_test.dart

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -489,26 +489,34 @@ void main() {
489489
expect(expected.millisecond, 0);
490490
expect(expected.microsecond, 12);
491491
});
492-
});
493492

494-
test('with null values', () {
495-
final initial = DateTime(2019, 2, 4, 24, 50, 45, 1, 1);
496-
final year = initial.copyWith(year: 2021);
497-
expect(year.year, 2021);
498-
final month = initial.copyWith(month: 10);
499-
expect(month.month, 10);
500-
final day = initial.copyWith(day: 28);
501-
expect(day.day, 28);
502-
final hour = initial.copyWith(hour: 12);
503-
expect(hour.hour, 12);
504-
final minute = initial.copyWith(minute: 45);
505-
expect(minute.minute, 45);
506-
final second = initial.copyWith(second: 10);
507-
expect(second.second, 10);
508-
final millisecond = initial.copyWith(millisecond: 0);
509-
expect(millisecond.millisecond, 0);
510-
final microsecond = initial.copyWith(microsecond: 12);
511-
expect(microsecond.microsecond, 12);
493+
test('with null values', () {
494+
final initial = DateTime(2019, 2, 4, 24, 50, 45, 1, 1);
495+
final year = initial.copyWith(year: 2021);
496+
expect(year.year, 2021);
497+
final month = initial.copyWith(month: 10);
498+
expect(month.month, 10);
499+
final day = initial.copyWith(day: 28);
500+
expect(day.day, 28);
501+
final hour = initial.copyWith(hour: 12);
502+
expect(hour.hour, 12);
503+
final minute = initial.copyWith(minute: 45);
504+
expect(minute.minute, 45);
505+
final second = initial.copyWith(second: 10);
506+
expect(second.second, 10);
507+
final millisecond = initial.copyWith(millisecond: 0);
508+
expect(millisecond.millisecond, 0);
509+
final microsecond = initial.copyWith(microsecond: 12);
510+
expect(microsecond.microsecond, 12);
511+
});
512+
513+
test('copyWith should save isUtc', () async {
514+
final now = DateTime.now().toUtc();
515+
expect(now.isUtc, isTrue);
516+
517+
final later = now.copyWith(hour: now.hour + 3);
518+
expect(later.isUtc, isTrue);
519+
});
512520
});
513521
});
514522
});

0 commit comments

Comments
 (0)