C语言时间提取与转换

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值