KItinerary

ssbv1ticket.cpp
1/*
2 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
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
13using namespace KItinerary;
14
15enum {
16 SSB_DATA_SIZE_MIN = 107,
17 SSB_DATA_SIZE_MAX = 111,
18 SSB_VERSION = 1,
19};
20
21SSBv1Ticket::SSBv1Ticket() = default;
22
23SSBv1Ticket::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
32SSBv1Ticket::~SSBv1Ticket() = default;
33
35{
36 return !m_data.isEmpty();
37}
38
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
48{
49 if (!isValid()) {
50 return {};
51 }
52 return dayNumberToDate(firstDayOfValidityDay(), contextDate);
53}
54
56{
57 if (!isValid() || departureTimeSlot() == 0 || departureTimeSlot() > 48) {
58 return {};
59 }
60
61 QDateTime dt(firstDayOfValidity(contextDate), {0, 0});
62 return dt.addSecs(60 * 30 * (departureTimeSlot() - 1));
63}
64
65QByteArray SSBv1Ticket::rawData() const
66{
67 return m_data;
68}
69
70#include "moc_ssbv1ticket.cpp"
static QDate dayNumberToDate(int days, const QDateTime &context)
Convert a SSBv1 or v2 day number to a date based on context.
Q_INVOKABLE QDateTime departureTime(const QDateTime &contextDate=QDateTime::currentDateTime()) const
Decoded departure time slot.
int firstDayOfValidityDay
Days since Jan 01.
Definition ssbv1ticket.h:30
bool isValid() const
Returns true if this is a valid SSB ticket.
int departureTimeSlot
departure time encoded as 48 30min time slots
Definition ssbv1ticket.h:41
static bool maybeSSB(const QByteArray &data)
Returns true if data might be an ERA SSB ticket.
Q_INVOKABLE QDate firstDayOfValidity(const QDateTime &contextDate=QDateTime::currentDateTime()) const
First day of validity.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
char at(qsizetype i) const const
bool isEmpty() const const
qsizetype size() const const
QDateTime addSecs(qint64 s) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:36 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.