【编程】代码片段 - 日期时间

类别:VC语言 点击:0 评论:0 推荐:
本人写的一些日期时间转换函数:

// =============================================================================

//  文件名称:     TimeUtil.hpp

//  内容说明:    

// 

//  编写人:         IYFT

//  E-Mail:           [email protected]

// 

//  创建日期:     2004年07月01日10:52:54

//  修改日期:     2004年09月13日15:08:35

// -----------------------------------------------------------------------------

// 

//  修改记录:

//  -

//  -

// 

// ============================================================================+

 

// =============================================================================

//  保证源文件只会被编译一次

// -----------------------------------------------------------------------------

#if _MSC_VER >= 1000

// 使源文件只会被编译一次

#pragma once

#endif // _MSC_VER >= 1000

 

#ifndef __HPP_TIMEUTIL_HPP__

#define __HPP_TIMEUTIL_HPP__

// =============================================================================

 

#define DTM_DATE_LENGTH                    10

#define DTM_DATETIME_LENGTH           20

 

// 返回当前本地时区(单位为秒,因为有些时区是小数)

// 北京时区为    +08:00 = +8*60*60         = +28800

// 德黑兰时区为       +03:30 = +3*60*60 + 30*60 = +12600

// 夏威夷时区为       -10:00 = -10*60*60        = -36000

STATIC LONG DTM_getTimeZone(OUT TIME_ZONE_INFORMATION **ppZT = NULL)

{

    STATIC TIME_ZONE_INFORMATION tz;

    STATIC DWORD nResult = 0;

    STATIC BOOL  bInited = FALSE;

 

    if(bInited == FALSE)

       {

        switch(::GetTimeZoneInformation(&tz))

              {

            case  TIME_ZONE_ID_INVALID:

                nResult = 0;

                            break;

                     default:

                nResult = ((tz.Bias + tz.StandardBias) * -60);

        }

        bInited = TRUE;

    }

       if(ppZT != NULL)

       {

              *ppZT = &tz;

       }

    return nResult;

}

 

// 返回当前本地时间

STATIC DWORD DTM_getLocaleCurrentDate()

{

       SYSTEMTIME t; ::GetLocalTime(&t);

       DWORD nDate   = t.wYear * 10000 + t.wMonth * 100 + t.wDay;

       return nDate;

}

 

// 返回当前本地时间

STATIC DWORD DTM_getLocaleCurrentDate(OUT LPTSTR sDate)

{

       SYSTEMTIME t; ::GetLocalTime(&t);

       DWORD nDate   = t.wYear * 10000 + t.wMonth * 100 + t.wDay;

       LPTSTR sPos = sDate;

       INT2T(sPos, 4, t.wYear,           4);   sPos+=4;

       *sPos++ = __T('-');

       INT2T(sPos, 2, t.wMonth, 2);   sPos+=2;

       *sPos++ = __T('-');

       INT2T(sPos, 2, t.wDay,            2);   sPos+=2;

       *sPos   = __T('\0');

       return nDate;

}

 

// 返回当前本地时间

STATIC VOID DTM_getLocaleCurrentString(OUT LPTSTR sDate)

{

       SYSTEMTIME t; ::GetLocalTime(&t);

       DWORD nDate   = t.wYear * 10000 + t.wMonth * 100 + t.wDay;

       LPTSTR sPos = sDate;

       INT2T(sPos, 4, t.wYear,           4);   sPos+=4;

       *sPos++ = __T('-');

       INT2T(sPos, 2, t.wMonth, 2);   sPos+=2;

       *sPos++ = __T('-');

       INT2T(sPos, 2, t.wDay,            2);   sPos+=2;

       *sPos++ = __T(' ');

       INT2T(sPos, 2, t.wHour,          2);   sPos+=2;

       *sPos++ = __T(':');

       INT2T(sPos, 2, t.wMinute, 2);   sPos+=2;

       *sPos++ = __T(':');

       INT2T(sPos, 2, t.wSecond,       2);   sPos+=2;

       *sPos   = __T('\0');

}

 

// 返回当前标准时间

// 注意:格林威治标准时间和本地时间有时差,北京是8个小时

STATIC DWORD DTM_getCurrentDate()

{

       SYSTEMTIME t; ::GetSystemTime(&t);

       DWORD nDate   = t.wYear * 10000 + t.wMonth * 100 + t.wDay;

       return nDate;

}

 

// 返回当前标准时间

// 注意:格林威治标准时间和本地时间有时差,北京是8个小时

STATIC DWORD DTM_getCurrentDate(OUT LPTSTR sDate)

{

       SYSTEMTIME t; ::GetSystemTime(&t);

       DWORD nDate   = t.wYear * 10000 + t.wMonth * 100 + t.wDay;

       LPTSTR sPos = sDate;

       INT2T(sPos, 4, t.wYear,           4);   sPos+=4;

       *sPos++ = __T('-');

       INT2T(sPos, 2, t.wMonth, 2);   sPos+=2;

       *sPos++ = __T('-');

       INT2T(sPos, 2, t.wDay,            2);   sPos+=2;

       *sPos   = __T('\0');

       return nDate;

}

 

// 返回当前标准时间

// 注意:格林威治标准时间和本地时间有时差,北京是8个小时

STATIC VOID DTM_getCurrentString(OUT LPTSTR sDate)

{

       SYSTEMTIME t; ::GetSystemTime(&t);

       DWORD nDate   = t.wYear * 10000 + t.wMonth * 100 + t.wDay;

       LPTSTR sPos = sDate;

       INT2T(sPos, 4, t.wYear,           4);   sPos+=4;

       *sPos++ = __T('-');

       INT2T(sPos, 2, t.wMonth, 2);   sPos+=2;

       *sPos++ = __T('-');

       INT2T(sPos, 2, t.wDay,            2);   sPos+=2;

       *sPos++ = __T(' ');

       INT2T(sPos, 2, t.wHour,          2);   sPos+=2;

       *sPos++ = __T(':');

       INT2T(sPos, 2, t.wMinute, 2);   sPos+=2;

       *sPos++ = __T(':');

       INT2T(sPos, 2, t.wSecond,       2);   sPos+=2;

       *sPos   = __T('\0');

}

 

// 返回日期时间字符串;格式为 [YYYY-MM-DD HH:MM:SS]

STATIC VOID DTM_getString(IN SYSTEMTIME t, OUT LPTSTR sTime)

{

       LPTSTR sPos = sTime;

       INT2T(sPos, 4, t.wYear,           4);   sPos+=4;

       *sPos++ = __T('-');

       INT2T(sPos, 2, t.wMonth, 2);   sPos+=2;

       *sPos++ = __T('-');

       INT2T(sPos, 2, t.wDay,            2);   sPos+=2;

       *sPos++ = __T(' ');

       INT2T(sPos, 2, t.wHour,          2);   sPos+=2;

       *sPos++ = __T(':');

       INT2T(sPos, 2, t.wMinute, 2);   sPos+=2;

       *sPos++ = __T(':');

       INT2T(sPos, 2, t.wSecond,       2);   sPos+=2;

       *sPos   = __T('\0'); 

}

 

// 返回时间字符串;格式为 [X 分 X 秒]

STATIC VOID DTM_getTimeText(IN DWORD nTime, OUT LPTSTR sTime)

{

       if(nTime < 1000*60)

       {

              DWORD nIntegral      = nTime/1000;

              DWORD nDecimal     = nTime%1000;

              nDecimal = (nDecimal*100)/1000;

              INT2T(sTime, 16,  nIntegral);

              UINT  nCount    = LPTSTRLen(sTime);

              if(nDecimal > 0)

              {

                     sTime[nCount++] = __T('.');

                     if(nDecimal >= 10)

                            INT2T(sTime+nCount, 2, (nTime%1000+5)/10, 2);

                     else

                            INT2T(sTime+nCount, 2, nDecimal, 2);

                     nCount += LPTSTRLen(sTime+nCount);

              }

              sTime[nCount++] = __T(' ');

#ifdef UNICODE

              sTime[nCount++] = __W('秒');

#else

              sTime[nCount++] = (ACHAR)0xC3;             // 秒 = 0xC3EB

              sTime[nCount++] = (ACHAR)0xEB;             // 秒 = 0xC3EB

#endif           

              sTime[nCount  ] = TCHR_NULL;

       }

       else

       {

              INT2T(sTime, 16,  nTime/1000/60);

              UINT  nCount    = LPTSTRLen(sTime);

              sTime[nCount++] = __T(' ');

#ifdef UNICODE

              sTime[nCount++] = __W('分');

#else

              sTime[nCount++] = (ACHAR)0xB7;              // 分 = 0xB7D6

              sTime[nCount++] = (ACHAR)0xD6;             // 秒 = 0xB7D6

#endif           

             

              nTime = nTime%(1000*60);

              DWORD nIntegral      = nTime/1000;

              DWORD nDecimal     = nTime%1000;

              if(nIntegral>0 || nDecimal>0)

              {

                     sTime[nCount++] = __T(' ');

                     nDecimal = (nDecimal*100)/1000;

                     INT2T(sTime+nCount, 16,  nIntegral);

                     nCount += LPTSTRLen(sTime+nCount);

                     if(nDecimal > 0)

                     {

                            sTime[nCount++] = __T('.');

                            if(nDecimal >= 10)

                                   INT2T(sTime+nCount, 2, (nTime%1000+5)/10, 2);

                            else

                                   INT2T(sTime+nCount, 2, nDecimal, 2);

                            nCount += LPTSTRLen(sTime+nCount);

                     }

                     sTime[nCount++] = __T(' ');

#ifdef UNICODE

                     sTime[nCount++] = __W('秒');

#else

                     sTime[nCount++] = (ACHAR)0xC3;             // 秒 = 0xC3EB

                     sTime[nCount++] = (ACHAR)0xEB;             // 秒 = 0xC3EB

#endif           

              }

              sTime[nCount  ] = TCHR_NULL;

       }

}

 

// 将 Unix Time time_t 转换为 SYSTEMTIME

STATIC VOID DTM_ConvertTime(IN time_t t,OUT LPSYSTEMTIME pst)

{

       FILETIME ft;

 

       LONGLONG ll;

 

       ll = Int32x32To64(t, 10000000) + 116444736000000000;

       ft.dwLowDateTime  = (DWORD)ll;

       ft.dwHighDateTime = (DWORD)(ll >> 32);

       ::FileTimeToSystemTime(&ft, pst);

}

 

// 将 Unix Time time_t 转换为 FILETIME

STATIC VOID DTM_ConvertTime(IN time_t t,OUT LPFILETIME pft)

{

       LONGLONG ll;

 

       ll = Int32x32To64(t, 10000000) + 116444736000000000;

       pft->dwLowDateTime  = (DWORD)ll;

       pft->dwHighDateTime = (DWORD)(ll >> 32);

}

 

// 将 FILETIME 转换为 Unix Time time_t

STATIC time_t DTM_ConvertTime(IN CONST LPSYSTEMTIME pst)

{

       FILETIME ft;

       ::SystemTimeToFileTime(pst, &ft);

       LONGLONG ll = ft.dwHighDateTime;

       ll <<= 32;

       ll  |= ft.dwLowDateTime;

       ll  -= 116444736000000000;

       return (time_t)(ll / 10000000);

}

 

// 将 FILETIME 转换为 Unix Time time_t

STATIC time_t DTM_ConvertTime(IN CONST LPFILETIME pft)

{

       LONGLONG ll = pft->dwHighDateTime;

       ll <<= 32;

       ll  |= pft->dwLowDateTime;

       ll  -= 116444736000000000;

       return (time_t)(ll / 10000000);

}

 

#endif     // #ifndef __HPP_TIMEUTIL_HPP__

本文地址:http://com.8s8s.com/it/it210.htm