c语言时间函数的具体使用方法,时间的加减

Python010

c语言时间函数的具体使用方法,时间的加减,第1张

#include <stdio.h>

#include <time.h> 

int main()

time_t rawtime 

struct tm * timeinfo 

time ( &rawtime ) 

timeinfo = localtime ( &rawtime ) 

printf ( "The current date/time is: %s", asctime (timeinfo) ) 

return 0

}

说明:

time_t // 时间类型(time.h 定义) 

struct tm { // 时间结构,time.h 定义如下: 

int tm_sec 

int tm_min 

int tm_hour 

int tm_mday 

int tm_mon 

int tm_year 

int tm_wday 

int tm_yday 

int tm_isdst 

time ( &rawtime )// 获取时间,以秒计,从1970年1月一日起算,存于rawtime 

localtime ( &rawtime )//转为当地时间,tm 时间结构 

asctime() // 转为标准ASCII时间格式: 

//就是直接打印tm,tm_year 从1900年计算,所以要加1900,月tm_mon,从0计算,所以要加1

1. ctime函数

函数: ctime

功 能: 把日期和时间转换为字符串

用 法: char *ctime(const time_t *time)

程序例:

#include<cstdio>

#include<ctime>

intmain(void)

{

time_tt

t=time(&t)

printf("Today'sdateandtime:%s\n",ctime(&t))

return0

}

注:若在linux下使用本函数,需要include <time.h>头文件

2.CTime类

CTime类的对象表示的时间是基于格林威治标准时间(GMT)的。CTimeSpan类的对象表示的是时间间隔。

CTime类一般不会被继承使用。其对象的大小是8个字节。

CTime表示的日期上限是2038年1月18日,下限是1970年1月1日 12:00:00 AM GMT。

CTime类的成员函数

CTime()

构造一个未经初始化的CTime对象。此构造函数使我们可以定义一个CTime对象的数组,在使用数组前需要以有效的时间值为其初始化。

CTime(__time64_t time)

以一个__time64_t(注意:最前面的下划线有两条)类型的数据来构造一个CTime对象。参数time是一个__time64_t类型的值,表示自GMT时间1970年1月1日零点以来的秒数,这里要注意的是,参数time代表的时间会转换为本地时间保存到构造的CTime对象中。例如,我们传递参数0构造一个CTime对象,然后调用CTime对象的GetHour成员函数将返回8,因为参数0代表的GMT时间转换为北京时间后为1970年1月1日 8:00:00。

CTime(

int nYear,

int nMonth,

int nDay,

int nHour,

int nMin,

int nSec,

int nDST = -1

)

以本地时间的年、月、日、小时、分钟、秒等几个时间分量构造CTime对象。参数nYear、nMonth、nDay、nHour、nMin、nSec分别表示年、月、日、小时、分钟、秒,取值范围如下:

时间分量 取值范围

nYear 1970-3000

nMonth 1-12

nDay 1-31

nHour 0-23

nMin 0-59

nSec 0-59

参数nDST指定是否实行夏令时,为0时表示实行标准时间,为正数时表示实行夏令时,为负数时由系统自动计算实行的是标准时间还是夏令时。

CTime(const SYSTEMTIME&st,int nDST = - 1)

以一个SYSTEMTIME结构体变量来构造CTime对象。SYSTEMTIME结构体也是我们对日期时间的常用表示方式。参数st为以本地时间表示的SYSTEMTIME对象,参数nDST同上。

static CTime WINAPI GetCurrentTime( )

获取系统当前日期和时间。返回表示当前日期和时间的CTime对象。

int GetYear( ) const;

获取CTime对象表示时间的年份。范围从1970年1月1日到2038年(包括2038年)1月18日。

int GetMonth( ) const

获取CTime对象表示时间的月份。范围为1到12。

int GetDay( ) const

获取CTime对象表示时间的日期。范围为1到31。

int GetHour( ) const

获取CTime对象表示时间的小时。范围为0到23。

int GetMinute( ) const

获取CTime对象表示时间的分钟。范围为0到59。

int GetSecond( ) const

获取CTime对象表示时间的秒。范围为0到59。

int GetDayOfWeek( ) const

此函数的返回值表示CTime对象代表的是星期几,1表示是周日,2表示是周一,以此类推。

CString Format(LPCTSTR pszFormat) const

将CTime对象中的时间信息格式化为字符串。参数pszFormat是格式化字符串,与printf中的格式化字符串类似,格式化字符串中带有%前缀的格式码将会被相应的CTime时间分量代替,而其他字符会原封不动的拷贝到返回字符串中。格式码及含义如下:

%a:周的英文缩写形式。

%A:周的英文全名形式。

%b: 月的英文缩写形式。

%B:月的英文全名形式。

%c: 完整的日期和时间。

%d:十进制形式的日期(01-31)。

%H:24小时制的小时(00-23)。

%I: 12小时制的小时(00-11)。

%j: 十进制表示的一年中的第几天(001-366)。

%m: 月的十进制表示(01-12)。

%M:十进制表示的分钟(00-59)。

%p: 12小时制的上下午标示(AM/PM)。

%S: 十进制表示的秒(00-59)。

%U: 一年中的第几个星期(00-51),星期日是一周的第一天。

%W: 一年中的第几个星期(00-51),星期一是一周的第一天。

%w: 十进制表示的星期几(0-6)。

%Y: 十进制表示的年。

CTime operator +(CTimeSpan timeSpan) const

将CTime对象和CTimeSpan对象相加,返回一个CTime对象。实际意义就是在一个时间的基础上推后一个时间间隔,得到一个新的时间。

CTime operator -(CTimeSpan timeSpan) const

将CTime对象和一个CTimeSpan相减,返回一个CTime对象。实际意义就是在一个时间的基础上提前一个时间间隔,得到一个新的时间。

CTimeSpan operator -(CTime time) const

将该CTime对象和另一个CTime对象相减,返回一个CTimeSpan对象。实际意义就是计算两个时间点的间隔,得到一个CTimeSpan对象。

CTime&operator +=(CTimeSpan span)

为该CTime对象增加一个span表示的时间间隔。

CTime&operator -=(CTimeSpan span)

为该CTime对象减去一个span表示的时间间隔。

CTime&operator =(__time64_t time)

为该CTime对象赋予一个新的时间值。

简单说下剩下的几个重载i运算符:

operator == : 比较两个绝对时间是否相等。

operator != : 比较两个绝对时间是否不相等。

operator >: 比较两个绝对时间,是否前一个大于后一个。

operator <: 比较两个绝对时间,是否前一个小于后一个。

operator >= : 比较两个绝对时间,是否前一个大于等于后一个。

operator <= : 比较两个绝对时间,是否前一个小于等于后一个。[1]

=====================================================================

C++中,CTime 与 CString转换

CTime m_StartTime1 = CTime::GetCurrentTime()

CString csStartTime = m_StartTime1.Format( "%Y%m%d%H%M%S" )

一.将CString转为CTime的几种方法

CString timestr = "2000年04月05日"

int a,b,c

sscanf(timestr.GetBuffer(timestr.GetLength()),"%d年%d月%d日",&a,&b,&c)

CTime time(a,b,c,0,0,0)

--------or - ---------------------

CString s("2001-8-29 19:06:23")

int nYear, nMonth, nDate, nHour, nMin, nSec

sscanf(s, "%d-%d-%d %d:%d:%d", &nYear, &nMonth, &nDate, &nHour, &nMin, &nSec)

CTime t(nYear, nMonth, nDate, nHour, nMin, nSec)

---- or ------------------------

CString timestr = "2000年04月05日"

int year,month,day

BYTE tt[5]

//get year

memset(tt, 0, sizeof(tt))

tt[0] = timestr[0]

tt[1] = timestr[1]

tt[2] = timestr[2]

tt[3] = timestr[3]

year= atoi((char *)tt)

//get month

memset(tt, 0, sizeof(tt))

tt[0] = timestr[6]

tt[1] = timestr[7]

month = atoi((char *)tt)

//get day

memset(tt, 0, sizeof(tt))

tt[0] = timestr[10]

tt[1] = timestr[11]

day = atoi((char *)tt)

CTime time(year,month,day,0,0,0)

从上面来看,很明显使用sscanf()函数的优势.

二.将CTIme转换为CString的方法:

CTime tmSCan = CTime::GetCurrentTime()

CString szTime = tmScan.Format("'%Y-%m-%d %H:%M:%S'")

这样得到的日期时间字符串就是以"2006-11-27 23:30:59"的格式.这是不是很方便呢?

//取得CTime中的日期

CString cstrDate = tmScan.Format("%Y-%m-%d")

//取得CTime中的时间

CString cstrTime = tmScan.Format("%H:%M-%S")

sprintf还有个不错的表妹:strftime,专门用于格式化时间字符串的,用法跟她表哥很像,也是一大堆格式控制符,只是毕竟小姑娘家心细,她还要调用者指定缓冲区的最大长度,可能是为了在出现问题时可以推卸责任吧。这里举个例子:

更多更好的sprintf()函数说明参考:《spirntf,你知道多少?》

time_t t = time(0)

//产生"YYYY-MM-DD hh:mm:ss"格式的字符串。

char s[32]

strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", localtime(&t))

sprintf在MFC中也能找到他的知音:CString::Format,strftime在MFC中自然也有她的同道:CTime::Format,这一对由于从面向对象哪里得到了赞助,用以写出的代码更觉优雅

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

main()

{

int year,month,day

int leapyear=0//闰年时使用

int daytime=0

int sum

static int month_date[12]={31,28,31,30,31,30,31,31,30,31,30,31}

printf("请输入日期:(例如:2010 5 27)")

scanf("%d%d%d",&year,&month,&day)

if(year<=0||month>12||month<1)//判断输入是否正确

{

printf("Error!\n")

getch()

exit(0)

}

if(year%400==0||(year%4==0&&year%100!=0))//判断是否为闰年

leapyear=1//是就加1

if(month==2)//判断日是否输入正确,2月是特殊的因为分闰年和平年

{ if((month_date[month-1]+leapyear)<day||day<=0)

{

printf("Error!\n")

getch()

exit(0)

}

}

if(month!=2)//当输入不是2月时,判断输入

{

if(month_date[month-1]<day||day<=0)

{

printf("Error!\n")

getch()

exit(0)

}

}

printf("\n请输入天数:")

scanf("%d",&daytime)//输入第N天后

if(daytime<0)

{

printf("Error!\n")

getch()

exit(0)

}

sum=daytime+day//当前日期与N天相加

do

{

if(month==2)//判断当月是不是二月份

month_date[month-1]+=leapyear

if(sum>month_date[month-1])

{

sum-=month_date[month-1]

month++//超过当月天数,加一个月

if(month==13)//当月份超过12月时,重新返到1月

{

year++//加一年

if(year%400==0||(year%4==0&&year%100!=0))//判断加一年后是不是闰年

leapyear=1

else

leapyear=0//不是闰年则为0

month=1//因为12月的下一个月是1月

}

}

}while(sum>month_date[month-1])//当加起来的天数少于当月的天数就停止循环

day=sum

printf("\n第%d天后是%d %d %d",daytime,year,month,day)//输出

getch()

}