dealskyalgo.cc

#include <stdio.h>
#include <memory.h>
#include <iostream>
#include <ctime>
#include <string>
#include <vector>
#include <map>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <time.h>

// 时间格式:2020-02-08 18:00:00
time_t strTime2unix(std::string timestamp, int offsettime)
{
	struct tm tm;
	memset(&tm, 0x0, sizeof(tm));
	
	sscanf(timestamp.c_str(), "%d-%d-%d %d:%d:%d",
			&tm.tm_year, &tm.tm_mon, &tm.tm_mday,
			&tm.tm_hour, &tm.tm_min, &tm.tm_sec);
	tm.tm_year -= 1900;
	tm.tm_mon--;
	
	return mktime(&tm) + offsettime;
}

// 时间格式:2020-02-08 18:00:00.123
time_t strTime2unix2(std::string timestamp, int offsettime)
{
	struct tm tm;
	memset(&tm, 0x0, sizeof(tm));
	int del;
	sscanf(timestamp.c_str(), "%d-%d-%d %d:%d:%d.%d",
			&tm.tm_year, &tm.tm_mon, &tm.tm_mday,
			&tm.tm_hour, &tm.tm_min, &tm.tm_sec, &del);
	tm.tm_year -= 1900;
	tm.tm_mon--;
	
	return mktime(&tm) + offsettime;
}

std::string unix2time(time_t timestamp)
{
	std::stringstream ss;
	struct tm tm;
	localtime_r(&timestamp, &tm);
	//ss << tm.tm_year+1900 << "-" << tm.tm_mon+1 << "-" << tm.tm_mday << " " << tm.tm_hour << ":" << tm.tm_min << ":" << tm.tm_sec;
	//return ss.str();
	char time[24] = {0};
	strftime(time, sizeof(time), "%Y-%m-%d %H:%M:%S", &tm);
	return time;
}

// 没用到
void Split(const std::string &src, const std::string &sep, std::vector<std::string> &dest)
{
	dest.clear();
	int iCurrentIndex = 0;
	int iSplitIndex;
	
	while (iCurrentIndex < src.size())
	{
		iSplitIndex = src.find(sep, iCurrentIndex);
		if (iSplitIndex == -1)
			iSplitIndex = src.size();
		
		if (iSplitIndex != iCurrentIndex)
			dest.push_back(src.substr(iCurrentIndex, iSplitIndex - iCurrentIndex));
		
		iCurrentIndex = iSplitIndex + sep.size();
	}
}

struct BegEndStruct
{
	BegEndStruct(int b, int e) : beg(b), end(e) { }
	BegEndStruct(const BegEndStruct &be)
	{
		beg = be.beg;
		end = be.end;
	}
	BegEndStruct &operator=(const BegEndStruct &be)
	{
		beg = be.beg;
		end = be.end;
		return *this;
	}
	int beg;
	int end;
};

int readCmpFile(std::string file, std::map<int, BegEndStruct> &tocmpMap)
{
	std::ifstream in;
	in.open(file.c_str());
	if (!in.is_open())
	{
		std::cout << "open file: " << file << " failure" << std::endl;
		return -1;
	}
	// 读文件行格式:
	// 1000000 2022-2-7 7:55:05    2022-2-7 13:05:58
	int userid;
	std::string begDate, begTime, endDate, endTime;
	while (!in.eof())
	{
		in >> userid >> begDate >> begTime >> endDate >> endTime;
		if (in.fail())
			break;
		tocmpMap.insert(std::make_pair(userid, BegEndStruct(strTime2unix(begDate + " " + begTime, 0), strTime2unix(endDate + " " + endTime, 0))));
		//in.get();
		//if (in.peek() == '\n')
		//	break;
	}
	in.close();
	
	return 0;
}

int readSkyalgoFile(std::string file, std::map<int, BegEndStruct> tocmpMap, std::map<int, std::vector<int> > &out, int offsettime)
{
	std::ifstream in;
	in.open(file.c_str());
	if (!in.is_open())
	{
		std::cout << "open file: " << file << " failure" << std::endl;
		return -1;
	}
	// 读文件行格式:
	// 2022-02-07 7:55:05.000 1000000   
	int userid;
	std::string connDate, connTime;
	while (!in.eof())
	{
		in >> connDate >> connTime >> userid;
		if (in.fail())
			break;
		//std::cout << userid << " " << connDate << " " << connTime << std::endl;
		
		std::map<int, BegEndStruct>::iterator iter = tocmpMap.find(userid);
		if (iter != tocmpMap.end())
		{
			int timestamp = strTime2unix2(connDate + " " + connTime, offsettime);
			if (timestamp >= iter->second.beg && timestamp <= iter->second.end)
			{
				out[userid].push_back(timestamp);
			}
		}
		//in.get(); //读取最后换行符
		//if (in.peek() == '\n')	// 如果是最后一行,则peek取不到下一个字符,则取上一次的换行符,但文本中有多个连续换行则解决不了
		//	break;
	}
	in.close();
	
	return 0;
}

int main(int argc, char *argv[])
{
	//std::string str = "2020-02-08 18:00:00";
	//time_t t = strTime2unix(str, -3600*8);
	//printf("unix timestamp: %d\n", t);
	//std::cout << ctime(&t) << std::endl;
	
	//std::string str2 = "2020-02-08 18:00:00";
	//time_t t2 = strTime2unix2(str, -3600*8);
	//printf("unix timestamp: %d\n", t2);
	//std::cout << ctime(&t2) << std::endl;
	
	std::map<int, BegEndStruct> tocmpMap;
	readCmpFile("tocmp", tocmpMap);
	
	std::map<int, std::vector<int> > out;
	readSkyalgoFile("userid.pm", tocmpMap, out, 0);
	readSkyalgoFile("userid.pm3", tocmpMap, out, -3600*8);
	
	std::map<int, std::vector<int> >::iterator iter2;
	for (iter2 = out.begin(); iter2 != out.end(); ++iter2)
	{
		if (iter2->second.size() >= atoi(argv[1]))
		{
			std::stringstream ss;
			for (int i =0; i != iter2->second.size(); ++i)
			{
				ss << unix2time((time_t)iter2->second[i]) << " ";
			}
			std::cout << iter2->first << " " << ss.str() << std::endl;
		}
	}
	
	return 0;
}
 
喜欢 0
分享