Salome HOME
PR: new python function getShortHostName() in Utils_Identity.py, based on socket...
[modules/yacs.git] / src / Batch / Batch_Date.cxx
1 /*
2  * Date.cxx : 
3  *
4  * Auteur : Ivan DUTKA-MALEN - EDF R&D
5  * Mail   : mailto:ivan.dutka-malen@der.edf.fr
6  * Date   : Wed Nov 26 14:11:42 2003
7  * Projet : Salome 2
8  *
9  */
10
11 #include <cstdio>
12 #include <ctime>
13 #include "Batch_Date.hxx"
14 using namespace std;
15
16 namespace Batch {
17
18   Date::Date(const long l)
19   {
20     struct tm * p_tm = localtime(&l);
21     _day   = p_tm->tm_mday;
22     _month = p_tm->tm_mon  + 1;
23     _year  = p_tm->tm_year + 1900;
24     _hour  = p_tm->tm_hour;
25     _min   = p_tm->tm_min;
26     _sec   = p_tm->tm_sec;
27   }
28
29   Date::Date(const string s)
30   { 
31     if ((s == "now") || (s == "Now") || (s == "NOW")) {
32       long l = time(0);
33       struct tm * p_tm = localtime(&l);
34       _day   = p_tm->tm_mday;
35       _month = p_tm->tm_mon  + 1;
36       _year  = p_tm->tm_year + 1900;
37       _hour  = p_tm->tm_hour;
38       _min   = p_tm->tm_min;
39       _sec   = p_tm->tm_sec;
40
41     } else {
42       char c;
43 //       istringstream ist(s);
44 //       ist >> _day   >> c
45 //        >> _month >> c
46 //        >> _year  >> c
47 //        >> _hour  >> c
48 //        >> _min   >> c
49 //        >> _sec;
50       sscanf(s.c_str(), "%ld/%ld/%ld-%ld:%ld:%ld", &_day, &_month, &_year, &_hour, &_min, &_sec);
51     }
52   }
53
54   Date & Date::operator =(long l)
55   {
56     struct tm * p_tm = localtime(&l);
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     return *this;
65   }
66
67   Date & Date::operator +(long l)
68   {
69     *this = epoch() + l;
70     return *this;
71   }
72
73   Date & Date::operator -(long l)
74   {
75     *this = epoch() - l;
76     return *this;
77   }
78
79   Date & Date::operator +=(long l)
80   {
81     *this = epoch() + l;
82     return *this;
83   }
84
85   Date & Date::operator -=(long l)
86   {
87     *this = epoch() - l;
88     return *this;
89   }
90
91   Date & Date::operator =(const string & s)
92   {
93     if ((s == "now") || (s == "Now") || (s == "NOW")) {
94       long l = time(0);
95       struct tm * p_tm = localtime(&l);
96       _day   = p_tm->tm_mday;
97       _month = p_tm->tm_mon  + 1;
98       _year  = p_tm->tm_year + 1900;
99       _hour  = p_tm->tm_hour;
100       _min   = p_tm->tm_min;
101       _sec   = p_tm->tm_sec;
102
103     } else {
104       char c;
105 //       istringstream ist(s);
106 //       ist >> _day   >> c
107 //        >> _month >> c
108 //        >> _year  >> c
109 //        >> _hour  >> c
110 //        >> _min   >> c
111 //        >> _sec;
112       sscanf(s.c_str(), "%ld/%ld/%ld-%ld:%ld:%ld", &_day, &_month, &_year, &_hour, &_min, &_sec);
113     }
114  
115     return *this;
116   }
117
118   string Date::str() const
119   {
120     char buf[64];
121     string datestr;
122
123     // _day to char * 
124     sprintf(buf, "%02ld", _day);
125     datestr += buf;
126     datestr += "/";
127
128     // _month to char * 
129     sprintf(buf, "%02ld", _month);
130     datestr += buf;
131     datestr += "/";
132
133     // _year to char * 
134     sprintf(buf, "%04ld", _year);
135     datestr += buf;
136     datestr += "-";
137
138     // _hour to char * 
139     sprintf(buf, "%02ld", _hour);
140     datestr += buf;
141     datestr += ":";
142
143     // _min to char * 
144     sprintf(buf, "%02ld", _min);
145     datestr += buf;
146     datestr += ":";
147
148     // _sec to char * 
149     sprintf(buf, "%02ld", _sec);
150     datestr += buf;
151
152     return datestr;
153   }
154
155   long Date::epoch() const
156   {
157     struct tm T;
158     T.tm_mday = _day;
159     T.tm_mon  = _month - 1;
160     T.tm_year = _year  - 1900;
161     T.tm_hour = _hour;
162     T.tm_min  = _min;
163     T.tm_sec  = _sec;
164     return mktime(&T);
165   }
166
167 }
168
169
170 // COMMENTS