`

获取系统时间(Time)的方法

    博客分类:
  • C++
阅读更多
void GetTime_Case_1()
{
// #include  
// #include  

//方案— :能精确到毫秒级并且使用简洁方便;
SYSTEMTIME cTime; //获取系统时间类 
CString TimeString; //转换 
char cBuffer[100] = {0};

GetLocalTime(&cTime); //获取本地时间 

TimeString.Format(_T("%4d-%2d-%2d %2d:%2d:%2d") //格式化输出到CString
,Time.wYear 
,Time.wMonth 
,Time.wDay 
,Time.wHour 
,Time.wMinute 
,Time.wSecond); 

sprintf(cBuffer,"%4d-%2d-%2d %2d:%2d:%2d \n",//格式化输出到cBuffer
Time.wYear,
Time.wMonth, 
Time.wDay, 
Time.wHour, 
Time.wMinute, 
Time.wSecond); 
}


void GetTime_Case_1()
{
// #include  
// #include  
//方案二 :仅使用C标准库;但是只能精确到秒级
time_t GetTime = time(0); 
char cBuffer[64]; 
strftime( cBuffer, sizeof(cBuffer), "%Y/%m/%d %X %A 本?年?第?%j天?%z",localtime(&GetTime) ); 

/* 
size_t strftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr); 
根据格式字符串生成字符串。
struct tm *localtime(const time_t *timer);
取得当地时间,localtime获取的结果由结构tm返回
返回的字符串可以依下列的格式而定:
%a 星期几的缩写。Eg:Tue
%A 星期几的全名。 Eg: Tuesday
%b 月份名称的缩写。
%B 月份名称的全名。
%c 本地端日期时间较佳表示字符串。
%d 用数字表示本月的第几天 (范围为 00 至 31)。日期
%H 用 24 小时制数字表示小时数 (范围为 00 至 23)。
%I 用 12 小时制数字表示小时数 (范围为 01 至 12)。
%j 以数字表示当年度的第几天 (范围为 001 至 366)。
%m 月份的数字 (范围由 1 至 12)。
%M 分钟。s
%p 以 ''AM'' 或 ''PM'' 表示本地端时间。
%S 秒数。
%U 数字表示为本年度的第几周,第一个星期由第一个周日开始。
%W 数字表示为本年度的第几周,第一个星期由第一个周一开始。
%w 用数字表示本周的第几天 ( 0 为周日)。
%x 不含时间的日期表示法。
%X 不含日期的时间表示法。 Eg: 15:26:30
%y 二位数字表示年份 (范围由 00 至 99)。
%Y 完整的年份数字表示,即四位数。 Eg:2008
%Z(%z) 时区或名称缩写。Eg:中国标准时间
%% % 字符。
*/
}


#include <time.h>  
time_t t_time = time(0);  
tm localTM;  
  
localtime_s(&localTM, &t_time);  
  
 cout<<localTM.tm_year+1900<<"-"<<localTM.tm_mon+1<<"-"<<localTM.tm_mday;  
 cout<<"-"<<localTM.tm_hour<<":"<<localTM.tm_min<<":"<<localTM.tm_sec<<" ";  
  
输出结果如下:  
2011-7-8-14:40:28  

/*
"tm_sec" - 秒数
"tm_min" - 分钟数
"tm_hour" - 小时
"tm_mday" - 月份中的第几日
"tm_mon" - 年份中的第几个月,从 0 开始表示一月
"tm_year" - 年份,从 1900 开始
"tm_wday" - 星期中的第几天
"tm_yday" - 一年中的第几天
"tm_isdst" - 夏令时当前是否生效
注释:月份从 0(一月)到 11(十二月),星期数从 0(星期天)到 6(星期六)。  
*/
  
//获取当前路径的简单方法  
#include <direct.h>  
string GetFileMainPath()  
{  
    char buffer[256];  
    _getcwd(buffer, 256);  
    string strTemp = buffer;  
    return strTemp;  
}


//获取时间间隔

//方法一
#include <time.h>

clock_t m_BeginTime;
clock_t m_EndTime; 

m_BeginTime = clock();
Sleep(123);
m_EndTime = clock();
printf("Execute Time: %.3f seconds\n", difftime(m_EndTime, m_BeginTime)/CLOCKS_PER_SEC);

//方法二
SYSTEMTIME sTime1; 
SYSTEMTIME sTime2; 
... 
... 

ULARGE_INTEGER fTime1;/*FILETIME*/ 
ULARGE_INTEGER fTime2;/*FILETIME*/ 


SystemTimeToFileTime(&sTime1,(FILETIME*)&fTime1); 
SystemTimeToFileTime(&sTime2,(FILETIME*)&fTime2); 

unsigned __int64 dft=fTime2.QuadPart-fTime1.QuadPart;



使用CTime类(获取系统当前时间,精确到秒)
http://www.cnblogs.com/lujin49/p/4624971.html
1.使用CTime类(获取系统当前时间,精确到秒)
CString str;
//获取系统时间
CTime tm;
tm=CTime::GetCurrentTime();//获取系统日期
str=tm.Format("现在时间是%Y年%m月%d日 %X");
MessageBox(str,NULL,MB_OK);a,从CTimet中提取年月日时分秒 

CTime t = CTime::GetCurrentTime();
 int d=t.GetDay(); //获得几号
 int y=t.GetYear(); //获取年份
 int m=t.GetMonth(); //获取当前月份
 int h=t.GetHour(); //获取当前为几时
 int mm=t.GetMinute(); //获取分钟
 int s=t.GetSecond(); //获取秒
 int w=t.GetDayOfWeek(); //获取星期几,注意1为星期天,7为星期六b,计算两段时间的差值,可以使用CTimeSpan类,具体使用方法如下:
CTime t1( 1999, 3, 19, 22, 15, 0 );
CTime t = CTime::GetCurrentTime();
CTimeSpan span=t-t1; //计算当前系统时间与时间t1的间隔
int iDay=span.GetDays(); //获取这段时间间隔共有多少天
int iHour=span.GetTotalHours(); //获取总共有多少小时
int iMin=span.GetTotalMinutes();//获取总共有多少分钟
int iSec=span.GetTotalSeconds();//获取总共有多少秒c,获得当前日期和时间,并可以转化为CString

CTime tm=CTime::GetCurrentTime(); 
CString str=tm.Format("%Y-%m-%d");//显示年月日
2.使用GetLocalTime:Windows API 函数,获取当地的当前系统日期和时间 (精确到毫秒)
 此函数会把获取的系统时间信息存储到SYSTEMTIME结构体里边
typedef struct _SYSTEMTIME
{
WORD wYear;//年
WORD wMonth;//月
WORD wDayOfWeek;//星期:0为星期日,1为星期一,2为星期二……
WORD wDay;//日
WORD wHour;//时
WORD wMinute;//分
WORD wSecond;//秒
WORD wMilliseconds;//毫秒
}SYSTEMTIME,*PSYSTEMTIME;例:
SYSTEMTIME st;
CString strDate,strTime;
GetLocalTime(&st);
strDate.Format("%4d-%2d-%2d",st.wYear,st.wMonth,st.wDay);
strTime.Format("%2d:%2d:%2d",st.wHour,st.wMinute,st.wSecond) ;
AfxMessageBox(strDate);
AfxMessageBox(strTime);3.使用GetTickCount:从操作系统启动到现在所经过(elapsed)的毫秒数,它的返回值是DWORD。(精确到毫秒)
//获取程序运行时间
long t1=GetTickCount();//程序段开始前取得系统运行时间(ms)
Sleep(500);
long t2=GetTickCount();();//程序段结束后取得系统运行时间(ms)
str.Format("time:%dms",t2-t1);//前后之差即 程序运行时间
AfxMessageBox(str);
//获取系统运行时间
long t=GetTickCount();
CString str,str1;
str1.Format("系统已运行 %d时",t/3600000);
str=str1;
t%=3600000;
str1.Format("%d分",t/60000);
str+=str1;
t%=60000;
str1.Format("%d秒",t/1000);
str+=str1;
AfxMessageBox(str);4.使用time_t time( time_t * timer ) :   仅使用C标准库(精确到秒)
得到从标准计时点(一般是1970年1月1日午夜)到当前时间的秒数 
计算时间差:double difftime( time_t timer1, time_t timer0)
struct tm *localtime(const time_t *timer);  取得当地时间,localtime获取的结果由结构tm返回 
返回的字符串可以依下列的格式而定: 
%a 星期几的缩写。Eg:Tue 
%A 星期几的全名。 Eg: Tuesday 
%b 月份名称的缩写。 
%B 月份名称的全名。 
%c 本地端日期时间较佳表示字符串。 
%d 用数字表示本月的第几天 (范围为 00 至 31)。日期 
%H 用 24 小时制数字表示小时数 (范围为 00 至 23)。 
%I 用 12 小时制数字表示小时数 (范围为 01 至 12)。 
%j 以数字表示当年度的第几天 (范围为 001 至 366)。 
%m 月份的数字 (范围由 1 至 12)。 
%M 分钟。 
%p 以 ''AM'' 或 ''PM'' 表示本地端时间。 
%S 秒数。 
%U 数字表示为本年度的第几周,第一个星期由第一个周日开始。 
%W 数字表示为本年度的第几周,第一个星期由第一个周一开始。 
%w 用数字表示本周的第几天 ( 0 为周日)。 
%x 不含时间的日期表示法。 
%X 不含日期的时间表示法。 Eg: 15:26:30 
%y 二位数字表示年份 (范围由 00 至 99)。 
%Y 完整的年份数字表示,即四位数。 Eg:2008 
%Z(%z) 时区或名称缩写。Eg:中国标准时间 
%% % 字符。 

含:年,月,日,周几,小时,分,秒,毫秒。[喝小酒的网摘]http://blog.const.net.cn/a/16370.htm



    #include <time.h>    

    tm localTM;
    memset(&localTM, 0, sizeof(localTM));

    localTM.tm_year = 2016-1900;
    localTM.tm_mon = 4-1;
    localTM.tm_mday = 27;

    time_t localTime = mktime(&localTM);

    localTM.tm_mday = 29;

    time_t localTime2 = mktime(&localTM);

    double fTemp1 =  difftime(localTime2, localTime);
    double fTemp2 =  difftime(localTime, localTime2);
    double fTemp3 =  difftime(localTime, localTime);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics