⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 difftime.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
字号:
/*
 * Copyright (C) 2003-2007 Funambol, Inc
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY, TITLE, NONINFRINGEMENT or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307  USA
 */
// difftime.cpp : Windows SYSTEMTIME diffing functions.
//

#include <windows.h>
#include "notify.h"

#if 0
// Example code

#ifdef _WIN32_WCE
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd ) {
#else
int main(int argc, char** argv) {
#endif

    SYSTEMTIME t;
    SYSTEMTIME s1;
    GetLocalTime (&t);

    wchar_t buff[100];
    // s1= DT_AddDiff(nano100SecInMin, 1,&t); //diff 1 minute from s2
    s1= DT_AddDiff(nano100SecInHour, 1,&t); //diff 1 minute from s2

     /*WinAPI function GetDateFormat uses for receive string with
       datetime in specifid format*/
    // Original day and minute
    GetDateFormat
     (
      LOCALE_USER_DEFAULT,
      NULL,
      &t,
      TEXT("yyyy MM dd"), //my format
      buff,
      100
     );
     //wsprintf(buff, TEXT("original date: %s") ,buff);
     MessageBox (NULL, buff, TEXT ("!!"), MB_SETFOREGROUND |MB_OK);

     GetTimeFormat
     (
      LOCALE_USER_DEFAULT,
      NULL,
      &t,
      TEXT("HH:mm:ss"), //my format
      buff,
      100
     );
     //wsprintf(buff, TEXT("original time: %s") ,buff);
     MessageBox (NULL, buff, TEXT ("!!"), MB_SETFOREGROUND |MB_OK);



     // minus a minute and minute
    GetDateFormat
     (
      LOCALE_USER_DEFAULT,
      NULL,
      &s1,
      TEXT("yyyy MM dd"), //my format
      buff,
      100
     );

     //wsprintf(buff, TEXT("modified date: %s") ,buff);
     MessageBox (NULL, buff, TEXT ("!!"), MB_SETFOREGROUND |MB_OK);


      GetTimeFormat
     (
      LOCALE_USER_DEFAULT,
      NULL,
      &s1,
      TEXT("HH:mm:ss"), //my format
      buff,
      100
     );
     //wsprintf(buff, TEXT("modified time: %s") ,buff);
     MessageBox (NULL, buff, TEXT ("!!"), MB_SETFOREGROUND |MB_OK);


    //MessageBox (NULL, TEXT("start"), TEXT ("!!"), MB_SETFOREGROUND |MB_OK);
    CeRunAppAtTime(TEXT("startservice.exe"), &s1);


    return 0;
 }
#endif

/*1. Diffence BETWEEN two datetime
////////////
//equivalent of DATEDIFF function from SQLServer
//Returns the number of date and time boundaries crossed
//between two specified dates.
//////////// */
double DT_PeriodsBetween (
             const __int64 datepart, /*datepart that we want to count, nano100SecInDay ...}*/
             const SYSTEMTIME* pst1, /*valid datetime*/
             const SYSTEMTIME* pst2  /*valid datetime*/
 ) {
     FILETIME ft1,ft2;
     __int64 *pi1,*pi2;

     /*convert SYSTEMTIME to FILETIME
     //SYSTEMTIME is only representation, so need convert to
     //FILETIME for make some calculation*/
     SystemTimeToFileTime (pst1,&ft1);
     SystemTimeToFileTime (pst2,&ft2);
     /*convert FILETIME to __int64
     //FILETIME is struct with two DWORD, for receive
     //ability calculation we must reference to FILETIME as to int*/
     pi1 = (__int64*)&ft1;
     pi2 = (__int64*)&ft2;
     /*compare two datetimes and (bigger date - smaller date)/period*/
     return (CompareFileTime(&ft1,&ft2)==1) ?
        (((*pi1)-(*pi2))/(double)datepart) : (((*pi2)-(*pi1))/(double)datepart);
}

/*2. Add/Subtract weeks,days,hours... to/from datetime
    //and receiving new datetime.
////////////
//equivalent of DATEADD function from SQLServer
//Returns a new datetime value based on adding an interval
// to the specified date.
/////////// */
SYSTEMTIME DT_AddDiff (
        const __int64 datepart, /*datepart with we want to manipulate, {nano100SecInDay ...}*/
        const __int64 num, /*value used to increment/decrement datepart*/
        const SYSTEMTIME* pst /*valid datetime which we want change*/
 ) {
     FILETIME ft;
     SYSTEMTIME st;
     __int64* pi;

     SystemTimeToFileTime (pst,&ft);
     pi = (__int64*)&ft;
     (*pi) += (__int64)num*datepart;

     /*convert FAILETIME to SYSTEMTIME*/
     FileTimeToSystemTime (&ft,&st);

     /*now, st contain new valid datetime, so return it*/
     return st;
}


/*
int main(int argc, char *argv[])
{
    int     nYear, nMonth, nDay;
    nYear = 2004;
    nMonth = 3;
    nDay = 2;
    const static char   aszWeekDayNames[][16] = {
        "Sunday", "Monday", "Tuesday", "Wednesday", "Thursay", "Friday", "Saturday"
    };
    sprintf( t, "%i", DayOfWeek( nYear, nMonth, nDay ));
    sprintf( t, "%s", aszWeekDayNames[ DayOfWeek( nYear, nMonth, nDay ) ] );
    return EXIT_SUCCESS;
}
*/

bool    IsLeapYear( int nYear )
{
    if( nYear % 4 != 0 )    return false;
    if( nYear % 100 != 0 )    return true;
    return ( nYear % 400 == 0 );
}

/*
 *    Count of days, which is Feb 29, from Jan 1, 0 to Jan 1, nYear
 *    There is no Feb 29 between Jan 1, 0 and Jan 1, -3, one between Jan 1, 0 and Jan 1, -4
 *    one between Jan 1, 0 and Jan 1, 3 AD, one between Jan 1, 0 and Jan 1, 4
 */
int        CountOfFeb29( int nYear )
{
    int        nCount = 0;
    if( nYear > 0 ) {
        nCount = 1;    /* Year 0 is a leap year */
        nYear--;/* Year nYear is not in the period */
    }
    nCount += nYear / 4 - nYear / 100 + nYear / 400;

    return nCount;
}

int     DayOfWeek( int nYear, int nMonth, int nDay )
{
    int            nDayOfWeek;
    const static int    pnDaysBeforeMonth[] = {
         0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
    };

    /* the day of Jan 1, nYear */
    nDayOfWeek = 6 + nYear % 7 + CountOfFeb29(nYear) % 7 + 14;/* + 14 : makes nDayOfWeek >= 0 */

    /* the day of nMonth 1, nYear */
    nDayOfWeek += pnDaysBeforeMonth[ nMonth ];
    if( nMonth > 2 && IsLeapYear(nYear) )    nDayOfWeek++;

    /* the day of nMonth nDay, nYear */
    nDayOfWeek += nDay - 1;
    nDayOfWeek %= 7;

    return nDayOfWeek;
}

/*void addTime(SYSTEMTIME* startTime)
{
    FILETIME ftTmp;
    SystemTimeToFileTime(startTime, &ftTmp);
    // convert FILETIME to time_t
    __int64 llTmp;
    memcpy (&llTmp, &ftTmp, sizeof (__int64));
    llTmp = (llTmp - 116444736000000000) / 10000000;
    time_t t = (time_t) llTmp;

    wchar_t buf[32];

    memset(buf, 0, 32*sizeof(wchar_t));
    getClientConfigurationInternal (TEXT(""), PROPERTY_SPDM_POLLING_NOTIFICATION,
                                 buf, FALSE);
    long pollingTime = wcstol(buf, NULL, 10);
    t += pollingTime*60;
    llTmp = t;
    llTmp = llTmp * 10000000 + 116444736000000000;
    memcpy (&ftTmp, &llTmp, sizeof (FILETIME));
    FileTimeToSystemTime(&ftTmp, startTime);
}*/


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -