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

Linux下时间编程(综合知识+综合编程):time,ctime,strftime,localtime,gmtime,gettimeofday

2016-08-27 15:46 609 查看
一:strftime() 函数将时间格式化

NAME

       strftime - format date and time

SYNOPSIS

       #include <time.h>

size_t strftime(char *s, size_t max, const char *format,

                       const struct tm *tm);

我们可以使用strftime()函数将时间格式化为我们想要的格式。它的原型如下:

size_t strftime(

     char *strDest,

     size_t maxsize,

     const char *format,

     const struct tm *timeptr

);

我们可以根据format指向字符串中格式命令把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 十进制的秒数

二:linux下time

 #include <time.h>

       char *asctime(const struct tm *tm);

       char *asctime_r(const struct tm *tm, char *buf);

       char *ctime(const time_t *timep);

       char *ctime_r(const time_t *timep, char *buf);

       struct tm *gmtime(const time_t *timep);

       struct tm *gmtime_r(const time_t *timep, struct tm *result);

       struct tm *localtime(const time_t *timep);

       struct tm *localtime_r(const time_t *timep, struct tm *result);

       time_t mktime(struct tm *tm);

然后使用以下两个函数将其进行标准时间转换 

#include   <time.h> 

struct   tm   *gmtime(const   time_t   *calptr)           /*将其转换为国际标准时间*/ 
struct   tm   *localtime(const   time_t   *calptr)     /*将其转换成本地时间*/ 

以上两个函数返回的是一个tm结构的指针 
tm结构如下: 

struct   tm{ 
      int   tm_sec;       /*[0,6]*/ 
      int   tm_min;       /*[0,59]*/ 
      int   tm_hour;     /*[0,23]*/ 
      int   tm_mday;     /*[1,31]*/ 
      int   tm_mon;       /*[0,11]*/ 
      int   tm_year;     /*year   since   1900*/ 
      int   tm_wday;     /*day   since   sunday   [0,6]*/ 
      int   tm_yday;     /*day   cince   january   1;[0,365]*/ 
      int   tm_isdst;   /*daylight   saving   time   flag: <0,0,> 0*/ 


得到这个结构后使用结构中相应的域值就可以取得时间了 

三:


1.简介:

在C语言中可以使用函数gettimeofday()函数来得到时间。它的精度可以达到微妙


2.函数原型:

#include<sys/time.h>

int gettimeofday(struct  timeval*tv,struct  timezone *tz )


3.说明:

gettimeofday()会把目前的时间用tv 结构体返回,当地时区的信息则放到tz所指的结构中


4.结构体:

1>timeval

struct  timeval{

   

       long  tv_sec;/*秒*/

       long  tv_usec;/*微妙*/

};

2>timezone 结构定义为:

struct  timezone{

        int tz_minuteswest;/*和greenwich 时间差了多少分钟*/

        int tz_dsttime;/*type of DST correction*/

}

3>在gettimeofday()函数中tv或者tz都可以为空。如果为空则就不返回其对应的结构体。

4>函数执行成功后返回0,失败后返回-1,错误代码存于errno中。

综合代码:

/*
#include<stdio.h>
#include <time.h>
#include <string.h>
#include <sys/time.h>
int main(void)
{
time_t tNow=-1;
struct tm tmNow;

tNow =time(NULL);
if(tNow<0)
{
perror("time");
return -1;
}
printf("time:%ld.\n",tNow);

printf("ctime:%s.\n",ctime(&tNow));
//	gmtime
memset(&tmNow, 0, sizeof(tmNow));
gmtime_r(&tNow,&tmNow);
printf("年%d月%d日%d时%d.\n",tmNow.tm_year,tmNow.tm_mon,tmNow.tm_mday,tmNow.tm_hour);
//
memset(&tmNow, 0, sizeof(tmNow));
localtime_r(&tNow,&tmNow);
printf("年%d月%d日%d时%d.\n",tmNow.tm_year,tmNow.tm_mon,tmNow.tm_mday,tmNow.tm_hour);

return 0;

}
*/

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

#include<stdio.h>
#include <time.h>
#include <string.h>
#include <sys/time.h>
int main(void)
{
time_t tNow=-1;
struct tm tmNow;

char buf[100];

struct timeval tv={0};
struct timezone tz={0};

int ret=-1;

tNow =time(NULL);
if(tNow<0)
{
perror("time");
return -1;
}
printf("time:%ld.\n",tNow);

printf("ctime:%s.\n",ctime(&tNow));
#if 0
//	gmtime_r
memset(&tmNow, 0, sizeof(tmNow));
gmtime_r(&tNow,&tmNow);
printf("年%d月%d日%d时%d.\n",tmNow.tm_year,tmNow.tm_mon,tmNow.tm_mday,tmNow.tm_hour);
//localtime_r
memset(&tmNow, 0, sizeof(tmNow));
localtime_r(&tNow,&tmNow);
printf("年%d月%d日%d时%d.\n",tmNow.tm_year,tmNow.tm_mon,tmNow.tm_mday,tmNow.tm_hour);
//asctime
printf("asctime:%s.\n",asctime(&tmNow);
#endif

#if 0
//  strftime()函数将时间格式化为我们想要的格式

memset(&tmNow, 0, sizeof(tmNow));
localtime_r(&tNow,&tmNow);
printf("年%d月%d日%d时%d.\n",tmNow.tm_year,tmNow.tm_mon,tmNow.tm_mday,tmNow.tm_hour);

memset(buf, 0, sizeof(buf));
strftime(buf,sizeof(buf),"%Y*%m*%d",&tmNow);
printf("时间为:[%s].\n",buf);
#endif

#if 1
ret=gettimeofday(&tv,&tz);
if(ret<0)
{
perror("gettimeofday");
return -1;
}
printf("seconde: %ld.\n", tv.tv_sec);
printf("timezone:%d.\n", tz.tz_minuteswest);

#endif

return 0;

}




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