CAKE-291 | fixed bug in the get_height_by_date.dart
OleksandrSobol committed
Mar 25, 2021 at 19:45 UTC
ce9f61b0ed46be74d81063694d3980389ef64274
1 file changed
+23
-16
lib/monero/get_height_by_date.dart
+23
-16
@@ -85,28 +85,35 @@ final dates = {
85
"2020-11": 2220000
86
};
87
88
-final heightCoefficient = 0.7;
89
-
88
int getHeigthByDate({DateTime date}) {
89
final raw = '${date.year}' + '-' + '${date.month}';
90
final lastHeight = dates.values.last;
91
int startHeight;
92
int endHeight;
95
- int height;
93
+ int height = 0;
94
97
- if ((dates[raw] == null)||(dates[raw] == lastHeight)) {
98
- startHeight = dates.values.toList()[dates.length - 2];
99
- endHeight = dates.values.toList()[dates.length - 1];
100
- final heightPerDay = (endHeight - startHeight) / 31;
101
- final daysHeight = (heightCoefficient * (date.day - 1) * heightPerDay).round();
102
- height = endHeight + daysHeight;
103
- } else {
104
- startHeight = dates[raw];
105
- final index = dates.values.toList().indexOf(startHeight);
106
- endHeight = dates.values.toList()[index + 1];
107
- final heightPerDay = ((endHeight - startHeight) / 31).round();
108
- final daysHeight = (date.day - 1) * heightPerDay;
109
- height = startHeight + daysHeight - heightPerDay;
95
+ try {
96
+ if ((dates[raw] == null)||(dates[raw] == lastHeight)) {
97
+ startHeight = dates.values.toList()[dates.length - 2];
98
+ endHeight = dates.values.toList()[dates.length - 1];
99
+ final heightPerDay = (endHeight - startHeight) / 31;
100
+ final endDateRaw = dates.keys.toList()[dates.length - 1].split('-');
101
+ final endYear = int.parse(endDateRaw[0]);
102
+ final endMonth = int.parse(endDateRaw[1]);
103
+ final endDate = DateTime(endYear, endMonth);
104
+ final differenceInDays = date.difference(endDate).inDays;
105
+ final daysHeight = (differenceInDays * heightPerDay).round();
106
+ height = endHeight + daysHeight;
107
+ } else {
108
+ startHeight = dates[raw];
109
+ final index = dates.values.toList().indexOf(startHeight);
110
+ endHeight = dates.values.toList()[index + 1];
111
+ final heightPerDay = ((endHeight - startHeight) / 31).round();
112
+ final daysHeight = (date.day - 1) * heightPerDay;
113
+ height = startHeight + daysHeight - heightPerDay;
114
+ }
115
+ } catch (e) {
116
+ print(e.toString());
117
}
118
119
return height;