c++时间戳转标准时间
2019-04-17 14:04:50 weixin_42310224 阅读数 25

标准时间转UTC时间戳

#include "pch.h"
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include<fstream>
#include<iostream>
#include<string>
#include <vector>
#include<sstream>
#include<algorithm>
#include<math.h>
#include "pch.h"
#include <iomanip> 
#include <time.h>
#include <comutil.h>  
using namespace std;

vector<string>  splitString(string srt, char symbol)
{
	istringstream istr(srt);
	vector<string> params;
	string param;

	while (getline(istr, param, symbol))
	{
		params.push_back(param);
	}
	return params;
}

//标准时间格式例如:2016:08:02 12:12:30
int standard_to_stamp(char *str_time)
{
	struct tm stm;
	int iY, iM, iD, iH, iMin, iS;
	memset(&stm, 0, sizeof(stm));
	iY = atoi(str_time);
	iM = atoi(str_time + 5);
	iD = atoi(str_time + 8);
	iH = atoi(str_time + 11);
	iMin = atoi(str_time + 14);
	iS = atoi(str_time + 17);
	stm.tm_year = iY - 1900;
	stm.tm_mon = iM - 1;
	stm.tm_mday = iD;
	stm.tm_hour = iH;
	stm.tm_min = iMin;
	stm.tm_sec = iS;
	return (int)mktime(&stm);
}

void readFile()
{
	ofstream ofile;
	ofile.open("F://huyu//轨迹数据//all_Output.csv", ios::out | ios::trunc); 	//输出文件路径
	string filePath = "F://huyu//轨迹数据//all.csv";											//输入文件路径
	ifstream infile;
	infile.open(filePath, ios::in);

	if (!infile.is_open())
	{
		cout << "open File Failed." << endl;
	}
	string line;
	int l = 0;
	int n_id = 0;
	string strCarId = "";
	ofile << "id" << "," << "openid" << "," << "lat" << "," << "lng" << "," << "time" << endl;
	int nl = 0;
	while (getline(infile, line))
	{
		if (nl == 0) {
			nl = 1;
		}
		else
		{
			cout << "正在处理第--------" << l++ << "--------行轨迹数据" << endl;
			vector<string> params;
			params = splitString(line, ',');

			string stime = params[4];
			stime.c_str();
			int utc = standard_to_stamp((char*)stime.c_str());
			ofile << setprecision(18) << params[0] << "," << params[1] << "," << params[2] << "," << params[3] << "," << utc << endl;//		
		}
	}
	infile.close();
	ofile.close();
	cout << "轨迹数据预处理完成!" << endl;
}
int main()
{
	readFile();
}

2019-04-14 00:18:21 weixin_42427338 阅读数 112

对方发过来的时间格式是时间戳,我这边需要解析成普通的日期格式,参考了下面的帖子

https://www.cnblogs.com/kaituorensheng/p/3922919.html

使用的是time.h里面函数,还是c的函数比较好用

void   JsonMeterMessage::timeConvert(string timeStamp)
{
     stringstream ss;
     time_t timeTemp;
     ss << timeStamp;
     ss >>  timeTemp;

     char temp[50];


    struct tm * timeSet = gmtime(&timeTemp);

    strftime(temp,50, "%Y-%m-%d %H:%M:%S", timeSet);

    cout << temp << endl;

    string time = string(temp);

    string Date = time.substr(0,time.find(" "));

    string Time = time.substr(time.find(" ") + 1,8);

    cout << Time << endl;
    cout << Date << endl;

}

输出

1555062603 /****输入的时间戳****/
2019-04-12 09:50:03
09:50:03
2019-04-12

 

2019-05-24 08:45:23 xiaoshunzi111 阅读数 22
#define _CRT_SECURE_NO_WARNINGS 1
#include "iostream"
#include "time.h"
#include "string.h"
using namespace std;
typedef struct times
{
	int Year;
	int Mon;
	int Day;
	int Hour;
	int Min;
	int Second;
}Times;
Times stamp_to_standard(int stampTime)
{
	time_t tick = (time_t)stampTime;
	struct tm tm; 
	char s[100];
	Times standard;
	tm = *localtime(&tick);
	strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", &tm);
	printf("%d: %s\n", (int)tick, s); 
	standard.Year = atoi(s);
	standard.Mon = atoi(s+5);
	standard.Day = atoi(s+8);
	standard.Hour = atoi(s+11);
	standard.Min = atoi(s+14);
	standard.Second = atoi(s+17);
	return standard;
}
int main()
{
	Times t = stamp_to_standard(1496299670);
	return 0;
}

注:里面的atoi(s+5);+8、+11、+14、+17表示的是传入参数的格式中每个数字距离字符串开头的长度;

 

运行结果如下:

来自:https://blog.csdn.net/e295166319/article/details/72846760

2017-06-02 18:08:21 e295166319 阅读数 5334

代码如下:

#include "iostream"
#include "time.h"
#include "string.h"

using namespace std;

typedef struct times
{
	int Year;
	int Mon;
	int Day;
	int Hour;
	int Min;
	int Second;
}Times;

Times stamp_to_standard(int stampTime)
{
	time_t tick = (time_t)stampTime;
	struct tm tm; 
	char s[100];
	Times standard;

	tm = *localtime(&tick);
	strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", &tm);
	printf("%d: %s\n", (int)tick, s); 

	standard.Year = atoi(s);
	standard.Mon = atoi(s+5);
	standard.Day = atoi(s+8);
	standard.Hour = atoi(s+11);
	standard.Min = atoi(s+14);
	standard.Second = atoi(s+17);

	return standard;
}

int main()
{
	Times t = stamp_to_standard(1496299670);

	return 0;
}

注:里面的atoi(s+5); +8、+11、+14、+17表示的是传入参数的格式中每个数字距离字符串开头的长度;

运行结果如下:




2017-06-02 17:40:10 e295166319 阅读数 3357

所使用的的头文件如下:

#include "iostream"
#include "time.h"
#include "string.h"

using namespace std;


代码如下:

int standard_to_stamp(char *str_time)  
{  
	struct tm stm;  
	int iY, iM, iD, iH, iMin, iS;  

	memset(&stm,0,sizeof(stm));  
	iY = atoi(str_time);  
	iM = atoi(str_time+5);  
	iD = atoi(str_time+8);  
	iH = atoi(str_time+11);  
	iMin = atoi(str_time+14);  
	iS = atoi(str_time+17);  

	stm.tm_year=iY-1900;  
	stm.tm_mon=iM-1;  
	stm.tm_mday=iD;  
	stm.tm_hour=iH;  
	stm.tm_min=iMin;  
	stm.tm_sec=iS;  

	printf("%d-%0d-%0d %0d:%0d:%0d\n", iY, iM, iD, iH, iMin, iS);   //标准时间格式例如:2016:08:02 12:12:30
	return (int)mktime(&stm);  
}  

int main()
{
	int stampTime = standard_to_stamp("2016:08:02 12:12:30");
	cout << stampTime << endl;

	return 0;
}

注:里面的atoi(s+5); +8、+11、+14、+17表示的是传入参数的格式中每个数字距离字符串开头的长度;

运行结果如下:



没有更多推荐了,返回首页