1970年1月1日00:00:00および指定時間以降のミリ秒数を取得
2595 ワード
</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
", buf);
int array[32];
L= find_num(buf, array, 32);
for(i = 0; i < L; i++)
{
printf("array[%d]=%d
",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
", 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
",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
", buf);
cost_time=time_now-((int64_time)t1*1000);//
printf("The cost time: %lld ms
", cost_time);
return(0);
}