嵌入式 把年月日转换为当前系统的秒数

 
把年月日转换为指定时间的秒数函数已经实现,需要的朋友可参考:
 
/*
Author : kj
Function : 
analyze the char string of time ,and
transform time mode to total seconds
Time : 2013-12-07 14:36
Instruction :
run the flow order
./ymd_to_seconds $1 $2
Notice :
$1 and $2 is :
$1 = 2013;12;07;14;36
$2 = 2013;12;07;14;36

 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define TIME_DATA_MAX_SIZE 8


int get_current_time(char *buf)  
{  
    struct tm *tmnow;  
    struct timeval tv;  
    gettimeofday(&tv,NULL);  
    tmnow = localtime(&tv.tv_sec);  
    sprintf(buf,"%04d-%02d-%02d %02d:%02d:%02d",tmnow->tm_year+1900, tmnow->tm_mon+1, tmnow->tm_mday,tmnow->tm_hour,tmnow->tm_min, tmnow->tm_sec);   
 
   return tv.tv_sec;  
}  


int analyze_separator_num(char *src,int src_length)
{
    char *cp_tmp;
    char *src_temp = NULL;
    char *src_temp_free = NULL;
    int counter = 0;
    src_temp = (char *)malloc((src_length+4)*sizeof(char));
    strcpy(src_temp,src);
    src_temp_free = src_temp;
    cp_tmp = strtok(src_temp, ":");

    if(cp_tmp)
    {
#ifdef _DEBUG_KJ
	printf("%s\n",cp_tmp);
#else
#endif
	counter++;
    }

    while((cp_tmp=strtok(NULL,":"))!=NULL)
    {	
	counter++;
#ifdef _DEBUG_KJ
	printf("%s\n",cp_tmp);
#else
#endif

    }
#ifdef _DEBUG_KJ
    printf("%s %d The string is %s !The num strtok of ':' are %d\n",__FUNCTION__,__LINE__,src,counter-1);
#else
#endif
    src_temp = src_temp_free;
    free(src_temp);
    src_temp = NULL;
    src_temp_free = NULL;
    return counter;

}

void analyze_socket_data(char *dest,char *src)
{
    char *cp_tmp;
    int counter = 0;
    int i = 1;

    cp_tmp = strtok(src, ":");
    if(cp_tmp)
    {
	strcpy(dest,cp_tmp);
#ifdef _DEBUG_KJ
	printf("dest  is %s\n",dest);
#else
#endif
	counter++;
    }

    while((cp_tmp=strtok(NULL,":"))!=NULL)
    {	
#ifdef _DEBUG_KJ
	printf("the lenght of the cp_tmp is %d\n",strlen(cp_tmp));
	printf("cp_tmp is %s \n",cp_tmp);
#else
#endif
	memcpy((dest+i*TIME_DATA_MAX_SIZE),cp_tmp,strlen(cp_tmp));
	strcpy(((dest+i*TIME_DATA_MAX_SIZE)+strlen(cp_tmp)),"\0");
	counter++;
	i++;
    }

    return;
}

int calculate_seconds(char in[6][8])
{
    int total_seconds = 0;
    struct tm tm_temp;
    int year;
    int mon;
    int day;
    int hour;
    int min;
    int sec;
#ifdef _DEBUG_KJ
    year = atoi(in[0]);
    mon = atoi(in[1]);
    day = atoi(in[2]);
    hour = atoi(in[3]);
    min = atoi(in[4]);
    sec = atoi(in[5]);
    printf("%s %d The year is %d\n",__FUNCTION__,__LINE__,year);  
    printf("%s %d The mon is %d\n",__FUNCTION__,__LINE__,mon);  
    printf("%s %d The day is %d\n",__FUNCTION__,__LINE__,day);  
    printf("%s %d The hour is %d\n",__FUNCTION__,__LINE__,hour);  
    printf("%s %d The min is %d\n",__FUNCTION__,__LINE__,min);  
    printf("%s %d The sec is %d\n",__FUNCTION__,__LINE__,sec);  
#else
#endif
    tm_temp.tm_year = atoi(in[0]) - 1900;  
    tm_temp.tm_mon = atoi(in[1]) - 1;  
    tm_temp.tm_mday = atoi(in[2]);  
    tm_temp.tm_hour = atoi(in[3]);  
    tm_temp.tm_min = atoi(in[4]);  
    tm_temp.tm_sec = atoi(in[5]);
    total_seconds = mktime(&tm_temp);  
    printf("%s %d The total of seconds is %d\n",__FUNCTION__,__LINE__,total_seconds);  

    return total_seconds;
}

int main(int argc,char *argv[])
{
    struct tm joseph_tm;
    int tm_begin_separator_num = 0;
    int tm_end_separator_num = 0;
    int i_temp = 0;
    int tm_begin_seconds = 0;
    int tm_end_seconds = 0;
    int current_seconds = 0;
    int the_seconds_of_record = 0; 
    char current_time[128] = {0};
    memset(current_time,0,128);
    current_seconds = get_current_time(current_time);
    printf("%s %d The current time is %s , The total seconds is %d!\n",__FUNCTION__,__LINE__,current_time,current_seconds);
    
    if(argc != 3)
    {
	printf("%s %d The param must be two ,please !\n",__FUNCTION__,__LINE__);
	return -1;
    }
    if((strlen(argv[1]) != 20) || (strlen(argv[2]) != 20))
    {
	printf("%s %d The length of param must be 20 ,please !\n",__FUNCTION__,__LINE__);
	return -1;
    }
    for(i_temp = 1; i_temp < argc;i_temp++)
    {
	printf("%s %d The %dth content is %s \n",__FUNCTION__,__LINE__,i_temp,argv[i_temp]);
    }

    tm_begin_separator_num = analyze_separator_num(argv[1],strlen(argv[1]));
    tm_end_separator_num = analyze_separator_num(argv[2],strlen(argv[2]));

    char tm_begin_temp_buf[tm_begin_separator_num][TIME_DATA_MAX_SIZE];
    char tm_end_temp_buf[tm_end_separator_num][TIME_DATA_MAX_SIZE];
    analyze_socket_data(tm_begin_temp_buf[0],argv[1]);
    analyze_socket_data(tm_end_temp_buf[0],argv[2]);
#ifdef _DEBUG_KJ
    for(i_temp = 0; i_temp < tm_begin_separator_num;i_temp++)
    {
	printf("%s %d The %dth content is %s \n",__FUNCTION__,__LINE__,i_temp,tm_begin_temp_buf[i_temp]);
    }
    for(i_temp = 0; i_temp < tm_end_separator_num;i_temp++)
    {
	printf("%s %d The %dth content is %s \n",__FUNCTION__,__LINE__,i_temp,tm_end_temp_buf[i_temp]);
    }
#else
#endif
    tm_begin_seconds = calculate_seconds(tm_begin_temp_buf);
    tm_end_seconds = calculate_seconds(tm_end_temp_buf);
    the_seconds_of_record = tm_end_seconds - tm_begin_seconds; 
    printf("%s %d The begin seconds is %d,The end seconds is %d ,The length of record video is %d\n",__FUNCTION__,__LINE__,tm_begin_seconds,tm_end_seconds,the_seconds_of_record);

    return 0;
}

 在我Linux上运行测试结果如下:

root@u12d32:/home/kongjun/work/hi_test/year_month_day_tosecond# ./ymd_to_second 2013:12:08:17:31:00: 2014:12:08:14:40:12:
main 160 The current time is 2013-12-08 17:40:59 , The total seconds is 1386495659!
main 174 The 1th content is 2013:12:08:17:31:00:
main 174 The 2th content is 2014:12:08:14:40:12:
calculate_seconds 142 The total of seconds is 1386495060
calculate_seconds 142 The total of seconds is 1418020812
main 198 The begin seconds is 1386495060,The end seconds is 1418020812 ,The length of record video is 31525752

下面是关于时间的小常识:

 
 
#include <stdio.h>
#include <time.h>
int main(void)
{
    time_t timep;
    struct tm *p;
    time(&timep);
    printf("time() : %d \n",timep);
    p=localtime(&timep);
    timep = mktime(p);
    printf("time()->localtime()->mktime():%d\n",timep);
    return 0;
}

struct tm小常识:

 
#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()为负。*/
 
long int tm_gmtoff; /*指定了日期变更线东面时区中UTC东部时区正秒数或UTC西部时区的负秒数*/
 
const char *tm_zone; /*当前时区的名字(与环境变量TZ有关)*/
 
};
 
#define _TM_DEFINED
 
#endif
 
ANSI C标准称使用tm结构的这种时间表示为分解时间(broken-down time)。



 

你可能感兴趣的:(嵌入式 把年月日转换为当前系统的秒数)