sscanf和snprintf格式化时间字符串的日期与时间戳相互转换用法

sscanf格式化时间字符串的用法

UTC:Coordinated Universal Time 协调世界时。因为地球自转越来越慢,每年都会比前一年多出零点几秒,每隔几年协调世界时组织都会给世界时+1秒,让基于原子钟的世界时和基于天文学(人类感知)的格林尼治标准时间相差不至于太大。并将得到的时间称为UTC,这是现在使用的世界标准时间。

协调世界时,又称世界统一时间、世界标准时间、国际协调时间。由于英文(CUT)和法文(TUC)的缩写不同,作为妥协,简称UTC。

(1)时间字符串尾部加Z 表示UTC。+0800表示加上8时区, 也即本地时间。

(2)为了操作方便,时间字符串的格式应该写为: 2020/02/22 10:00:00 这样的格式。

(3)年月日中间用横杠连接且没有指定时分秒则默认表示UTC时间. 可以指定UTC+0800表示时间字符串格式。 (4)时分秒后面加Z表示UTC时间, 不指定Z则表示本地时间。为了防止出错, 建议年月日中间用斜杠‘/’连接。

使用sscanf函数常常会报错,这个时候我们只要在开头加上,#define _CRT_SECURE_NO_WARNINGS
即可取消警告。

sscanf的作用:从一个字符串中读进于指定格式相符的数据。利用它可以从字符串中取出整数、浮点数和字符串。

sscanf和scanf的区别:scanf是以键盘作为输入源,sscanf是以字符串作为输入源。

简单来说就是从 s 处读取c语言字符串类型的数据源,再根据format对应的格式存储到附加参数所给出的位置当中去,效果是类似于scanf()的,但是这个的读入是直接以这个字符串作为输入源。如果成功,则回返回成功填充的参数个数。

在这里插入图片描述

sscanf:

原型:

int sscanf(const char *str, const char *format,......);

说明:

sscanf()会将参数str的字符串根据参数format字符串来转换格式并格式化数据。转换后的结果存于对应的参数内。 成功则返回参数数目,失败则返回0

日期时间与时间戳的相互转换

struct tm
{
    int tm_sec;   // seconds after the minute - [0, 60] including leap second
    int tm_min;   // minutes after the hour - [0, 59]
    int tm_hour;  // hours since midnight - [0, 23]
    int tm_mday;  // day of the month - [1, 31]
    int tm_mon;   // months since January - [0, 11]
    int tm_year;  // years since 1900
    int tm_wday;  // days since Sunday - [0, 6]
    int tm_yday;  // days since January 1 - [0, 365]
    int tm_isdst; // daylight savings time flag
};

时间戳转日期时间 1680537600

先将time_t转换为tm,在格式化输出。

struct tm* gmtime(const time_t* timer);
struct tm* localtime(const time_t* timer);
struct tm* localtime(const time_t* timer);
localtime()函数考虑时区,将时间戳转为本地日历时间。

其中 gmtime() 函数是将时间戳转化为 UTC+0 的日历时间,即0时区的世界标准时间,并返回一个struct tm结构体来保存这个时间。

而localtime()函数考虑时区,将时间戳转为本地日历时间。比如用gmtime()函数获得的时间是2018-07-30 00:00:00,那么用localtime()函数在中国地区获得的本地时间会比世界标准时间早8个小时,即2018-07-30 08:00:00。

std::string Snprintf(time_t t) {
	//时间戳转日期时间
	if (t < 0) {
		return "";
	}
	tm tm_ = *localtime(&t);
	char str[100] = {0};
	snprintf(str, sizeof(str), "%04d-%02d-%02d %02d:%02d:%02d",
		tm_.tm_year+1900, tm_.tm_mon+1, tm_.tm_mday, tm_.tm_hour, tm_.tm_min, tm_.tm_sec);
	return std::string(str);
}

日期时间转时间戳:2023/4/4 0:0:0

(1)当前的UTC时间 是从 1970年01月01日 0:00:00 开始到现在所相差的秒数. 如果需要转换为日期, 那么需要通过函数进行转换。

(2)C标准库里的time_t类型, 它代表一个秒数, 从 1970.1.1 00:00 开始到 time_t所代表的时间的间隔的秒数。 (3)C标准库里的日期时间结构体,需要注意的 tm_year 是从1900开始的年份差, 如果需要得到实际的年份,需要+1900,月份+1。如果得到时间戳的话年份需要-1900,月份-1。

使用sscanf将日期时间字符串转为struct tm,再转为time_t。

int64_t Sscanf(const std::string& str) {
	//日期时间转时间戳
	//使用sscanf将日期时间字符串转为struct tm,再转为time_t。
	tm tm_ = { 0 };
	if (3== sscanf(str.c_str(), "%d/%d/%d",
		&tm_.tm_year, &tm_.tm_mon, &tm_.tm_mday)) {
		tm_.tm_year = tm_.tm_year - 1900;
		tm_.tm_mon = tm_.tm_mon - 1;
	}
	time_t t_ = mktime(&tm_);
	return t_ * 1000;
}

主函数:

int main(){
	std::cout << Snprintf(1680537600) << std::endl;
	std::string str = "2023/4/4";
	int64_t tm = Sscanf(str);
	std::cout << tm << std::endl;
	return 0;
}

输出结果:

2023-04-04 00:00:00
1680537600000
请按任意键继续. . .

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44918090/article/details/130096285