TimeUtil.cc 5.04 KB
/**
 * TimeUtil.cc
 *
 *  Created on: 29 oct. 2012
 *      Author: AKKA IS
 */

#include "TimeUtil.hh"

#include <iostream>
#include <sstream>
#include <iomanip>

#include "DD_time.hh"

namespace AMDA {

TimeUtil::TimeUtil() {
}

TimeUtil::~TimeUtil() {
}

int TimeUtil::monthday[][12] =
{
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
};

void TimeUtil::formatTimeInIso(const double time, std::ostream& pOs, t_DDTimeKind timeKind) {


	   dd_tmstr_t tm;
	   int visocos;

	   tm.times = time;
	   SetIntNew(&tm, timeKind);
	   if (((tm.year & 3) == 0)&&(((tm.year % 100) != 0)||((tm.year % 400)==0))) visocos = 1; else visocos = 0;
	   int month = 0;
	   while((month < 12) && (tm.day >= monthday[visocos][month])) month++;



	int day = tm.day +1 - monthday[visocos][month-1];
	pOs << std::setfill('0') << tm.year << "-" << std::setw(2)
			<< std::right << month << "-" << std::setw(2) << std::right << day
			<< "T" << std::setw(2) << std::right << tm.hour << ":"
			<< std::setw(2) << std::right << tm.min << ":" << std::setw(2) <<tm.sec
			<< "." << std::setw(3) << tm.msec;
}

void TimeUtil::formatTimeInIsoWithoutMs(const double time, std::ostream& pOs, t_DDTimeKind timeKind) {


	   dd_tmstr_t tm;
	   int visocos;

	   tm.times = time;
	   SetIntNew(&tm, timeKind);
	   if (((tm.year & 3) == 0)&&(((tm.year % 100) != 0)||((tm.year % 400)==0))) visocos = 1; else visocos = 0;
	   int month = 0;
	   while((month < 12) && (tm.day >= monthday[visocos][month])) month++;



	int day = tm.day +1 - monthday[visocos][month-1];
	pOs << std::setfill('0') << tm.year << "-" << std::setw(2)
			<< std::right << month << "-" << std::setw(2) << std::right << day
			<< "T" << std::setw(2) << std::right << tm.hour << ":"
			<< std::setw(2) << std::right << tm.min << ":" << std::setw(2) <<tm.sec;
}

void TimeUtil::formatTimeInIsoZ(const double time, std::ostream& pOs, t_DDTimeKind timeKind) {


	   dd_tmstr_t tm;
	   int visocos;

	   tm.times = time;
	   SetIntNew(&tm, timeKind);
	   if (((tm.year & 3) == 0)&&(((tm.year % 100) != 0)||((tm.year % 400)==0))) visocos = 1; else visocos = 0;
	   int month = 0;
	   while((month < 12) && (tm.day >= monthday[visocos][month])) month++;



	int day = tm.day +1 - monthday[visocos][month-1];
	pOs << std::setfill('0') << tm.year << "-" << std::setw(2)
			<< std::right << month << "-" << std::setw(2) << std::right << day
			<< "T" << std::setw(2) << std::right << tm.hour << ":"
			<< std::setw(2) << std::right << tm.min << ":" << std::setw(2) <<tm.sec
			<< "." << std::setw(3) << tm.msec<<"Z";
}

double TimeUtil::readTimeInIso(char *Stime) {

    dd_tmstr_t  *UT;
	unsigned tabTime[7];
    char c;
	std::stringstream ss;
	ss << Stime;
	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 (ss.eof())
		tabTime[6] = 0;
	else
	{
		ss.width(1);
		ss >> c; //.
		ss.width(3);
		ss >> tabTime[6]; //ms;
	}


	UT = UT2double(tabTime);

	return UT->times;
}


std::string TimeUtil::formatTimeInIso(const double time, t_DDTimeKind timeKind)
{
	std::stringstream ss;
	formatTimeInIso(time, ss, timeKind);

  return ss.str();
}

std::string TimeUtil::DD2ISO_Time(std::string time, t_DDTimeKind timeKind) {
	return TimeUtil::formatTimeInIso(DD_Time2Double(const_cast<char *>(time.c_str())), timeKind);
}


void TimeUtil::double2DD_Time(const double time, std::ostream& pOs, t_DDTimeKind timeKind, const int offsetDay)
 {
    dd_tmstr_t  UT;

    UT.times =   time;
   SetIntNew(&UT, timeKind); //
   //sprintf(UTstring,"%04d%03d%02d%02d%02d%03d",(UT.year),(UT.day),(UT.hour), (UT.min), (UT.sec),(UT.msec));
	UT.day+= offsetDay;
	pOs << std::setfill('0') <<std::setw(4) << UT.year << std::setw(3)
			<< std::right << UT.day <<  std::setw(2) << std::right << UT.hour
			<< std::setw(2) << std::right << UT.min
			<< std::setw(2) << std::right << UT.sec << std::setw(3) << UT.msec;
   return;
 }

std::string TimeUtil::double2DD_Time(const double time, t_DDTimeKind timeKind)
{
	std::stringstream ss;
	double2DD_Time(time, ss, timeKind);
   
  return ss.str();
}

void TimeUtil:: formatTimeDateWithSpaces(const double time,std::ostream& pOs) {
	  dd_tmstr_t tm;
	   int visocos;

	   tm.times = time;
	   SetIntNew(&tm, DD_TM_DATE);
	   if (((tm.year & 3) == 0)&&(((tm.year % 100) != 0)||((tm.year % 400)==0))) visocos = 1; else visocos = 0;
	   int month = 0;
	   while((month < 12) && (tm.day >= monthday[visocos][month])) month++;



	int day = tm.day +1 - monthday[visocos][month-1];
	pOs << std::setfill('0') << tm.year << " " << std::setw(2)
			<< std::right << month << " " << std::setw(2) << std::right << day
			<< " " << std::setw(2) << std::right << tm.hour << " "
			<< std::setw(2) << std::right << tm.min << " " << std::setw(2) <<tm.sec
			<< " " << std::setw(3) << tm.msec;
}

} /* namespace AMDA */