-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord.cpp
96 lines (82 loc) · 2.94 KB
/
record.cpp
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
#include "record.h"
#include "header.h"
#include <QByteArray>
#include <QList>
#include <QLocale>
#include <fmt/format.h>
namespace El {
Record::Record(int lineno, QByteArray const &line, Header const &hdr)
{
_valid = process(lineno, line, hdr);
}
auto Record::process(int lineno, QByteArray const &line, Header const &hdr) -> bool
{
using namespace Qt::Literals::StringLiterals;
constexpr int SECS_IN_MIN = 60;
constexpr int SECS_IN_HOUR = 3'600;
QList<QByteArray> fields = line.split(';');
if (fields.size() < hdr.numFields()) {
fmt::print("WARNING: Invalid number of fields on line #{}\n", lineno);
return false;
}
// Start time
_begin = QDateTime::fromString(fields.at(hdr.idxStartTime()), u"dd.MM.yyyy hh:mm"_s);
if (!_begin.isValid()) {
fmt::print("WARNING: Invalid start time on line #{}\n", lineno);
return false;
}
// End time
if (hdr.idxEndTime() >= 0) {
_end = QDateTime::fromString(fields.at(hdr.idxEndTime()), u"dd.MM.yyyy hh:mm"_s).addSecs(-SECS_IN_MIN);
if (!_end.isValid()) {
fmt::print("WARNING: Invalid end time on line #{}\n", lineno);
return false;
}
}
else {
_end = _begin.addSecs(SECS_IN_HOUR - SECS_IN_MIN);
}
// kWh
bool ok = false;
QLocale locale(QLocale::Estonian, QLocale::Estonia);
_kWh = locale.toDouble(fields.at(hdr.idxConsumption()), &ok);
if (!ok) {
// if failed, try without the locale
_kWh = fields.at(2).toDouble(&ok);
}
if (!ok) {
fmt::print("WARNING: Invalid consumption value on line #{}\n", lineno);
return false;
}
if (hdr.idxConsumptionType() < 0) {
constexpr int NIGHT_START = 23;
constexpr int NIGHT_END = 7;
QDateTime nightStart(_begin.date(), QTime(NIGHT_START, 0));
QDateTime nightEnd(_begin.date().addDays(1), QTime(NIGHT_END, 0));
if (_begin.time() < QTime(NIGHT_END, 0)) {
nightStart = nightStart.addDays(-1);
nightEnd = nightEnd.addDays(-1);
}
if (_begin.isDaylightTime()) {
constexpr int NIGHT_START_DST = 24;
constexpr int NIGHT_END_DST = 8;
nightStart = QDateTime(_begin.date(), QTime(NIGHT_START_DST, 0));
nightEnd = QDateTime(_begin.date(), QTime(NIGHT_END_DST, 0));
if (_begin.time() >= QTime(NIGHT_END_DST, 0)) {
nightStart = nightStart.addDays(1);
nightEnd = nightEnd.addDays(1);
}
}
constexpr int DOW_SAT = 6;
constexpr int DOW_SUN = 7;
if (_begin.date().dayOfWeek() == DOW_SAT || _begin.date().dayOfWeek() == DOW_SUN ||
(_begin >= nightStart && _end < nightEnd)) {
_night = true;
}
}
else {
_night = QString::fromUtf8(fields.at(hdr.idxConsumptionType())).contains(u"öö"_s, Qt::CaseInsensitive);
}
return true;
}
} // namespace El