您的位置:首页 > 其它

格式化一个时间字符串

2007-12-17 10:50 260 查看
格式化一个时间字符串

Article last modified on 2002-4-30

----------------------------------------------------------------

The information in this article applies to:

- Microsoft Visual C++ 6.0

----------------------------------------------------------------

利用Run-Time Library

具体的用法范例[/b]:[/b]

string strTime;

CurrentTimeStr(&strTime, TRUE);

CurrentTimeStr定义为:

[/b][/b]#include <Time.h>

//////////////////////////////////////////////////////////////////////

//

// STL的常用头文件和声明:

//

//////////////////////////////////////////////////////////////////////

#pragma warning(disable:4786)

#include <string>

using namespace std;

//

//////////////////////////////////////////////////////////////////////

//

// 构造出当前时间的字符串

//

void CurrentTimeStr(string *pstrTimeString,

BOOL bWantMore)

{

char szTimeString[128];

memset(szTimeString, 0, 128);

使用的结构[/i]tm[/i]的说明[/i]:[/i]

在[/i]WCHAR.H[/i]中定义[/i]:[/i]

#ifndef _TM_DEFINED[/i]

struct tm {[/i]

int tm_sec; /* seconds after the minute - [0,59] */[/i]

int tm_min; /* minutes after the hour - [0,59] */[/i]

int tm_hour; /* hours since [/i]midnight[/i] - [0,23] */[/i]

int tm_mday; /* day of the month - [1,31] */[/i]

int tm_mon; /* months since January - [0,11] */[/i]

int tm_year; /* years since 1900 */[/i]

int tm_wday; /* days since Sunday - [0,6] */[/i]

int tm_yday; /* days since January 1 - [0,365] */[/i]

int tm_isdst; /* daylight savings time flag */[/i]

};[/i]

#define _TM_DEFINED[/i]

#endif[/i]

struct tm *today;

time_t[/i]类型是在[/i]WCHAR.H[/i]中定义[/i]:[/i]

#ifndef _TIME_T_DEFINED[/i]

typedef long time_t;[/i]

#define _TIME_T_DEFINED[/i]

#endif[/i][/i]

time_t ltime;

定义在[/i]Time.H[/i]中[/i][/i]

Set time zone from TZ environment variable. If TZ is not set,[/i]

the operating system is queried to obtain the default value [/i]

for the variable. [/i][/i]

_tzset();

定义在[/i]Time.H[/i]中[/i][/i]

Get UNIX-style time and display as number and string.[/i] [/i]

time( <ime );

这个函数的定义是:[/i][/i]

struct tm * __cdecl localtime(const time_t *);[/i]

它主要是用来[/i]Use time structure to build a customized time string[/i] and correct for the local time zone[/i]. [/i]

它将返回一个指针,指向[/i]tm[/i]的结构,如果这个时间值是早于[/i]1970[/i]年[/i]1[/i]月[/i]1[/i]日午夜的,则会返回[/i]NULL[/i]。[/i][/i]

today = localtime( <ime );

strftime[/i]指定的第一个参数就是目标字符串,第二个参数就是该目标字符串的最大[/i][/i]

长度;[/i][/i]

第三个参数将制定格式化的定义;[/i][/i]

第四个参数是时间结构[/i]tm[/i]

下面说明格式化命令的含义:[/i][/i]

%m[/i] [/i]

Month as decimal number (01 – 12) [/i]

%d[/i] [/i]

Day of month as decimal number (01 – 31)[/i]

%H[/i] [/i]

Hour in 24-hour format (00 – 23) [/i]

%M[/i] [/i]

Minute as decimal number (00 – 59) [/i]

%S[/i] [/i]

Second as decimal number (00 – 59) [/i]

strftime( szTimeString, 128,

"=时间: %m月%d日%H时%M分%S秒 ", today );

(*pstrTimeString).append(szTimeString);

}

//

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=12664
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: