From e7dc40a4deb2b1f83056d602814733f680a37f9e Mon Sep 17 00:00:00 2001 From: Crondung <1922635@stud.hs-mannheim.de> Date: Mon, 6 Mar 2023 00:04:59 +0100 Subject: [PATCH] util funciton to calculate duration between two times => calc sleepTime --- lib/models/sleep.dart | 2 ++ lib/utils/timer_util.dart | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/models/sleep.dart b/lib/models/sleep.dart index 4447be4..991de20 100644 --- a/lib/models/sleep.dart +++ b/lib/models/sleep.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:smoke_cess_app/interface/db_record.dart'; +import 'package:smoke_cess_app/utils/timer_util.dart'; class Sleep implements DatabaseRecord { final int _sleepQualityValue; @@ -13,6 +14,7 @@ class Sleep implements DatabaseRecord { DateTime get date => _date; int get sleepQualitiyValue => _sleepQualityValue; + TimeOfDay get sleepDuration => _sleptAt.durationBetween(_wokeUpAt); @override factory Sleep.fromDatabase(Map map) { diff --git a/lib/utils/timer_util.dart b/lib/utils/timer_util.dart index a63030f..42a9180 100644 --- a/lib/utils/timer_util.dart +++ b/lib/utils/timer_util.dart @@ -1,3 +1,5 @@ +import 'package:flutter/material.dart'; + String formatTime(int seconds) { Duration duration = Duration(seconds: seconds); String formattedTime = ''; @@ -12,3 +14,12 @@ String formatTime(int seconds) { formattedTime += formattedSeconds; return formattedTime; } + +extension TimeOfDayExtension on TimeOfDay { + TimeOfDay durationBetween(TimeOfDay time) { + int hourOffset = time.minute - minute < 0 ? 1 : 0; + return TimeOfDay( + hour: (time.hour - hour - hourOffset) % 24, + minute: (time.minute - minute) % 60); + } +}