TimeTableCatalogUtil.cc 3.19 KB
/*
 * TimeTableCatalogUtil.cc
 *
 *  Created on: 6 août 2013
 *      Author: CS
 */

#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/lexical_cast.hpp>

#include "TimeTableCatalogUtil.hh"
#include "TimeUtil.hh"
#include "DD_time.hh"
#include <sstream>
#include <iomanip>

namespace TimeTableCatalog {

double readISOTime(const std::string& ptime) {
	return readISOTime(ptime, getTimeFormat(ptime));
}

TimeTable::TIME_FORMAT getTimeFormat(const std::string& ptime) {
	if (std::string::npos != ptime.find(".")) {
		return TimeTable::TIME_FORMAT::YYYYMMDDThhmmssmsk;
	}
	return TimeTable::TIME_FORMAT::YYYYMMDDThhmmss;
}

double readISOTime(const std::string& ptime,
		const TimeTable::TIME_FORMAT pformat) {
	if (ptime.length() != TimeTable::YYYYMMDDThhmmss_LENGTH
			&& ptime.length() != TimeTable::YYYYMMDDThhmmssmsk_LENGTH) {
		return -1;
	}

	dd_tmstr_t *UT;
	unsigned tabTime[7];
	char c;
	std::stringstream ss(ptime);
	ss << ptime;
	ss.width(4);
	ss >> tabTime[0]; //year;

	ss.width(1);
	ss >> c; //-
	ss.width(2);
	ss >> tabTime[1]; //month

	ss.width(1);
	ss >> c; //-
	ss.width(2);
	ss >> tabTime[2]; //day;

	ss.width(1);
	ss >> c; //T
	ss.width(2);
	ss >> tabTime[3]; //hour;

	ss.width(1);
	ss >> c; //:
	ss.width(2);
	ss >> tabTime[4]; //min;

	ss.width(1);
	ss >> c; //:
	ss.width(2);
	ss >> tabTime[5]; //sec;

	if (pformat == TimeTable::TIME_FORMAT::YYYYMMDDThhmmssmsk) {
		ss.width(1);
		ss >> c; //.
		ss.width(3);
		ss >> tabTime[6]; //ms;
	} else {
		tabTime[6] = 0;
	}

	UT = UT2double(tabTime);

	return UT->times;
}

void writeISOTime(const double pTime, const TimeTable::TIME_FORMAT pFormat,
		std::ostream& pOut) {
	if (pTime == -1) {
		return;
	}

	std::ostringstream os;
	AMDA::TimeUtil::formatTimeDateInIso(pTime, os);
	if (TimeTable::TIME_FORMAT::YYYYMMDDThhmmss == pFormat) {
		// remove milliseconds
		pOut << os.str().substr(0, os.str().length() - 4);
	} else { // UNKNOWN or with millisecond
		pOut << os.str();
	}
}

// ----------------- EXTERN --------------------------

bool contains(const std::string & pline, const std::string & pkeyword,
		const std::string& pprefix) {
	std::string simpleline(pline);
	// remove all whitespace
	boost::algorithm::replace_all(simpleline, " ", "");
	// to lower case
	std::transform(simpleline.begin(), simpleline.end(), simpleline.begin(),
			::tolower);

	std::string simpleKeyword(pprefix + pkeyword);
	// remove all whitespace
	boost::algorithm::replace_all(simpleKeyword, " ", "");
	// to lower case
	std::transform(simpleKeyword.begin(), simpleKeyword.end(),
			simpleKeyword.begin(), ::tolower);

	return std::string::npos != simpleline.find(simpleKeyword);
}

void extractvalue(const std::string & pline, std::string & pvalue) {
	// get value after ":"
	pvalue = pline.substr(pline.find(":") + 1);
	// remove trailing with spaces
	boost::algorithm::trim(pvalue);
	// remove ; character at the end of the line
	boost::algorithm::replace_last(pvalue, ";", "");
}

std::vector<std::string> &split(const std::string &s, char delim,
		std::vector<std::string> &elems) {
	std::stringstream ss(s);
	std::string item;
	while (std::getline(ss, item, delim)) {
		elems.push_back(item);
	}
	return elems;
}

} /* namespace TimeTableCatalog */