2010年2月9日 星期二

CuTimeZone -

//


#define RV_TIMEZONES    TEXT("Time Zones")
#define RK_CLOCK        TEXT("Software\\Microsoft\\Clock")
#define RV_INDST        TEXT("HomeDST")                     // are we currently in DST
#define RV_AUTODST      TEXT("AutoDST")
#define RV_DSTUI        TEXT("ShowDSTUI") 
#define RV_TIMEZONES    TEXT("Time Zones")
#define RV_DISPLAY      TEXT("Display")
#define RV_TZI          TEXT("TZI")
#define RV_DLT          TEXT("Dlt")
#define RV_STD          TEXT("Std")

#define ZoneHasDaylightTime(tzi)    (tzi.DaylightDate.wMonth && tzi.DaylightBias)

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

class CuTimeZone
{
    TIME_ZONE_INFORMATION m_TimeZoneInformation;

    vector<wstring> m_vecTimeZoneList;
    wstring m_strCurrZone;
    int m_nCurrZone;

public:
    CuTimeZone();
    virtual ~CuTimeZone();

    void Init();
    vector<wstring> &GetTimeZoneList(void);

    wstring GetCurrZone(){ return m_strCurrZone; }
    int GetCurrZoneIndex(){ return m_nCurrZone; }

    void SetCurrZone(int nCurrZone);
    void SetCurrZone(wstring strCurrZone);


};


//

CuTimeZone::~CuTimeZone()
{

}


CuTimeZone::CuTimeZone()
{
    CReg   Reg1(HKEY_LOCAL_MACHINE, RV_TIMEZONES), Reg2;

    TCHAR  szTimeZone[64];
    TCHAR  szTimeZoneStd[64];
    
//  TIME_ZONE_INFORMATION tziCurrent;
    DWORD tzid = GetTimeZoneInformation(&m_TimeZoneInformation);


    // Get DAYLIGHT States
    BOOL fInDST = (TIME_ZONE_ID_DAYLIGHT == tzid );

    CReg reg(HKEY_LOCAL_MACHINE, RK_CLOCK);
    reg.SetDW(RV_DSTUI, 1);

    
    while(Reg1.EnumKey(szTimeZone,64))
    {
        LPTSTR szTemp;
        Reg2.Reset();
        
        if(Reg2.Open(Reg1,szTimeZone))
        {
        
            // use the display name rather than the standard name 
            szTemp = (LPTSTR)Reg2.ValueSZ(RV_DISPLAY);

            Reg2.ValueSZ(RV_STD, szTimeZoneStd, sizeof(szTimeZoneStd));


            if(szTemp && 
                    ( !lstrcmp(szTimeZone, m_TimeZoneInformation.StandardName) ||
                      !lstrcmp(szTimeZoneStd, m_TimeZoneInformation.StandardName) )
                )
            {
                //lstrcpy(szZoneCurr,szTemp);
                m_nCurrZone = m_vecTimeZoneList.size();
                m_strCurrZone = szTemp;
            }

            m_vecTimeZoneList.push_back(szTemp);
        }

    }

}



vector<wstring> &CuTimeZone::GetTimeZoneList(void)
{
    return m_vecTimeZoneList;
}

void CuTimeZone::SetCurrZone(int nCurrZone)
{

    wstring strCurrZone = m_vecTimeZoneList[nCurrZone];
    SetCurrZone(strCurrZone);
}


void CuTimeZone::SetCurrZone(wstring strCurrZone)
{
    // Iterate over all timezones. Init new timezone to current just in case 
    TCHAR   szTimeZone[32];
    CReg    Reg1(HKEY_LOCAL_MACHINE, RV_TIMEZONES), Reg2;
    CReg reg(HKEY_LOCAL_MACHINE, RK_CLOCK);
    BOOL fUseDST =  ZoneHasDaylightTime(m_TimeZoneInformation);

    reg.SetDW(RV_AUTODST, fUseDST);
    while(Reg1.EnumKey(szTimeZone,32))
    {
        Reg2.Reset();
        if(Reg2.Open(Reg1,szTimeZone))
        {
            // if the display name matches the one in the combobox get the TZI data.
            if(!lstrcmp(strCurrZone.c_str(), Reg2.ValueSZ(RV_DISPLAY)))
            {
                TZREG  *pTZR = (TZREG*)Reg2.ValueBinary(RV_TZI);
                if(pTZR)
                {
                    m_TimeZoneInformation.Bias = pTZR->Bias;
                    m_TimeZoneInformation.StandardBias = pTZR->StandardBias;
                    m_TimeZoneInformation.DaylightBias = pTZR->DaylightBias;
                    m_TimeZoneInformation.StandardDate = pTZR->StandardDate;
                    m_TimeZoneInformation.DaylightDate = pTZR->DaylightDate;
                            
                    Reg2.ValueSZ(RV_DLT, m_TimeZoneInformation.DaylightName, 32);
                    lstrcpy(m_TimeZoneInformation.StandardName, szTimeZone);

                    m_strCurrZone = strCurrZone;
                    break;
                }
            }
        }
    }
    SetTimeZoneInformation(&m_TimeZoneInformation);


}
很特別的程式片段, 從APP裡移出來後就沒在理他, 等他被review 或需要改動功能時, 在來調整了

沒有留言:

張貼留言