中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Visual C++ > 综合文章
格式化一个时间字符串
作者:未知 时间:2002-05-18 12:12 出处:Blog 责编:chinaitpower
              摘要:格式化一个时间字符串

格式化一个时间字符串

 

Article last modified on 2002-4-30

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

The information in this article applies to:

  - Microsoft Visual C++ 6.0

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

 

利用Run-Time Library

具体的用法范例

string strTime;

CurrentTimeStr(&strTime, TRUE);

 

CurrentTimeStr定义为:

#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);

  

使用的结构tm的说明:

WCHAR.H中定义:

#ifndef _TM_DEFINED

struct tm {

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

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

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

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

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

        int tm_year;    /* years since 1900 */

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

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

        int tm_isdst;   /* daylight savings time flag */

        };

#define _TM_DEFINED

#endif

       struct tm *today;

 

time_t类型是在WCHAR.H中定义:

#ifndef _TIME_T_DEFINED

typedef long time_t;

#define _TIME_T_DEFINED

#endif

       time_t ltime;

      

        定义在Time.H

Set time zone from TZ environment variable. If TZ is not set,

     the operating system is queried to obtain the default value

     for the variable.

    _tzset();

 

        定义在Time.H

Get UNIX-style time and display as number and string.

    time( &ltime );

 

这个函数的定义是:

struct tm * __cdecl localtime(const time_t *);

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

它将返回一个指针,指向tm的结构,如果这个时间值是早于197011日午夜的,则会返回NULL

    today = localtime( &ltime );

 

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

长度;

第三个参数将制定格式化的定义;

第四个参数是时间结构tm

下面说明格式化命令的含义:

%m

Month as decimal number (01 – 12)

%d

Day of month as decimal number (01 – 31)

%H

Hour in 24-hour format (00 – 23)

%M

Minute as decimal number (00 – 59)

%S

Second as decimal number (00 – 59)

       strftime( szTimeString, 128,

                      "=时间: %m%d%H%M%S ", today );

      

       (*pstrTimeString).append(szTimeString);

}

//

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有