Skip to content

Instantly share code, notes, and snippets.

@gabonator
Created April 26, 2012 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabonator/2499776 to your computer and use it in GitHub Desktop.
Save gabonator/2499776 to your computer and use it in GitHub Desktop.
Timezones export
// TimeZonesDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TimeZonesMFC.h"
#include "TimeZoneDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CTimeZonesDlg dialog
CTimeZonesDlg::CTimeZonesDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTimeZonesDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTimeZonesDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CTimeZonesDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON1, &CTimeZonesDlg::OnBnClickedButton1)
END_MESSAGE_MAP()
// CTimeZonesDlg message handlers
BOOL CTimeZonesDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTimeZonesDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTimeZonesDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
// Days since Sunday 0-6
// Based on Zeller
// ASSERT Year>1, Month>=1 && <=12.
int GetFirstDayInMonth(unsigned int Year, unsigned int Month)
{
const int day=1;
if (Month < 3)
{
Month +=12;
Year -= 1;
}
return ((day+1+(Month*2)+(int)((Month+1)*3/5)+Year+(int)(Year/4)-
(int)(Year/100)+(int)(Year/400))%7);
}
int GetDaysInMonth(int nYear, int nMonth)
{
int nTable[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (nMonth!=2)
return nTable[nMonth];
if (nMonth == 2)
if ((nYear%4) == 0)
if ((nYear%100) == 0)
{
if ((nYear%400) == 0)
return 29;
} else
return 29;
return nTable[nMonth];
}
void GetRealDate(SYSTEMTIME &d)
{
if (d.wMonth == 0)
return;
int n = GetDaysInMonth(2007, d.wMonth);
int i;
int dy = GetFirstDayInMonth(2007, d.wMonth);
int occ = 0;
int nRet = 0;
_ASSERT(d.wDay != 0);
for (i=1; i<=n; i++)
{
if (dy==d.wDayOfWeek)
{
nRet = i;
occ++;
if (occ==d.wDay)
break;
}
dy++;
dy%=7;
}
dy = GetFirstDayInMonth(2007, d.wMonth);
// iny algoritmus:
// - zistime prvy den v mesiaci kedy sedi, 1. = dy, chceme d.wDay
int nFirst = 1 + d.wDayOfWeek - dy;
if ( nFirst < 1 )
nFirst += 7;
nFirst += 7*(d.wDay-1);
while (nFirst > n)
nFirst -= 7;
_ASSERT(nFirst == nRet);
//d.wDay = nRet;
}
void CTimeZonesDlg::OnBnClickedButton1()
{
struct TZIStruct {
LONG Bias;
LONG StandardBias;
LONG DaylightBias;
SYSTEMTIME StandardDate;
SYSTEMTIME DaylightDate;
};
struct Zone {
CString strDisplay;
TZIStruct TZI;
CString strGMTZone;
Zone()
{
}
Zone& operator=(Zone &entry )
{
strGMTZone = entry.strGMTZone;
strDisplay = entry.strDisplay;
TZI = entry.TZI;
return *this;
}
};
CArray <CString, CString> arrFolders;
CArray <Zone, Zone> arrZones;
HKEY key;
if ( RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_ALL_ACCESS, &key) == ERROR_SUCCESS )
{
DWORD dwIndex = 0;
DWORD nLen = 255;
TCHAR Name[256];
while ( RegEnumKeyEx( key, dwIndex, Name, &nLen, NULL, NULL, NULL, NULL) == ERROR_SUCCESS )
{
dwIndex++;
arrFolders.Add( CString(Name) );
nLen = 255;
}
}
RegCloseKey(key);
CString strT;
for (int i=0; i<arrFolders.GetSize(); i++)
{
CString strKey = _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\") + arrFolders[i]; // + _T("\\Display");
if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKey, 0, KEY_ALL_ACCESS, &key) != ERROR_SUCCESS )
_ASSERT(0);
DWORD dwType = 0;
DWORD dwDataSize = 127;
TCHAR chLoad[128];
Zone z;
if (RegQueryValueEx(key, _T("TZI"), 0, &dwType, (PBYTE)chLoad, &dwDataSize)==ERROR_SUCCESS)
{
memcpy(&z.TZI, chLoad, sizeof(TZIStruct));
}
dwDataSize=127;
if (RegQueryValueEx(key, _T("Display"), 0, &dwType, (PBYTE)chLoad, &dwDataSize)==ERROR_SUCCESS)
{
CString strDisplay(chLoad);
CString strGMT;
if (strDisplay[10]==')')
{
strGMT = strDisplay.Mid(4, 6);
strDisplay = strDisplay.Mid(12);
} else
{
strGMT = "+00:00";
strDisplay = strDisplay.Mid(6);
}
z.strDisplay = strDisplay;
z.strGMTZone = strGMT;
}
RegCloseKey(key);
arrZones.Add(z);
}
// find duplicate zones
CArray <CString, CString> arrUsedZonesS;
CArray <int, int> arrUsedZonesI;
for (int i=0; i<arrZones.GetSize(); i++)
{
bool bFound = false;
for (int j=0; j<arrUsedZonesS.GetSize(); j++)
if ( arrUsedZonesS[j].Compare(arrZones[i].strGMTZone) == 0 )
{
arrUsedZonesI.SetAt(j, 0);
bFound = true;
break;
}
if (bFound)
continue;
arrUsedZonesS.Add(arrZones[i].strGMTZone);
arrUsedZonesI.Add(-1);
}
CFile f;
f.Open( _T("output.txt"), CFile::modeCreate | CFile::modeWrite );
for (int i=0; i<arrZones.GetSize(); i++)
{
// TimeZoneInfoToTimeZoneInformation(TZI2, TZI);
SYSTEMTIME ss = arrZones[i].TZI.StandardDate;
SYSTEMTIME sd = arrZones[i].TZI.DaylightDate;
/*
FILETIME fs;
FILETIME fd;
SystemTimeToFileTime( &ss, &fs );
SystemTimeToFileTime( &sd, &fd );
FileTimeToSystemTime( &fs, &ss );
FileTimeToSystemTime( &fd, &sd );
*/
CString strS;
CString strD;
CString strDisp = arrZones[i].strDisplay;
CString strGMT = arrZones[i].strGMTZone;
BOOL bNoTZI = false;
if (ss.wDay == 0 )
{
_ASSERT(ss.wDayOfWeek == 0);
_ASSERT(sd.wDayOfWeek == 0);
_ASSERT(ss.wMonth == 0);
_ASSERT(sd.wMonth == 0);
bNoTZI = true;
} else
{
_ASSERT(ss.wYear == 0);
_ASSERT(sd.wYear == 0);
_ASSERT(ss.wDay > 0 && ss.wDay <= 5);
_ASSERT(sd.wDay > 0 && sd.wDay <= 5);
strS.Format(_T("%d%d%02d%02d%02d"), ss.wDay, ss.wDayOfWeek, ss.wMonth, ss.wHour, ss.wMinute);
strD.Format(_T("%d%d%02d%02d%02d"), sd.wDay, ss.wDayOfWeek, sd.wMonth, sd.wHour, sd.wMinute);
_ASSERT(strS.GetLength() == 8);
_ASSERT(strD.GetLength() == 8);
bNoTZI = false;
}
CString strLine;
/*
strLine.Format(_T("standard=%d\x9 daylight=%d\x9 standardDate=%s\x9 daylightDate=%s\x09 %s\n"),
arrZones[i].TZI.StandardBias,
arrZones[i].TZI.DaylightBias,
strS,
strD,
strDisp);
*/
if (strDisp.Find(_T(": ")) != -1)
strDisp = strDisp.Mid( strDisp.Find(_T(": "))+2 );
char cid;
for (int j=0; j<arrUsedZonesS.GetSize(); j++)
if ( arrUsedZonesS[j].Compare( arrZones[i].strGMTZone ) == 0 )
{
cid = arrUsedZonesI[j];
if (cid != -1)
{
arrUsedZonesI.SetAt(j, cid+1);
}
break;
}
if (cid!=-1)
{
CString strSuffix;
strSuffix.Format(_T("_%c"), _T('a')+cid);
strGMT += strSuffix;
}
if (bNoTZI)
strLine.Format(_T("%s=%s\n"), strGMT, strDisp);
else
strLine.Format(_T("%s=%s;%d,%s,%d,%s\n"), strGMT, strDisp,
-arrZones[i].TZI.StandardBias,
strS,
-arrZones[i].TZI.DaylightBias,
strD);
for (int j=0; j<strLine.GetLength(); j++)
{
char ch = (char)strLine[j];
f.Write(&ch, 1);
}
GetRealDate(ss);
GetRealDate(sd);
}
f.Close();
// Save();
// Load();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment