TimeManager.cpp 9.95 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
#include "TimeManager.h"

#include <iostream>

#include <cstdio>
#include <cstring>
#include <cstdlib>

#include "cdf.h"

#include "../Common/Toolbox.h"

using namespace TREPS::Common;

namespace TREPS
{
	namespace TimeManager
	{
		TimeManagerClass::TimeManagerClass(void): app(NULL), loader(NULL), crtPattern(NULL)
		{
			this->app = ApplicationClass::getInstance();
			//create a time pattern class
			this->crtPattern = new TimePatternClass();
		}

		TimeManagerClass::~TimeManagerClass(void)
		{
			if (this->loader != NULL)
			{
				this->loader->close();
				delete this->loader;
				this->loader = NULL;
			}
			if (this->crtPattern != NULL)
			{
				delete this->crtPattern;
				this->crtPattern = NULL;
			}	
		}

		bool TimeManagerClass::init(const char *file_path)
		{
			//init loader
			if (this->loader == NULL)
				this->loader = new XMLManagerClass();

			this->loader->close();

			if (!this->loader->isExist(file_path))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot find times file : " << file_path);
				return false;
			}

			//load time formats file
			if (!this->loader->loadFile(file_path))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot load times file : " << file_path);
				return false;
			}

			//check xsd validity
			if (!this->loader->isValid(TREPS_TIMES_XSD))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Invalid times file : " << file_path);
				return false;
			}

			return true;
                }

		NodeList TimeManagerClass::getTimeNodeList(void)
		{
			NodeList timeList;
			timeList.clear();

			if (this->loader == NULL)
				return timeList;

			if (!this->loader->isInUse())
				return timeList;

			Node *timesNode = this->loader->getChildFromRoot("times");

			if (timesNode == NULL)
				return timeList;

			timeList = this->loader->getChildrenFromNode("time",timesNode);

			return timeList;
		}

		string TimeManagerClass::getPatternFromTimeId(const char *timeId)
		{
			NodeList timeNodes = this->getTimeNodeList();

			if (timeNodes.empty())
				return "";

			for (NodeList::iterator timeNode = timeNodes.begin(); timeNode != timeNodes.end(); ++timeNode)
			{
				string id = this->loader->getAttributeFromNode("id",*timeNode);

				if (id.compare(timeId) == 0)
				{
					//try to get time pattern if exist
					Node *patternNode = this->loader->getChildFromNode("pattern",(*timeNode));

					if (patternNode == NULL)
						return "";

					return this->loader->getNodeContent(patternNode);
				}
			}

			return "";
		}

		string TimeManagerClass::getTimeIdFromPattern(const char *pattern)
		{
			//retrieve time format by time pattern
			NodeList timeNodes = this->getTimeNodeList();

			if (timeNodes.empty())
				return "";

			for (NodeList::iterator timeNode = timeNodes.begin(); timeNode != timeNodes.end(); ++timeNode)
			{
				Node *patternNode = this->loader->getChildFromNode("pattern",(*timeNode));

				if (patternNode == NULL)
					continue;

				string p = this->loader->getNodeContent(patternNode);

				if (p.compare("") == 0)
					continue;

				if (p.compare(pattern) == 0)
					return this->loader->getAttributeFromNode("id",*timeNode);
			}

			return "";
		}

		t_TimeFormat TimeManagerClass::getFormatFromTimeId(const char *timeId)
		{
			if (strcmp(timeId,"pattern") == 0)
                                return TF_PATTERN;
                        else if (strcmp(timeId,"decyear") == 0)
                                return TF_DECIMAL;
                        else if (strcmp(timeId,"epoch") == 0)
                                return TF_EPOCH;
			else if (strcmp(timeId,"tt2000") == 0)
				return TF_TT2000;
                        else if (strcmp(timeId, "ddtime") == 0)
                                return TF_DDTIME;
			else if (strcmp(timeId, "timestamp") == 0)
				return TF_TIMESTAMP;
			else
			{
				//try to get pattern
				string pattern = this->getPatternFromTimeId(timeId);
				if (pattern.compare("") != 0)
					return TF_PATTERN;

			}
                        return TF_NONE;
		}

		string TimeManagerClass::getTimeIdFromFormatAndPattern(t_TimeFormat format, const char *pattern)
		{
			switch (format)
			{
				case TF_NONE    :
					return "";
				case TF_PATTERN :
				{
					//the pattern can be already defined for a time format
					string timeId = this->getTimeIdFromPattern(pattern);
					if (timeId.compare("") != 0)
						return timeId;
					return "pattern";
				}
				case TF_DECIMAL :
					return "decyear";
				case TF_EPOCH   :
					return "epoch";
				case TF_TT2000 :
					return "tt2000";
				case TF_DDTIME :
					return "ddtime";
				case TF_TIMESTAMP :
					return "timestamp";
			}

			return "";
		}

		bool TimeManagerClass::setCurrentPattern(const char *pattern)
		{
			return this->crtPattern->init(pattern);
		}

		int TimeManagerClass::getCurrentPatternSize(void)
		{
			return this->crtPattern->getSize();
		}

		bool TimeManagerClass::from_PATTERN(const char *time, t_Time &t, bool verbose)
		{
			return this->crtPattern->load(time,t,verbose);
		}

		bool TimeManagerClass::from_TIMESTAMP(time_t tt, t_Time &t)
		{
			//unix time
			struct tm tm;
			tm = *localtime(&tt);
			tmToTime(tm, t);
			return true;
		}

		bool TimeManagerClass::from_DECIMAL(double d, t_Time &t)
		{
			//decimal time
			t.ye = (long int)d;

			double part = d - (double)t.ye;

			int totalDay = isLeapYear(t.ye) ? 366 : 365;

			long long int mls = (part * ((long long int)totalDay * 24 * 60 * 60 * 1000));

			t.se = (long int)((double)mls/1000.);
			t.ml = mls - (t.se * 1000);

			t.mi = (long int)((double)t.se/60.);
			t.se -= (t.mi * 60);

			t.ho = (long int)((double)t.mi/60.);
			t.mi -= (t.ho * 60);

			long int doy = 1 + (long int)((double)t.ho/24.);
			t.ho -= ((doy -1) * 24);

			setDOY(t,doy);

			return true;
		}

		bool TimeManagerClass::from_EPOCH(double e, t_Time &t)
		{
			//epoch time (CDF)
			EPOCHbreakdown(e,&t.ye,&t.mo,&t.da,&t.ho,&t.mi,&t.se,&t.ml);
			return true;
		}

		bool TimeManagerClass::from_TT2000(long long int tt, t_Time &t)
		{
			//tt 2000 time (CDF)
			double ye, mo, da, ho, mi, se, ml, mic, nan, pic;
			CDF_TT2000_to_UTC_parts(tt,&ye,&mo,&da,&ho,&mi,&se,&ml,&mic,&nan,&pic);

			t.ye = ye;
			t.mo = mo;
			t.da = da;
			t.ho = ho;
			t.mi = mi;
			t.se = se;
			t.ml = ml;
			t.mic = mic;
			t.nan = nan;
			t.pic = pic;

			return true;
		}

		bool TimeManagerClass::from_DDTIME(const char *dd_time, t_Time &t)
		{
			//DDTime (CDPP/DDServer)
			long int yday;

			int n = sscanf(dd_time, "%04ld%03ld%02ld%02ld%02ld%03ld", &t.ye, &yday, &t.ho, &t.mi, &t.se, &t.ml);

			if (n != 6)
				return false;

			setDOY(t,yday+1);

			return true;
		}

		string TimeManagerClass::to_PATTERN(t_Time t)
		{
			//pattern time
			string time = "";
			this->crtPattern->write(t,time);
			return time;
		}

		time_t TimeManagerClass::to_TIMESTAMP(t_Time t)
		{
			//unix time
			//note : we lost milliseconds during this conversion

			struct tm tt;
			memset(&tt,0,sizeof(struct tm));

			tt.tm_hour = t.ho;
			tt.tm_min  = t.mi;
			tt.tm_sec  = t.se;
			tt.tm_year = t.ye-1900;
			tt.tm_mon  = t.mo-1;
			tt.tm_mday = t.da;

			return mktime(&tt);
			
		}

		double TimeManagerClass::to_DECIMAL(t_Time t)
		{
			//decimal time
			long int totalDay = isLeapYear(t.ye) ? 366 : 365;

			long long int total = (long long int)totalDay * 60 * 60 * 24 * 1000;

			long int doy = getDOY(t);

			long long int part = (long long int)t.ml;
			part += (long long int)t.se * 1000;
			part += (long long int)t.mi * 60 * 1000;
			part += (long long int)t.ho * 60 * 60 * 1000;
			part += (long long int)(doy - 1) * 24 * 60 * 60 * 1000;

			double d = (double)t.ye + (double)((double)part / (double)total);

			return d;
		}

		double TimeManagerClass::to_EPOCH(t_Time t)
		{
			//epoch time (CDF)
			return computeEPOCH(t.ye,t.mo,t.da,t.ho,t.mi,t.se,t.ml);
		}

		long long int TimeManagerClass::to_TT2000(t_Time t)
		{
			//tt 2000 time (CDF)
			return CDF_TT2000_from_UTC_parts((double)t.ye,(double)t.mo,(double)t.da,(double)t.ho,
							(double)t.mi,(double)t.se,(double)t.ml,(double)t.mic,
							(double)t.nan,(double)t.pic);
		}

		string TimeManagerClass::to_DDTIME(t_Time t)
		{
			//DDTime (CDPP/DDServer)
			long int doy = getDOY(t);
			char buf[17];
			sprintf(buf, "%04ld%03ld%02ld%02ld%02ld%03ld", t.ye, (doy-1), t.ho, t.mi, t.se, t.ml);
			buf[16] = '\0';
			return buf;
		}

		string TimeManagerClass::to_string(t_Time t, t_TimeFormat format, const char *pattern)
		{
			//time to string function
			switch (format)
			{
				case TF_NONE :
					return "";
				case TF_PATTERN :
					this->setCurrentPattern(pattern);
					return this->to_PATTERN(t);
				case TF_TIMESTAMP :
				{
					time_t time =  this->to_TIMESTAMP(t);
					return longToStr(time);
				}
				case TF_DECIMAL :
				{
					double time = this->to_DECIMAL(t);
					return doubleToStr(time);
				}
				case TF_EPOCH :
				{
					double time = this->to_EPOCH(t);
					return doubleToStr(time);
				}
				case TF_TT2000 :
				{
					long long int time = this->to_TT2000(t);
					return longlongToStr(time);
				}
				case TF_DDTIME :
					return this->to_DDTIME(t);
			}
			return "";
		}

		string TimeManagerClass::detectPattern(const char *value)
		{
			//try to detect the time pattern of a value
			NodeList timeNodes = this->getTimeNodeList();

			t_StringList patternList;
			patternList.clear();

			for (NodeList::iterator timeNode = timeNodes.begin(); timeNode != timeNodes.end(); ++timeNode)
			{
				//string id = this->loader->getAttributeFromNode("id",*timeNode);

				Node *patternNode = this->loader->getChildFromNode("pattern",(*timeNode));

				if (patternNode == NULL)
					continue;

				string p = this->loader->getNodeContent(patternNode);

				if (p.compare("") == 0)
					continue;

				if (!this->setCurrentPattern(p.c_str()))
					continue;

				t_Time time;

				if (this->from_PATTERN(value,time,false))
					patternList.push_back(p);
			}

			if (patternList.empty())
				return "";

			//get the longer one
			string pattern = "";
			for (t_StringList::iterator p = patternList.begin(); p != patternList.end(); ++p)
			{
				if ((*p).length() > pattern.length())
					pattern = (*p);
			}

			return pattern;
		}
	}
}