栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > C/C++/C#

C++时间戳转换

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++时间戳转换

目录
  • 一、参考链接
  • 二、代码实现
  • 三、结果展示
  • 四、时间转换工具
  • 五、资源下载

一、参考链接

[1] C++11获取系统当前时间(精确到微秒)
[2] C++11的chrono库,可实现毫秒微秒级定时
[3] c++11:std::chrono::duration

二、代码实现

ReadTimeFromTXT.h

#pragma once
#include 
#include 
#include 
#include 
#include  // for split
#include 
#include 


#pragma region
bool DataReader(const std::string& filename, std::vector& data)
{
	std::ifstream fs;
	fs.open(filename.c_str(), std::ios::binary);
	if (!fs.is_open() || fs.fail())
	{
		fs.close();
		return (false);
	}

	std::string line;
	std::vector st;

	while (!fs.eof())
	{
		std::getline(fs, line);
		// Ignore empty lines
		if (line.empty())
			continue;
		// Tokenize the line
		boost::trim(line);
		boost::split(st, line, boost::is_any_of("tr "), boost::token_compress_on);

		if ((st.size() != 1))
			continue;
		data.push_back(time_t(atof(st[0].c_str())));
	}
	fs.close();


    return (true);
}
#pragma endregion

main.cpp

#include 
#include 
#include "ReadTimeFromTXT.h"

int main()
{

	std::string inputName = "time.txt";      // 待计算的文件名

	std::vector timesTamp;
	DataReader(inputName, timesTamp);
	std::string outputName = inputName.substr(0, inputName.rfind("."));//获取不带文件路径和后缀的文件名
	std::ofstream mycout(outputName + "_cycle.txt"); // 创建输出流对象并提供txt文件名
#pragma region// ----------------------------时间转换-----------------------------------
	for (auto& seconds_since_epoch : timesTamp)
	{
		std::tm current_time = *std::localtime(&seconds_since_epoch);    // 获取当前时间(精确到秒)
		std::tm gm_time = *std::gmtime(&seconds_since_epoch);            // 格林尼治时间(精确到秒)

		std::chrono::duration duration_since_epoch(seconds_since_epoch); // 从1970-01-01 00:00:00到当前时间点的时长
		time_t microseconds_since_epoch = std::chrono::duration_cast(duration_since_epoch).count(); // 将时长转换为微秒数
		time_t milliseconds_since_epoch = std::chrono::duration_cast(duration_since_epoch).count(); // 将时长转换为毫秒数
		time_t tm_microsec = microseconds_since_epoch % 1000;         // 当前时间的微妙数
		time_t tm_millisec = milliseconds_since_epoch % 1000;         // 当前时间的毫秒数


		mycout << "时间戳:" << seconds_since_epoch << "------------------------>" << "北京时间:"
			<< current_time.tm_year + 1900 << "年" << current_time.tm_mon + 1 << "月" << current_time.tm_mday << "日"
			<< current_time.tm_hour << ":" << current_time.tm_min << ":" << current_time.tm_sec << ":" << tm_millisec << ":" << tm_microsec;

		mycout << "------------------------>" << "格林尼治时间:"
			<< gm_time.tm_year + 1900 << "年" << gm_time.tm_mon + 1 << "月" << gm_time.tm_mday << "日"
			<< gm_time.tm_hour << ":" << gm_time.tm_min << ":" << gm_time.tm_sec << ":" << tm_millisec << ":" << tm_microsec

			<< std::endl;
	}
#pragma endregion
	mycout.close(); // 关闭打开的文件
	printf("时间转换完毕!!");

	return 0;
}
三、结果展示

原始数据

899783
899783
899783
899783
899783
899783
899783
899783
899783
899783
899783
899783
899783
899783
899783
899783
899783

转换结果

时间戳:899783------------------------>北京时间:1970年1月11日17:56:23:0:0------------------------>格林尼治时间:1970年1月11日9:56:23:0:0
时间戳:899783------------------------>北京时间:1970年1月11日17:56:23:0:0------------------------>格林尼治时间:1970年1月11日9:56:23:0:0
时间戳:899783------------------------>北京时间:1970年1月11日17:56:23:0:0------------------------>格林尼治时间:1970年1月11日9:56:23:0:0
时间戳:899783------------------------>北京时间:1970年1月11日17:56:23:0:0------------------------>格林尼治时间:1970年1月11日9:56:23:0:0
时间戳:899783------------------------>北京时间:1970年1月11日17:56:23:0:0------------------------>格林尼治时间:1970年1月11日9:56:23:0:0
时间戳:899783------------------------>北京时间:1970年1月11日17:56:23:0:0------------------------>格林尼治时间:1970年1月11日9:56:23:0:0
时间戳:899783------------------------>北京时间:1970年1月11日17:56:23:0:0------------------------>格林尼治时间:1970年1月11日9:56:23:0:0
时间戳:899783------------------------>北京时间:1970年1月11日17:56:23:0:0------------------------>格林尼治时间:1970年1月11日9:56:23:0:0
时间戳:899783------------------------>北京时间:1970年1月11日17:56:23:0:0------------------------>格林尼治时间:1970年1月11日9:56:23:0:0
时间戳:899783------------------------>北京时间:1970年1月11日17:56:23:0:0------------------------>格林尼治时间:1970年1月11日9:56:23:0:0
时间戳:899783------------------------>北京时间:1970年1月11日17:56:23:0:0------------------------>格林尼治时间:1970年1月11日9:56:23:0:0
时间戳:899783------------------------>北京时间:1970年1月11日17:56:23:0:0------------------------>格林尼治时间:1970年1月11日9:56:23:0:0
时间戳:899783------------------------>北京时间:1970年1月11日17:56:23:0:0------------------------>格林尼治时间:1970年1月11日9:56:23:0:0
四、时间转换工具

在线时间戳转换工具

五、资源下载

时间戳转换C++代码实现

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1037200.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号