/*
#include <time.h>
time_t time( time_t *time );
函数time()返回当前时间
若参数time给定,则当前时间存储在time指针中
在VC中,若需要给定time参数,并且返回值和time储存值相同,或是赋予NULL的值
time_t类型即long类型,其值是系统从1970年1月1日00:00:00到现在总共的秒数
#include <time.h>
struct tm *localtime( const time_t *time );
函数将time的值转换为当地时间,用结构体tm存储
time的值可有上诉函数time()获取
该函数可结合下面的函数使用
#include <time.h>
char *asctime( const struct tm *ptr );
将结构体*ptr的内容转换为如下形式的字符串
day month date hours:minutes:seconds year
eg.
Mon Sep 24 16:18:20 2007
#include <time.h>
size_t strftime( char *str, size_t maxsize, const char *fmt, struct tm *time );
函数以fmt的特定格式将time格式化后存储在str字符串中
%a abbreviated weekday name (e.g. Fri)
%A full weekday name (e.g. Friday)
%b abbreviated month name (e.g. Oct)
%B full month name (e.g. October)
%c the standard date and time string
%d day of the month, as a number (1-31)
%H hour, 24 hour format (0-23)
%I hour, 12 hour format (1-12)
%j day of the year, as a number (1-366) [Page]
%m month as a number (1-12). Note: some versions of Microsoft Visual C++ may use values that range from 0-11.
%M minute as a number (0-59)
%p locale’s equivalent of AM or PM
%S second as a number (0-59)
%U week of the year, (0-53), where week 1 has the first Sunday
%w weekday as a decimal (0-6), where Sunday is 0
%W week of the year, (0-53), where week 1 has the first Monday
%x standard date string
%X standard time string
%y year in decimal, without the century (0-99)
%Y year in decimal, with the century
%Z time zone name
%% a percent sign
#include <time.h>
time_t mktime( struct tm *time );
函数将结构体参数time转换为从1970年1月1日到现在时间的秒数
它是time的相反函数
#include <time.h>
struct tm *gmtime( const time_t *time );
函数将time转换为结构体tm的形式存储,以Coordinated Universal Time的形式
与localtime函数类似
#include <time.h>
double difftime( time_t time2, time_t time1 );
函数返回time2 - time1的值
注:struct tm结构体成员如下 [Page]
tm_hour Hours since midnight (0 – 23)
tm_isdst Positive if daylight saving time is in effect; 0 if daylight saving time is not in effect;
negative if status of daylight saving time is unknown.
The C run-time library assumes the United States’s rules for implementing the calculation
of Daylight Saving Time (DST).
tm_mday Day of month (1 – 31)
tm_min Minutes after hour (0 – 59)
tm_mon Month (0 – 11; January = 0)
tm_sec Seconds after minute (0 – 59)
tm_wday Day of week (0 – 6; Sunday = 0)
tm_yday Day of year (0 – 365; January 1 = 0)
tm_year Year (current year minus 1900)
*/
#include <stdio.h>
#include <time.h>
#include \"MSDNTest.h\"
void main()
...{
time_t curTime;
tm *ts;
time_t testTime = time(&curTime);
printf(\"the current time is : %ld. \", curTime);
printf(\"the test time is : %ld. \", testTime);
ts = localtime(&curTime); [Page]
printf(\"the current in sting format is : %s\", asctime(ts));
struct tm *tsgm = gmtime(&curTime);
printf(\"the current in CUT format string is : %s \", asctime(tsgm));
char strTimebuf[1000];
printf(\"the full use of strftime function: \");
strftime(strTimebuf, 1000, \"now the abbreviated weekday time is %a.
the full weekday name is %A. the abbreviated month name is %b.
the full month name is %B. the standard date and time string is %c.
the day of the month as a number is %d. the 24 format hour is %H.
the 12 format hour is %I. the day of the year is %j.
the month number is %m. the minute number is %M.
the locale’s equivalent of AM or PM is %p. the second number is %S.
the week of the year is %U(Monday is the first). the decimal weekday is %w.
the week of the year is %W(Sunday is the first). the standard date string is %x.
the year in decimal without century is %y. the year in decimal with century is %Y. [Page]