Unix环境编程-时间和日期
一.文件的时间
对每个文件保存三个时间字段,它们是:
1.
名称:: |
utime |
功能: |
修改文件的存取和修改时间 |
头文件: |
#include <utime.h> #include <sys/types.h> |
函数原形: |
int utime(const char *filename,const struct utimbuf buf); |
参数: |
pathname 文件名 buf 文件时间信息 |
返回值: |
若成功则返回0,若出错则返回-1 |
此函数所使用的结构是:
struct utimbuf{
time_t actime; 文件数据的最后存取时间
time_t modtime; 文件数据的最后修改时间
}
二.系统的时间
2.
名称:: |
time |
功能: |
get time in seconds |
头文件: |
#include <time.h> |
函数原形: |
time_t time(time_t *calptr); |
参数: |
calptr 时间值 |
返回值: |
若成功则返回时间值,若出错则返回-1 |
time返回的是计算自国家标准时间公元1970年1月1日00:00:00以来经过的秒数。时间值总是作为时间值返回,如果参数不为空则时间值也存放在由calptr所指向的单元内。
下面的程序可以实现touch命令的部分功能,可以把文件的最后存储时间和最后修改时间改为执行命令时的系统时间。
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <utime.h>
int main(int argc,char *argv[]) { int i; struct utimbuf timebuf;
for(i=1;i<argc;i++)
timebuf.actime=time(); timebuf.actime=time(); if(utime(argv[i],&timebuf)<0) perror(“error”); exit(0); } } |
3.
名称:: |
gettimeofday/settimeofday |
功能: |
get/set time |
头文件: |
#include <sys/time.h> |
函数原形: |
int gettimeofday(struct timeval *tv,struct timezone *tz); int settimofday(const struct timeval *tv,const struct timezone *tz); |
参数: |
tv tz |
返回值: |
总是返回0 |
与time函数相比,gettimeofday提供了更高的分辨率,最高为微秒级,这对某些要求实时性较高的程序来说是很重要的。
gettimeoffday函数将当前时间存放在tp指向的timeval结构中,而该结构存储秒和微妙。tz的唯一合法值是NULL,其他值则将产生不确定结构。某些平台支持用tz说明时区,但这完全依现实而定。
struct timeval{
time_t tv_sec;
long tv_usec;
};
4.
名称:: |
gmtime/localtime |
功能: |
transform date and time |
头文件: |
#include <time.h> |
函数原形: |
struct tm *gmtime(const time_t *calptr) struct tm *locatltime(const time_t calptr); |
参数: |
calptr time_t类型的时间 |
返回值: |
返回指向tm类型的指针。 |
localtime和gmtime之间的区别是:localtime将日历时间转换成为本地时间,而gmtiem将日历时间转换为国际标准时间。两个函数的返回值类型都是tm.该类型的定义如下。
struct tm{
int tm_sec;
int tm_min;
int tm_hout;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
下面是一个例子,打印当前系统的本地时间和国际标准时间。
/*6_2.c*/ #include <stdio.h> #include <time.h>
main() { time_t ti; struct tm *gtime; strcut tm *ltime;
if((ti=time(NULL))<0) perror(“error”); gtime=(struct tm*)malloc(sizeof(struct tm)); ltime=( struct tm*)malloc(sizeof(struct tm)); if((gtime=gmtime(&ti))<0) perror(“error”); if((ltime=localtime(&ti))<0) perror(“error”); printf(“gmtime:%d year %d month %d day %d:%d:%d\n”,gtime->tm_year, gtime->tm_mon,gtime->tm_mday,gtime->tm_ hour,gtime->tm_min,gtime->tm_sec); printf(“localtime:%d year %d month %d day %d:%d:%d\n”,ltime->tm_year, ltime->tm_mon,ltime->tm_mday,ltime->tm_ hour,ltime->tm_min,ltime->tm_sec); free(gtime); free(ltime); } |
5.
名称:: |
mktime |
功能: |
transform date and time |
头文件: |
#include <time.h> |
函数原形: |
time_t mktime(struct tm *tmptr); |
参数: |
tmptr |
返回值: |
若成功则返回日历时间,若出错则返回-1 |
mktime以本地时间的年,月,日等为参数,将其转换成time_t型。是locatltime函数的功能正好相反。
6.
名称:: |
ctime/asctime |
功能: |
transform date and time |
头文件: |
#include <time.h> |
函数原形: |
char *asctiem(const struct tm *tmptr); char *ctime(const time_t *calptr); |
参数: |
tmptr tmptr类型时间值 calptr calptr类型时间值 |
返回值: |
指向以NULL结尾的字符串指针 |
Asctime和ctime函数产生形式的26字节字符串,这与date(1)命令的系统默认输出形式类似:
Wed Oct 4 07:13:20 CST 2006
7.
名称:: |
strftime |
功能: |
format date and time |
头文件: |
#include <time.h> |
函数原形: |
size_t strftime(char *s,size_t max,const char *format,const struct tm *tm); |
参数: |
|
返回值: |
若有空间则返回存入数组的字符数,否则返回0。 |
下面是应用这些函数的一个程序:
/*6_3.c*/ #include <time.h> main() { time_t *nowtime; struct tm *gtime; struct tm *ltime; char *t1; char *t2;
time(nowtime); gtime=gmtime(nowtime); ltime=localtime(nowtime);
t1=ctime(nowtime); t2=asctme(gtime);
printf(“time:%s\n”,t1); printf(“time:%s\n”,t2); printf(“hour:%d min:%d sec:%d\n”,gtime->tm_hour,gtime->tm_min,gtime->tm_sec); free(nowtime); free(gtime); free(ltime); } |
转载于:https://www.cnblogs.com/dubingsky/archive/2009/06/18/1505960.html