📄 timezone.cpp
字号:
QCString TimeZoneLocation::lonStr() const{ return mLonStr.latin1();}QString TimeZoneLocation::description() const{ return qApp->translate( "TimeZone", mDescription );}QString TimeZoneLocation::area() const{ QCString displayArea = mArea;#if QT_VERSION < 0x030000 displayArea.replace( QRegExp("_"), " ");#else displayArea.replace( '_', ' ' );#endif return qApp->translate( "TimeZone", displayArea );}QString TimeZoneLocation::city() const{ QCString displayCity = mCity;#if QT_VERSION < 0x030000 displayCity.replace( QRegExp("_"), " ");#else displayCity.replace( '_', ' ' );#endif return qApp->translate( "TimeZone", displayCity );}QCString TimeZoneLocation::countryCode() const{ return mCountryCode;}QCString TimeZoneLocation::id() const{ return mId;}void TimeZoneLocation::dump() const{ if (!this) { return; } qDebug("%s at [%s,%s] [%d,%d]: %s", id().data(), mLatStr.data(), mLonStr.data(), lat(), lon(), mDescription ); qDebug("City :%s, Area: %s, Country Code %s", city().latin1(), area().data(), countryCode().data());}QStringList TimeZoneLocation::languageList(){#if QT_VERSION >= 0x040000# error "Use Global::languageList()"#endif QString lang; QStringList langs;#ifdef QTOPIA_DESKTOP langs = gQtopiaDesktopConfig->languages();#else if (lang.isEmpty()) lang = getenv("LANG"); int i = lang.find("."); if ( i > 0 ) lang = lang.left( i ); langs.append(lang); i = lang.find( "_" ); if ( i > 0 ) langs.append(lang.left(i));#endif return langs;}void TimeZoneLocation::load( QAsciiDict< TimeZoneLocation > &store ){ QStringList langs = languageList(); for (QStringList::ConstIterator lit = langs.begin(); lit!=langs.end(); ++lit) { QString lang = *lit; QTranslator * trans = new QTranslator(qApp); QString tfn = QPEApplication::qpeDir()+"i18n/"+lang+"/timezone.qm"; if ( trans->load( tfn )) qApp->installTranslator( trans ); else delete trans; } QFile file( TimeZonePrivate::zoneFile() ); if ( !file.open( IO_ReadOnly ) ) { qWarning( "Unable to open %s", file.name().latin1() ); qWarning( "Timezone data must be installed at %s", TimeZonePrivate::zonePath().data() ); qApp->exit(1); return; } char line[2048]; TimeZoneLocation *tz = NULL; while ( !file.atEnd() ) { file.readLine( line, 2048 ); if ( line[0] == '#' ) continue; tz = new TimeZoneLocation( line ); if ( !tz->isValid() ) { qWarning("TimeZoneLocation::load Unable to parse line %s", (const char *)line ); delete tz; tz = NULL; continue; } store.insert( tz->id(), tz ); } file.close();}/******************************************************************* * * TzCache * *******************************************************************/class TzCache{public: static TzCache &instance(); TimeZoneData *data( const QCString &id ); TimeZoneLocation *location( const QCString &id ); QStrList ids();private: TzCache(); QAsciiDict< TimeZoneData > mDataDict; QAsciiDict< TimeZoneLocation > mLocationDict; static TzCache *sInstance;};TzCache *TzCache::sInstance = NULL;TzCache &TzCache::instance(){ if ( !sInstance ) sInstance = new TzCache(); return *sInstance;}TzCache::TzCache(){ // load the zone.tab file TimeZoneLocation::load( mLocationDict );}TimeZoneData *TzCache::data( const QCString &id ){ if ( id.isEmpty() ) return TimeZoneData::null; TimeZoneData *d = mDataDict.find( id ); if ( d ) return d; d = new TimeZoneData( id ); if ( !d->isValid() ) { qWarning("TimeZone::data Can't create a valid data object for '%s'", id.data() ); delete d; return TimeZoneData::null; } mDataDict.insert( id, d ); return d;}TimeZoneLocation *TzCache::location( const QCString &id ){ if ( id.isEmpty() ) return 0; TimeZoneLocation * l = mLocationDict[ id ];#if 0 if ( !l ) qDebug("TzCache::location unable to find %s ", id.data());#endif return l;}QStrList TzCache::ids(){ QStrList l; for ( QAsciiDictIterator<TimeZoneLocation> it( mLocationDict ); it.current(); ++it ) l.append( it.currentKey() ); return l;}/******************************************************************* * * TimeZone * *******************************************************************//*! \class TimeZone timezone.h \brief The TimeZone class provides access to time zone data. TimeZone provides access to timezone data and conversion between times in different time zones and formats. First availability: Qtopia 1.6 \ingroup qtopiaemb*//*! Construct an invalid time zone.*/TimeZone::TimeZone() : d( new TimeZonePrivate( 0 ) ){}/*! Construct a TimeZone for location \a locId.*/TimeZone::TimeZone( const char * locId ) : d( new TimeZonePrivate( locId ) ){}/*! Copy constructor.*/TimeZone::TimeZone( const TimeZone & copyFrom ) : d( new TimeZonePrivate( copyFrom.d->id ) ){}/*! Destruct TimeZone.*/TimeZone::~TimeZone(){ delete d; d = 0;}/*! Sets the current time zone id to \a id.*/void TimeZone::setId( const char *id ){ d->id = id;}/*! Assign \a from to this.*/TimeZone &TimeZone::operator=( const TimeZone &from){ d->id = from.d->id; return *this;}/*! Returns TRUE if \a c is equal to this, otherwise FALSE.*/bool TimeZone::operator==( const TimeZone &c) const{ return (d->id == c.d->id);}/*! Returns TRUE if \a c is not equal to this, otherwise FALSE.*/bool TimeZone::operator!=( const TimeZone &c) const{ return (d->id != c.d->id);}/*! \internal*/void TimeZone::dump() const{ TzCache::instance().data( d->id )->dump(); TzCache::instance().location( d->id )->dump();}/*! Return a time zone located at the UTC reference.*/TimeZone TimeZone::utc(){ return TimeZone("Europe/London");}/*! Return the UTC date and time.*/QDateTime TimeZone::utcDateTime(){ return TimeZonePrivate::setUtcTime( time(0) );}/*! Returns the date and time in this time zone from the number of seconds since 1 January 1970.*/QDateTime TimeZone::fromTime_t( uint secs ) const{ QDateTime utc = TimeZonePrivate::setUtcTime( secs ); return fromUtc( utc );}/*! Returns the date and time \a thisT as the number of seconds since 1 January 1970.*/uint TimeZone::toTime_t( const QDateTime &thisT ) const{ QDateTime utc = toUtc( thisT ); return TimeZonePrivate::toTime_t( utc );}/*! Returns the date and time \a thisT in this time zone as UTC.*/QDateTime TimeZone::toUtc( const QDateTime &thisT ) const{ TimeZoneData *data = TzCache::instance().data( d->id ); return data->toUtc( thisT );}/*! Returns the UTC date and time \a utc as the date and time in this time zone.*/QDateTime TimeZone::fromUtc( const QDateTime &utc ) const{ TimeZoneData *data = TzCache::instance().data( d->id ); return data->fromUtc( utc );}/*! Returns the date and time \a thisT in this time zone as the date and time in the current time zone.*/QDateTime TimeZone::toCurrent( const QDateTime &thisT ) const{ TimeZone curTz = current(); return curTz.convert( thisT, *this );}/*! Returns the date and time \a curT in the current time zone as the date and time in this time zone.*/QDateTime TimeZone::fromCurrent( const QDateTime &curT ) const{ TimeZone curTz = current(); return convert( curT, curTz );}/*! Return the date and time \a dt in time zone \a dtTz as the date and time int this time zone.*/QDateTime TimeZone::convert( const QDateTime &dt, const TimeZone &dtTz ) const{ QDateTime utc = dtTz.toUtc( dt ); return fromUtc( utc );}QCString lastZoneRead;QCString lastLocRead;/*! Returns the current system time zone.*/TimeZone TimeZone::current(){ QCString cZone; cZone = getenv("TZ");#ifdef Q_WS_MAC TimeZone env(cZone); if ( env.isValid() ) return env; time_t now = time(0); cZone = localtime(&now)->tm_zone; TimeZone lt(cZone); if ( lt.isValid() ) return lt; QFileInfo el("/etc/localtime"); QString zone = el.readLink(); int z = zone.find("/zoneinfo/"); if ( z >= 0 ) { TimeZone zi(zone.mid(z+10)); if ( zi.isValid() ) return zi; }#else QString currentLoc; if (lastLocRead.isEmpty() || lastZoneRead != cZone) { Config lconfig("locale"); lconfig.setGroup( "Location" ); currentLoc = lconfig.readEntry( "Timezone" ).latin1(); lastZoneRead = cZone; lastLocRead = currentLoc; } else { currentLoc = lastLocRead; } if ( !currentLoc.isEmpty() ) return TimeZone( currentLoc );#ifndef Q_OS_WIN32 qWarning("TimeZone::current Location information is not set in the Config file locale!");#endif // this is mainly for windows side code, in the initial case tzset(); QCString standardAbbrev, daylightAbbrev;#ifndef Q_OS_WIN32 standardAbbrev = tzname[0]; daylightAbbrev = tzname[1];#endif QDateTime today = QDateTime::currentDateTime(); QStrList allIds = TzCache::instance().ids(); for ( QStrListIterator it( allIds); it.current() && currentLoc.isEmpty(); ++it ) { QCString id = it.current(); //qDebug("\tchecking %s", id.data() ); TimeZoneData *data = TzCache::instance().data( id ); if ( data->match( today, timezone, daylight, standardAbbrev, daylightAbbrev ) ) currentLoc = it.current(); } if ( !currentLoc.isEmpty() ) { Config lconfig("locale"); lconfig.setGroup( "Location" ); lconfig.writeEntry( "Timezone", currentLoc.data() ); return TimeZone (currentLoc); }#endif return TimeZone();}/*! Return the time zone identifier, e.g. Europe/London*/QCString TimeZone::id() const{ return d->id;}/*! Returns TRUE if this is a valid time zone, otherwise FALSE.*/bool TimeZone::isValid() const{ TimeZoneData *data = TzCache::instance().data( d->id ); TimeZoneLocation *loc = TzCache::instance().location( d->id );#if 0 if ( data->isValid() && (!loc || !loc->isValid() )) { if ( !loc ) qWarning("data but no loc"); else { qWarning("invalid loc for valid data"); loc->dump(); } }#endif return data->isValid() && loc && loc->isValid();}/*! Returns the Daylight Savings Time (DST) time zone abbreviation.*/QCString TimeZone::dstAbbreviation() const{ TimeZoneData *data = TzCache::instance().data( d->id ); return data->dstAbbreviation();}/*! Returns the time zone abbreviation, e.g. EST*/QCString TimeZone::standardAbbreviation() const{ TimeZoneData *data = TzCache::instance().data( d->id ); return data->dstAbbreviation();}/*! Returns a list of the time zone ids.*/QStrList TimeZone::ids(){ return TzCache::instance().ids();}/*! Returns the latitude in seconds.*/int TimeZone::lat() const{ TimeZoneLocation *loc = TzCache::instance().location( d->id ); if ( loc ) return loc->lat(); return 0;}/*! Returns the longitude in seconds.*/int TimeZone::lon() const{ TimeZoneLocation *loc = TzCache::instance().location( d->id ); if ( loc ) return loc->lon(); return 0;}/*! Returns the latitude as a string. The format is: +-DDMM[SS]*/QCString TimeZone::latStr() const{ TimeZoneLocation *loc = TzCache::instance().location( d->id ); if ( loc ) return loc->latStr(); return "";}/*! Returns the longitude as a string. The format is: +-DDMM[SS]*/QCString TimeZone::lonStr() const{ TimeZoneLocation *loc = TzCache::instance().location( d->id ); if ( loc ) return loc->lonStr(); return "";}/*! Returns the translated description of this time zone.*/QString TimeZone::description() const{ TimeZoneLocation *loc = TzCache::instance().location( d->id ); if ( loc ) return loc->description(); return QString::null;}/*! Returns the translated area of this time zone, e.g. Europe.*/QString TimeZone::area() const{ TimeZoneLocation *loc = TzCache::instance().location( d->id ); if ( loc ) return loc->area(); return QString::null;}/*! Returns the translated city of this time zone, e.g. Oslo.*/QString TimeZone::city() const{ TimeZoneLocation *loc = TzCache::instance().location( d->id ); if ( loc ) return loc->city(); return QString::null;}/*! Returns the ISO 3166 2-character country code.*/QCString TimeZone::countryCode(){ TimeZoneLocation *loc = TzCache::instance().location( d->id ); if ( loc ) return loc->countryCode(); return "";}/*! \internal*/int TimeZone::distance( const TimeZone &e ) const{ TimeZoneLocation *loc = TzCache::instance().location( d->id ); TimeZoneLocation *comp = TzCache::instance().location( e.d->id ); if ( loc && comp ) return loc->distance( *comp ); return 0;}QDataStream &operator<<( QDataStream &o, const TimeZone &t ){ o << t.id(); return o;}QDataStream &operator>>( QDataStream &o, TimeZone &t ){ QCString id; o >> id; t.setId( id ); return o;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -