这里直接利用的宏就可以实现,月份字符串转月份数值功能,例如将“jan” 转为数字1,即1月份。
datetime.h
#ifndef __DATETIME_H_
#define __DATETIME_H_
#include <rtthread.h>
#define MONTH_TABLE() \
DEF_MONTH(Jan) \
DEF_MONTH(Feb) \
DEF_MONTH(Mar) \
DEF_MONTH(Apr) \
DEF_MONTH(May) \
DEF_MONTH(Jun) \
DEF_MONTH(Jul) \
DEF_MONTH(Aug) \
DEF_MONTH(Oct) \
DEF_MONTH(Nov) \
DEF_MONTH(Dec)
//定义月份枚举,其枚举元素名和月份缩略字符串同名
typedef enum
{
E_MONTH_START = 0,
#define DEF_MONTH(v) v,
MONTH_TABLE()
#undef DEF_MONTH
E_MONTH_END,
}E_MONTH;
//这里实现了几个实用的函数
uint32_t datetimeToUtc(const char* date, const char*time);
void datetimeFormat(const char* date, char*out);
uint32_t versionStrToInt(const char* version);
#endif
datetime.c
#include "datetime.h"
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "log.h"
//巧用'#'符,直接将枚举转为对应字符串
const char* MonthStringOf(E_MONTH c) {
switch (c) {
#define DEF_MONTH(v) \
case v: \
return #v;
MONTH_TABLE()
#undef DEF_MONTH
default:
return "Unknown";
}
}
//同理,这里使用'#'符,将枚举转化为字符串后,在和输入参数比较,相等则返回该月份缩略字符串代表的月份数值
int getMonthNumber(const char* month) {
#define DEF_MONTH(v) \
if(0 == rt_strcmp(#v, month)) return v;
MONTH_TABLE()
#undef DEF_MONTH
}
void mySplit(const char *src, const char *separator, char dest[][20], int *num)
{
char *pPre = NULL;
char *pNext = NULL;
//记录分隔符数量
int count = 0;
//原字符串为空
if (src == NULL || strlen(src) == 0)
return;
//未输入分隔符
if (separator == NULL || strlen(separator) == 0)
return;
//获得第一个由分隔符分割的字符串
pNext = strtok(src,separator);
while(pNext != NULL) {
//存入到目的字符串数组中
pPre = pNext;
pNext = strtok(NULL, separator);
if (pPre && pNext)
{
strncpy(dest[count++], pPre, (pNext-pPre-1));
}
else if(pPre)
{
strcpy(dest[count++], pPre);
}
}
*num = count;
}
//时间字符串转UTC秒数
//date输入格式:Aug 15 2023, time输入格式:11:08:22
uint32_t datetimeToUtc(const char* date, const char*time)
{
char result[10][20];
int num = 0;
struct tm local_time;
if (date == NULL || time == NULL) return 0;
rt_memset(&local_time, 0, sizeof(local_time));
rt_memset(result, 0, sizeof(result));
mySplit(date, " ", result, &num);
if (num < 3)
{
LOGT("","date:%s num < 3! num=%d\n",date, num);
return 0;
}
else
{
local_time.tm_mon = getMonthNumber(result[0]) - 1;
local_time.tm_mday = atoi(result[1]);
local_time.tm_year = atoi(result[2]) - 1900;
}
rt_memset(result, 0, sizeof(result));
num = 0;
mySplit(time, ":", result, &num);
if (num < 3)
{
LOGT("","time:%s num < 3! num=%d\n",time, num);
return 0;
}
else
{
local_time.tm_hour = atoi(result[0]);
local_time.tm_min = atoi(result[1]);
local_time.tm_sec = atoi(result[2]);
}
time_t utc_time = mktime(&local_time);
return utc_time;
}
//时间字符串格式化输出
//date输入格式:Aug 15 2023, 输出格式:230815
void datetimeFormat(const char* date, char* out)
{
char result[10][20];
int num = 0;
if (date == NULL || time == NULL) return;
rt_memset(result, 0, sizeof(result));
mySplit(date, " ", result, &num);
if (num < 3)
{
LOGT("","date:%s num < 3! num=%d\n",date, num);
return;
}
else
{
rt_sprintf(out, "%2d%02d%02d", atoi(result[2])%100, getMonthNumber(result[0]), atoi(result[1]));
}
}
//版本字符串转整数1.3.1 -> 10301 ,次版本号,修订版本号分别占两位数
uint32_t versionStrToInt(const char* version)
{
char result[10][20];
int num = 0;
uint32_t i_vertion = 0;
if (version == NULL) return 0;
rt_memset(result, 0, sizeof(result));
mySplit(version, ".", result, &num);
if (num < 3)
{
LOGT("","version:%s num < 3! num=%d\n",num);
return 0;
}
else
{
i_vertion = atoi(result[0])*10000;
i_vertion += atoi(result[1])*100;
i_vertion += atoi(result[2]);
}
return i_vertion;
}
标签:字符,月份,MONTH,char,num,result,time,C语言,DEF
From: https://www.cnblogs.com/HuangLiDi/p/18131518