Commit e75f012d authored by Volker Krause's avatar Volker Krause
Browse files

Use country subdivision code for determining the timezone as well

parent 7b3ace7c
Pipeline #145865 passed with stages
in 1 minute and 28 seconds
......@@ -137,10 +137,10 @@ private Q_SLOTS:
const auto coord = KnowledgeDb::coordinateForAirport(KnowledgeDb::IataCode{"HKG"});
QVERIFY(coord.isValid());
QVERIFY(LocationUtil::distance(coord.latitude, coord.longitude, 22.31600, 113.93688) < 500);
QCOMPARE(KnowledgeDb::timezoneForLocation(coord.latitude, coord.longitude, {}), QTimeZone("Asia/Hong_Kong"));
QCOMPARE(KnowledgeDb::timezoneForLocation(coord.latitude, coord.longitude, {}, {}), QTimeZone("Asia/Hong_Kong"));
const auto country = KnowledgeDb::countryForAirport(KnowledgeDb::IataCode{"HKG"});
QCOMPARE(country, KnowledgeDb::CountryId{"CN"});
QCOMPARE(KnowledgeDb::timezoneForLocation(coord.latitude, coord.longitude, u"CN"), QTimeZone("Asia/Shanghai"));
QCOMPARE(KnowledgeDb::timezoneForLocation(coord.latitude, coord.longitude, u"CN", {}), QTimeZone("Asia/Shanghai"));
tz = KnowledgeDb::timezoneForAirport(KnowledgeDb::IataCode{"HKG"});
QCOMPARE(tz.id(), QByteArray("Asia/Shanghai"));
}
......
......@@ -9,14 +9,10 @@ BEGIN:VEVENT
DTSTAMP:20171227T111649Z
X-KDE-KITINERARY-RESERVATION:[{"@context":"http://schema.org"\,"@type":
"RentalCarReservation"\,"dropoffLocation":{"@type":"Place"\,"address":
{"@type":"PostalAddress"\,"addressCountry":"US"\,"addressLocality":"Los
Angeles"\,"addressRegion":"CA"\,"postalCode":"94043"\,"streetAddress":
"1234 First Street"}\,"name":"Hertz LAX"}\,"dropoffTime":"2027-08-06T20:
00:00-07:00"\,"pickupLocation":{"@type":"Place"\,"address":{"@type":
"PostalAddress"\,"addressCountry":"US"\,"addressLocality":"San
Diego"\,"addressRegion":"CA"\,"postalCode":"94043"\,"streetAddress":"1500
Orange Avenue"}\,"name":"Hertz San Diego Airport"}\,"pickupTime":
"2027-08-05T16:00:00-07:00"\,"reservationFor":{"@type":
{"@type":"PostalAddress"\,"addressCountry":"US"\,"addressLocality":"Los Angeles"\,"addressRegion":"CA"\,"postalCode":"94043"\,"streetAddress":
"1234 First Street"}\,"name":"Hertz LAX"}\,"dropoffTime":{"@type":"QDateTime"\,"@value":"2027-08-06T20:00:00-07:00"\,"timezone":"America/Los_Angeles"}\,"pickupLocation":{"@type":"Place"\,"address":{"@type":
"PostalAddress"\,"addressCountry":"US"\,"addressLocality":"San Diego"\,"addressRegion":"CA"\,"postalCode":"94043"\,"streetAddress":"1500 Orange Avenue"}\,"name":"Hertz San Diego Airport"}\,"pickupTime":{"@type":"QDateTime"\,"@value":"2027-08-05T16:00:00-07:00"\,"timezone":"America/Los_Angeles"}
\,"reservationFor":{"@type":
"RentalCar"\,"brand":{"@type":"Brand"\,"name":"Honda"}\,"model":
"Civic"\,"name":"Economy Class Car"\,"rentalCompany":{"@type":
"Organization"\,"name":"Hertz"}}\,"reservationNumber":
......@@ -30,8 +26,8 @@ DESCRIPTION:Pickup location: Hertz San Diego Airport\n1500 Orange
Avenue\nSAN DIEGO\, CA 94043\n\nUNITED STATES\n\nDropoff location: Hertz LAX\n1234 First Street\nLOS ANGELES\, CA 94043\n\nUNITED STATES\n\nBooking reference: 546323\nUnder name: John Smith
SUMMARY:Rental car reservation: Economy Class Car
LOCATION:Hertz San Diego Airport
DTSTART;TZID="UTC-07:00":20270805T160000
DTEND;TZID="UTC-07:00":20270806T200000
DTSTART;TZID=America/Los_Angeles:20270805T160000
DTEND;TZID=America/Los_Angeles:20270806T200000
TRANSP:TRANSPARENT
END:VEVENT
END:VCALENDAR
......@@ -102,6 +102,7 @@ private Q_SLOTS:
QCOMPARE(newEvent->dtStart(), refEvent->dtStart());
QCOMPARE(newEvent->dtEnd(), refEvent->dtEnd());
QCOMPARE(newEvent->customProperty("KITINERARY", "RESERVATION"), refEvent->customProperty("KITINERARY", "RESERVATION"));
#if KContacts_VERSION >= QT_VERSION_CHECK(5, 92, 0)
QCOMPARE(newEvent->description(), refEvent->description());
QVERIFY(*newEvent == *refEvent);
......
......@@ -242,9 +242,11 @@ private Q_SLOTS:
{
using namespace KnowledgeDb;
QCOMPARE(timezoneForLocation(NAN, NAN, u"DE"), QTimeZone("Europe/Berlin"));
QCOMPARE(timezoneForLocation(NAN, NAN, u"FR"), QTimeZone("Europe/Paris"));
QCOMPARE(timezoneForLocation(NAN, NAN, u"BR"), QTimeZone());
QCOMPARE(timezoneForLocation(NAN, NAN, u"DE", {}), QTimeZone("Europe/Berlin"));
QCOMPARE(timezoneForLocation(NAN, NAN, u"FR", {}), QTimeZone("Europe/Paris"));
QCOMPARE(timezoneForLocation(NAN, NAN, u"BR", {}), QTimeZone());
QCOMPARE(timezoneForLocation(NAN, NAN, u"US", {}), QTimeZone());
QCOMPARE(timezoneForLocation(NAN, NAN, u"US", u"CA"), QTimeZone("America/Los_Angeles"));
}
void testTimezoneForLocation()
......@@ -252,57 +254,57 @@ private Q_SLOTS:
using namespace KnowledgeDb;
// basic checks in all quadrants
QCOMPARE(timezoneForLocation(52.4, 13.1, {}), QTimeZone("Europe/Berlin"));
QCOMPARE(timezoneForLocation(-8.0, -35.0, {}), QTimeZone("America/Recife"));
QCOMPARE(timezoneForLocation(-36.5, 175.0, {}), QTimeZone("Pacific/Auckland"));
QCOMPARE(timezoneForLocation(44.0, -79.5, {}), QTimeZone("America/Toronto"));
QCOMPARE(timezoneForLocation(52.4, 13.1, {}, {}), QTimeZone("Europe/Berlin"));
QCOMPARE(timezoneForLocation(-8.0, -35.0, {}, {}), QTimeZone("America/Recife"));
QCOMPARE(timezoneForLocation(-36.5, 175.0, {}, {}), QTimeZone("Pacific/Auckland"));
QCOMPARE(timezoneForLocation(44.0, -79.5, {}, {}), QTimeZone("America/Toronto"));
// Special case: Northern Vietnam has a Thai timezone
QCOMPARE(timezoneForLocation(21.0, 106.0, {}), QTimeZone("Asia/Bangkok"));
QCOMPARE(timezoneForLocation(21.0, 106.0, {}, {}), QTimeZone("Asia/Bangkok"));
// Maastricht (NL), very close to the BE border
QCOMPARE(timezoneForLocation(50.8505, 5.6881, QString()), QTimeZone("Europe/Amsterdam"));
QCOMPARE(timezoneForLocation(50.8505, 5.6881, u"NL"), QTimeZone("Europe/Amsterdam"));
QCOMPARE(timezoneForLocation(50.8505, 5.6881, QString(), {}), QTimeZone("Europe/Amsterdam"));
QCOMPARE(timezoneForLocation(50.8505, 5.6881, u"NL", {}), QTimeZone("Europe/Amsterdam"));
// Aachen, at the BE/DE/NL corner
QCOMPARE(timezoneForLocation(50.7717, 6.04235, QString()), QTimeZone("Europe/Berlin"));
QCOMPARE(timezoneForLocation(50.7717, 6.04235, u"DE"), QTimeZone("Europe/Berlin"));
QCOMPARE(timezoneForLocation(50.7717, 6.04235, QString(), {}), QTimeZone("Europe/Berlin"));
QCOMPARE(timezoneForLocation(50.7717, 6.04235, u"DE", {}), QTimeZone("Europe/Berlin"));
//QCOMPARE(timezoneForLocation(50.7727, 6.01565, QString()), QTimeZone("Europe/Brussels"));
QCOMPARE(timezoneForLocation(50.7727, 6.01565, u"BE"), QTimeZone("Europe/Brussels"));
QCOMPARE(timezoneForLocation(50.7727, 6.01565, u"BE", {}), QTimeZone("Europe/Brussels"));
// Geneva (CH), very close to the FR border
QCOMPARE(timezoneForLocation(46.23213, 6.10636, u"CH"), QTimeZone("Europe/Zurich"));
QCOMPARE(timezoneForLocation(46.23213, 6.10636, u"CH", {}), QTimeZone("Europe/Zurich"));
// Busingen (DE), enclosed by CH, and in theory its own timezone (which we ignore)
QCOMPARE(timezoneForLocation(47.69947, 8.68833, u"DE"), QTimeZone("Europe/Berlin"));
QCOMPARE(timezoneForLocation(47.67904, 8.68813, {}), QTimeZone("Europe/Zurich"));
QCOMPARE(timezoneForLocation(47.69947, 8.68833, u"DE", {}), QTimeZone("Europe/Berlin"));
QCOMPARE(timezoneForLocation(47.67904, 8.68813, {}, {}), QTimeZone("Europe/Zurich"));
// Baarle, the ultimate special case, NL/BE differs house by house
QCOMPARE(timezoneForLocation(51.44344, 4.93373, u"BE"), QTimeZone("Europe/Brussels"));
QCOMPARE(timezoneForLocation(51.44344, 4.93373, u"NL"), QTimeZone("Europe/Amsterdam"));
const auto tz = timezoneForLocation(51.44344, 4.93373, {});
QCOMPARE(timezoneForLocation(51.44344, 4.93373, u"BE", {}), QTimeZone("Europe/Brussels"));
QCOMPARE(timezoneForLocation(51.44344, 4.93373, u"NL", {}), QTimeZone("Europe/Amsterdam"));
const auto tz = timezoneForLocation(51.44344, 4.93373, {}, {});
QVERIFY(tz == QTimeZone("Europe/Amsterdam") || tz == QTimeZone("Europe/Brussels"));
// Eliat Airport (IL), close to JO, and with a minor timezone variation due to different weekends
QCOMPARE(timezoneForLocation(29.72530, 35.00598, u"IL"), QTimeZone("Asia/Jerusalem"));
QCOMPARE(timezoneForLocation(29.60908, 35.02038, u"JO"), QTimeZone("Asia/Amman"));
QCOMPARE(timezoneForLocation(29.72530, 35.00598, u"IL", {}), QTimeZone("Asia/Jerusalem"));
QCOMPARE(timezoneForLocation(29.60908, 35.02038, u"JO", {}), QTimeZone("Asia/Amman"));
// Tijuana (MX), close to US, tests equivalent tz search in the neighbouring country
QCOMPARE(timezoneForLocation(32.54274, -116.97505, u"MX"), QTimeZone("America/Tijuana"));
QCOMPARE(timezoneForLocation(32.55783, -117.04773, u"US"), QTimeZone("America/Los_Angeles"));
QCOMPARE(timezoneForLocation(32.54274, -116.97505, u"MX", {}), QTimeZone("America/Tijuana"));
QCOMPARE(timezoneForLocation(32.55783, -117.04773, u"US", {}), QTimeZone("America/Los_Angeles"));
// Cordoba (AR), AR has several sub-zones that are all equivalent
QCOMPARE(timezoneForLocation(-31.4, -64.2, u"AR"), QTimeZone("America/Argentina/Buenos_Aires"));
QCOMPARE(timezoneForLocation(-31.4, -64.2, u"AR", {}), QTimeZone("America/Argentina/Buenos_Aires"));
// polar regions
QCOMPARE(timezoneForLocation(-90.0, 0.0, {}), QTimeZone());
QCOMPARE(timezoneForLocation(90.0, 0.0, {}), QTimeZone());
QCOMPARE(timezoneForLocation(-90.0, 0.0, {}, {}), QTimeZone());
QCOMPARE(timezoneForLocation(90.0, 0.0, {}, {}), QTimeZone());
// Hong Kong seems problematic on FreeBSD
QCOMPARE(timezoneForLocation(22.31600, 113.93688, {}), QTimeZone("Asia/Hong_Kong"));
QCOMPARE(timezoneForLocation(22.31600, 113.93688, {}, {}), QTimeZone("Asia/Hong_Kong"));
// coordinates not provided
QCOMPARE(timezoneForLocation(NAN, NAN, u"LU"), QTimeZone("Europe/Luxembourg"));
QCOMPARE(timezoneForLocation(NAN, NAN, u"LU", {}), QTimeZone("Europe/Luxembourg"));
}
void testUICCountryCodeLookup()
......
......@@ -287,7 +287,7 @@ QDateTime ExtractorPostprocessorPrivate::processTrainTripTime(QDateTime dt, QDat
return dt;
}
const auto tz = KnowledgeDb::timezoneForLocation(station.geo().latitude(), station.geo().longitude(), station.address().addressCountry());
const auto tz = KnowledgeDb::timezoneForLocation(station.geo().latitude(), station.geo().longitude(), station.address().addressCountry(), station.address().addressRegion());
if (!tz.isValid()) {
return dt;
}
......@@ -582,7 +582,7 @@ QDateTime ExtractorPostprocessorPrivate::processTimeForLocation(QDateTime dt, co
return dt;
}
const auto tz = KnowledgeDb::timezoneForLocation(place.geo().latitude(), place.geo().longitude(), place.address().addressCountry());
const auto tz = KnowledgeDb::timezoneForLocation(place.geo().latitude(), place.geo().longitude(), place.address().addressCountry(), place.address().addressRegion());
if (!tz.isValid()) {
return dt;
}
......
......@@ -44,7 +44,7 @@ QTimeZone timezoneForAirport(IataCode iataCode)
return {};
}
return KnowledgeDb::timezoneForLocation((*it).coordinate.latitude, (*it).coordinate.longitude, (*it).country.toString());
return KnowledgeDb::timezoneForLocation((*it).coordinate.latitude, (*it).coordinate.longitude, (*it).country.toString(), {});
}
KnowledgeDb::CountryId countryForAirport(IataCode iataCode)
......
......@@ -68,13 +68,17 @@ static bool isEquivalentTimezone(const QTimeZone &lhs, const QTimeZone &rhs)
return true;
}
QTimeZone KnowledgeDb::timezoneForLocation(float lat, float lon, QStringView alpha2CountryCode)
QTimeZone KnowledgeDb::timezoneForLocation(float lat, float lon, QStringView alpha2CountryCode, QStringView regionCode)
{
const auto coordTz = KTimeZone::fromLocation(lat, lon);
const auto coordZone = toQTimeZone(coordTz);
const auto country = KCountry::fromAlpha2(alpha2CountryCode);
const auto countryTzs = timezonesForCountry(country);
auto countryTzs = KCountrySubdivision::fromCode(QString(alpha2CountryCode + QLatin1Char('-') + regionCode)).timeZoneIds();
if (countryTzs.isEmpty()) {
countryTzs = timezonesForCountry(country);
}
const auto countryFromTz = KTimeZone::country(coordTz);
// if we determine a different country than was provided, search for an equivalent timezone
......
......@@ -20,7 +20,7 @@ namespace KnowledgeDb {
/** Returns the timezone for the given location consisting of coordinates and/or country.
* Either argument can be omitted, if both are available better results can be provided.
*/
KITINERARY_EXPORT QTimeZone timezoneForLocation(float lat, float lon, QStringView alpha2CountryCode);
KITINERARY_EXPORT QTimeZone timezoneForLocation(float lat, float lon, QStringView alpha2CountryCode, QStringView regionCode);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment