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

📄 timeutils.cpp

📁 一个WinCE6。0下的IP phone的源代码
💻 CPP
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#include "timeutils.h"

// Utility Functions
//////////////////////////////

//GetTimeDIfference finds the difference between two systemtimes
BOOL GetTimeDifference(const SYSTEMTIME *pstEnd, const SYSTEMTIME *pstStart, SYSTEMTIME *pstRes)
{
    BOOL fRes = TRUE;
    ULONGLONG   ullStart = 0, ullEnd = 0;

    //convert starttime to ULL
    fRes = SystemTimeToBigInt(pstStart, &ullStart);

    if (fRes)
    {
        //convert endtime to ULL
        fRes = SystemTimeToBigInt(pstEnd, &ullEnd);
    }

    if (fRes) 
    {
        if (ullStart > ullEnd)
        {
            fRes = FALSE;
        }
    }

    if (fRes)
    {
        //Subtract ULL's and convert back to systime
        fRes = BigIntToHMSSystemTime(ullEnd - ullStart, pstRes);
    }

    return fRes;
}

//Converts systime to ULL
BOOL    SystemTimeToBigInt(const SYSTEMTIME *pstIn, ULONGLONG *pull)
{
    FILETIME    ft = {0};

    //param checking
    if (pstIn == NULL || pull == NULL)
        return FALSE;

    //convert systime to filetime
    if (!SystemTimeToFileTime(pstIn, &ft))
        return FALSE;

    //Filetime = struct with 2 DWORDS : HHHHHHHH and LLLLLLLL
    //ULL looks like HHHHHHHHLLLLLLLL;

    *pull = ((((ULONGLONG)ft.dwHighDateTime) << (sizeof(DWORD)*8)) | ft.dwLowDateTime);

    return TRUE;
}

//Convert ULL to systime with only hours/min/sec (& days) filled
BOOL    BigIntToHMSSystemTime(ULONGLONG ull, SYSTEMTIME *pstOut)
{
    if (pstOut == NULL)
        return FALSE;

    ull /= 10000; // convert to ms -- 1 ns is 10^6 ms so 100 ns is 10^4 ms
    
    pstOut->wMilliseconds = (WORD)(ull % 1000);
    ull /= 1000;

    pstOut->wSecond       = (WORD)(ull % 60);
    ull /= 60;

    pstOut->wMinute       = (WORD)(ull % 60);
    ull /= 60;

    pstOut->wHour         = (WORD)(ull % 24);
    ull /= 24;

    pstOut->wDay          = (WORD)ull;

    pstOut->wYear         = 0; 
    pstOut->wDayOfWeek    = 0; 
    pstOut->wMonth        = 0;
    
    return TRUE;        
}

//Copies source systime into destination
BOOL    SysTimeCopy(SYSTEMTIME *pstTo, const SYSTEMTIME *pstFrom)
{
    if (pstFrom == NULL || pstTo == NULL)
        return FALSE;

    memcpy(pstTo, pstFrom, sizeof(SYSTEMTIME));
    return TRUE;
}

BOOL IsValidTime(const SYSTEMTIME *pst)
{
    FILETIME ft = {0};

    if(!pst)
    {
        return FALSE;
    }

    if(!SystemTimeToFileTime(pst, &ft))
    {
        return FALSE;
    }

    if(ft.dwHighDateTime == 0 && ft.dwLowDateTime == 0)
    {
        return FALSE;
    }

    return TRUE;
}

VOID InvalidateTime(SYSTEMTIME *pst)
{
    FILETIME ft = {0};

    if(!pst)
    {
        return;
    }

    (VOID)FileTimeToSystemTime(&ft, pst);
}

⌨️ 快捷键说明

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