Skip to content

Commit decb6c2

Browse files
committed
fix: include milliseconds and microseconds in timeOfDay getter
1 parent bf7c7b0 commit decb6c2

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

lib/src/extensions.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ extension DateTimeTimeExtension on DateTime {
3737
DateTime get date => isUtc ? DateTime.utc(year, month, day) : DateTime(year, month, day);
3838

3939
/// Returns only the time
40-
Duration get timeOfDay => hour.hours + minute.minutes + second.seconds;
40+
Duration get timeOfDay =>
41+
hour.hours + minute.minutes + second.seconds + millisecond.milliseconds + microsecond.microseconds;
4142

4243
/// Returns if today, true
4344
bool get isToday {

test/time_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,19 @@ void main() {
113113
);
114114
});
115115

116+
test('can get complete time with milliseconds and microseconds', () {
117+
expect(
118+
DateTime(2020, 4, 10, 15, 27, 30, 500, 250).timeOfDay,
119+
Duration(hours: 15, minutes: 27, seconds: 30, milliseconds: 500, microseconds: 250),
120+
);
121+
});
122+
123+
test('timeOfDay preserves precision when reconstructing DateTime', () {
124+
final original = DateTime(2020, 1, 1, 12, 30, 15, 500, 250);
125+
final reconstructed = original.date + original.timeOfDay;
126+
expect(reconstructed, equals(original));
127+
});
128+
116129
test('can handle isToday', () {
117130
final today = date;
118131
withClock(Clock.fixed(today), () {

0 commit comments

Comments
 (0)