ctime中提供两种数据:tm结构体和time_t
tm结构体里面显示存储年月日,小时,分钟,秒,毫秒。 sizeof(tm) == 36。
time_t是个int64_或者double类型。 sizeof(time_t) == 8
tm可以格式化为字符串。时间的比较,求和作差各种运算需要把tm转化为time_t,time_t 运算以秒为最小单位。
tm中tm_year是从1900开始,如果保存2011年,tm_year = 2011 - 1900。
tm_month 范围是0 - 11,所以正常月份需要减去1
mktime 函数将tm表示的本地时间转化为time_t, tm => time_t
localtime 函数将time_t表示的本地时间转化为tm结构体 time_t => tm
注意如果使用gmtime函数,结果会跟本地时间差8个小时,这个地理常识可以理解。
例子代码:
/* ctime example */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
void test1()
{
tm start,end;
start.tm_year = 2011-1900; //年必须这么写
start.tm_mon = 6;
start.tm_mday = 20;
start.tm_hour = 20;
start.tm_min = 46;
start.tm_sec = 45;
//start.tm_isdst = 0;
end.tm_year = 2011-1900;
end.tm_mon = 6;
end.tm_mday = 20;
end.tm_hour = 20;
end.tm_min = 45;
end.tm_sec = 50;
//end.tm_isdst = 0;
double dif;
//dif = difftime ( mktime(&end), mktime(&start) );
time_t sta = mktime(&start);
time_t ed = mktime(&end);
dif = ed - sta;
//dif = difftime ( mktime(&end), mktime(&start) );
printf ("%.5lf minutes./n", dif / 60 );
time_t new_end = ed + 3*60; //3 minutes
tm* new1 = localtime(&new_end); //gmtime 是把time_t转化为国际标准时间
// printf("sizeof tm = %d/n", sizeof(tm)); // 36
}
void test2()
{
char timeStr[] = "2011-6-20 21:15:30";
printf("source time string is: /t %s/n", timeStr);
tm timeinfo;
sscanf(timeStr, "%d-%d-%d %d:%d:%d",
&timeinfo.tm_year, &timeinfo.tm_mon, &timeinfo.tm_mday,
&timeinfo.tm_hour, &timeinfo.tm_min, &timeinfo.tm_sec);
timeinfo.tm_year -= 1900;
timeinfo.tm_mon -= 1; //month: 0-11
//
// transform tm to time_t
//
timeinfo.tm_year += 1900;
timeinfo.tm_mon += 1;
// tm => string
char dest[100] = {0};
sprintf(dest, "%d-%d-%d %d:%d:%d",
timeinfo.tm_year, timeinfo.tm_mon, timeinfo.tm_mday,
timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
printf("dest time string is: /t %s/n", dest);
// string => tm
}
// source time string is: 2011-6-20 21:15:30
// dest time string is: 2011-6-20 21:15:30
//
// "type = 2 server_channel = 6 time_section = [2011:06:01-14:40:30 2011:06:01-14:58:20]";
bool parseInitString(char* str, int& type, int& channel, char* timestr)
{
char* token = "[] =";
char* pch;
type = -1;
channel = -1;
pch = strtok(str, token); // type
if ( 0!=strcmp(pch, "type") )
return false;
pch = strtok(NULL, token); // 1
type = atoi(pch);
pch = strtok(NULL, token); // server_channel
if ( 0!=strcmp(pch, "server_channel") )
return false;
pch = strtok(NULL, token); // 6
channel = atoi(pch);
pch = strtok(NULL, token); // time_section
if ( 0!=strcmp(pch, "time_section") )
return false;
pch = strtok(NULL, token); // 2011:06:01-14:40:30
strcat(timestr, pch);
strcat(timestr, " ");
pch = strtok(NULL, token); // 2011:06:01-14:58:20
strcat(timestr, pch);
return true;
}
void tesr3()
{
char str[] = "2011:06:01-14:40:30 2011:06:01-14:58:20";
char str1[20] = {0};
char str2[20] = {0};
//sscanf(str, "%s %s", str1, str2);
sscanf(str, "%[0-9,:-] %[0-9,:-]", str1, str2);
int length = sizeof(str);
puts(str1);
puts(str2);
int year;
int month;
int day;
int hour;
int minute;
int second;
sscanf(str1, "%d:%d:%d-%d:%d:%d",
&year, &month, &day, &hour, &minute, &second);
printf("%d:%d:%d-%d:%d:%d/n", year, month, day, hour, minute, second);
char initStr[] = "type = 2 server_channel = 6 time_section = [2011:06:01-14:40:30 2011:06:01-14:58:20]";
char timestr[50] = {0};
int type;
int channel;
if ( parseInitString(initStr, type, channel, timestr) )
{
printf("type = %d/n", type);
printf("channel = %d/n", channel);
printf("timesection = %s/n", timestr);
}
}
int main ()
{
//test1();
//test2();
//test3();
time_t begtime = 1309502430;
time_t end = begtime + 3*60;
// localtime / gmtime 区别
tm* tm_end = localtime(&end);
//
tm start; //2011:06:01-14:40:30
start.tm_year = 2011-1900; //年必须这么写
start.tm_mon = 6;
start.tm_mday = 1;
start.tm_hour = 14;
start.tm_min = 40;
start.tm_sec = 30;
time_t beg1 = mktime(&start); // 将tm转化为本地时间
tm* start1 = localtime(&beg1); // 将time_t转化为当地时间
system("PAUSE");
return 0;
}