[libcalamares] Extend TZ with location and country

main
Adriaan de Groot 5 years ago
parent f4509f3380
commit 1a8439069e

@ -18,6 +18,8 @@
#include "TimeZone.h"
#include "utils/Logger.h"
#include <QFile>
#include <QStringList>
#include <QTextStream>
@ -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 )
{

@ -21,6 +21,8 @@
#include "DllMacro.h"
#include "utils/Logger.h"
#include <QAbstractListModel>
#include <QObject>
#include <QString>
@ -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:

Loading…
Cancel
Save