Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to use GetTimeZoneInformation

How to use GetTimeZoneInformation

Scheduled Pinned Locked Moved C / C++ / MFC
questionjsonhelptutorial
20 Posts 3 Posters 0 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.
  • D dan o

    yes... like where strInput = "-09:00" CString strTest; int i; for(i=0;i<=m_pszTimeZones.GetUpperBound();i++) { strTest = m_pszTimeZones.GetAt(i); strTest = strTest.Mid(4,6); if ( strTest == strInput) break; }

    C Offline
    C Offline
    CodingLover
    wrote on last edited by
    #11

    Ok, thanks. I'll check it and let you know.

    I appreciate your help all the time... Eranga :)

    1 Reply Last reply
    0
    • D dan o

      yes... like where strInput = "-09:00" CString strTest; int i; for(i=0;i<=m_pszTimeZones.GetUpperBound();i++) { strTest = m_pszTimeZones.GetAt(i); strTest = strTest.Mid(4,6); if ( strTest == strInput) break; }

      C Offline
      C Offline
      CodingLover
      wrote on last edited by
      #12

      Hey dan o, your code works fine. Where I'm going wrong is the use of my buffer. I'm not sure what exactly happened, but the changing of the buffer size for different time zones gives an error. Thanks buddy, At the same time, I'm try to do another thing. Say I want to get both display name and standard zone name. How can I do that through the above your code.

      I appreciate your help all the time... Eranga :)

      D 1 Reply Last reply
      0
      • C CodingLover

        Hey dan o, your code works fine. Where I'm going wrong is the use of my buffer. I'm not sure what exactly happened, but the changing of the buffer size for different time zones gives an error. Thanks buddy, At the same time, I'm try to do another thing. Say I want to get both display name and standard zone name. How can I do that through the above your code.

        I appreciate your help all the time... Eranga :)

        D Offline
        D Offline
        dan o
        wrote on last edited by
        #13

        ok, anyway just closing this item now... just make a new stringarray CStringArray m_pszTimeZonesNames if (iRet == ERROR_SUCCESS) { ZeroMemory(&buffer, sizeof(buffer)); dwSize = sizeof(buffer); //again iRet = RegQueryValueEx(Key, _T("Std"), NULL, &dwType, (BYTE *)buffer, &dwSize); if (iRet == ERROR_SUCCESS) { strValue.Format(_T("%s"), buffer); m_pszTimeZonesNames.Add( strValue ); } have a nice day

        C 1 Reply Last reply
        0
        • D dan o

          ok, anyway just closing this item now... just make a new stringarray CStringArray m_pszTimeZonesNames if (iRet == ERROR_SUCCESS) { ZeroMemory(&buffer, sizeof(buffer)); dwSize = sizeof(buffer); //again iRet = RegQueryValueEx(Key, _T("Std"), NULL, &dwType, (BYTE *)buffer, &dwSize); if (iRet == ERROR_SUCCESS) { strValue.Format(_T("%s"), buffer); m_pszTimeZonesNames.Add( strValue ); } have a nice day

          C Offline
          C Offline
          CodingLover
          wrote on last edited by
          #14

          That's what I have tried. But when I add a CStringArray and compile its ok, but I can't run the code. When I run following exception given,

          Unhandled exception at 0x00414ff3 in TimeZone.exe: 0xC0000005: Access violation writing location 0x00000020.

          on following line of code.

          m_pMainWnd = &dlg;

          I have no idea about that. Even on CStringArray documentation there is no such explanation.

          I appreciate your help all the time... Eranga :)

          D 1 Reply Last reply
          0
          • C CodingLover

            That's what I have tried. But when I add a CStringArray and compile its ok, but I can't run the code. When I run following exception given,

            Unhandled exception at 0x00414ff3 in TimeZone.exe: 0xC0000005: Access violation writing location 0x00000020.

            on following line of code.

            m_pMainWnd = &dlg;

            I have no idea about that. Even on CStringArray documentation there is no such explanation.

            I appreciate your help all the time... Eranga :)

            D Offline
            D Offline
            dan o
            wrote on last edited by
            #15

            i wonder if you did exactly as below .h m_pszTimeZones m_pszTimeZonesNames .cpp void ::TestTZ() { HKEY hKey, Key; DWORD dwType = REG_SZ; int iRet = 0; CString strName, strValue, strPath, strKey; strPath = _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones"); iRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strPath, 0, KEY_READ, &hKey); if (iRet != ERROR_SUCCESS) return; TCHAR achKey[MAX_PATH]; TCHAR achClass[MAX_PATH] = _T(""); DWORD cchClassName = MAX_PATH; DWORD cSubKeys; DWORD cbMaxSubKey; DWORD cchMaxClass; DWORD cValues; DWORD cchMaxValue; DWORD cbMaxValueData; DWORD cbSecurityDescriptor; FILETIME ftLastWriteTime; DWORD i; DWORD retCode; TCHAR buffer[MAX_PATH]; DWORD dwSize = sizeof(buffer); // Get the class name and the value count. RegQueryInfoKey(hKey, // key handle achClass, // buffer for class name &cchClassName, // length of class string NULL, // reserved &cSubKeys, // number of subkeys &cbMaxSubKey, // longest subkey size &cchMaxClass, // longest class string &cValues, // number of values for this key &cchMaxValue, // longest value name &cbMaxValueData, // longest value data &cbSecurityDescriptor, // security descriptor &ftLastWriteTime); // last write time // Enumerate the child keys, until RegEnumKeyEx fails. Then // get the name of each child key and copy it into the list box. for (i = 0, retCode = ERROR_SUCCESS;retCode == ERROR_SUCCESS; i++) { ZeroMemory(&achKey, sizeof(achKey)); cbMaxSubKey = sizeof(achKey); retCode = RegEnumKeyEx(hKey, i, achKey, &cbMaxSubKey, NULL, NULL, NULL, &ftLastWriteTime); if (retCode == (DWORD)ERROR_SUCCESS) { strName.Format( _T("%s"), achKey); strKey = strPath + "\\" + strName; iRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKey, 0, KEY_READ, &Key); if (iRet == ERROR_SUCCESS) { ZeroMemory(&buffer, sizeof(buffer)); dwSize = sizeof(buffer); //again iRet = RegQueryValueEx(Key, _T("Display"), NULL, &dwType, (BYTE *)buffer, &dwSize); if (iRet == ERROR_SUCCESS) { strValue.Format(_T("%s"), buffer); m_pszTimeZones.Add( strValue ); } if (iRet == ERROR_SUCCESS)

            C 1 Reply Last reply
            0
            • D dan o

              i wonder if you did exactly as below .h m_pszTimeZones m_pszTimeZonesNames .cpp void ::TestTZ() { HKEY hKey, Key; DWORD dwType = REG_SZ; int iRet = 0; CString strName, strValue, strPath, strKey; strPath = _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones"); iRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strPath, 0, KEY_READ, &hKey); if (iRet != ERROR_SUCCESS) return; TCHAR achKey[MAX_PATH]; TCHAR achClass[MAX_PATH] = _T(""); DWORD cchClassName = MAX_PATH; DWORD cSubKeys; DWORD cbMaxSubKey; DWORD cchMaxClass; DWORD cValues; DWORD cchMaxValue; DWORD cbMaxValueData; DWORD cbSecurityDescriptor; FILETIME ftLastWriteTime; DWORD i; DWORD retCode; TCHAR buffer[MAX_PATH]; DWORD dwSize = sizeof(buffer); // Get the class name and the value count. RegQueryInfoKey(hKey, // key handle achClass, // buffer for class name &cchClassName, // length of class string NULL, // reserved &cSubKeys, // number of subkeys &cbMaxSubKey, // longest subkey size &cchMaxClass, // longest class string &cValues, // number of values for this key &cchMaxValue, // longest value name &cbMaxValueData, // longest value data &cbSecurityDescriptor, // security descriptor &ftLastWriteTime); // last write time // Enumerate the child keys, until RegEnumKeyEx fails. Then // get the name of each child key and copy it into the list box. for (i = 0, retCode = ERROR_SUCCESS;retCode == ERROR_SUCCESS; i++) { ZeroMemory(&achKey, sizeof(achKey)); cbMaxSubKey = sizeof(achKey); retCode = RegEnumKeyEx(hKey, i, achKey, &cbMaxSubKey, NULL, NULL, NULL, &ftLastWriteTime); if (retCode == (DWORD)ERROR_SUCCESS) { strName.Format( _T("%s"), achKey); strKey = strPath + "\\" + strName; iRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKey, 0, KEY_READ, &Key); if (iRet == ERROR_SUCCESS) { ZeroMemory(&buffer, sizeof(buffer)); dwSize = sizeof(buffer); //again iRet = RegQueryValueEx(Key, _T("Display"), NULL, &dwType, (BYTE *)buffer, &dwSize); if (iRet == ERROR_SUCCESS) { strValue.Format(_T("%s"), buffer); m_pszTimeZones.Add( strValue ); } if (iRet == ERROR_SUCCESS)

              C Offline
              C Offline
              CodingLover
              wrote on last edited by
              #16

              Oops, it's work. I have to clear the array after certain time I think. Do this after some time of use the array.

              m_pszTimeZones.RemoveAll();

              Now seems it is work. Another thing also tested by me. On comparing of user input with array element, if the user input is not found what happened.

              for(i=0;i<=m\_pszTimeZones.GetUpperBound();i++)
              {
              	strTest = m\_pszTimeZones.GetAt(i);
              	strTemp = m\_pszTimeZones.GetAt(i);
              	stdTT = m\_pszNames.GetAt(i);
              	strTest = strTest.Mid(4,6);
              	if ( strTest == strIn)
              	{
              		// Here find the correct zone name
              		SetDetails(strTemp);
              		SetDlgItemText(IDC\_STD, stdTT);
              		break;
              	}
              	else
              	{
              		AfxMessageBox("Error");
              		break;
              	}
              }
              

              This is what I have tried. Because of the second break it wont iterate. If removed the second break, get the error message until it is found. How should I avoid it.

              I appreciate your help all the time... Eranga :)

              D 1 Reply Last reply
              0
              • C CodingLover

                Oops, it's work. I have to clear the array after certain time I think. Do this after some time of use the array.

                m_pszTimeZones.RemoveAll();

                Now seems it is work. Another thing also tested by me. On comparing of user input with array element, if the user input is not found what happened.

                for(i=0;i<=m\_pszTimeZones.GetUpperBound();i++)
                {
                	strTest = m\_pszTimeZones.GetAt(i);
                	strTemp = m\_pszTimeZones.GetAt(i);
                	stdTT = m\_pszNames.GetAt(i);
                	strTest = strTest.Mid(4,6);
                	if ( strTest == strIn)
                	{
                		// Here find the correct zone name
                		SetDetails(strTemp);
                		SetDlgItemText(IDC\_STD, stdTT);
                		break;
                	}
                	else
                	{
                		AfxMessageBox("Error");
                		break;
                	}
                }
                

                This is what I have tried. Because of the second break it wont iterate. If removed the second break, get the error message until it is found. How should I avoid it.

                I appreciate your help all the time... Eranga :)

                D Offline
                D Offline
                dan o
                wrote on last edited by
                #17

                BOOL bFound = FALSE; for(i=0;i<=m_pszTimeZones.GetUpperBound();i++) { strTest = m_pszTimeZones.GetAt(i); strTemp = m_pszTimeZones.GetAt(i); stdTT = m_pszNames.GetAt(i); strTest = strTest.Mid(4,6); if ( strTest == strIn) { // Here find the correct zone name SetDetails(strTemp); SetDlgItemText(IDC_STD, stdTT); bFound = TRUE; break; } } if (!bFound) { AfxMessageBox("Error, No Found"); }

                C 1 Reply Last reply
                0
                • D dan o

                  BOOL bFound = FALSE; for(i=0;i<=m_pszTimeZones.GetUpperBound();i++) { strTest = m_pszTimeZones.GetAt(i); strTemp = m_pszTimeZones.GetAt(i); stdTT = m_pszNames.GetAt(i); strTest = strTest.Mid(4,6); if ( strTest == strIn) { // Here find the correct zone name SetDetails(strTemp); SetDlgItemText(IDC_STD, stdTT); bFound = TRUE; break; } } if (!bFound) { AfxMessageBox("Error, No Found"); }

                  C Offline
                  C Offline
                  CodingLover
                  wrote on last edited by
                  #18

                  Hmm, thanks it works. So, it's better to rate your advice to use of CStringArray. :)

                  I appreciate your help all the time... Eranga :)

                  1 Reply Last reply
                  0
                  • D dan o

                    i will use something like this: i will store all zones into m_pszTimeZones and after that it is easy to get information you want .h CStringArray m_pszTimeZones; .cpp void ::TestTM { HKEY hKey, Key; DWORD dwType = REG_SZ; int iRet = 0; CString strName, strValue, strPath, strKey; strPath = _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones"); iRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strPath, 0, KEY_READ, &hKey); if (iRet != ERROR_SUCCESS) return; TCHAR achKey[MAX_PATH]; TCHAR achClass[MAX_PATH] = _T(""); DWORD cchClassName = MAX_PATH; DWORD cSubKeys; DWORD cbMaxSubKey; DWORD cchMaxClass; DWORD cValues; DWORD cchMaxValue; DWORD cbMaxValueData; DWORD cbSecurityDescriptor; FILETIME ftLastWriteTime; DWORD i; DWORD retCode; TCHAR buffer[MAX_PATH]; DWORD dwSize = sizeof(buffer); // Get the class name and the value count. RegQueryInfoKey(hKey, // key handle achClass, // buffer for class name &cchClassName, // length of class string NULL, // reserved &cSubKeys, // number of subkeys &cbMaxSubKey, // longest subkey size &cchMaxClass, // longest class string &cValues, // number of values for this key &cchMaxValue, // longest value name &cbMaxValueData, // longest value data &cbSecurityDescriptor, // security descriptor &ftLastWriteTime); // last write time // Enumerate the child keys, until RegEnumKeyEx fails. Then // get the name of each child key and copy it into the list box. for (i = 0, retCode = ERROR_SUCCESS;retCode == ERROR_SUCCESS; i++) { ZeroMemory(&achKey, sizeof(achKey)); cbMaxSubKey = sizeof(achKey); retCode = RegEnumKeyEx(hKey, i, achKey, &cbMaxSubKey, NULL, NULL, NULL, &ftLastWriteTime); if (retCode == (DWORD)ERROR_SUCCESS) { strName.Format( _T("%s"), achKey); strKey = strPath + "\\" + strName; iRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKey, 0, KEY_READ, &Key); if (iRet == ERROR_SUCCESS) { ZeroMemory(&buffer, sizeof(buffer)); dwSize = sizeof(buffer); //again iRet = RegQueryValueEx(Key, _T("Display"), NULL, &dwType, (BYTE *)buffer, &dwSize);

                    C Offline
                    C Offline
                    CodingLover
                    wrote on last edited by
                    #19

                    Hi all, After completing the time zone application now I'm going to workout a similar application. Say I have a folder which contain large number of files. I want to read that folder and collect some information on those files. In details view of a folder we can see that lots of details are available. eg: File Name, Size, Date Created, Date Modified and many more. I want to read a specific folder and collect those details. So from where I should start this work.

                    I appreciate your help all the time... Eranga :)

                    D 1 Reply Last reply
                    0
                    • C CodingLover

                      Hi all, After completing the time zone application now I'm going to workout a similar application. Say I have a folder which contain large number of files. I want to read that folder and collect some information on those files. In details view of a folder we can see that lots of details are available. eg: File Name, Size, Date Created, Date Modified and many more. I want to read a specific folder and collect those details. So from where I should start this work.

                      I appreciate your help all the time... Eranga :)

                      D Offline
                      D Offline
                      dan o
                      wrote on last edited by
                      #20

                      perhaps next code snippet helps you CStringArray m_pszFileNames void ::TestFile() { struct _finddata_t g_file; long hFile, lTime; CString strName, strPath, strCurrentPath; m_pszFileNames.RemoveAll(); strPath = "c:\testpath"; /* Get the current working directory: */ char buffer[_MAX_PATH]; _getcwd( buffer, _MAX_PATH ); strCurrentPath.Format( _T("%s"), buffer); _tchdir(strPath); if( (hFile = _findfirst( "*.*", &g_file )) == -1L ) { printf( "No files in current directory!\n" ); } else { strName = g_file.name; m_pszFileNames.Add(strName); /* Find the rest of the *.* files */ while( _findnext( hFile, &g_file ) == 0 ) { strName = g_file.name; m_pszFileNames.Add(strName); lTime = g_file.time_write; } _findclose( hFile ); } _tchdir(strCurrentPath); }

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups