ctime函数
- #include <stdio.h>
- #include <time.h>
- int main(void)
- {
- struct tm *local;
- time_t t;
- t=time(NULL);
- local=gmtime(&t);
- printf("UTC hour is: %d\n",local->tm_hour);
- local=localtime(&t);
- printf("Local hour is: %d\n",local->tm_hour);
- printf("asctime local time is: %s",asctime(local));
- printf("ctime local time is: %s",ctime(&t));
- return 0;
- }
- james@jsh:~/C_stuty/src$ ./a.out
- UTC hour is: 12
- Local hour is: 20
- asctime local time is: Fri Mar 29 20:56:29 2013
- ctime local time is: Fri Mar 29 20:56:29 2013
- james@jsh:~/C_stuty/src$
自定义时间格式
size_t strftime(
char *strDest,
size_t maxsize,
const char *format,
const struct tm *timeptr
);
我们可以根据 forma t 指向字符串中格式命令把 timeptr 中保存的时间信息放在 strDest
指向的字符串中,最多向 strDest 中存放 maxsize个字符。该函数返回向 strDest 指向的字符
串中放置的字符数。
函数 strftime() 的操作有些类似于 sprintf():识别以百分号(%) 开始的格式命令集合,格式
化输出结果放在一个字符串中。格式 化命令说明 串strDest 中各种日期和时间信息的确切
表示方法。格式串中的其他字符原样放进串中。格式命令列在下面,它们是区分大小写的。
%a 星期几的简写
%A 星期几的全称
%b 月分的简写
%B 月份的全称
%c 标准的日期的时间串
%C 年份的后两位数字
%d 十进制表示的每月的第几天
%D 月/天/年
%e 在两字符域中,十进制表示的每月的第几天
%F 年-月- 日
%g 年份的后两位数字,使用基于周的年
%G 年分,使用基于周的年
%h 简写的月份名
%H 24 小时制的小时
%I 12小时制的小时
%j 十进制表示的每年的第几天
%m 十进制表示的月份
%M 十时制表示的分钟数
%n 新行符
%p 本地的 AM 或PM 的等价显示
%r 12小时的时间
%R 显示小时和分钟:hh:mm
%S 十进制的秒数
%t 水平制表符
%T 显示时分秒:hh:mm:ss
%u 每周的第几天,星期一为第一天 (值从 0 到6,星期一为 0 )
%U 第年的第几周,把星期日做为第一天(值从 0 到53)
%V 每年的第几周,使用基于周的年
%w 十进制表示的星期几(值从 0 到6 ,星期天为 0 )
%W 每年的第几周,把星期一做为第一天(值从 0 到53)
%x 标准的日期串
%X 标准的时间串
%y 不带世纪的十进制年份(值从 0 到99)
%Y 带世纪部分的十进制年份
%z,%Z 时区名称,如果不能得到时区名称则返回空字符。
%% 百分号
如果想显示现在是几点了,并以 12小时制显示,就象下面这段程序
- #include <stdio.h>
- #include <time.h>
- int main( void )
- {
- struct tm *newtime;
- char tmpbuf[128];
- time_t lt1;
- time( <1 ) ;
- newtime=localtime(<1);
- strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y.\n", newtime);
- printf("tmpbuf = %s",tmpbuf);
- }
- james@jsh:~/C_stuty/src$ ./a.out
- tmpbuf = Today is Friday, day 29 of March in the year 2013.
计算持续时间的长度
C语言函数difftime
功 能:返回两个time_t型 变量之间的时间间隔,即 计算两个时刻之间的时间差。
用 法: double difftime(time_t time2, time_t time1);
- #include <time.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- int main()
- {
- time_t start ,end;
- start = time(NULL);
- usleep(10000000);
- end = time(NULL);
- printf("the time used %f second\n",difftime(end ,start));
-
- return 0;
- }
- james@jsh:~/C_stuty/src$ ./a.out
- the time used 10.000000 second
gettimeofday()函数的使用方法:
- #include <time.h>
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- time_t start ,end;
- start = time(NULL);
- usleep(10000000);
- end = time(NULL);
- printf("the time used %f second\n",difftime(end ,start));
- return 0;
- }
- james@jsh:~/C_stuty/src$ ./a.out
- the time used 10.000000 second
gettimeofday()函数的使用方法:
1.简介:
在C语言中可以使用函数gettimeofday()函数来得到时间。它的精度可以达到微妙
在C语言中可以使用函数gettimeofday()函数来得到时间。它的精度可以达到微妙
2.函数原型:
#include<sys/time.h>
int gettimeofday(struct timeval*tv,struct timezone *tz )
#include<sys/time.h>
int gettimeofday(struct timeval*tv,struct timezone *tz )
3.说明:
gettimeofday()会把目前的时间用tv 结构体返回,当地时区的信息则放到tz所指的结构中
gettimeofday()会把目前的时间用tv 结构体返回,当地时区的信息则放到tz所指的结构中
4.结构体:
1>timeval
struct timeval{
long tv_sec;/*秒*/
long tv_usec;/*微妙*/
};
2>timezone 结构定义为:
struct timezone{
int tz_minuteswest;/*和UTC时间差了多少分钟*/
int tz_dsttime;/*type of DST correction夏令时间相差的分钟*/
}
什么是夏令时间?特意去百度了下,下面是描述:
1986年4月,我国采取夏令时,具体作法是:每年从四月中旬第一个星期日的凌晨2时整(北京时间),将时钟拨快一小时,即将表针由2时拨至3时,夏令时开始;到九月中旬第一个星期日的凌晨2时整(北京夏令时),再将时钟拨回一小时,即将表针由2时拨至1时,夏令时结束。从1986年到1991年的六个年度,除1986年因是实行夏时制的第一年,从5月4日开始到9月14日结束外,其它年份均按规定的时段施行。在夏令时开始和结束前几天,新闻媒体均刊登有关部门的通告。1992年起,夏令时暂停实行。
3>在gettimeofday()函数中tv或者tz都可以为空。如果为空则就不返回其对应的结构体。
4>函数执行成功后返回0,失败后返回-1,错误代码存于errno中。
1>timeval
struct timeval{
long tv_sec;/*秒*/
long tv_usec;/*微妙*/
};
2>timezone 结构定义为:
struct timezone{
int tz_minuteswest;/*和UTC时间差了多少分钟*/
int tz_dsttime;/*type of DST correction夏令时间相差的分钟*/
}
什么是夏令时间?特意去百度了下,下面是描述:
1986年4月,我国采取夏令时,具体作法是:每年从四月中旬第一个星期日的凌晨2时整(北京时间),将时钟拨快一小时,即将表针由2时拨至3时,夏令时开始;到九月中旬第一个星期日的凌晨2时整(北京夏令时),再将时钟拨回一小时,即将表针由2时拨至1时,夏令时结束。从1986年到1991年的六个年度,除1986年因是实行夏时制的第一年,从5月4日开始到9月14日结束外,其它年份均按规定的时段施行。在夏令时开始和结束前几天,新闻媒体均刊登有关部门的通告。1992年起,夏令时暂停实行。
3>在gettimeofday()函数中tv或者tz都可以为空。如果为空则就不返回其对应的结构体。
4>函数执行成功后返回0,失败后返回-1,错误代码存于errno中。
settimeofday - 头文件
#include<sys/time.h>
#include<unistd.h>
#include<sys/time.h>
#include<unistd.h>
settimeofday - 函数原型
int settimeofday ( const struct timeval *tv,const struct timezone *tz);
int settimeofday ( const struct timeval *tv,const struct timezone *tz);
settimeofday - 说明
settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。注意,只有root权限才能使用此函数修改时间。成功则返回0,失败返回-1,错误代码存于errno。
EPERM 并非由root权限调用settimeofday(),权限不够。
EINVAL 时区或某个数据是不正确的,无法正确设置时间。
settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。注意,只有root权限才能使用此函数修改时间。成功则返回0,失败返回-1,错误代码存于errno。
EPERM 并非由root权限调用settimeofday(),权限不够。
EINVAL 时区或某个数据是不正确的,无法正确设置时间。
- #include <stdio.h>
- #include <sys/time.h>
- #include <errno.h>
- int main()
- {
- struct timeval tv;
- struct timeval set_tv;
- struct timezone tz;
- gettimeofday(&tv,&tz);
- printf("tv.tv_sec = %ld \n",tv.tv_sec);
- printf("tv.tv_usec = %ld \n",tv.tv_usec);
- printf("tz.tz_minuteswest = %d \n",tz.tz_minuteswest);
- printf("tz.tz_dsttime = %d \n",tz.tz_dsttime);
- set_tv.tv_sec = 10;
- set_tv.tv_usec = 20;
- if (settimeofday(&set_tv,NULL) == -1){
- perror("fial to settimeofday");
- printf("%s\n",strerror(errno));
- }
- gettimeofday(&tv,&tz);
- printf("tv.tv_sec = %ld \n",tv.tv_sec);
- printf("tv.tv_usec = %ld \n",tv.tv_usec);
- return 0;
- }
结果:
tz.tz_minuteswest = -480
tz.tz_dsttime = 0
fial to settimeofday: Operation not permitted
Operation not permitted
tv.tv_sec = 574
tv.tv_usec = 392690
tv.tv_usec = 352124
tz.tz_minuteswest = -480
tz.tz_dsttime = 0
tv.tv_sec = 10
tv.tv_usec = 49