From 1a8439069e2706e25b9ef4bd09049d375b42d418 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 10 Dec 2019 15:36:53 +0200 Subject: [PATCH] [libcalamares] Extend TZ with location and country --- src/libcalamares/locale/TimeZone.cpp | 60 ++++++++++++++++++++++++++++ src/libcalamares/locale/TimeZone.h | 17 ++++++++ 2 files changed, 77 insertions(+) diff --git a/src/libcalamares/locale/TimeZone.cpp b/src/libcalamares/locale/TimeZone.cpp index 784d77b9c..4bf23150a 100644 --- a/src/libcalamares/locale/TimeZone.cpp +++ b/src/libcalamares/locale/TimeZone.cpp @@ -18,6 +18,8 @@ #include "TimeZone.h" +#include "utils/Logger.h" + #include #include #include @@ -26,6 +28,35 @@ static const char TZ_DATA_FILE[] = "/usr/share/zoneinfo/zone.tab"; +static double +getRightGeoLocation( QString str ) +{ + double sign = 1, num = 0.00; + + // Determine sign + if ( str.startsWith( '-' ) ) + { + sign = -1; + str.remove( 0, 1 ); + } + else if ( str.startsWith( '+' ) ) + { + str.remove( 0, 1 ); + } + + if ( str.length() == 4 || str.length() == 6 ) + { + num = str.mid( 0, 2 ).toDouble() + str.mid( 2, 2 ).toDouble() / 60.0; + } + else if ( str.length() == 5 || str.length() == 7 ) + { + num = str.mid( 0, 3 ).toDouble() + str.mid( 3, 2 ).toDouble() / 60.0; + } + + return sign * num; +} + + namespace CalamaresUtils { namespace Locale @@ -151,12 +182,34 @@ TZRegion::fromFile( const char* fileName ) regions.append( region ); model.append( new TZRegion( region.toUtf8().data() ) ); } + + QString countryCode = list.at( 0 ).trimmed(); + if ( countryCode.size() != 2 ) + { + continue; + } + + timezoneParts.removeFirst(); + TZZone z( timezoneParts.join( '/' ).toUtf8().constData(), countryCode, list.at( 1 ) ); + cDebug() << "Region" << region << z; } std::sort( model.begin(), model.end(), []( const TZRegion* l, const TZRegion* r ) { return *l < *r; } ); return model; } +TZZone::TZZone( const char* zoneName, const QString& country, QString position ) + : CStringPair( zoneName ) + , m_country( country ) +{ + int cooSplitPos = position.indexOf( QRegExp( "[-+]" ), 1 ); + if ( cooSplitPos > 0 ) + { + m_latitude = getRightGeoLocation( position.mid( 0, cooSplitPos ) ); + m_longitude = getRightGeoLocation( position.mid( cooSplitPos + 1 ) ); + } +} + QString TZZone::tr() const { @@ -164,6 +217,13 @@ TZZone::tr() const return QObject::tr( m_human, "tz_names" ); } +void +TZZone::print( QDebug& log ) const +{ + log << key() << '(' << m_country << ' ' << m_latitude << ',' << m_longitude << ')'; +} + + TZRegionModel::TZRegionModel( TZRegionList l ) : m_regions( l ) { diff --git a/src/libcalamares/locale/TimeZone.h b/src/libcalamares/locale/TimeZone.h index ff92141fa..6e4ba062d 100644 --- a/src/libcalamares/locale/TimeZone.h +++ b/src/libcalamares/locale/TimeZone.h @@ -21,6 +21,8 @@ #include "DllMacro.h" +#include "utils/Logger.h" + #include #include #include @@ -100,8 +102,23 @@ class TZZone : public CStringPair public: using CStringPair::CStringPair; QString tr() const override; + + TZZone( const char* zoneName, const QString& country, QString position ); + + void print( QDebug& ) const; + +protected: + QString m_country; + double m_latitude = 0.0, m_longitude = 0.0; }; +inline QDebug& +operator<<( QDebug& log, const TZZone& z ) +{ + z.print( log ); + return log; +} + class DLLEXPORT TZRegionModel : public QAbstractListModel { public: