SYSTEMTIME相关

最近写了一些SYSTEMTIME相关的程序,贴上来,欢迎拍砖。

#include <windows.h>
#include <time.h>
#include <string>
#include <fstream>

bool String2SystemTimeW(const WCHAR* pszstring23, SYSTEMTIME& st)
{
    int nLen = wcslen(pszstring23);
    int nYear, nMonth, nDay, nHour, nMinute, nSecond, nMillisecond;
    if (nLen == 23 && (7 == swscanf_s(pszstring23, L"%d-%02d-%02d,%02d:%02d:%02d:%03d", &nYear, &nMonth, &nDay, &nHour, &nMinute, &nSecond, &nMillisecond)))
    {
        st.wYear = nYear;
        st.wMonth = nMonth;
        st.wDay = nDay;
        st.wHour = nHour;
        st.wMinute = nMinute;
        st.wSecond = nSecond;
        st.wMilliseconds = nMillisecond;
        return true;
    }
    return false;
}

bool String2SystemTimeA(const CHAR* pszstring23, SYSTEMTIME& st)
{
    int nLen = strlen(pszstring23);
    int nYear, nMonth, nDay, nHour, nMinute, nSecond, nMillisecond;
    if (nLen == 23 && (7 == sscanf_s(pszstring23, "%d-%02d-%02d,%02d:%02d:%02d:%03d", &nYear, &nMonth, &nDay, &nHour, &nMinute, &nSecond, &nMillisecond)))
    {
        st.wYear = nYear;
        st.wMonth = nMonth;
        st.wDay = nDay;
        st.wHour = nHour;
        st.wMinute = nMinute;
        st.wSecond = nSecond;
        st.wMilliseconds = nMillisecond;
        return true;
    }
    return false;
}

#ifdef UNICODE
#define String2SystemTime  String2SystemTimeW
#else
#define String2SystemTime  String2SystemTimeA
#endif // !UNICODE

inline __time64_t MakeMilliseconds(const SYSTEMTIME& st)
{
    struct tm atm;
    atm.tm_sec = st.wSecond;
    atm.tm_min = st.wMinute;
    atm.tm_hour = st.wHour;
    atm.tm_mday = st.wDay;
    atm.tm_mon = st.wMonth;        // tm_mon is 0 based
    atm.tm_year = st.wYear - 1900;     // tm_year is 1900 based
    atm.tm_isdst = -1;
    __time64_t t = _mktime64(&atm);
    t = t * 1000 + st.wMilliseconds;
    return t;
}

inline int SystemTimeCompare(const SYSTEMTIME& stL, const SYSTEMTIME& stR)
{
    __time64_t sL = MakeMilliseconds(stL);
    __time64_t sR = MakeMilliseconds(stR);
    return int(sL - sR);
}

inline bool TimeInterval(const SYSTEMTIME& st, const SYSTEMTIME& stL, const SYSTEMTIME& stR)
{
    return  SystemTimeCompare(st, stL) >= 0 && 0 >= SystemTimeCompare(st, stR);
}

int _tmain(int argc, _TCHAR* argv[])
{
    if (argc != 4)
    {
        perror("usage: splitfile <file name> <time from> <time to> ...\n");
        perror("eg. splitfile src.txt 2013-06-27,14:38:17:445 2013-06-27,14:38:20:446 ...\n");
        return -1;
    }

    const TCHAR* psrcfile = argv[1];
    const TCHAR* ptimefrom = argv[2];
    const TCHAR* ptimeto = argv[3];

    SYSTEMTIME stFrom, stTo;

    if (!String2SystemTime(ptimefrom, stFrom) || !String2SystemTime(ptimeto, stTo))
    {
        perror("input time param error ...\n");
        return -2;
    }

    std::string strOneLine;
    std::ifstream file;

    file.open(psrcfile);
    if (!file.is_open())
    {
        perror("file cannot be open ...\n");
        return -3;
    }

    char buf[24] = {0};
    SYSTEMTIME st = {0};

    while (true)
    {
        if (file.eof())
            break;

        std::getline(file, strOneLine);

        if (strOneLine.size() > 34)
        {
            const char* p = strOneLine.c_str();
            memcpy_s(buf, sizeof(buf), p, 23);
            buf[23] = 0;
            if (String2SystemTimeA(buf, st) && TimeInterval(st, stFrom, stTo))
            {
                p += 34;
                printf_s("%s", p);
            }
        }
    }
    file.close();
    return 0;
}

PS:两个一定格式的时间字符串,可以直接strcmp就可以知道时间的前后。比如,2013-06-27,14:38:17:432与2013-06-27,14:38:17:445比较,不用转换成SYSTEMTIME就可以知道时间先后。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值