KItinerary

ssbv1ticket.cpp
1 /*
2  SPDX-FileCopyrightText: 2021 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "ssbv1ticket.h"
8 
9 #include <QDebug>
10 
11 #include <cstring>
12 
13 using namespace KItinerary;
14 
15 enum {
16  SSB_DATA_SIZE_MIN = 107,
17  SSB_DATA_SIZE_MAX = 111,
18  SSB_VERSION = 1,
19 };
20 
21 SSBv1Ticket::SSBv1Ticket() = default;
22 
23 SSBv1Ticket::SSBv1Ticket(const QByteArray &data)
24 {
25  if (maybeSSB(data)) {
26  m_data = data;
27  } else {
28  qWarning() << "Trying to construct an SSB ticket from invalid data!";
29  }
30 }
31 
32 SSBv1Ticket::~SSBv1Ticket() = default;
33 
34 bool SSBv1Ticket::isValid() const
35 {
36  return !m_data.isEmpty();
37 }
38 
39 bool SSBv1Ticket::maybeSSB(const QByteArray& data)
40 {
41  if (data.size() < SSB_DATA_SIZE_MIN || data.size() > SSB_DATA_SIZE_MAX) {
42  return false;
43  }
44  return (data.at(0) >> 4) == SSB_VERSION;
45 }
46 
47 QDate SSBv1Ticket::firstDayOfValidity(const QDateTime &contextDate) const
48 {
49  if (!isValid() || firstDayOfValidityDay() == 0 || firstDayOfValidityDay() > 366) {
50  return {};
51  }
52  QDate d(contextDate.date().year(), 1, 1);
53  return d.addDays(firstDayOfValidityDay() - 1);
54 }
55 
56 QDateTime SSBv1Ticket::departureTime(const QDateTime &contextDate) const
57 {
58  if (!isValid() || departureTimeSlot() == 0 || departureTimeSlot() > 48) {
59  return {};
60  }
61 
62  QDateTime dt(firstDayOfValidity(contextDate), {0, 0});
63  return dt.addSecs(60 * 30 * (departureTimeSlot() - 1));
64 }
65 
66 QByteArray SSBv1Ticket::rawData() const
67 {
68  return m_data;
69 }
QDateTime addSecs(qint64 s) const const
int year() const const
char at(int i) const const
bool isEmpty() const const
QDate date() const const
int size() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Aug 13 2022 04:08:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.