utils.cpp
来自「funambol window mobile客户端源代码」· C++ 代码 · 共 2,314 行 · 第 1/5 页
CPP
2,314 行
// If timeStart equals timeEnd then this time zone does not support DST.
return FALSE;
}
BOOL IsTzInDstChangingList(WCHAR* dstName)
{
if (wcsicmp(dstName, TZ_ALASKA) == 0)
return TRUE;
if (wcsicmp(dstName, TZ_CENTRAL_US) == 0)
return TRUE;
if (wcsicmp(dstName, TZ_EASTERN_US) == 0)
return TRUE;
if (wcsicmp(dstName, TZ_MOUNTAIN) == 0)
return TRUE;
if (wcsicmp(dstName, TZ_PACIFIC_US) == 0)
return TRUE;
return FALSE;
}
BOOL IsDateAfterDstChanging(SYSTEMTIME& date)
{
if (date.wYear >= 2007)
return TRUE;
else
return FALSE;
}
time_t GetLocalTimeT()
{
SYSTEMTIME currentSysTime;
FILETIME currentFileTime;
GetLocalTime(¤tSysTime);
SystemTimeToFileTime(¤tSysTime, ¤tFileTime);
FILETIME currentFt= currentFileTime;
__int64 llCurrent = 0;
memcpy (&llCurrent, ¤tFt, sizeof (__int64));
llCurrent = (llCurrent - 116444736000000000) / 10000000;
time_t currentT = (time_t) llCurrent;
return currentT;
}
void UTCToLocalTime2(SYSTEMTIME &sysTime) {
BOOL changedDST = FALSE;
// FIXME: this value - regFlag - will be read from registry
BOOL regFlag = TRUE;
FILETIME LocalFileTime, SystemFileTime;
if (IsDateAfterDstChanging(sysTime))
changedDST = TRUE;
LocalFileTime.dwLowDateTime = 0;
LocalFileTime.dwHighDateTime = 0;
SystemFileTime.dwLowDateTime = 0;
SystemFileTime.dwHighDateTime = 0;
SystemTimeToFileTime(&sysTime, &SystemFileTime);
FileTimeToLocalFileTime(&SystemFileTime, &LocalFileTime);
FileTimeToSystemTime(&LocalFileTime, &sysTime);
time_t t = FileTimeToUnixTime(LocalFileTime);
TIME_ZONE_INFORMATION tzi;
SafeGetTimeZoneInformation(&tzi);
// if the date is from 2007 and the locale is one of US locales
// change the standard/DST rules to the ones that will apply there from 2007
if (regFlag && changedDST && IsTzInDstChangingList(tzi.StandardName))
{
// change the tzi info for StandardTime and DaylightTime
tzi.DaylightDate.wMonth = 3;
tzi.DaylightDate.wDay = 2;
tzi.StandardDate.wMonth = 11;
tzi.StandardDate.wDay = 1;
}
if ((IsDST(&tzi, t) && // Is date t in daylight saving time?
!IsDST(&tzi, GetLocalTimeT()))) // and current date is not in DST?
{
// Subtract the number of minutes for the DST bias. This is
// normally -60, meaning that we add 60 minutes.
t -= (tzi.DaylightBias * 60);
}
if ((!IsDST(&tzi, t) && // Is date t in standard time?
IsDST(&tzi, GetLocalTimeT()))) // and current date is in DST?
{
// Add the number of minutes for the DST bias. This is
// normally -60, meaning that we substract 60 minutes.
t += (tzi.DaylightBias * 60);
}
// Subtract the number of minutes for the StandardBias.
t -= (tzi.StandardBias * 60);
UnixTimeToFileTime(t, LocalFileTime);
FileTimeToSystemTime(&LocalFileTime, &sysTime);
}
/**
* Convert FILETIME to Unix time_t
*/
time_t FileTimeToUnixTime(FILETIME &ft)
{
__int64 llTmp;
memcpy (&llTmp, &ft, sizeof (__int64));
llTmp = (llTmp - 116444736000000000) / 10000000;
return (time_t) llTmp;
}
/**
* Convert Unix time_t to FILETIME
*/
void UnixTimeToFileTime(time_t t, FILETIME &ft)
{
__int64 llTmp = t;
llTmp = llTmp * 10000000 + 116444736000000000;
memcpy (&ft, &llTmp, sizeof (FILETIME));
}
void localTimeToUTC2(SYSTEMTIME &sysTime)
{
BOOL changedDST = FALSE;
// FIXME: this value - regFlag - will be read from registry
BOOL regFlag = TRUE;
FILETIME LocalFileTime, SystemFileTime;
if (IsDateAfterDstChanging(sysTime))
changedDST = TRUE;
SystemTimeToFileTime(&sysTime, &LocalFileTime);
LocalFileTimeToFileTime(&LocalFileTime, &SystemFileTime);
FileTimeToSystemTime(&SystemFileTime, &sysTime);
time_t t = FileTimeToUnixTime(SystemFileTime);
TIME_ZONE_INFORMATION tzi;
SafeGetTimeZoneInformation(&tzi);
// if the date is from 2007 and the locale is one of US locales
// change the standard/DST rules to the ones that will apply there from 2007
if (regFlag && changedDST && IsTzInDstChangingList(tzi.StandardName))
{
// change the tzi info for StandardTime and DaylightTime
tzi.DaylightDate.wMonth = 3;
tzi.DaylightDate.wDay = 2;
tzi.StandardDate.wMonth = 11;
tzi.StandardDate.wDay = 1;
}
if (IsDST(&tzi, t) && // Is date t in daylight saving time?
!IsDST(&tzi, GetLocalTimeT())) // and current date is not in DST?
{
// Add the number of minutes for the DST bias. This is
// normally -60, meaning that we subtract 60 minutes.
t += (tzi.DaylightBias * 60);
}
if (!IsDST(&tzi, t) && // Is date t in standard time?
IsDST(&tzi, GetLocalTimeT())) // and current date is in DST?
{
// Add the number of minutes for the DST bias. This is
// normally -60, meaning that we add 60 minutes.
t -= (tzi.DaylightBias * 60);
}
// Add the number of minutes for the StandardBias.
t += (tzi.StandardBias * 60);
FILETIME ftTmp;
UnixTimeToFileTime(t, ftTmp);
FileTimeToSystemTime(&ftTmp, &sysTime);
}
wchar_t* FileTime2UTCTimeFormatter(FILETIME &ft, wchar_t* out) {
SYSTEMTIME t;
FileTimeToSystemTime(&ft, &t);
wchar_t month [10];
wchar_t day [10];
wchar_t hour [10];
wchar_t minute[10];
wchar_t second[10];
// month
if (t.wMonth < 10)
wsprintf(month, TEXT("0%i"), t.wMonth);
else
wsprintf(month, TEXT("%i"), t.wMonth);
// day
if (t.wDay < 10)
wsprintf(day, TEXT("0%i"), t.wDay);
else
wsprintf(day, TEXT("%i"), t.wDay);
// hour
if (t.wHour < 10)
wsprintf(hour, TEXT("0%i"), t.wHour);
else
wsprintf(hour, TEXT("%i"), t.wHour);
// minute
if (t.wMinute < 10)
wsprintf(minute, TEXT("0%i"), t.wMinute);
else
wsprintf(minute, TEXT("%i"), t.wMinute);
// second
if (t.wSecond < 10)
wsprintf(second, TEXT("0%i"), t.wSecond);
else
wsprintf(second, TEXT("%i"), t.wSecond);
if (out == NULL)
out = new wchar_t[20];
wsprintf(out, TEXT("%i%s%sT%s%s%sZ"), t.wYear, month, day, hour, minute, second);
return out;
}
/**
* Returns true if dataType is one of accepted SIF formats. (wstring version)
*/
bool isSIF(const wstring& dataType) {
if ( dataType == L"text/x-s4j-sifc" ||
dataType == L"text/x-s4j-sife" ||
dataType == L"text/x-s4j-sift" ||
dataType == L"text/x-s4j-sifn" ){
return true;
}
else {
return false;
}
}
/**
* Returns true if dataType is one of accepted SIF formats. (string version)
*/
bool isSIF(const string& dataType) {
if ( dataType == "text/x-s4j-sifc" ||
dataType == "text/x-s4j-sife" ||
dataType == "text/x-s4j-sift" ||
dataType == "text/x-s4j-sifn" ) {
return true;
}
else {
return false;
}
}
bool checkSIF(int dataType) {
const char arg[] = "dummy";
DMTree* dmt = DMTreeFactory::getDMTree(arg);
ManagementNode* node = NULL;
bool ret = FALSE;
if(!dmt)
return false;
if (dataType == olFolderContacts)
node = dmt->readManagementNode(CONTACTS_CONTEXT);
else if(dataType == olFolderCalendar)
node = dmt->readManagementNode(APPOINTMENTS_CONTEXT);
else if(dataType == olFolderTasks)
node = dmt->readManagementNode(TASKS_CONTEXT);
if(!node)
return false;
char* val = node->readPropertyValue("useSIF");
if (dmt) { delete dmt; dmt = NULL; }
if (node) { delete node; node = NULL; }
if(!val)
return false;
if(!strcmp(val, "1"))
ret = true;
else
ret = false;
delete [] val;
return ret;
}
/*
* Used for filter in mail sync source.
*
*/
#define DAY_LOWPART 711000000
#define DAY_HIGHPART 201 // 201*2^32 =~ 8.6e11
wchar_t* getDayBefore(int numDayBefore) {
SYSTEMTIME t;
SYSTEMTIME t2;
wchar_t* ret = NULL;
FILETIME ft;
FILETIME ft2, utctime;
LARGE_INTEGER time;
LARGE_INTEGER time2;
LARGE_INTEGER Days;
if (numDayBefore < 0)
goto finally;
GetLocalTime (&t);
t.wHour = 0;
t.wMinute = 0;
t.wSecond = 0;
//GetSystemTime (&t);
SystemTimeToFileTime(&t, &ft);
Days.LowPart = DAY_LOWPART;
Days.HighPart = DAY_HIGHPART;
Days.QuadPart = Days.QuadPart * numDayBefore;
time.LowPart = ft.dwLowDateTime;
time.HighPart = ft.dwHighDateTime;
time2.QuadPart = time.QuadPart - Days.QuadPart;
ft2.dwLowDateTime = time2.LowPart;
ft2.dwHighDateTime = time2.HighPart;
LocalFileTimeToFileTime(&ft2, &utctime);
FileTimeToSystemTime(&utctime, &t2);
ret = new wchar_t[32];
memset(ret, 0, 32*sizeof(wchar_t));
/*
wchar_t month [10];
wchar_t day [10];
wchar_t hour [10];
wchar_t min [10];
// month
if (t2.wMonth < 10)
wsprintf(month, TEXT("0%i"), t2.wMonth);
else
wsprintf(month, TEXT("%i"), t2.wMonth);
// day
if (t2.wDay < 10)
wsprintf(day, TEXT("0%i"), t2.wDay);
else
wsprintf(day, TEXT("%i"), t2.wDay);
// hour
if (t2.wHour < 10)
wsprintf(hour, TEXT("0%i"), t2.wHour);
else
wsprintf(hour, TEXT("%i"), t2.wHour);
// min
if (t2.wMinute < 10)
wsprintf(min, TEXT("0%i"), t2.wMinute);
else
wsprintf(min, TEXT("%i"), t2.wMinute);
*/
wsprintf(ret, TEXT("%i%02d%02dT%02d%02d00Z"), t2.wYear, t2.wMonth, t2.wDay, t2.wHour, t2.wMinute);
finally:
return ret;
}
bool convertRrule(IRecurrencePattern* pRecPat, wchar_t* rrule, DATE startDate) {
bool ret = true;
wchar_t* str = new wchar_t[100];
wcscpy(str, rrule);
wchar_t seps[] = TEXT(" ");
wchar_t* token;
token = wcstok( str, seps );
//first will be D|W|MP|MD|YM|YD<interval>
long recType = -1;
long interval;
long occurences;
DATE endDate;
long dayOfMonth = -1;
long weekOfMonth = -1;
long monthOfYear = -1;
if(token[0] == TEXT('D')) {
recType = olRecursDaily;
token++;
}
else if(token[0] == TEXT('W')) {
recType = olRecursWeekly;
token++;
}
else if(token[0] == TEXT('M') && token[1] == TEXT('D')) {
recType = olRecursMonthly;
token = token + 2;
}
else if(token[0] == TEXT('M') && token[1] == TEXT('P')) {
recType = olRecursMonthNth;
token = token + 2;
}
//not supported
else if(token[0] == TEXT('Y') && token[1] == TEXT('D'))
recType = -1;
else if(token[0] == TEXT('Y') && token[1] == TEXT('M')) {
recType = olRecursYearly; //!!!
token = token + 2;
}
interval = _wtol(token);
if(!interval || recType == -1) {
if(str)
delete [] str;
return false;
}
wchar_t* days = new wchar_t[20];
wcscpy(days, TEXT(""));
wchar_t* mOfYear = new wchar_t[20];
wcscpy(mOfYear, TEXT(""));
while( token != NULL ) {
token = wcstok( NULL, seps );
if(token) {
if(recType == olRecursDaily) {
//this toke should be even number of occurrences or the end date of the pattern
if(wcschr(token, TEXT('#'))) {
pRecPat->put_RecurrenceType(recType);
token++;
occurences = _wtol(token);
if(occurences == 0)
pRecPat->put_NoEndDate(VARIANT_TRUE);
else
pRecPat->put_Occurrences(occurences);
pRecPat->put_Interval(interval);
}
else if(token[8] == TEXT('T')) {
pRecPat->put_RecurrenceType(recType);
systemTimeToDouble(token, &endDate, NULL);
pRecPat->put_PatternEndDate(endDate);
pRecPat->put_Interval(interval);
}
}
else if(recType == olRecursWeekly) {
if(wcschr(token, TEXT('#'))) {
pRecPat->put_RecurrenceType(recType);
token++;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?