KItinerary

fcbutil.cpp
1/*
2 SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "fcbutil.h"
7
8using namespace KItinerary;
9
10QString FcbUtil::stringifyUicStationIdentifier(int num, const QByteArray &ia5)
11{
12 if (num >= 10'00000 && num <= 99'99999) {
13 return QLatin1StringView("uic:") + QString::number(num);
14 }
15 if (ia5.size() == 7) {
16 return QLatin1StringView("uic:") + QString::fromLatin1(ia5);
17 }
18
19 return {};
20}
21
22QString FcbUtil::stringifyStationIdentifier(bool numIsSet, int num, const QByteArray &ia5)
23{
24 if (numIsSet) {
25 return QString::number(num);
26 }
27 return QString::fromLatin1(ia5);
28}
29
30QString FcbUtil::classCodeToString(Fcb::v13::TravelClassType classCode)
31{
32 switch (classCode) {
33 case Fcb::v13::notApplicable: return {};
34 case Fcb::v13::first: return QString::number(1);
35 case Fcb::v13::second: return QString::number(2);
36 default:
37 qCWarning(Log) << "Unhandled FCB class code" << classCode;
38 }
39 return {};
40}
41
42QString FcbUtil::classCodeToString(Fcb::v2::TravelClassType classCode)
43{
44 switch (classCode) {
45 case Fcb::v2::notApplicable: return {};
46 case Fcb::v2::first:
47 case Fcb::v2::standardFirst:
48 return QString::number(1);
49 case Fcb::v2::second:
50 case Fcb::v2::standardSecond:
51 return QString::number(2);
52 default:
53 qCWarning(Log) << "Unhandled FCB class code" << classCode;
54 }
55 return {};
56}
57
58QString FcbUtil::classCodeToString(Fcb::v3::TravelClassType classCode)
59{
60 switch (classCode) {
61 case Fcb::v3::notApplicable: return {};
62 case Fcb::v3::first:
63 case Fcb::v3::standardFirst:
64 return QString::number(1);
65 case Fcb::v3::second:
66 case Fcb::v3::standardSecond:
67 return QString::number(2);
68 default:
69 qCWarning(Log) << "Unhandled FCB class code" << classCode;
70 }
71 return {};
72}
73
74QDate FcbUtil::decodeDate(int year, std::optional<int> day)
75{
76 QDate d(year, 1, 1);
77 if (day) {
78 d = d.addDays((*day) - 1);
79 }
80 return d;
81}
82
83QDate FcbUtil::decodeDifferentialDate(const QDate &base, int year, std::optional<int> day)
84{
85 QDate d(base.year(), 1, 1);
86 d = d.addYears(year);
87 if (day) {
88 d = d.addDays(*day);
89 }
90 return d;
91}
92
93QDateTime FcbUtil::issuingDateTime(int year, int day, std::optional<int> time)
94{
95 QDate date = decodeDate(year, day);
96 if (time) {
97 return QDateTime(date, QTime(0,0).addSecs(*time * 60), QTimeZone::UTC);
98 }
99 return QDateTime(date, {});
100}
101
102QDateTime FcbUtil::decodeDifferentialTime(const QDateTime &baseDt, int day, std::optional<int> time, std::optional<int> utcOffset)
103{
104 if (!time) {
105 return {};
106 }
107
108 QDate d = baseDt.date().addDays(day);
109 QTime t = QTime(0, 0).addSecs((*time) * 60);
110 if (utcOffset) {
111 return QDateTime(d, t, QTimeZone::fromSecondsAheadOfUtc(- (*utcOffset) * 15 * 60));
112 }
113 return QDateTime(d, t);
114}
115
116QDateTime FcbUtil::decodeDifferentialStartTime(const QDateTime &baseDt, int day, std::optional<int> time, std::optional<int> utcOffset)
117{
118 QDate d = baseDt.date().addDays(day);
119 QTime t = time ? QTime(0, 0).addSecs((*time) * 60) : QTime();
120 if (utcOffset) {
121 return QDateTime(d, t, QTimeZone::fromSecondsAheadOfUtc(- (*utcOffset) * 15 * 60));
122 }
123 return QDateTime(d, t);
124}
125
126QDateTime FcbUtil::decodeDifferentialEndTime(const QDateTime &baseDt, int day, std::optional<int> time, std::optional<int> utcOffset)
127{
128 QDate d = baseDt.date().addDays(day);
129 QTime t = time ? QTime(0, 0).addSecs((*time) * 60) : QTime(23, 59, 59);
130 if (utcOffset) {
131 return QDateTime(d, t, QTimeZone::fromSecondsAheadOfUtc(- (*utcOffset) * 15 * 60));
132 }
133 return QDateTime(d, t);
134}
static QString classCodeToString(Fcb::v13::TravelClassType classCode)
Convert a class code enum value to a string for human representation.
Definition fcbutil.cpp:30
static QDateTime decodeDifferentialTime(const QDateTime &baseDt, int day, std::optional< int > time, std::optional< int > utcOffset)
Decode differential time, relative to baseDt.
Definition fcbutil.cpp:102
static QDateTime issuingDateTime(int year, int day, std::optional< int > time)
Decode FCB issuing time.
Definition fcbutil.cpp:93
static QDate decodeDate(int year, std::optional< int > day)
Decode FCB date.
Definition fcbutil.cpp:74
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
qsizetype size() const const
QDate addDays(qint64 ndays) const const
int year() const const
QDate date() const const
QString fromLatin1(QByteArrayView str)
QString number(double n, char format, int precision)
QTime addSecs(int s) const const
QTimeZone fromSecondsAheadOfUtc(int offset)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 4 2025 12:02:17 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.