Salome HOME
Removed CASCatch
[modules/kernel.git] / src / Batch / Batch_Date.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 /*
21  * Date.cxx : 
22  *
23  * Auteur : Ivan DUTKA-MALEN - EDF R&D
24  * Mail   : mailto:ivan.dutka-malen@der.edf.fr
25  * Date   : Wed Nov 26 14:11:42 2003
26  * Projet : Salome 2
27  *
28  */
29
30 #include <cstdio>
31 #include <ctime>
32 #include "Batch_Date.hxx"
33 using namespace std;
34
35 namespace Batch {
36
37   Date::Date(const long l)
38   {
39     time_t l_t = l;
40     struct tm * p_tm = localtime(&l_t);
41     _day   = p_tm->tm_mday;
42     _month = p_tm->tm_mon  + 1;
43     _year  = p_tm->tm_year + 1900;
44     _hour  = p_tm->tm_hour;
45     _min   = p_tm->tm_min;
46     _sec   = p_tm->tm_sec;
47   }
48
49   Date::Date(const string s)
50   { 
51     if ((s == "now") || (s == "Now") || (s == "NOW")) {
52       long l = time(0);
53       time_t l_t = l;
54       struct tm * p_tm = localtime(&l_t);
55       _day   = p_tm->tm_mday;
56       _month = p_tm->tm_mon  + 1;
57       _year  = p_tm->tm_year + 1900;
58       _hour  = p_tm->tm_hour;
59       _min   = p_tm->tm_min;
60       _sec   = p_tm->tm_sec;
61
62     } else {
63       char c;
64 //       istringstream ist(s);
65 //       ist >> _day   >> c
66 //        >> _month >> c
67 //        >> _year  >> c
68 //        >> _hour  >> c
69 //        >> _min   >> c
70 //        >> _sec;
71       sscanf(s.c_str(), "%ld/%ld/%ld-%ld:%ld:%ld", &_day, &_month, &_year, &_hour, &_min, &_sec);
72     }
73   }
74
75   Date & Date::operator =(long l)
76   {
77     time_t l_t = l;
78     struct tm * p_tm = localtime(&l_t);
79     _day   = p_tm->tm_mday;
80     _month = p_tm->tm_mon  + 1;
81     _year  = p_tm->tm_year + 1900;
82     _hour  = p_tm->tm_hour;
83     _min   = p_tm->tm_min;
84     _sec   = p_tm->tm_sec;
85
86     return *this;
87   }
88
89   Date & Date::operator +(long l)
90   {
91     *this = epoch() + l;
92     return *this;
93   }
94
95   Date & Date::operator -(long l)
96   {
97     *this = epoch() - l;
98     return *this;
99   }
100
101   Date & Date::operator +=(long l)
102   {
103     *this = epoch() + l;
104     return *this;
105   }
106
107   Date & Date::operator -=(long l)
108   {
109     *this = epoch() - l;
110     return *this;
111   }
112
113   Date & Date::operator =(const string & s)
114   {
115     if ((s == "now") || (s == "Now") || (s == "NOW")) {
116       long l = time(0);
117       time_t l_t = l;
118       struct tm * p_tm = localtime(&l_t);
119       _day   = p_tm->tm_mday;
120       _month = p_tm->tm_mon  + 1;
121       _year  = p_tm->tm_year + 1900;
122       _hour  = p_tm->tm_hour;
123       _min   = p_tm->tm_min;
124       _sec   = p_tm->tm_sec;
125
126     } else {
127       char c;
128 //       istringstream ist(s);
129 //       ist >> _day   >> c
130 //        >> _month >> c
131 //        >> _year  >> c
132 //        >> _hour  >> c
133 //        >> _min   >> c
134 //        >> _sec;
135       sscanf(s.c_str(), "%ld/%ld/%ld-%ld:%ld:%ld", &_day, &_month, &_year, &_hour, &_min, &_sec);
136     }
137  
138     return *this;
139   }
140
141   string Date::str() const
142   {
143     char buf[64];
144     string datestr;
145
146     // _day to char * 
147     sprintf(buf, "%02ld", _day);
148     datestr += buf;
149     datestr += "/";
150
151     // _month to char * 
152     sprintf(buf, "%02ld", _month);
153     datestr += buf;
154     datestr += "/";
155
156     // _year to char * 
157     sprintf(buf, "%04ld", _year);
158     datestr += buf;
159     datestr += "-";
160
161     // _hour to char * 
162     sprintf(buf, "%02ld", _hour);
163     datestr += buf;
164     datestr += ":";
165
166     // _min to char * 
167     sprintf(buf, "%02ld", _min);
168     datestr += buf;
169     datestr += ":";
170
171     // _sec to char * 
172     sprintf(buf, "%02ld", _sec);
173     datestr += buf;
174
175     return datestr;
176   }
177
178   long Date::epoch() const
179   {
180     struct tm T;
181     T.tm_mday = _day;
182     T.tm_mon  = _month - 1;
183     T.tm_year = _year  - 1900;
184     T.tm_hour = _hour;
185     T.tm_min  = _min;
186     T.tm_sec  = _sec;
187     return mktime(&T);
188   }
189
190 }
191
192
193 // COMMENTS