现在的位置: 首页 > 综合 > 正文

关于时间一系列类的说明和用法

2013年10月11日 ⁄ 综合 ⁄ 共 2914字 ⁄ 字号 评论关闭

一. 基本概念:

■ 时间的分类

◆ 本地时间

◆ 协调世界时间(Coordinated Universal Time ,UTC).也即我们常常说的格林威治时 间.

格林威治时间与本地时间的差值,也就是我们通常说的时差.由于我们这边是北京时间(也称东八区).所有差值是8.

■ 常用类(结构)

● CRT提供的时间

◆ time_t:ong.我们一般无法理解这个值表示的意义.必须要进行转化才行.

◆ tm结构定义如下:

struct tm 

{   

int tm_sec;     /* seconds after the minute - [0,59] */  

int tm_min;     /* minutes after the hour - [0,59] */  

int tm_hour;    /* hours since midnight - [0,23] */  

int tm_mday;    /* day of the month - [1,31] */  

int tm_mon;     /* months since January - [0,11] */  

int tm_year;    /* years since 1900 */  

int tm_wday;    /* days since Sunday - [0,6] */  

int tm_yday;    /* days since January 1 - [0,365] */  

int tm_isdst;   /* daylight savings time flag */  

}; 

这个结构我们看起来就比较容易理解了.

如果得到time_t格式的时间,我们可以利用localtime来进行转化.

如果想要进行反向转化(tm->time_t)就需要使用

time_t  mktime(struct tm* timeptr);

另外如果想要将其转化为UTC对应的时间,则需要使用

errno_t gmtime_s(struct tm* _tm,const time_t* time);

● SDK时间.上述两类时间是针对CRT,下面是对应的SDK的两个等价时间

◆  typedef struct _FILETIME 

{  

DWORD dwLowDateTime;  

DWORD dwHighDateTime;  

} FILETIME, *PFILETIME; 

对应于time_t

◆ typedef struct _SYSTEMTIME 

{  

WORD wYear;  

WORD wMonth;  

WORD wDayOfWeek;  

WORD wDay;  

WORD wHour;  

WORD wMinute;  

WORD wSecond;  

        WORD wMilliseconds; 

} SYSTEMTIME, *PSYSTEMTIME;

对应于tm,但是这里多了最后一项(说明更加精确了)

■ 常用的一些获取及其转化函数

● 获取UTC时间:

Void GetSystemTime( LPSYSTEM lpSystemTime);

● 获取本地时间(即北京时间)

Void GetLocalTime(LPSYSTEM lpSystemTime);

● FILETIME 转为SYSTEMTIME函数

BOOL SystemTimeToFileTime(const SYSTEMTIME* lpSystemTime,

LPFILETIME lpFIleTime);

● SYSTEMTIME转为FILETIME函数

BOOL FileTimeToSystemTime(const FILETIME* lpFileTime,

LPSYSTEMTIME lpSystem);

另外也提供UTC和本地时间互转的两个函数

BOOL LocalFileTimeToFileTime( const FILETIME* lpLocalFileTime,

LPFILETIME lpFileTime);

BOOL FileTimeToLocalFileTime(const FILETIME* lpFileTime,

LPFILETIME lpLocalFileTime);

最后一个就是ULARGE_INTEGER结构,这个结构的定义如下:

typedef union ULARGE_INTEGER {

struct {

    DWORD LowPart;

    DWORD HighPart;

  };

  struct {

    DWORD LowPart;

    DWORD HighPart;

  } u;

  ULONGLONG QuadPart;

} ULARGE_INTEGER;

这个结构和FILETIME没有很大差别,主要是前者32为对齐,后者64位对齐,若要进行转化,则必须要进行赋值操作.

一些简单的参考代码:

CTime ct = CTime::GetCurrentTime();
//
time_t tt = ct.GetTime();
// 
  tm *pt = localtime( &tt );
// CString strTime;
// strTime.Format( "%d-%d-%d %d:%d:%d",pt->tm_year + 1900,pt->tm_mon + 1,pt->tm_mday,pt->tm_hour,pt->tm_min,pt->tm_sec );
// MessageBox( strTime );
// SYSTEMTIME stUTC,stLocal;
// GetSystemTime( &stUTC );
// GetLocalTime( &stLocal );

HANDLE hFile = CreateFile( "C:\\ysl.txt",GENERIC_READ|GENERIC_WRITE, 0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_READONLY,NULL );
FILETIME ftCreate,ftAccess,ftWrite,ftLocal;
SYSTEMTIME st;

GetFileTime( hFile,&ftCreate,&ftAccess,&ftWrite );
FileTimeToLocalFileTime( &ftCreate,&ftLocal );
FileTimeToSystemTime( &ftLocal,&st );

ULARGE_INTEGER uli;
uli.HighPart = ftLocal.dwHighDateTime;
uli.LowPart  = ftLocal.dwLowDateTime;

CString strTime;
strTime.Format( "%d",uli.QuadPart );
MessageBox( strTime );
/*
strTime.Empty();
strTime.Format( "%d-%d-%d %d:%d:%d",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond );
MessageBox( strTime );
*/

 

抱歉!评论已关闭.