Chinaunix首页 | 论坛 | 博客
  • 博客访问: 244673
  • 博文数量: 44
  • 博客积分: 1795
  • 博客等级: 上尉
  • 技术积分: 465
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-04 12:02
文章分类

全部博文(44)

文章存档

2015年(1)

2011年(1)

2009年(7)

2008年(2)

2007年(7)

2006年(26)

我的朋友

分类: C/C++

2006-07-28 16:49:52

用同一块buffer,通过下面两个程序测试出来.
#include ;
#include ;
using namespace std;
int main()
{
 time_t now;
 char * ptime;
 if(time(&now)<0)
 {
  cout << "error\n" << endl;
  exit(-1);
 }
#ifdef _AIX
    ptime = asctime(gmtime((time_t *)&now));
#else 
    ptime = asctime(gmtime((long *)&now));
#endif
 cout << now << endl;
 cout << ctime(&now) << endl;
 cout << now << endl;
 cout << ptime << endl;
 printf("haha\n ");
 return 0;
}
 
 
#include ;
#include ;
using namespace std;
int main()
{
 time_t now;
 char * ptime;
 if(time(&now)<0)
 {
  cout << "error\n" << endl;
  exit(-1);
 }
 cout << now << endl;
 cout << ctime(&now) << endl;
#ifdef _AIX
    ptime = asctime(gmtime((time_t *)&now));
#else 
    ptime = asctime(gmtime((long *)&now));
#endif
 cout << now << endl;
 cout << ptime << endl;
 printf("haha\n ");
 return 0;
}
 
localtime()和gmtime()之间的区别是:localtime将日历时间转换成本地时间(考虑到本地时区和夏时制标志),而gmtime则将日历时间转换成国际标准时间的年、月、日、时、分、秒、周日。它们的定义如下:

     struct tm* gmtime(const time_t* mem);
     struct tm* localtime(const time_t* mem);

     函数mktime()则正好相反,它是以存放有本地时间年、月、日等的tm结构作为参数,将其转换成time_t类型的秒值。mktime()函数的定义是:

     time_t mktime(struct tm* tmptr);  //成功返回日历时间,失败则返回-1 

     函数asctime()和ctime()可以获得人们可读的时间字符串,表示形式如同使用date命令所获得的系统默认的时间输出形式。它们的定义如下:

     char* asctime(const struct tm* tmptr); //参数是指向存放有本地时间年、月、日等的tm结构的指针
     char* ctime(const time_t* mem);   //参数是指向日历时间的指针

     函数strftime()是最为复杂的时间函数,可用于用户自定义时间的表示形式。函数strftime()的定义如下:

     size_t strftime(char* buf, size_t maxsize, const char* format,
                       const struct tm* tmptr);  //有空间则返回所存入数组的字符数,否则为0

自定义格式的结果存放在一个长度为maxsize的buf数组中,如果buf数组长度足以存放格式化结果及一个null终止符,则该函数返回在buf数组中存放的字符数(不包括null终止符),否则该函数返回0。format参数用于控制自定义时间的表示格式,格式的定义是在百分号之后跟一个特定字符,format中的其他字符则按原样输出。其中特别应注意的是,两个连续的百分号则是表示输出一个百分号。常用的定义格式如下表所示。

格式
说明
例子
% a
缩写的周日名
Tue
% A
全周日名
Tuesday
% b
缩写的月名
Jan
% B
月全名
January
% c
日期和时间
Wed Aug 17 19:40:30 2005
% d
月日:[01, 31]
14
% H
小时(每天2 4小时):[00, 23]
19
% I
小时(上、下午各1 2小时[01, 12]
07
% j
年日:[001, 366]
014
% m
月:[01, 12]
01
% M
分:[00, 59]
40
% p
A M / P M
PM
% S
秒:[00, 61]
30
% U
星期日周数:[00, 53]
02
% w
周日:[ 0 =星期日,6 ]
2
% W
星期一周数:[00, 53]
02
% x
日期
08/17/05
% X
时间
19:40:30
% y
不带公元的年:[00, 991]
05
% Y
带公元的年
2005
% Z
时区名
MST

 
阅读(2075) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~