您的位置:首页 > 运维架构 > Linux

c/c++ 时间函数总结 linux

2013-10-06 13:55 483 查看

Linux c/c++关于时间函数的总结

很想花点时间整理下Linux c/c++关于时间函数,今天…


关于时间的存储

linux下存储时间常见的有两种存储方式,一个是从1970年到现在经过了多少秒,一个是用一个结构来分别存储年月日时分秒的。

time_t 这种类型就是用来存储从1970年到现在经过了多少秒,要想更精确一点,可以用结构struct timeval,它精确到微妙。

struct timeval

{

   long tv_sec; /*秒*/

   long tv_usec; /*微秒*/

};

而直接存储年月日的是一个结构:

struct  tm

{

    inttm_sec;  /*秒,正常范围0-59, 但允许至61*/

    inttm_min;  /*分钟,0-59*/

    inttm_hour; /*小时, 0-23*/

    inttm_mday; /*日,即一个月中的第几天,1-31*/

    inttm_mon;  /*月, 从一月算起,0-11*/ 1+p->tm_mon;

    inttm_year;  /*年, 从1900至今已经多少年*/  1900+ p->tm_year;

    inttm_wday; /*星期,一周中的第几天, 从星期日算起,0-6*/

    inttm_yday; /*从今年1月1日到目前的天数,范围0-365*/

    int tm_isdst; /* 夏令时标识符,夏令时tm_isdst为正;不实行夏令时tm_isdst为0;*/

};

注:年份是从1900年起至今多少年,而不是直接存储如2013年,月份从0开始的,0表示一月,星期也是从0开始的, 0表示星期日,1表示星期一。

下面介绍一下我们常用的时间函数:

#include <time.h>

char *asctime(const struct tm* timeptr)

将结构中的信息转换为真实世界的时间,以字符串的形式显示

char *ctime(const time_t *timep)

将timep转换为真是世界的时间,以字符串显示,它和asctime不同就在于传入的参数形式不一

#include<iostream>
#include<ctime>
using namespace std;
int  main()
{
time_t t;
t=time(NULL);
cout <<ctime(&t);
return 0;
}


double difftime(time_t time1, time_t time2)

返回两个时间相差的秒数


int stime(time_t *tp);

本函数用于设定电脑系统之日期与时间,传回值为0

格式化日期时间(strftime)

size_tstrftime(char *s, size_t maxsize, const char *fmt, const struct tm *t)

将t结构之资料依格式fmt方式设定给字串s,字串长度最长为maxsize,若转换错误则传回0,fmt之输出格式以ANSI方式为之。 下列为ANSI内定格式指定字及说明,使用在strftime内之格式。

%%:显示字元%

%a:星期之缩写

%A:星期之全名

%b:月份之缩写

%B:月份之全名

%c:日期与时间

%d:两位数之日(01 - 31)

%H:两位数之24小时制(00 - 23)

%I :两位数之12小时制(01 - 12)

%j:三位数之日数(001 - 366)

%m:两位数之月(1 - 12)

%M:两位数之分(00 - 59)

%p:代表AM或PM

%S:两位数之秒(00 - 59)

%U:两位数之星期数(00 - 53),以星期日为第一天

%w:星期之表示(0 - 6),0为星期日

%W:两位数之星期数(00 - 53),以星期一为第一天(00 - 53)

%x:日期, %X:时间, %y:两位数之年(00to 99)

%Y:西元年, %Z:时间区名称

 

类似于snprintf函数,我们可以根据format指向的格式字符串,将struct tm结构体中信息输出到s指针指向的字符串中,最多为max个字节。当然s指针指向的地址需提前分配空间,比如字符数组或者malloc开辟的堆空间

简单使用方法如下

#include<stdio.h>
#include<string.h>
#include<time.h>
int main( void )
{
struct tm *newtime;
char tmpbuf[128];
time_t test;

time(&test);
newtime=localtime(&test);

strftime(tmpbuf, 128, "Today is %A, day %d of %B in the year %Y.\n", newtime);
printf(tmpbuf);

return 0;
}


char *_strtime(char *buf)

将现在时刻以HH:MM:SS方式输出放在buf内,字串至少9个Bytes长

char *_strtdate(char *buf);

取得目前PC系统日期及时间

#include<iostream>
#include<time>
using namespace std;
void main()
{
charsdate[9];
charstime[9];
_strdate(sdate);
_strtime(stime);
cout<<"目前日期:"<<sdate<<endl<<"现在时刻:"<<stime<<endl;
}

支持windows

struct tm* gmtime(const time_t *timep)

将time_t表示的时间转换为没有经过时区转换的UTC时间(在计算机中看到的utc时间都是从(1970年01月01日 0:00:00)开始计算秒数的。所看到的UTC时间那就是从1970年这个时间点起到具体时间共有多少秒。),是一个struct tm结构指针

stuct tm* localtime(const time_t *timep)

和gmtime类似,但是它是经过时区转换的时间。

time_t mktime(struct tm* timeptr)

将structtm 结构的时间转换为从1970年至今的秒数

time_t time(time_t *t)

取得从1970年1月1日至今的秒数。 

参考代码:

#include<time.h>
int main()
{
char *wday[] = {"Sun","Mon", "Tue", "Wed", "Thu","Fri", "Sat"};
time_t timep;
struct tm *p;

time(&timep); /*获得time_t结构的时间,UTC时间*/
p = localtime(&timep); /*转换为struct tm结构的当地时间*/
printf("%d/%d/%d ", 1900 + p->tm_year, 1 +p->tm_mon, p->tm_mday);
printf("%s %d:%d:%d\n", wday[p->tm_wday], p->tm_hour,p->tm_min, p->tm_sec);
return 0;
}

clock函数

#include<time.h>

clock_tclock(void);                  

返回从程序开始运行到程序中调用clock()函数之间的CPU时钟计时单元数

例子如下:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
long   loop = 10000000L;
double duration;
clock_t start, end;
printf("Time to do %ld empty loops is", loop);
start = clock();
while(loop--)   ;
end = clock();
duration =(double)(end-start)/CLOCKS_PER_SEC;
printf("%f seconds\n", duration);
return(0);
}


utime函数

更改文件的存取和修改时间

#include<time.h>

intutime(const char pathname, const struct utimbuf *times);

返回值:成功返回0,失败返回-1

times为空指针,存取和修改时间设置为当前时间

#include<stdio.h>
#include<time.h>
int main(int argc, char *argv[])
{
if(argc < 2){
fprintf(stderr, "Error:usging command file_path");
exit(1);
}

utime(argv[1], NULL);

return(0);
}


编译、运行:

$touch file_test
$ ls-al file_test  // 先创建一个文件file_test,查看一下他的创建时间
-rw-r--r--1 hongdy hongdy 3431 05-01 05:59 file_test
$ gccutime.c –o utime
$./utime file_test
$ ls-al file_test
-rw-r--r--1 hongdy hongdy 3431 05-01 06:00 file_test


settimeofday函数

设置目前时间

#include<sys/time.h>

#include<unistd.h>

int settimeofday ( const& nbspstruct timeval*tv,const struct timezone *tz);

函数说明  settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。注意,只有root权限才能使用此函数修改时间。

返回值  成功则返回0,失败返回-1,错误代码存于errno。

 

错误代码  EPERM 并非由root权限调用settimeofday(),权限不够。

EINVAL时区或某个数据是不正确的,无法正确设置时间。

times为空指针,存取和修改时间设置为当前时间

/************************************************

设置操作系统时间

参数:*dt数据格式为"2013-10-20 20:30:30"

调用方法:

char*pt="2013-10-20 20:30:30";

SetSystemTime(pt);

**************************************************/

intSetSystemTime(char *dt)
{
structrtc_time tm;
structtm _tm;
structtimeval tv;
time_ttimep;
sscanf(dt,"%d-%d-%d %d:%d:%d", &tm.tm_year,
&tm.tm_mon,&tm.tm_mday,&tm.tm_hour,
&tm.tm_min,&tm.tm_sec);
_tm.tm_sec= tm.tm_sec;
_tm.tm_min= tm.tm_min;
_tm.tm_hour= tm.tm_hour;
_tm.tm_mday= tm.tm_mday;
_tm.tm_mon= tm.tm_mon - 1;
_tm.tm_year= tm.tm_year - 1900;

timep= mktime(&_tm);
tv.tv_sec= timep;
tv.tv_usec= 0;
if(settimeofday(&tv, (struct timezone *) 0) < 0)
{
printf ("Setsystem datatime error!\n");
return -1;
}
return 0;
}


gettimeofday函数取得目前的时间

#include<time.h>

intgettimeofday ( struct& nbsptimeval * tv , struct timezone * tz );

函数说明  gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中。

timeval结构定义为:

structtimeval{

longtv_sec; /*秒*/

longtv_usec; /*微秒*/

};

timezone结构定义为:

structtimezone{

inttz_minuteswest; /*和Greenwich时间差了多少分钟*/

inttz_dsttime; /*日光节约时间的状态*/

};

上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime 所代表的状态如下

DST_NONE/*不使用*/

DST_USA/*美国*/

DST_AUST/*澳洲*/

DST_WET/*西欧*/

DST_MET/*中欧*/

DST_EET/*东欧*/

DST_CAN/*加拿大*/

DST_GB/*大不列颠*/

DST_RUM/*罗马尼亚*/

DST_TUR/*土耳其*/

DST_AUSTALT/*澳洲(1986年以后)*/

返回值  成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。

times为空指针,存取和修改时间设置为当前时间

 gettimeofday函数的例子

#include<sys/time.h>
#include<unistd.h>
main(){
structtimeval tv;
structtimezone tz;
gettimeofday(&tv , &tz);
printf(“tv_sec;%d\n”, tv,.tv_sec)
printf(“tv_usec;%d\n”,tv.tv_usec);
printf(“tz_minuteswest;%d\n”, tz.tz_minuteswest);
printf(“tz_dsttime,%d\n”,tz.tz_dsttime);
}


编译、运行:
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0
tv_sec:974857339


timer_struct结构

struct timer_struct {

   unsigned long expires; //定时器被激活的时刻

void (*fn)(void); //定时器激活后的处理函数

}

 

                                                                                                        
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息