#include <stdlib.h>
#include <stdio.h>
typedef struct
{
unsigned char DeviceID[10];//IMEI
unsigned char date[10];
unsigned char time[10];
int tempature;
int humidity;
int voltage;
int current;
float longitude;
float latigude;
int airDistance;
char errorCode;
}Info_send;
typedef struct
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
}timebase;
char timestring[30];
typedef struct
{
char year[10];
char mon[10];
char day[10];
char hour[10];
char min[10];
char sec[10];
}time_struct;
time_struct time;
timebase tm;
Info_send message_Send;
int main()
{
//识别字符串
strcpy(timestring,"+CCLK: \"21/07/23,14:56:30+32\"");
printf("%s\n",timestring);
sscanf(timestring,"+CCLK: \"%d/%d/%d,%d:%d:%d\"",&tm.tm_year,&tm.tm_mon,&tm.tm_mday,&tm.tm_hour,&tm.tm_min,&tm.tm_sec);
printf("%d-%d-%d %d:%d:%d\n",tm.tm_year,tm.tm_mon,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
Transform_Time();
return 0;
}
void Transform_Time()
{
//转换
itoa(tm.tm_year+2000,time.year,10);
itoa(tm.tm_mon,time.mon,10);
itoa(tm.tm_mday,time.day,10);
itoa(tm.tm_hour,time.hour,10);
itoa(tm.tm_min,time.min,10);
itoa(tm.tm_sec,time.sec,10);
//清零
memset(message_Send.date,0,15);
//赋值字符串
strcat(message_Send.date,time.year);
strcat(message_Send.date,"-");
strcat(message_Send.date,time.mon);
strcat(message_Send.date,"-");
strcat(message_Send.date,time.day);
strcat(message_Send.time,time.hour);
strcat(message_Send.time,":");
strcat(message_Send.time,time.min);
strcat(message_Send.time,":");
strcat(message_Send.time,time.sec);
printf("数字日期message_Send.date:转换后的字符串为:%s\n",message_Send.date);
printf("数字日期message_Send.time:转换后的字符串为:%s\n",message_Send.time);
}
C语言时间提取与转换
最新推荐文章于 2022-08-29 14:01:34 发布