对ctime和astime的理解
来源:互联网 发布:江西省软件考试 编辑:程序博客网 时间:2024/06/11 04:38
用同一块buffer,通过下面两个程序测试出来.
#include <iostream>;
#include <time.h>;
#include <time.h>;
using namespace std;
int main()
{
time_t now;
char * ptime;
if(time(&now)<0)
{
cout << "error/n" << endl;
exit(-1);
}
{
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
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;
cout << ctime(&now) << endl;
cout << now << endl;
cout << ptime << endl;
printf("haha/n ");
return 0;
}
return 0;
}
#include <iostream>;
#include <time.h>;
#include <time.h>;
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;
{
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
ptime = asctime(gmtime((time_t *)&now));
#else
ptime = asctime(gmtime((long *)&now));
#endif
cout << now << endl;
cout << ptime << endl;
cout << ptime << endl;
printf("haha/n ");
return 0;
}
return 0;
}
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
格式
说明
例子
% 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
- 对ctime和astime的理解
- CTime和COleDateTime的区别
- ctime和mtime的区别
- CString , COleDateTime, CTime相互转换和对时间的一些处理
- CString , COleDateTime, CTime相互转换和对时间的一些处理
- FileTime 和 CTime之间的转换问题
- CTime COleDateTime的常用操作和比较
- CTime COleDateTime的常用操作和比较
- time_t、struct tm和CTime的转换
- CTime和CString的相互转换
- 【转帖】ctime和cstring之间的转换
- CTime和DWORD类型的相互转换
- CTime和DWORD类型的相互转换
- FileTime 和 CTime之间的转换问题
- CTime和DWORD类型的相互转换
- 转载:CTime和CString的相互转换
- CTime 和CString的互相转换
- MFC 中CTime 和 COleDateTime的区别
- 一些常用的电脑词汇解释
- 视频会议系统/还是视频会议系统
- i/o流常控制符
- linux的库操作命令ar和nm
- 拿出证据来,缴枪的不杀
- 对ctime和astime的理解
- 雅虎公司C#笔试题
- C++中的文件输入/输出(6):一些有用的函数
- C++stream
- 如何去做一个称职的领导?
- Java IO 实现文件复制 -Java 学习笔记 (25)
- acm
- C++的iostream标准库介绍以及对左移与右移运算符的重载
- 看老外眼里的“中式英语”PK“标准英语”