Important: Please read the Qt Code of Conduct -

SetTimeZoneInfo unable to read the timezone name for the next time

  • Hi All

    I have used SetTimeZoneInfo to change time zone of my system.

    The date and time is changed according to the timezone i have chosen.

    But , in the Timezone settings dialog box, it says "Your current time zone is not recognized. Please select a valid time zone."

    Because of this , i'm not able to read the system time zone for the next time.

    I tried changing in the registry too. even then i'm facing the same problem.

    The registry path i used is : "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation"

    and "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\TimeZoneInformation"*
    My code is here*
    QString RegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones";
    QSettings settings(RegKey, QSettings::NativeFormat);
    QStringList childKeys = settings.childGroups();
    int i = 0;
    QString strStdName;
    for (QStringList::iterator it = childKeys.begin();it != childKeys.end(); ++it)
    QString strtimeZone = RegKey + "\" + *it;
    QSettings settings1(strtimeZone, QSettings::NativeFormat);
    QString str = settings1.value("Display",0).toString();
    strStdName = settings1.value("Std",0).toString();
    if(str == strDisplayName)
    QString RegKey1 = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones";
    QString newRegKey = RegKey1 + "\" + strStdName;
    LPCWSTR lpString = newRegKey.utf16();
    if( RegOpenKey(HKEY_LOCAL_MACHINE,lpString,&hk) == ERROR_SUCCESS)
    dwType = REG_SZ;

                if( RegQueryValueEx(hk,L"TZI",0, &dwType, (BYTE *) &binary_data, &size) == ERROR_SUCCESS)
                    tz.Bias = binary_data.Bias;
                    tz.DaylightBias = binary_data.DaylightBias;
                    tz.DaylightDate = binary_data.DaylightDate;
                    tz.StandardBias = binary_data.StandardBias;
                    tz.StandardDate = binary_data.StandardDate;
                    QSettings settingsTimeZone(newRegKey, QSettings::NativeFormat);
                     muiStd = settingsTimeZone.value("MUI_Std").toString();
                     muiDlt= settingsTimeZone.value("MUI_Dlt",0).toString();
                     muiDisplay= settingsTimeZone.value("MUI_Display",0).toString();
    bStatus =  SetTimeZoneInformation(&tz);
    if( !SetTimeZoneInformation( &tz ) )
        qDebug("STZI failed (%s)\n", GetLastError());
        return 0;
    QSettings settingsTimeZoneCurrent("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation" , QSettings::NativeFormat);        
    QSettings settingsTimeZoneCurrent001("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\TimeZoneInformation" , QSettings::NativeFormat);        
    if(bStatus == false)
        DWORD dwErrorCode = GetLastError();
        return false;
    // Disable the privilege
    tkp.Privileges[0].Attributes = 0;
    AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);

    strDisplayName is the name of the TimeZone which will come from UI.

    Can anyone help me in this regard please.

    Thanks in advance.

    [Please wrap code in @ tags; mlong]

  • Hi Vignesh97 and all,

    I'm sorry it is a not a answer. I have to implement same thing as you done. Could you please say how I can use SetTimeZoneInformation within a Qt application (created by Qt Creator)? Which lib files should it be included to .Qt's pro file?

  • Use

    #include <windows.h>

    Hope it will help u.

  • It is required to add Advapi32.lib library to .pro file so that the registry functions of Win32 API can be recognised by Qt.

  • It seem that you miss two field data of "tz":
    @WCHAR StandardName[32];
    WCHAR DaylightName[32]; @

    @ bStatus = SetTimeZoneInformation(&tz);
    if( !SetTimeZoneInformation( &tz ) )
    qDebug("STZI failed (%s)\n", GetLastError());
    return 0;

    typedef struct _TIME_ZONE_INFORMATION {
    LONG Bias;
    WCHAR StandardName[32];
    SYSTEMTIME StandardDate;
    LONG StandardBias;
    WCHAR DaylightName[32];
    SYSTEMTIME DaylightDate;
    LONG DaylightBias;

Log in to reply