Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SetTimeZoneInfo unable to read the timezone name for the next time
Qt 6.11 is out! See what's new in the release blog

SetTimeZoneInfo unable to read the timezone name for the next time

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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();
                }
                break;
            }
    
         }
     }
    RegCloseKey(hk);
    
    
    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);        
    
    settingsTimeZoneCurrent.setValue("TimeZoneKeyName",strStdName);
    settingsTimeZoneCurrent.setValue("StandardName",muiStd);
    settingsTimeZoneCurrent.setValue("DaylightName",muiDlt);
    settingsTimeZoneCurrent001.setValue("TimeZoneKeyName",strStdName);
    settingsTimeZoneCurrent001.setValue("StandardName",muiStd);
    settingsTimeZoneCurrent001.setValue("DaylightName",muiDlt);
    
    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]

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      yaseminyilmaz
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Use

        #include <windows.h>

        Hope it will help u.

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          yaseminyilmaz
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • R Offline
            R Offline
            raidsan
            wrote on last edited by
            #5

            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;
            } TIME_ZONE_INFORMATION
            @

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved