1 | #include "sopnamsp.h"
|
---|
2 | #include "machdefs.h"
|
---|
3 | #include <stdio.h>
|
---|
4 | #include <time.h>
|
---|
5 | #include <ctype.h>
|
---|
6 | #include <math.h>
|
---|
7 | #include "timestamp.h"
|
---|
8 | #include "pexceptions.h"
|
---|
9 | #include <iostream>
|
---|
10 |
|
---|
11 | /*!
|
---|
12 | \class SOPHYA::TimeStamp
|
---|
13 | \ingroup BaseTools
|
---|
14 | A simple class for representing date and time. Simple operations
|
---|
15 | on date/time are also provided.
|
---|
16 |
|
---|
17 | \code
|
---|
18 | // Create a object with the current date and time and prints it to cout
|
---|
19 | TimeStamp ts;
|
---|
20 | cout << ts << endl;
|
---|
21 | // Create an object with a specified date and time
|
---|
22 | TimeStamp ts2("01/01/1905","00:00:00");
|
---|
23 | // Get the number of days since 0 Jan 1901
|
---|
24 | cout << ts2.ToDays() << endl;
|
---|
25 | \endcode
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifdef Linux
|
---|
29 | // La fonction trunc non declaree ds math.h sous Linux
|
---|
30 | extern "C" { double trunc (double x); }
|
---|
31 | #endif
|
---|
32 | //-------------------------------------------------------------------------
|
---|
33 | //-------------------------- Classe TimeStamp --------------------------
|
---|
34 | //-------------------------------------------------------------------------
|
---|
35 |
|
---|
36 | enum Jour {jour_Lundi=0, jour_Mardi, jour_Mercredi, jour_Jeudi, jour_Vendredi, jour_Samedi, jour_Dimanche};
|
---|
37 | enum Mois {mois_Janvier=1, mois_Fevrier, mois_Mars, mois_Avril, mois_Mai, mois_Juin, mois_Juillet,
|
---|
38 | mois_Aout, mois_Septembre, mois_Octobre, mois_Novembre, mois_Decembre};
|
---|
39 |
|
---|
40 | /*
|
---|
41 | //! Day of the week enum
|
---|
42 | enum WeekDay {day_Monday=0, day_Tuesday, day_Wednesday, day_Thursday, day_Friday, day_Saturday, day_Sunday};
|
---|
43 | */
|
---|
44 |
|
---|
45 | TimeStamp::TimeStamp()
|
---|
46 | {
|
---|
47 | mSeconds = 0.;
|
---|
48 | mDays = 0;
|
---|
49 | SetNow();
|
---|
50 | }
|
---|
51 |
|
---|
52 | TimeStamp::TimeStamp(TimeStamp const & ts)
|
---|
53 | {
|
---|
54 | Set(ts);
|
---|
55 | mSeconds = ts.mSeconds;
|
---|
56 | mDays = ts.mDays;
|
---|
57 | }
|
---|
58 |
|
---|
59 | TimeStamp::TimeStamp(int year, int month, int day, int hour, int min, double sec)
|
---|
60 | {
|
---|
61 | SetDate(year, month, day);
|
---|
62 | SetHour(hour, min, sec);
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | TimeStamp::TimeStamp(double days)
|
---|
67 | {
|
---|
68 | Set(days);
|
---|
69 | }
|
---|
70 |
|
---|
71 | TimeStamp::TimeStamp(int_8 days, r_8 seconds)
|
---|
72 | {
|
---|
73 | Set(days, seconds);
|
---|
74 | }
|
---|
75 |
|
---|
76 | TimeStamp::TimeStamp(string& date, string& hour)
|
---|
77 | {
|
---|
78 | SetDate(date);
|
---|
79 | SetHour(hour);
|
---|
80 | }
|
---|
81 |
|
---|
82 | TimeStamp::TimeStamp(const char* date, const char* hour)
|
---|
83 | {
|
---|
84 | SetDate(date);
|
---|
85 | SetHour(hour);
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | void TimeStamp::Set(TimeStamp const & ts)
|
---|
90 | {
|
---|
91 | mSeconds = ts.mSeconds;
|
---|
92 | mDays = ts.mDays;
|
---|
93 | }
|
---|
94 |
|
---|
95 | void TimeStamp::Set(double days)
|
---|
96 | {
|
---|
97 | if (days >= 0.) {
|
---|
98 | mDays = trunc(days);
|
---|
99 | mSeconds = (days-trunc(days))*86400.;
|
---|
100 | }
|
---|
101 | else {
|
---|
102 | if ( (trunc(days)-days) > 0.) {
|
---|
103 | mDays = trunc(days)-1;
|
---|
104 | mSeconds = (days-mDays)*86400.;
|
---|
105 | }
|
---|
106 | else {
|
---|
107 | mDays = trunc(days);
|
---|
108 | mSeconds = 0.;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | void TimeStamp::Set(int_8 days, r_8 seconds)
|
---|
114 | {
|
---|
115 | if ( (seconds < 0.) || (seconds > 86400.) )
|
---|
116 | throw ParmError("TimeStamp::Set(int_8, r_8) seconds<0 or seconds>86400.");
|
---|
117 | mDays = days;
|
---|
118 | }
|
---|
119 |
|
---|
120 | void TimeStamp::SetNow()
|
---|
121 | {
|
---|
122 | time_t t = time(NULL);
|
---|
123 | struct tm* TM = gmtime(&t);
|
---|
124 |
|
---|
125 | int JJ,MM,AA;
|
---|
126 | int hh,mm;
|
---|
127 | double ss;
|
---|
128 |
|
---|
129 | AA = TM->tm_year + 1900;
|
---|
130 | MM = TM->tm_mon+1;
|
---|
131 | JJ = TM->tm_mday;
|
---|
132 | hh = TM->tm_hour;
|
---|
133 | mm = TM->tm_min;
|
---|
134 | ss = TM->tm_sec;
|
---|
135 | SetDate(AA,MM,JJ);
|
---|
136 | SetHour(hh,mm,ss);
|
---|
137 | }
|
---|
138 |
|
---|
139 | void TimeStamp::SetDate(int year, int month, int day)
|
---|
140 | {
|
---|
141 | mDays = ConvertToDays(year, month, day);
|
---|
142 | }
|
---|
143 |
|
---|
144 | void TimeStamp::SetDate(const char* date)
|
---|
145 | {
|
---|
146 | int day,month,year;
|
---|
147 | sscanf(date,"%d/%d/%d", &day, &month, &year);
|
---|
148 | mDays = ConvertToDays(year, month, day);
|
---|
149 | }
|
---|
150 |
|
---|
151 | void TimeStamp::SetHour(int hour, int min, double sec)
|
---|
152 | {
|
---|
153 | mSeconds = hour*3600.+min*60+sec;
|
---|
154 | }
|
---|
155 |
|
---|
156 | void TimeStamp::SetHour(const char* shour)
|
---|
157 | {
|
---|
158 | int hour, min;
|
---|
159 | double sec;
|
---|
160 | sscanf(shour,"%d:%d:%lf",&hour, &min, &sec);
|
---|
161 | mSeconds = hour*3600.+min*60+sec;
|
---|
162 |
|
---|
163 | }
|
---|
164 |
|
---|
165 | void TimeStamp::GetDate(int& year, int& month, int& day) const
|
---|
166 | {
|
---|
167 | int_8 jours = mDays;
|
---|
168 | // Recherche de l'annee
|
---|
169 | if (jours < 0) {
|
---|
170 | year = 1901;
|
---|
171 | while(jours < 0) {
|
---|
172 | year--;
|
---|
173 | jours += YearDays(year);
|
---|
174 | }
|
---|
175 | }
|
---|
176 | else {
|
---|
177 | year = 1901;
|
---|
178 | while(jours > YearDays(year)) {
|
---|
179 | jours -= YearDays(year);
|
---|
180 | year++;
|
---|
181 | }
|
---|
182 | }
|
---|
183 | // Recherche du mois
|
---|
184 | month = 1;
|
---|
185 | while(jours > MonthDays(year, month) ) {
|
---|
186 | jours -= MonthDays(year, month);
|
---|
187 | month++;
|
---|
188 | }
|
---|
189 | day = jours;
|
---|
190 | }
|
---|
191 |
|
---|
192 | void TimeStamp::GetHour(int& hour, int& min, double& sec) const
|
---|
193 | {
|
---|
194 | double seconds = mSeconds;
|
---|
195 | hour = trunc(seconds/3600.);
|
---|
196 | seconds -= hour*3600;
|
---|
197 | min = trunc(seconds/60.);
|
---|
198 | while (min >= 60) { hour++; min -= 60; }
|
---|
199 | sec = seconds-min*60;
|
---|
200 | while (sec >= 60.) { min++; sec -= 60.; }
|
---|
201 | }
|
---|
202 |
|
---|
203 | double TimeStamp::ToDays() const
|
---|
204 | {
|
---|
205 | return((double)mDays + mSeconds/86400.);
|
---|
206 | }
|
---|
207 |
|
---|
208 | /*!
|
---|
209 | \param fgday : if false, ignore the date (dd/mm/yy) part
|
---|
210 | \param fghour : if false, ignore the hour (hh:mm:ss) part
|
---|
211 | */
|
---|
212 | string TimeStamp::ToString(bool fgday, bool fghour) const
|
---|
213 | {
|
---|
214 | char buff[128];
|
---|
215 | int aa, mm, jj;
|
---|
216 | int hh, min;
|
---|
217 | double sec;
|
---|
218 | GetDate(aa, mm, jj);
|
---|
219 | GetHour(hh, min, sec);
|
---|
220 | if (!fghour)
|
---|
221 | sprintf(buff,"%02d/%02d/%02d ", jj,mm,aa);
|
---|
222 | else if (!fgday)
|
---|
223 | sprintf(buff,"%02d:%02d:%02.3f ", hh,min,sec);
|
---|
224 | else
|
---|
225 | sprintf(buff,"%02d/%02d/%02d %02d:%02d:%02.1f GMT", jj,mm,aa,hh,min,sec);
|
---|
226 | return buff;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /*!
|
---|
230 | \param fgday : if false, ignore the date (dd/mm/yy) part
|
---|
231 | \param fghour : if false, ignore the hour (hh:mm:ss) part
|
---|
232 | */
|
---|
233 | void TimeStamp::Print(ostream& os, bool fgday, bool fghour) const
|
---|
234 | {
|
---|
235 | os << " " << ToString(fgday, fghour) << " ";
|
---|
236 | }
|
---|
237 |
|
---|
238 | int TimeStamp::MonthDays(int annee, int mois)
|
---|
239 | {
|
---|
240 | if (mois<1 || mois>12) throw ParmError("TimeStamp::MonthDays month out of range");
|
---|
241 |
|
---|
242 | switch(mois) {
|
---|
243 | case mois_Janvier:
|
---|
244 | case mois_Mars:
|
---|
245 | case mois_Mai:
|
---|
246 | case mois_Juillet:
|
---|
247 | case mois_Aout:
|
---|
248 | case mois_Octobre:
|
---|
249 | case mois_Decembre:
|
---|
250 | return 31;
|
---|
251 | case mois_Avril:
|
---|
252 | case mois_Juin:
|
---|
253 | case mois_Septembre:
|
---|
254 | case mois_Novembre:
|
---|
255 | return 30;
|
---|
256 | case mois_Fevrier:
|
---|
257 | return (((annee%4 == 0) && (annee%100 != 0)) || (annee%400 == 0)) ? 29 : 28;
|
---|
258 | }
|
---|
259 | return -1;
|
---|
260 | }
|
---|
261 |
|
---|
262 | int TimeStamp::YearDays(int annee)
|
---|
263 | // Retourne le nombre de jours dans l'année
|
---|
264 | {
|
---|
265 | return (((annee%4 == 0) && (annee%100 != 0)) || (annee%400 == 0)) ? 366 : 365;
|
---|
266 | }
|
---|
267 |
|
---|
268 | int_8 TimeStamp::ConvertToDays(int AA, int MM, int JJ)
|
---|
269 | {
|
---|
270 | int_8 t = 0;
|
---|
271 | // if (!UndetDate()) {
|
---|
272 | int nban = AA-1901;
|
---|
273 | if (nban >= 0)
|
---|
274 | t = nban*365 + (nban/4) - (nban/100) + ((nban+300)/400);
|
---|
275 | else
|
---|
276 | t = nban*365 + (nban/4) - (nban/100) + ((nban-100)/400);
|
---|
277 | for (int i=1; i<MM; i++) t += TimeStamp::MonthDays(AA, i);
|
---|
278 | t += JJ;
|
---|
279 | // }
|
---|
280 | return t;
|
---|
281 | }
|
---|