TimeUtil.cc
2.8 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
/**
* 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 % 4 == 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;
}
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)
{
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));
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();
}
} /* namespace AMDA */