</pre><pre name="code" class="cpp">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <ctype.h>
typedef long long int int64_time;
int t_year,t_mon,t_mday;
int64_time cost_time;
size_t find_num(const char *str, int *container, size_t max_size)
{
size_t str_l = strlen(str), i, cur;
int buf = 0;
char flag = 0;
for(i = 0, cur = 0; i < str_l && cur < max_size; i++) {
if(isdigit(str[i])) {
buf = buf * 10 + str[i] - '0';
flag = 1;
} else if(flag) {
container[cur++] = buf;
buf = 0;
flag = 0;
}
}
if(flag && cur < max_size)
container[cur++] = buf;
return cur;
}
int Get_Current_Time(int64_time count)
{
time_t now;
struct tm ts;
char buf[80];
size_t L;
int i;
now=count/1000;//转换为秒
ts = *localtime(&now);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
printf("The current time CST = %s\n", buf);
int array[32];
L= find_num(buf, array, 32);
for(i = 0; i < L; i++)
{
printf("array[%d]=%d\n",i,array[i]);
}
t_year=array[0];//当前年份
t_mon=array[1];//当前月份
t_mday=array[2];//当前天
return 0;
}
int main(void)
{
char buf[80];
struct tm tm1;
struct tm *tm2;
struct timeval t_time;
//获取当前时间
//get start time
gettimeofday(&t_time, NULL);
int64_time time_now = ((int64_time)t_time.tv_sec)*1000+(int64_time)t_time.tv_usec/1000;
printf("The current time: %lld ms\n", time_now);
Get_Current_Time(time_now);
//获取1970年到指定时间以来的毫秒数
tm1.tm_sec = 0;/* 秒 – 取值区间为[0,59] */
tm1.tm_min = 0;/* 分 - 取值区间为[0,59] */
tm1.tm_hour = 0;/* 时 - 取值区间为[0,23] */
tm1.tm_year = t_year-1900;/* 年份,其值等于实际年份减去1900 */
tm1.tm_mon = t_mon-1;/* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
tm1.tm_mday = t_mday;/* 一个月中的日期 - 取值区间为[1,31] */
time_t t1 = mktime(&tm1);//mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。
printf("From 1970-1-1 00:00:00 To %d-%d-%d 00:00:00 = %lld ms\n",t_year,t_mon,t_mday,(int64_time)t1*1000);//打印从公元1970年1月1日0时0分0到指定时间的毫秒数
if(NULL == ( tm2 =localtime(&t1)))
{
return(0);
}
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", tm2);
printf("%s\n", buf);
cost_time=time_now-((int64_time)t1*1000);//当前时间的毫秒数减去当天凌晨时的毫秒数
printf("The cost time: %lld ms\n", cost_time);
return(0);
}