Chinaunix首页 | 论坛 | 博客
  • 博客访问: 426695
  • 博文数量: 83
  • 博客积分: 2622
  • 博客等级: 少校
  • 技术积分: 1345
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-17 08:59
个人简介

一直在努力

文章分类

全部博文(83)

文章存档

2014年(3)

2013年(9)

2012年(46)

2010年(25)

分类: C/C++

2012-08-27 14:15:21


  1. struct timeval {
  2. time_t tv_sec; /* seconds */
  3. suseconds_t tv_usec; /* microseconds */
  4. };

  5. time_t time(time_t *); 获取当前时间为一个长整数
  6. char * ctime(const time_t *);将时间结构time_t转换为字符串
  7. char * asctime(const struct tm *);将tm结构转换为字符串
  8. struct tm * gmtime(const time_t *);将时间结构转换为tm结构 GMT
  9. struct tm * localtime(const time_t *);将时间结构转换为tm结构 LT
  10. time_t mktime(struct tm *);将tm结构转换为time_t结构
  11. double difftime(time_t, time_t);将前一个减去后一个的差额
  12. clock_t clock(void);返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数

  13. 另外tm结构如下:
  14. struct tm {
  15.         int tm_sec; /* seconds after the minute - [0,59] * /
  16.         int tm_min; /* minutes after the hour - [0,59] * /
  17.         int tm_hour; /* hours since midnight - [0,23] * /
  18.         int tm_mday; /* day of the month - [1,31] * /
  19.         int tm_mon; /* months since January - [0,11] * /
  20.         int tm_year; /* years since 1900 * /
  21.         int tm_wday; /* days since Sunday - [0,6] * /
  22.         int tm_yday; /* days since January 1 - [0,365] * /
  23.         int tm_isdst; /* daylight savings time flag * /
  24.         };
strftime()函数将时间格式化
我们可以使用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 十进制的秒数 
%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小时制显示,就象下面这段程序:

#i nclude “time.h”
#i nclude “stdio.h”
int main(void)
{
struct tm *ptr;
time_t lt;
char str[80];
lt=time(NULL);
ptr=localtime(<);
strftime(str,100,"It is now %I %p",ptr);
printf(str);
return 0;
}

其运行结果为:
It is now 4PM

而下面的程序则显示当前的完整日期:

#i nclude 
#i nclude 

void 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);
}

运行结果:

Today is Saturday, day 30 of July in the year 2005.


阅读(3583) | 评论(0) | 转发(0) |
0

上一篇:nginx进程模型之epoll

下一篇:linux md5认证

给主人留下些什么吧!~~