source: Sophya/trunk/SophyaLib/BaseTools/timestamp.cc@ 2805

Last change on this file since 2805 was 2805, checked in by ansari, 20 years ago

MAJ commentaires pour documentation doxygen - Reza 9 Juin 2005

File size: 5.2 KB
Line 
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
30extern "C" { double trunc (double x); }
31#endif
32//-------------------------------------------------------------------------
33//-------------------------- Classe TimeStamp --------------------------
34//-------------------------------------------------------------------------
35
36enum Jour {jour_Lundi=0, jour_Mardi, jour_Mercredi, jour_Jeudi, jour_Vendredi, jour_Samedi, jour_Dimanche};
37enum 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
45TimeStamp::TimeStamp()
46{
47 mSeconds = 0.;
48 mDays = 0;
49 SetNow();
50}
51
52TimeStamp::TimeStamp(TimeStamp const & ts)
53{
54 Set(ts);
55 mSeconds = ts.mSeconds;
56 mDays = ts.mDays;
57}
58
59TimeStamp::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
66TimeStamp::TimeStamp(double days)
67{
68 Set(days);
69}
70
71TimeStamp::TimeStamp(string& date, string& hour)
72{
73 SetDate(date);
74 SetHour(hour);
75}
76
77TimeStamp::TimeStamp(const char* date, const char* hour)
78{
79 SetDate(date);
80 SetHour(hour);
81}
82
83
84void TimeStamp::Set(TimeStamp const & ts)
85{
86 mSeconds = ts.mSeconds;
87 mDays = ts.mDays;
88}
89
90void TimeStamp::Set(double days)
91{
92 if (days < 0.) {
93 mDays = days;
94 mSeconds = (days-mDays)*86400.;
95 }
96 else {
97 mDays = trunc(days)-1;
98 mSeconds = (days-mDays)*86400.;
99 }
100}
101
102void TimeStamp::SetNow()
103{
104 time_t t = time(NULL);
105 struct tm* TM = gmtime(&t);
106
107 int JJ,MM,AA;
108 int hh,mm;
109 double ss;
110
111 AA = TM->tm_year + 1900;
112 MM = TM->tm_mon+1;
113 JJ = TM->tm_mday;
114 hh = TM->tm_hour;
115 mm = TM->tm_min;
116 ss = TM->tm_sec;
117 SetDate(AA,MM,JJ);
118 SetHour(hh,mm,ss);
119}
120
121void TimeStamp::SetDate(int year, int month, int day)
122{
123 mDays = ConvertToDays(year, month, day);
124}
125
126void TimeStamp::SetDate(const char* date)
127{
128 int day,month,year;
129 sscanf(date,"%d/%d/%d", &day, &month, &year);
130 mDays = ConvertToDays(year, month, day);
131}
132
133void TimeStamp::SetHour(int hour, int min, double sec)
134{
135 mSeconds = hour*3600.+min*60+sec;
136}
137
138void TimeStamp::SetHour(const char* shour)
139{
140 int hour, min;
141 double sec;
142 sscanf(shour,"%d:%d:%lf",&hour, &min, &sec);
143 mSeconds = hour*3600.+min*60+sec;
144
145}
146
147void TimeStamp::GetDate(int& year, int& month, int& day) const
148{
149 int_8 jours = mDays;
150 // Recherche de l'annee
151 if (jours < 0) {
152 year = 1901;
153 while(jours < 0) {
154 year--;
155 jours += YearDays(year);
156 }
157 }
158 else {
159 year = 1901;
160 while(jours > YearDays(year)) {
161 jours -= YearDays(year);
162 year++;
163 }
164 }
165 // Recherche du mois
166 month = 1;
167 while(jours > MonthDays(year, month) ) {
168 jours -= MonthDays(year, month);
169 month++;
170 }
171 day = jours;
172}
173
174void TimeStamp::GetHour(int& hour, int& min, double& sec) const
175{
176 double seconds = mSeconds;
177 hour = trunc(seconds/3600.);
178 seconds -= hour*3600;
179 min = trunc(seconds/60.);
180 sec = seconds-min*60;
181}
182double TimeStamp::ToDays() const
183{
184 return((double)mDays + mSeconds/86400.);
185}
186
187string TimeStamp::ToString() const
188{
189 char buff[128];
190 int aa, mm, jj;
191 int hh, min;
192 double sec;
193 GetDate(aa, mm, jj);
194 GetHour(hh, min, sec);
195 sprintf(buff,"%02d/%02d/%02d %02d:%02d:%02.1f GMT", jj,mm,aa,hh,min,sec);
196 return buff;
197}
198
199void TimeStamp::Print(ostream& os) const
200{
201 os << " " << ToString() << " ";
202}
203
204int TimeStamp::MonthDays(int annee, int mois)
205{
206 if (mois<1 || mois>12) throw ParmError("TimeStamp::MonthDays month out of range");
207
208 switch(mois) {
209 case mois_Janvier:
210 case mois_Mars:
211 case mois_Mai:
212 case mois_Juillet:
213 case mois_Aout:
214 case mois_Octobre:
215 case mois_Decembre:
216 return 31;
217 case mois_Avril:
218 case mois_Juin:
219 case mois_Septembre:
220 case mois_Novembre:
221 return 30;
222 case mois_Fevrier:
223 return (((annee%4 == 0) && (annee%100 != 0)) || (annee%400 == 0)) ? 29 : 28;
224 }
225 return -1;
226}
227
228int TimeStamp::YearDays(int annee)
229// Retourne le nombre de jours dans l'année
230{
231 return (((annee%4 == 0) && (annee%100 != 0)) || (annee%400 == 0)) ? 366 : 365;
232}
233
234int_8 TimeStamp::ConvertToDays(int AA, int MM, int JJ)
235{
236 int_8 t = 0;
237 // if (!UndetDate()) {
238 int nban = AA-1901;
239 if (nban >= 0)
240 t = nban*365 + (nban/4) - (nban/100) + ((nban+300)/400);
241 else
242 t = nban*365 + (nban/4) - (nban/100) + ((nban-100)/400);
243 for (int i=1; i<MM; i++) t += TimeStamp::MonthDays(AA, i);
244 t += JJ;
245 // }
246 return t;
247}
Note: See TracBrowser for help on using the repository browser.