File: ktimezone_win.cpp

package info (click to toggle)
kde4libs 4%3A4.4.5-2%2Bsqueeze3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 88,360 kB
  • ctags: 94,980
  • sloc: cpp: 721,043; xml: 146,205; ansic: 8,395; java: 4,060; perl: 3,118; yacc: 2,058; sh: 1,458; python: 1,050; ruby: 715; lex: 248; asm: 166; jsp: 128; haskell: 116; f90: 99; ml: 75; erlang: 54; awk: 38; tcl: 29; lisp: 24; makefile: 23; php: 9
file content (568 lines) | stat: -rw-r--r-- 18,595 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
   This file is part of the KDE libraries
   Copyright (c) 2008 Marc Mutz <mutz@kde.org>, Till Adam <adam@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/


#include "ktimezone_win.h"
#include <config.h>

#include <kdebug.h>

#include <QStringList>
#include <QLibrary>

#include <windows.h>

#include <memory>
#include <string>
#include <cassert>

typedef BOOL (WINAPI *PtrTzSpecificLocalTimeToSystemTime )(LPTIME_ZONE_INFORMATION lpTimeZoneInformation,
                                                           LPSYSTEMTIME lpLocalTime,
                                                           LPSYSTEMTIME lpUniversalTime
);
static PtrTzSpecificLocalTimeToSystemTime pTzSpecificLocalTimeToSystemTime = 0;

namespace {
    class HKeyCloser {
        const HKEY hkey;
        Q_DISABLE_COPY( HKeyCloser )
    public:
        explicit HKeyCloser( HKEY hk ) : hkey( hk ) {}
        ~HKeyCloser() { RegCloseKey(  hkey ); }
    };

    struct TZI {
        LONG Bias;
        LONG StandardBias;
        LONG DaylightBias;
        SYSTEMTIME StandardDate;
        SYSTEMTIME DaylightDate;
    };
}

// TCHAR can be either uchar, or wchar_t:
#ifdef UNICODE

static inline QString tchar_to_qstring( const TCHAR * str ) {
    return QString::fromUtf16( reinterpret_cast<const ushort*>( str ) );
}

static inline const TCHAR * qstring_to_tchar( const QString& str ) {
    return reinterpret_cast<const TCHAR*>( str.utf16() );
}

static inline std::basic_string<TCHAR> qstring_to_tcharstring( const QString& str ) {
    return std::basic_string<TCHAR>( qstring_to_tchar(str) );
}

#else

static inline QString tchar_to_qstring( const TCHAR * str ) {
    return QString::fromLocal8Bit( str );
}

static inline const TCHAR * qstring_to_tchar( const QString& str ) {
    return str.toLocal8Bit().constData();
}

static inline std::basic_string<TCHAR> qstring_to_tcharstring( const QString& str ) {
    return std::basic_string<TCHAR>( qstring_to_tchar(str) );
}

#endif

static const TCHAR timeZonesKey[] = TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones");
static inline QDateTime systemtime_to_qdatetime( const SYSTEMTIME & st ) {
    return QDateTime( QDate( st.wYear, st.wMonth, st.wDay ),
                      QTime( st.wHour, st.wMinute, st.wSecond, st.wMilliseconds ) );
}

static SYSTEMTIME qdatetime_to_systemtime( const QDateTime & dt ) {
    const QDate d = dt.date();
    const QTime t = dt.time();
    const SYSTEMTIME st = {
        d.year(),
        d.month(),
        d.dayOfWeek() % 7, // 1..7 (Mon..Sun)->0..6(Sun..Sat)
        d.day(),
        t.hour(),
        t.minute(),
        t.second(),
        t.msec(),
    };
    return st;
}

static bool TzSpecificLocalTimeToSystemTime_Portable( TIME_ZONE_INFORMATION* tz,
                                                      SYSTEMTIME *i_stLocal,
                                                      SYSTEMTIME *o_stUniversal )
{

    // the method below was introduced in XP. If it's there, use it, otherwise
    // fall back to doing things manually
    if (!pTzSpecificLocalTimeToSystemTime) {
        QLibrary kernelLib(QLatin1String("kernel32"));
        pTzSpecificLocalTimeToSystemTime  = (PtrTzSpecificLocalTimeToSystemTime)kernelLib.resolve("TzSpecificLocalTimeToSystemTime");
    }

    if ( pTzSpecificLocalTimeToSystemTime )
        return pTzSpecificLocalTimeToSystemTime( tz, i_stLocal , o_stUniversal ) != 0;
    
    // the algorithm is:
    // - switch to the desired timezone temporarily
    // - convert system time to (local) file time in that timezone
    // - convert local file time to utc file time
    // - convert utc file time to system time
    // - reset timezone
    FILETIME ft, ft_utc;
    int result = 1;
    TIME_ZONE_INFORMATION currentTimeZone;
    result = GetTimeZoneInformation(&currentTimeZone);
    if ( result == TIME_ZONE_ID_INVALID ) {
        kWarning(161) << "Getting time zone information failed";
        return false;
    }
    result = SetTimeZoneInformation(tz);
    if ( result == 0 ) {
        kWarning(161) << "Setting temporary time zone failed";
        return false;
    }
    result = SystemTimeToFileTime(i_stLocal, &ft);
    if ( result == 0 ) {
        kWarning(161) << "SysteTimeToFileTime failed";
        return false;
    }
    result = LocalFileTimeToFileTime(&ft, &ft_utc);
    if ( result == 0 ) {
        kWarning(161) << "LocalFileTimeToFileTime failed";
        return false;
    }
    result = FileTimeToSystemTime(&ft_utc,o_stUniversal);
    if ( result == 0 ) {
        kWarning(161) << "FileTimeToSystemTime failed";
        return false;
    }
    result = SetTimeZoneInformation(&currentTimeZone);
    if ( result == 0 ) {
        kWarning(161) << "Re-setting time zone information failed";
        return false;
    }
    return true;
}




static bool get_binary_value( HKEY key, const TCHAR * value, void * data, DWORD numData, DWORD * outNumData=0 ) {
    DWORD size = numData;
    DWORD type = REG_BINARY;
    if ( RegQueryValueEx( key, value, 0, &type, (LPBYTE)data, &size ) != ERROR_SUCCESS )
        return false;
    assert( type == REG_BINARY );
    if (  type != REG_BINARY )
        return false;
    if ( outNumData )
        *outNumData = size;
    return true;
}

static bool get_string_value( HKEY key, const TCHAR * value, TCHAR * dest, DWORD destSizeInBytes ) {
    DWORD size = destSizeInBytes;
    DWORD type = REG_SZ;
    dest[0] = '\0';
    if ( RegQueryValueEx( key, value, 0, &type, (LPBYTE)dest, &size ) != ERROR_SUCCESS )
        return false;
    //dest[ qMin( size, destSizeInBytes - sizeof( WCHAR ) ) / sizeof( WCHAR ) ] = 0;
    assert( type == REG_SZ );
    if ( type != REG_SZ )
        return false;
    return true;
}

//
//
// Backend interface impl:
//
//

static bool check_prereq( const KTimeZone * caller, const QDateTime & dt, Qt::TimeSpec spec ) {
    return caller && caller->isValid() && dt.isValid() && dt.timeSpec() == spec ;
}

static inline bool check_local( const KTimeZone * caller, const QDateTime & dt ) {
    return check_prereq( caller, dt, Qt::LocalTime );
}

static inline bool check_utc( const KTimeZone * caller, const QDateTime & dt ) {
    return check_prereq( caller, dt, Qt::UTC );
}

static bool has_transition( const TIME_ZONE_INFORMATION & tz ) {
    return tz.StandardDate.wMonth != 0 && tz.DaylightDate.wMonth != 0 ;
}

static int win_dayofweek_to_qt_dayofweek( int wdow ) {
    // Sun(0)..Sat(6) -> Mon(1)...Sun(7)
    return wdow ? wdow : 7 ;
}

static int qt_dayofweek_to_win_dayofweek( int qdow ) {
    // Mon(1)...Sun(7) -> Sub(0)...Sat(6)
    return qdow % 7;
}

static QDate find_nth_weekday_in_month_of_year( int nth, int dayOfWeek, int month, int year ) {
    assert( nth >= 1 );
    assert( nth <= 5 );

    const QDate first( year, month, 1 );
    const int actualDayOfWeek = first.dayOfWeek();
    QDate candidate = first.addDays( ( nth - 1 ) * 7 + dayOfWeek - actualDayOfWeek );
    assert( candidate.dayOfWeek() == dayOfWeek );
    if ( nth == 5 )
        if ( candidate.month() != month )
            candidate = candidate.addDays( -7 );
    assert( candidate.month() == month );
    return candidate;
}

static QDateTime transition( const SYSTEMTIME & st, int year ) {
    assert( st.wYear == 0 );
    assert( st.wMonth != 0 );
    return QDateTime( find_nth_weekday_in_month_of_year( st.wDay, win_dayofweek_to_qt_dayofweek( st.wDayOfWeek ), st.wMonth, year ),
                      QTime( st.wHour, st.wMinute, st.wSecond, st.wMilliseconds ) );
}

struct Transitions {
    QDateTime stdStart, dstStart;
};

Transitions transitions( const TIME_ZONE_INFORMATION & tz, int year ) {
    const Transitions t = {
        transition( tz.StandardDate, year ), transition( tz.DaylightDate, year )
    };
    return t;
}


static const int MAX_KEY_LENGTH = 255;

static QStringList list_key( HKEY key ) {

    DWORD numSubKeys = 0;
    QStringList result;

    if ( RegQueryInfoKey( key, 0, 0, 0, &numSubKeys, 0, 0, 0, 0, 0, 0, 0 ) == ERROR_SUCCESS )
        for ( DWORD i = 0 ; i < numSubKeys ; ++i ) {
            TCHAR name[MAX_KEY_LENGTH+1];
            DWORD nameLen = MAX_KEY_LENGTH;
            if ( RegEnumKeyEx( key, i, name, &nameLen, 0, 0, 0, 0 ) == ERROR_SUCCESS )
                result.push_back( tchar_to_qstring( name ) );
        }

    return result;
}

static QStringList list_standard_names()
{
    QStringList standardNames;
    
    HKEY timeZones;
    QStringList keys;
    if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, timeZonesKey, 0, KEY_READ, &timeZones ) == ERROR_SUCCESS )
        keys = list_key(timeZones);

    std::basic_string<TCHAR> path( timeZonesKey );
    path += TEXT( "\\" );

    const HKeyCloser closer( timeZones );
    Q_FOREACH( const QString & keyname, keys ) {
    
        std::basic_string<TCHAR> keypath(path);
        keypath += qstring_to_tcharstring(keyname);
    HKEY key;
    if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, keypath.c_str(), 0, KEY_READ, &key ) != ERROR_SUCCESS ) {
        return standardNames; // FIXME what's the right error handling here?
    }

    const HKeyCloser closer( key );

    TIME_ZONE_INFORMATION tz;
    get_string_value( key, L"Std", tz.StandardName, sizeof( tz.StandardName ) );

    standardNames << tchar_to_qstring(tz.StandardName);
    }
    return standardNames;
}

static std::basic_string<TCHAR> pathFromZoneName(const KTimeZone& zone)
{
    std::basic_string<TCHAR> path( timeZonesKey );
    path += TEXT( "\\" );

    HKEY timeZones;
    QStringList keys;
    if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, timeZonesKey, 0, KEY_READ, &timeZones ) == ERROR_SUCCESS )
        keys = list_key(timeZones);

    const HKeyCloser closer( timeZones );
    Q_FOREACH( const QString & keyname, keys ) {
    
        std::basic_string<TCHAR> keypath(path);
        keypath += qstring_to_tcharstring(keyname);
    HKEY key;
    if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, keypath.c_str(), 0, KEY_READ, &key ) != ERROR_SUCCESS ) {
        return 0; // FIXME what's the right error handling here?
    }

    const HKeyCloser closer( key );

    TIME_ZONE_INFORMATION tz;
    get_string_value( key, L"Std", tz.StandardName, sizeof( tz.StandardName ) );

    if ( tchar_to_qstring(tz.StandardName) == zone.name() ) {
        return keypath;
    }
    }
    Q_ASSERT(false);

    return path;
}

/******************************************************************************/

class KSystemTimeZoneSourceWindowsPrivate
{
public:
    KSystemTimeZoneSourceWindowsPrivate() {}
    ~KSystemTimeZoneSourceWindowsPrivate() {}
};


class KSystemTimeZoneBackendWindows : public KTimeZoneBackend
{
public:
  KSystemTimeZoneBackendWindows(KTimeZoneSource *source, const QString &name)
  : KTimeZoneBackend(source, name) {}

  ~KSystemTimeZoneBackendWindows() {}

  KSystemTimeZoneBackendWindows *clone() const;

  QByteArray type() const;

  int offsetAtZoneTime(const KTimeZone *caller, const QDateTime &zoneDateTime, int *secondOffset) const;
  int offsetAtUtc(const KTimeZone *caller, const QDateTime &utcDateTime) const;
  int offset(const KTimeZone *caller, time_t t) const;
  bool isDstAtUtc(const KTimeZone *caller, const QDateTime &utcDateTime) const;
  bool isDst(const KTimeZone *caller, time_t t) const;
};

class KSystemTimeZoneDataWindows : public KTimeZoneData
{
public:
  KSystemTimeZoneDataWindows()
  :KTimeZoneData()
  {

  }
  TIME_ZONE_INFORMATION _tzi;
  QString displayName;

  const TIME_ZONE_INFORMATION & tzi( int year = 0 ) const { Q_UNUSED( year ); return _tzi; }
};

KSystemTimeZoneSourceWindows::KSystemTimeZoneSourceWindows()
:d( new KSystemTimeZoneSourceWindowsPrivate )
{
}

KTimeZoneData* KSystemTimeZoneSourceWindows::parse(const KTimeZone &zone) const
{
    KSystemTimeZoneDataWindows* data = new KSystemTimeZoneDataWindows();

    std::basic_string<TCHAR> path = pathFromZoneName(zone);

    HKEY key;
    if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, path.c_str(), 0, KEY_READ, &key ) != ERROR_SUCCESS ) {
        delete data;
        return 0; // FIXME what's the right error handling here?
    }

    const HKeyCloser closer( key );

    TZI tzi = { 0 };

    if ( !get_binary_value( key, TEXT( "TZI" ), &tzi, sizeof( TZI ) ) ) {
        delete data;
        return 0; // ?
    }

    get_string_value( key, L"Std", data->_tzi.StandardName, sizeof( data->_tzi.StandardName ) );
    get_string_value( key, L"Dlt", data->_tzi.DaylightName, sizeof( data->_tzi.DaylightName ) );

    TCHAR display[512];
    get_string_value( key, L"Display", display, sizeof( display ) );
    data->displayName = tchar_to_qstring( display );

#define COPY( name ) data->_tzi.name = tzi.name
    COPY( Bias );
    COPY( StandardBias );
    COPY( StandardDate );
    COPY( DaylightBias );
    COPY( DaylightDate );
#undef COPY

    return data;
}

Transitions transitions( const KTimeZone * caller, int year ) {
    return transitions( static_cast<const KSystemTimeZoneDataWindows*>( caller->data(true) )->tzi( year ), year );
}

static bool is_dst( const TIME_ZONE_INFORMATION & tzi, const QDateTime & utc, int year ) {
    if ( !has_transition( tzi ) )
        return false;
    const Transitions trans = transitions( tzi, year );
    if ( trans.stdStart < trans.dstStart )
        return trans.dstStart <= utc || utc < trans.stdStart ;
    else
        return trans.dstStart <= utc && utc < trans.stdStart ;
}

static bool is_dst( const KTimeZone * caller, const QDateTime & utc ) {
    assert( caller );
    assert( caller->isValid() );
    const int year = utc.date().year();
    const TIME_ZONE_INFORMATION & tzi = static_cast<const KSystemTimeZoneDataWindows*>( caller->data(true) )->tzi( year );
    return is_dst( tzi, utc, year );
}

static int effective_offset( const TIME_ZONE_INFORMATION& tz, bool isDst ) {
    int bias = tz.Bias;
    if ( has_transition( tz ) )
        if ( isDst )
            bias += tz.DaylightBias;
        else
            bias += tz.StandardBias;
    return bias * -60; // min -> secs
}

static int offset_at_utc( const KTimeZone * caller, const QDateTime & utc ) {
    assert( caller );
    assert( caller->isValid() );
    const int year = utc.date().year();
    const TIME_ZONE_INFORMATION & tz = static_cast<const KSystemTimeZoneDataWindows*>( caller->data(true) )->tzi( year );
    return effective_offset( tz, is_dst( tz, utc, year ) );
}

static const int OneHour = 3600; //sec

static int difference( const SYSTEMTIME & st1, const SYSTEMTIME & st2 ) {
    return systemtime_to_qdatetime( st1 ).secsTo( systemtime_to_qdatetime( st2 ) );
}

static int offset_at_zone_time( const KTimeZone * caller, const SYSTEMTIME & zone, int * secondOffset ) {
    assert( caller );
    assert( caller->isValid() );
    assert(caller->data(true));
    const KSystemTimeZoneDataWindows * const data = static_cast<const KSystemTimeZoneDataWindows*>( caller->data(true) );
    const TIME_ZONE_INFORMATION & tz = data->tzi( zone.wYear );
    SYSTEMTIME utc;
    if ( !TzSpecificLocalTimeToSystemTime_Portable( const_cast<LPTIME_ZONE_INFORMATION>( &tz ), const_cast<LPSYSTEMTIME>( &zone ), &utc ) )
        return 0;
    const bool isDst = is_dst( tz, systemtime_to_qdatetime( utc ), utc.wYear );
    int result = effective_offset( tz, isDst );
    if ( secondOffset ) {
        const SYSTEMTIME utcplus1 = qdatetime_to_systemtime( systemtime_to_qdatetime( utc ).addSecs( OneHour ) );
        const SYSTEMTIME utcminus1 = qdatetime_to_systemtime( systemtime_to_qdatetime( utc ).addSecs( -OneHour ) );
        SYSTEMTIME zoneplus1, zoneminus1;
        if ( !SystemTimeToTzSpecificLocalTime( const_cast<LPTIME_ZONE_INFORMATION>( &tz ), const_cast<LPSYSTEMTIME>( &utcplus1 ), &zoneplus1 ) ||
             !SystemTimeToTzSpecificLocalTime( const_cast<LPTIME_ZONE_INFORMATION>( &tz ), const_cast<LPSYSTEMTIME>( &utcminus1 ), &zoneminus1 ) )
            return result;
        if ( difference( zoneminus1, zone ) != OneHour ||
             difference( zone, zoneplus1 ) != OneHour )
        {
            *secondOffset = effective_offset( tz, !isDst );
            if ( result < *secondOffset )
                qSwap( result, *secondOffset );
        }
    }
    return result;
}



KSystemTimeZoneBackendWindows * KSystemTimeZoneBackendWindows::clone() const
{
    return new KSystemTimeZoneBackendWindows(*this);
}

QByteArray KSystemTimeZoneBackendWindows::type() const
{
    return "KSystemTimeZoneWindows";
}

int KSystemTimeZoneBackendWindows::offsetAtZoneTime(const KTimeZone *caller, const QDateTime &zoneDateTime, int *secondOffset) const
{
    if (!caller->isValid()  ||  !zoneDateTime.isValid()  ||  zoneDateTime.timeSpec() != Qt::LocalTime)
        return 0;
    if ( !check_local( caller, zoneDateTime ) )
        return 0;

    return offset_at_zone_time( caller, qdatetime_to_systemtime( zoneDateTime ), secondOffset );
}

int KSystemTimeZoneBackendWindows::offsetAtUtc(const KTimeZone *caller, const QDateTime &utcDateTime) const
{
    if (!caller->isValid()  ||  !utcDateTime.isValid())
        return 0;
    if ( !check_utc( caller, utcDateTime ) )
        return 0;
    return offset_at_utc( caller, utcDateTime );
}

int KSystemTimeZoneBackendWindows::offset(const KTimeZone *caller, time_t t) const
{
    if (!caller->isValid()  ||  t == KTimeZone::InvalidTime_t)
        return 0;
    return offsetAtUtc( caller, KTimeZone::fromTime_t( t ) );
}

bool KSystemTimeZoneBackendWindows::isDstAtUtc(const KTimeZone *caller, const QDateTime &utcDateTime) const
{
    return check_utc( caller, utcDateTime ) && is_dst( caller, utcDateTime );
}


bool KSystemTimeZoneBackendWindows::isDst(const KTimeZone *caller, time_t t) const
{
    return isDstAtUtc( caller, KTimeZone::fromTime_t( t ) );
}

KSystemTimeZoneWindows::KSystemTimeZoneWindows(KTimeZoneSource *source, const QString &name)
: KTimeZone(new KSystemTimeZoneBackendWindows(source, name))
{}

QStringList KSystemTimeZoneWindows::listTimeZones() 
{
    return list_standard_names();
}