Linux下使用C语言获取ISO8601时间格式字符串

#include <time.h>
#include <stdio.h>
#include <string.h>

char * iso8601(char local_time[32])
{
	time_t t_time;
	struct tm *t_tm;

	t_time = time(NULL);
	t_tm = localtime(&t_time);
	if (t_tm == NULL)
		return NULL;

	if(strftime(local_time, 32, "%FT%T%z", t_tm) == 0)
		return NULL;
								
	local_time[25] = local_time[24];
	local_time[24] = local_time[23];
	local_time[22] = ':';
	local_time[26] = '\0';
	return local_time;
}

int main()
{
	char time[32] = {0};

	printf("ISO Time: %s\n", iso8601(time));
}
ISO Time: 2020-04-08T14:35:13+08:00

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页