时间,我们每天都在与之打交道。
程序的世界中更是,时间无处不在。
在你编写程序的时候,很多时候需要获取当前的时间,并且进行格式化输出,所以心血来潮,就想着整理搜集几个关于“时间”的函数。
但是需要强调一点,本博客里所介绍的函数都是C++语言中的。
time函数
原型:
time_t time (time_t* timer);
作用:
Get the current calendar time as a value of type time_t.
The function returns this value, and if the argument is not a null pointer, it also sets this value to the object pointed by timer.
The value returned generally represents the number of seconds since 00:00 hours, Jan 1, 1970 UTC (i.e., the current unix timestamp).
这里需要说明一下 什么是时间戳?
Unix时间戳(Unix timestamp),或称Unix时间(Unix time)、POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。Unix时间戳不仅被使用在Unix系统、类Unix系统中(比如Linux系统),也在许多其他操作系统中被广泛采用。
使用:
#include <stdio.h> /* printf */
#include <time.h> /* time_t, struct tm, difftime, time, mktime */
int main ()
{
time_t timer;
struct tm y2k = {0};
double seconds;
y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0;
y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;
time(&timer); /* get current time; same as: timer = time(NULL) */
seconds = difftime(timer,mktime(&y2k));
printf ("%.f seconds since January 1, 2000 in the current timezone", seconds);
return 0;
}
Although libraries may use a different representation of time: Portable programs should not use the value returned by this function directly, but always rely on calls to other elements of the standard library to translate them to portable types (such as localtime, gmtime or difftime).
localtime函数
struct tm * localtime (const time_t * timer);
作用:
Uses the value pointed by timer to fill a tm structure with the values that represent the corresponding time, expressed for the local timezone.
应用:
#include <stdio.h> /* puts, printf */
#include <time.h> /* time_t, struct tm, time, localtime */
int main ()
{
time_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
printf ("Current local time and date: %s", asctime(timeinfo));
return 0;
}
gmtime函数
原型:
struct tm * gmtime (const time_t * timer);
作用:
Convert time_t to tm as UTC time
使用:
#include <stdio.h> /* puts, printf */
#include <time.h> /* time_t, struct tm, time, gmtime */
#define MST (-7)
#define UTC (0)
#define CCT (+8)
int main ()
{
time_t rawtime;
struct tm * ptm;
time ( &rawtime );
ptm = gmtime ( &rawtime );
puts ("Current time around the World:");
printf ("Phoenix, AZ (U.S.) : %2d:%02d\n", (ptm->tm_hour+MST)%24, ptm->tm_min);
printf ("Reykjavik (Iceland) : %2d:%02d\n", (ptm->tm_hour+UTC)%24, ptm->tm_min);
printf ("Beijing (China) : %2d:%02d\n", (ptm->tm_hour+CCT)%24, ptm->tm_min);
return 0;
}
tm结构
在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下:
#ifndef _TM_DEFINED
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()为负。*/
strftime函数
原型:
size_t strftime (char* ptr, size_t maxsize, const char* format,
const struct tm* timeptr );
作用:
Format time as string
specifier Replaced by Example
%a Abbreviated weekday name * Thu
%A Full weekday name * Thursday
%b Abbreviated month name * Aug
%B Full month name * August
%c Date and time representation * Thu Aug 23 14:55:02 2001
%C Year divided by 100 and truncated to integer (00-99) 20
%d Day of the month, zero-padded (01-31) 23
%D Short MM/DD/YY date, equivalent to %m/%d/%y 08/23/01
%e Day of the month, space-padded ( 1-31) 23
%F Short YYYY-MM-DD date, equivalent to %Y-%m-%d 2001-08-23
%g Week-based year, last two digits (00-99) 01
%G Week-based year 2001
%h Abbreviated month name * (same as %b) Aug
%H Hour in 24h format (00-23) 14
%I Hour in 12h format (01-12) 02
%j Day of the year (001-366) 235
%m Month as a decimal number (01-12) 08
%M Minute (00-59) 55
%n New-line character ('\n')
%p AM or PM designation PM
%r 12-hour clock time * 02:55:02 pm
%R 24-hour HH:MM time, equivalent to %H:%M 14:55
%S Second (00-61) 02
%t Horizontal-tab character ('\t')
%T ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S 14:55:02
%u ISO 8601 weekday as number with Monday as 1 (1-7) 4
%U Week number with the first Sunday as the first day of week one (00-53) 33
%V ISO 8601 week number (00-53) 34
%w Weekday as a decimal number with Sunday as 0 (0-6) 4
%W Week number with the first Monday as the first day of week one (00-53) 34
%x Date representation * 08/23/01
%X Time representation * 14:55:02
%y Year, last two digits (00-99) 01
%Y Year 2001
%z ISO 8601 offset from UTC in timezone (1 minute=1, 1 hour=100)
If timezone cannot be determined, no characters +100
%Z Timezone name or abbreviation *
If timezone cannot be determined, no characters CDT
%% A % sign %
以下内容,出自博客http://blog.csdn.net/shellching/article/details/8114266
time_t tmBegin = 1351118531; //2012-10-25 06:42:11
time_t tmEnd = 1351218731; //2012-10-26 10:32:11
tm* ptmBegin = localtime(&tmBegin );
tm* ptmEnd = localtime(&tmEnd );//第二次调用会修改上次调用的tm结构体,若上次的数据未保存则会丢失
//tm* ptm3 = gmtime(&tmEnd ); //效果同上条语句,也会重写之前的数据
char ctmBegin1[26], ctmEnd[26];
strftime(ctmBegin, 26, "%Y%m%d%H%M%S", ptmBegin); //这里输出的将是tmEnd的时间值
strftime(ctmEnd, 26, "%Y%m%d%H%M%S", ptmEnd);
/*MSDN中有相关说明: Both the 32-bit and 64-bit versions ofgmtime, mktime, mkgmtime, and localtimeall use a single tm structure per thread for the conversion. Each call to one of these routines destroys the result of the previous call. */
所以要记住,一旦调用了localtime函数,应该马上取出tm结构中的内容
time_t tmBegin = 1351118531; //2012-10-25 06:42:11
time_t tmEnd = 1351218731; //2012-10-26 10:32:11
char ctmBegin1[26], ctmEnd[26];
tm* ptmBegin = localtime(&tmBegin );
strftime(ctmBegin, 26, "%Y%m%d%H%M%S", ptmBegin);
tm* ptmEnd = localtime(&tmEnd );
strftime(ctmEnd, 26, "%Y%m%d%H%M%S", ptmEnd);
//tm* ptm3 = gmtime(&tmEnd );