-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatgpt_sleep_wake_date_time_calculator.dart
109 lines (92 loc) · 3.14 KB
/
chatgpt_sleep_wake_date_time_calculator.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import 'package:intl/intl.dart';
void main() {
List<String> sleepTimeHistoryLst = [
"09-09-2023 02:57",
"3:48",
"4:02",
"5:50"
];
List<String> wakeTimeHistoryLst = [
"09-09-2023 06:45",
"0:38",
"0:40",
// "0:19" // in case the Sleep Duration is finished not in
// wake but in sleep mode, which doesn't normally
// happens !
];
List<String> sleepWakeHistoryLst = computeSleepWakeHistoryLst(
sleepTimeHistoryLst: sleepTimeHistoryLst,
wakeTimeHistoryLst: wakeTimeHistoryLst
);
print("Full list");
print(sleepWakeHistoryLst);
}
List<String> computeSleepWakeHistoryLst({
required List<String> sleepTimeHistoryLst,
required List<String> wakeTimeHistoryLst,
}) {
List<String> sleepTimeDateTime = [
// adding the first sleep date time to the sleepDateTime
// list
"${sleepTimeHistoryLst[0]}, ${sleepTimeHistoryLst[1]}"
];
List<String> wakeTimeDateTime = [
// adding the first wake date time to the wakeDateTime
// list
"${wakeTimeHistoryLst[0]}, ${wakeTimeHistoryLst[1]}"
];
for (int i = 1; i < sleepTimeHistoryLst.length - 1; i++) {
if (i == 1) {
// computing the second sleep date time
DateTime sleepTime = DateFormat('dd-MM-yyyy HH:mm')
.parse(sleepTimeHistoryLst[i - 1])
.add(Duration(minutes: _getMinutes(sleepTimeHistoryLst[i])))
.add(Duration(minutes: _getMinutes(wakeTimeHistoryLst[i])));
sleepTimeDateTime.add(
"${DateFormat('dd-MM-yyyy HH:mm').format(sleepTime)}, ${sleepTimeHistoryLst[i + 1]}");
} else {
DateTime sleepTime = DateFormat('dd-MM-yyyy HH:mm')
.parse(sleepTimeDateTime[i - 1])
.add(Duration(minutes: _getMinutes(sleepTimeHistoryLst[i])))
.add(Duration(minutes: _getMinutes(wakeTimeHistoryLst[i])));
sleepTimeDateTime.add(
"${DateFormat('dd-MM-yyyy HH:mm').format(sleepTime)}, ${sleepTimeHistoryLst[i + 1]}");
}
}
print("Sleep date time list");
for (String sleepDateTimeStr in sleepTimeDateTime) {
print(sleepDateTimeStr);
}
int i = 2;
for (String sleepDateTimeStr in sleepTimeDateTime.sublist(1)) {
if (i < wakeTimeHistoryLst.length) {
DateTime wakeDateTime = DateFormat('dd-MM-yyyy HH:mm')
.parse(sleepDateTimeStr.split(',')[0])
.add(Duration(minutes: _getMinutes(sleepDateTimeStr.split(',')[1])));
wakeTimeDateTime.add(
"${DateFormat('dd-MM-yyyy HH:mm').format(wakeDateTime)}, ${wakeTimeHistoryLst[i++]}");
}
}
print("\nWake date time list");
for (String wakeDateTimeStr in wakeTimeDateTime) {
print(wakeDateTimeStr);
}
sleepTimeHistoryLst.addAll(wakeTimeHistoryLst);
return sleepTimeHistoryLst;
}
int _getMinutes(String timeStr) {
List<String> parts = timeStr.split(':');
if (parts.length == 2) {
return int.parse(parts[0]) * 60 + int.parse(parts[1]);
}
return int.parse(timeStr);
}
String _formatDuration(Duration d) {
String twoDigits(int n) {
if (n >= 10) return "$n";
return "0$n";
}
String twoDigitMinutes = twoDigits(d.inMinutes.remainder(60));
String twoDigitHours = twoDigits(d.inHours);
return "$twoDigitHours:$twoDigitMinutes";
}