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