util funciton to calculate duration between two times => calc sleepTime

main
Julian Gegner 2023-03-06 00:04:59 +01:00
parent 6c3915b6ab
commit e7dc40a4de
2 changed files with 13 additions and 0 deletions

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:smoke_cess_app/interface/db_record.dart'; import 'package:smoke_cess_app/interface/db_record.dart';
import 'package:smoke_cess_app/utils/timer_util.dart';
class Sleep implements DatabaseRecord { class Sleep implements DatabaseRecord {
final int _sleepQualityValue; final int _sleepQualityValue;
@ -13,6 +14,7 @@ class Sleep implements DatabaseRecord {
DateTime get date => _date; DateTime get date => _date;
int get sleepQualitiyValue => _sleepQualityValue; int get sleepQualitiyValue => _sleepQualityValue;
TimeOfDay get sleepDuration => _sleptAt.durationBetween(_wokeUpAt);
@override @override
factory Sleep.fromDatabase(Map<String, dynamic> map) { factory Sleep.fromDatabase(Map<String, dynamic> map) {

View File

@ -1,3 +1,5 @@
import 'package:flutter/material.dart';
String formatTime(int seconds) { String formatTime(int seconds) {
Duration duration = Duration(seconds: seconds); Duration duration = Duration(seconds: seconds);
String formattedTime = ''; String formattedTime = '';
@ -12,3 +14,12 @@ String formatTime(int seconds) {
formattedTime += formattedSeconds; formattedTime += formattedSeconds;
return formattedTime; 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);
}
}