SystemTimeToFileTime、FileTimeToLocalFileTime、LocalFileTimeToFileTime三函数的跨平台实现

// test.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include <stdlib.h>  
#include <stdio.h>  
#include <time.h>  
#include <windows.h>  


typedef struct 
{
    int year;
    int month;
    int day;
}S_DATE, *PS_DATE;


bool IsLeafYear(int year)
{
    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) // 闰年的计算,归结起来就是通常说的:四年一闰;百年不闰,四百年再闰。 
    {
        return true; // 是闰年
    }
    else
    {
        return false; // 不是闰年
    }
}


bool IsValidDate(const S_DATE &date)
{
    if(2 == date.month)
    {
        if(IsLeafYear(date.year))
        {
            if (date.day > 29 || date.day <= 0)
            {
                return false;
            }
        }
        else
        {
            if (date.day > 28 || date.day <= 0)
            {
                return false;
            }
        }
    }
    else if(4 == date.month || 6 == date.month || 9 == date.month || 11 == date.month)
    {
        if (date.day > 30 || date.day <= 0)
        {
            return false;
        }
    }
    else
    {
        if (date.day > 31 || date.day <= 0)
        {
            return false;
        }
    }
    return true;
}


int PassDay(const S_DATE &date) // 该年的第几天(包含闰年+1)
{
    // 方法1:
    int day = date.day;
    int i;
    for(i = 1; i < date.month; i++)
    {
        if(2 == i)
        {
            if(IsLeafYear(date.year))
            {
                day += 29;
            }
            else
            {
                day += 28;
            }
        }
        else if(4 == i || 6 == i || 9 == i || 11 == i)
        {
            day += 30;
        }
        else
        {
            day += 31;
        }
    }
    return day;


    // 方法2:
    /*struct tm _tm;
    ::memset(&_tm, 0, sizeof(_tm));
    _tm.tm_year = date.year - 1900;
    _tm.tm_mon = date.month - 1;
    _tm.tm_mday = date.day;
    ::mktime(&_tm);
    return _tm.tm_yday;*/
}
#include <windef.h>
void GetDayDiff(const S_DATE &date1, const S_DATE &date2, int &daydiff) // 获取两日期间相隔天数(date1为小日期,date2为大日期)
{
    if(date1.year == date2.year)
    {
        daydiff = PassDay(date2) - PassDay(date1);
    }
    else if (date1.year > date2.year)
    {
        if (IsLeafYear(date2.year))
        {
            daydiff = PassDay(date1) + (366 - PassDay(date2));
        }
        else
        {
            daydiff = PassDay(date1) + (365 - PassDay(date2));
        }


        // 两日期年份相差大于一年的情况
        if (date1.year - date2.year > 1)
        {
            int i = date1.year - 1;
            for (; i > date2.year; i--)
            {
                if (IsLeafYear(i))
                {
                    daydiff += 366;
                }
                else
                {
                    daydiff += 365;
                }
            }
        }


        daydiff = -daydiff;
    }
    else
    {
        if (IsLeafYear(date1.year))
        {
            daydiff = PassDay(date2) + (366 - PassDay(date1));
        }
        else
        {
            daydiff = PassDay(date2) + (365 - PassDay(date1));
        }


        // 两日期年份相差大于一年的情况
        if (date2.year - date1.year > 1)
        {
            int i = date2.year - 1;
            for (; i > date1.year; i--)
            {
                if (IsLeafYear(i))
                {
                    daydiff += 366;
                }
                else
                {
                    daydiff += 365;
                }
            }
        }
    }
}


BOOL SystemTimeToFileTime2(__in CONST SYSTEMTIME *lpSystemTime, __out LPFILETIME lpFileTime)
{
    if (NULL == lpSystemTime || NULL == lpFileTime)
        return FALSE;


    S_DATE date1 = {1601, 1, 1};
    S_DATE date2 = {lpSystemTime->wYear, lpSystemTime->wMonth, lpSystemTime->wDay};
    int nDiff = 0;
    GetDayDiff(date1, date2, nDiff);
    ULONGLONG ull = (ULONGLONG)nDiff * 24 * 60 * 60 * 1000 * 1000 * 10; // 单位为100ns
    ull += ((ULONGLONG)lpSystemTime->wHour * 60 * 60 * 1000 * 1000 * 10);
    ull += ((ULONGLONG)lpSystemTime->wMinute * 60 * 1000 * 1000 * 10);
    ull += ((ULONGLONG)lpSystemTime->wSecond * 1000 * 1000 * 10);
    ull += ((ULONGLONG)lpSystemTime->wMilliseconds * 1000 * 10);


    lpFileTime->dwHighDateTime = (ull & 0XFFFFFFFF00000000) >> 32;
    lpFileTime->dwLowDateTime = ull & 0X00000000FFFFFFFF;


    return TRUE;
}


BOOL FileTimeToLocalFileTime2(__in CONST FILETIME *lpFileTime, __out LPFILETIME lpLocalFileTime)
{
    if (NULL == lpFileTime || NULL == lpLocalFileTime)
        return FALSE;


    // 获取时区
    time_t tt = 0;
    struct tm *ptm = NULL;
    tt = ::time(&tt);
    ptm = ::localtime(&tt);
    int nHourLocal = ptm->tm_hour;
    ptm = ::gmtime(&tt);
    int nHourUTC = ptm->tm_hour;
    int nTimeZoom = nHourLocal - nHourUTC;


    ULARGE_INTEGER uli = {lpFileTime->dwLowDateTime, lpFileTime->dwHighDateTime};
    uli.QuadPart += ((ULONGLONG)nTimeZoom * 60 * 60 * 1000 * 1000 * 10);
    lpLocalFileTime->dwLowDateTime = uli.LowPart;
    lpLocalFileTime->dwHighDateTime = uli.HighPart;


    return TRUE;
}


BOOL LocalFileTimeToFileTime2(__in CONST FILETIME *lpLocalFileTime, __out LPFILETIME lpFileTime)
{
    if (NULL == lpLocalFileTime || NULL == lpFileTime)
        return FALSE;


    // 获取时区
    time_t tt = 0;
    struct tm *ptm = NULL;
    tt = ::time(&tt);
    ptm = ::localtime(&tt);
    int nHourLocal = ptm->tm_hour;
    ptm = ::gmtime(&tt);
    int nHourUTC = ptm->tm_hour;
    int nTimeZoom = nHourLocal - nHourUTC;


    ULARGE_INTEGER uli = {lpLocalFileTime->dwLowDateTime, lpLocalFileTime->dwHighDateTime};
    uli.QuadPart -= ((ULONGLONG)nTimeZoom * 60 * 60 * 1000 * 1000 * 10);
    lpFileTime->dwLowDateTime = uli.LowPart;
    lpFileTime->dwHighDateTime = uli.HighPart;


    return TRUE;
}


int main()
{
    SYSTEMTIME      stLocal, stUTC;
    FILETIME        ftLocal, ftUTC, ftLocal2, ftUTC2;
    ULARGE_INTEGER  uliLocal, uliUTC, uliLocal2, uliUTC2;


    ::GetLocalTime(&stLocal); // 获取本地时间
    ::GetSystemTime(&stUTC); // 获取UTC时间
    printf("Local System Time: %d-%d-%d %d:%d:%d\n", stLocal.wYear, stLocal.wMonth, 
        stLocal.wDay, stLocal.wHour, stLocal.wMinute, stLocal.wSecond);
    printf("UTC System Time  : %d-%d-%d %d:%d:%d\n\n", stUTC.wYear, stUTC.wMonth, 
        stUTC.wDay, stUTC.wHour, stUTC.wMinute, stUTC.wSecond);


    SystemTimeToFileTime2(&stLocal, &ftLocal); // 本地时间转化成FILETIME结构
    SystemTimeToFileTime2(&stUTC, &ftUTC); // UTC时间转化成FILETIME结构
    uliLocal.LowPart = ftLocal.dwLowDateTime;
    uliLocal.HighPart = ftLocal.dwHighDateTime;
    uliUTC.LowPart = ftUTC.dwLowDateTime;
    uliUTC.HighPart = ftUTC.dwHighDateTime;
    printf("Local File Time  : %llu\n", uliLocal.QuadPart);
    printf("UTC File Time    : %llu\n", uliUTC.QuadPart);
    printf("Diff File Time   : %llu = 8*60*60*1000*1000*10(单位为100ns)\n\n", uliLocal.QuadPart - uliUTC.QuadPart);


    FileTimeToLocalFileTime2(&ftUTC, &ftLocal2); // 将UTC的FILETIME时间转换为相应的本地的FILETIME时间。
    LocalFileTimeToFileTime2(&ftLocal, &ftUTC2); // 将本地的FILETIME时间转换为相应的UTC的FILETIME时间。
    uliLocal2.LowPart = ftLocal2.dwLowDateTime;
    uliLocal2.HighPart = ftLocal2.dwHighDateTime;
    uliUTC2.LowPart = ftUTC2.dwLowDateTime;
    uliUTC2.HighPart = ftUTC2.dwHighDateTime;
    printf("Local File Time from UTC File Time : %llu\n", uliLocal2.QuadPart);
    printf("UTC File Time from Local File Time : %llu\n\n", uliUTC2.QuadPart);


    system("pause");
    return EXIT_SUCCESS;
}


/*///////////////////////// 执行打印開始 //////////////////////////
Local System Time: 2014-7-6 18:11:37
UTC System Time  : 2014-7-6 10:11:37


Local File Time  : 130491438974890000
UTC File Time    : 130491150974890000
Diff File Time   : 288000000000 = 8*60*60*1000*1000*10(单位为100ns)


Local File Time from UTC File Time : 130491438974890000
UTC File Time from Local File Time : 130491150974890000


请按随意键继续. . .
////////////////////////// 执行打印结束 /////////////////////////*/

你可能感兴趣的:(System)