Blame view

src/TimeUtil/TimeUtil.cc 5.04 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
/**
 * TimeUtil.cc
 *
 *  Created on: 29 oct. 2012
 *      Author: AKKA IS
 */

#include "TimeUtil.hh"

#include <iostream>
#include <sstream>
#include <iomanip>
fbe3c2bb   Benjamin Renard   First commit
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

#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);
b03f2eca   Benjamin Renard   Improve amdaParam...
38
	   if (((tm.year & 3) == 0)&&(((tm.year % 100) != 0)||((tm.year % 400)==0))) visocos = 1; else visocos = 0;
fbe3c2bb   Benjamin Renard   First commit
39
40
41
42
43
44
45
46
47
48
49
50
51
	   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;
}

b3096d5d   Hacene SI HADJ MOHAND   correcting time i...
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
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";
}

fbe3c2bb   Benjamin Renard   First commit
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
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;

8e99b602   Benjamin Renard   Function to parse...
130
131
132
133
134
135
136
137
138
	if (ss.eof())
		tabTime[6] = 0;
	else
	{
		ss.width(1);
		ss >> c; //.
		ss.width(3);
		ss >> tabTime[6]; //ms;
	}
fbe3c2bb   Benjamin Renard   First commit
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158


	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);
}

e8595a44   Hacene SI HADJ MOHAND   J'ai corrigĂ© les ...
159
160

void TimeUtil::double2DD_Time(const double time, std::ostream& pOs, t_DDTimeKind timeKind, const int offsetDay)
fbe3c2bb   Benjamin Renard   First commit
161
162
163
164
165
166
 {
    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));
e8595a44   Hacene SI HADJ MOHAND   J'ai corrigĂ© les ...
167
	UT.day+= offsetDay;
fbe3c2bb   Benjamin Renard   First commit
168
169
170
171
172
173
174
175
176
177
178
179
180
181
	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();
}
31dbec88   Benjamin Renard   date spaces ok
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

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;
}

fbe3c2bb   Benjamin Renard   First commit
203
} /* namespace AMDA */