参考:http://www.cnblogs.com/doublesnke/archive/2011/08/05/2128558.html
方案—:使用C标准库
优点:提供跨平台性;
缺点:只能精确到秒级;
C运行库关于时间的主要函数介绍:
所需头文件:
#include <time.h>
1. time()
函数功能:
获取当前的系统时间
函数原型:
time_t time( time_t *timer );
参数:
timer
存储时间的内存空间;
返回值:
返回的结果是一个time_t结构体
注意:没有错误会返回
关于time_t的说明如下:
time_t数据类型用来表示日历时间(Calendar Time),即从一个时间点(1970年1月1日0时0分0秒)到此时的秒数。
在time.h中,我们也可以看到time_t是一个长整型数:
#ifndef _TIME_T_DEFINED
typedef long time_t; /* 时间值 */
#define _TIME_T_DEFINED /* 避免重复定义 time_t */
#endif
使用方法:
1> 传入参数为NULL
time_t t=time(NULL);
2> 传入参数为time_t类型的指针
time_t t;
time(&t);
2. localtime()/gmtime()
功能:
此函数将tm结构体保存的时间,转化为本地时间/格林尼治时间。
函数原型:
struct tm *localtime( const time_t *timer );
struct tm *gmtime( const time_t *timer );
struct tm的定义如下:
struct tm
{
int tm_sec; /* 秒 – 取值区间为[0,59] */
int tm_min; /* 分 - 取值区间为[0,59] */
int tm_hour; /* 时 - 取值区间为[0,23] */
int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
int tm_year; /* 年份,其值等于实际年份减去1900 */
int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
};
示例:
#include <time.h>
#include <stdio.h>
void main()
{
time_t cTime=time(NULL);
//获取本地时间
tm * lTime=localtime(&cTime);
//获取格林尼治时间
tm * gTime=gmtime(&cTime);
printf("%s\n", asctime(lTime));
printf("%s\n", asctime(gTime));
}
疑问:输出结果相同,如下:
Tue Apr 24 06:16:05 2012
Tue Apr 24 06:16:05 2012
3. mktime()
功能:
将时间结构数据转换成日历日期(与localtime()的作用刚好相反)
函数原型:
time_t mktime( struct tm *timeptr );
4. strftime()/wcsftime()
函数功能:
将时间格式化一个时间字符串
函数原型:
size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr );
size_t wcsftime( wchar_t *strDest, size_t maxsize, const wchar_t *format, const struct tm *timeptr );
参数:
strDest:指针,指向输出字符串的首地址;
maxsize:字符串的最大长度;
format:转换格式;
timeptr:tm结构体指针;
可以采用的格式:
%a:星期几的英文名字的缩写;
%A:星期几的英文名字;
%b:月的英文名字缩写;
%B:月的英文名字;
%c:日期和时间的本地表示;
如:04/24/12 13:08:57
%d:一月中的多少天(01-31)
%H:24小时制的小时
%I:12小时制的小时
%j:一年中的多少天
%m:月
%M:分钟
%p:12小时制的本地A.M/P.M
%S:秒
%U:一年中的第几周(以周日为一周的第一天)
%w:星期几(周日为0)
%W:一年中的第几周(以周一为一周的第一天)
%x:日期;
%X:时间(时:分:秒)
%y:不带有世纪的年数(取值0-99),如12
%Y:带有世纪的年数,如2012
%z, %Z:时区名字(如果时区不明的话,不会输出字符串)
%%:百分号
返回值:
如果调用成功:
strftime 返回放到strDest中的字符数(包括终结符null),最大不超过maxsize
wcsftime 返回对应的宽字符数;
调用失败:
返回0,strDest中的内容不确定;
5. asctime()/ctime()
功能:
把时间结构体转换为字符串;
函数原型:
char *asctime( const struct tm *timeptr );
wchar_t *_wasctime( const struct tm *timeptr );
char *ctime( const time_t *timer );
wchar_t *_wctime( const time_t *timer );
上述函数,只是参数类型不同,但转换结果相同
转换结果例如:Tue Apr 24 14:07:26 2012
示例代码如下:
#include <time.h>
#include <stdio.h>
void main()
{
//获取时间
time_t cTime=time(NULL);
char sTime[64]={0};
//格式化时间
struct tm * localTime=localtime(&cTime);
strftime(sTime, sizeof(sTime), "%Y/%m/%d %X %A 本年第%j天 %z\n", localTime);
printf(sTime);
}
输出结果如下:
2012/04/24 13:13:37 Tuesday 本年第115天 中国标准时间
方案二:使用Windows提供的API
优点:能精确到毫秒级;
缺点:使用了windows API,平台移植性差
(1) 函数介绍:
GetLocalTime()
功能:
该函数用来获取当地的当前系统日期和时间
函数原型:
void GetLocalTime( LPSYSTEMTIME lpSystemTime );
参数:
lpSystemTime:用来指向SYSTEMTIME结构体,以存储现在的本地日期和时间返回值:
没有返回值
其中,SYSTEMTIME结构体是Windows提供的,其定义如下:
typedef struct _SYSTEMTIME
{
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
}SYSTEMTIME;
注意:该结构体
可以精确到毫秒级
(2) 示例:
#include <windows.h>
#include <stdio.h>
void main()
{
SYSTEMTIME sysTime;
GetLocalTime(&sysTime);
printf( "%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d\n",sysTime.wYear,sysTime.wMonth,sysTime.wDay,sysTime.wHour,sysTime.wMinute, sysTime.wSecond,sysTime.wMilliseconds,sysTime.wDayOfWeek);
}
输出结果:2012/04/24 13:29:13.140 星期2
方案三:利用系统调用
优点:可以修改系统时间
(1) 函数介绍:
system()/_wsystem()
功能:
发出一个DOS命令
函数原型:
int system( const char *command );
int _wsystem( const wchar_t *command );
参数:
command:待执行的命令
返回值:
如果command为NULL,并且命令解释器找到的话,函数返回一个非0值;
如果命令解释器没有找到,则返回0,并设置errno为ENOENT;
如果命令不为NULL,且命令解释器找到的话,函数返回命令解释器的结果;
如果返回结果为0,说明:没有找到命令解释器;
如果返回结果为-1,则说明发生了错误,并设置相应的errno;
可能的errno如下:
E2BIG
Argument list (which is system-dependent) is too big.
ENOENT
Command interpreter cannot be found.
ENOEXEC
Command-interpreter file has invalid format and is not executable.
ENOMEM
Not enough memory is available to execute command; or available memory has been corrupted; or invalid block exists, indicating that process making call was not allocated properly.
(2) 示例代码:
#include <stdlib.h>
#include <iostream>
using namespace std;
void main()
{
system("time");
}
在示例中调用 time 系统命令,与在命令行下,输入“ time ”命令效果相同;