获取1970年1月1日 00:00:00及标准时间到指定时间以来的毫秒数

</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);
} 


  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

硬核程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值