]> SALOME platform Git repositories - modules/kernel.git/blob - src/Batch/Batch_Date.cxx
Salome HOME
9ac52b12d3a356213f29043acfe76e6f397250f9
[modules/kernel.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
15 namespace Batch {
16
17   Date::Date(const long l)
18   {
19     struct tm * p_tm = localtime(&l);
20     _day   = p_tm->tm_mday;
21     _month = p_tm->tm_mon  + 1;
22     _year  = p_tm->tm_year + 1900;
23     _hour  = p_tm->tm_hour;
24     _min   = p_tm->tm_min;
25     _sec   = p_tm->tm_sec;
26   }
27
28   Date::Date(const string s)
29   { 
30     if ((s == "now") || (s == "Now") || (s == "NOW")) {
31       long l = time(0);
32       struct tm * p_tm = localtime(&l);
33       _day   = p_tm->tm_mday;
34       _month = p_tm->tm_mon  + 1;
35       _year  = p_tm->tm_year + 1900;
36       _hour  = p_tm->tm_hour;
37       _min   = p_tm->tm_min;
38       _sec   = p_tm->tm_sec;
39
40     } else {
41       char c;
42 //       istringstream ist(s);
43 //       ist >> _day   >> c
44 //        >> _month >> c
45 //        >> _year  >> c
46 //        >> _hour  >> c
47 //        >> _min   >> c
48 //        >> _sec;
49       sscanf(s.c_str(), "%ld/%ld/%ld-%ld:%ld:%ld", &_day, &_month, &_year, &_hour, &_min, &_sec);
50     }
51   }
52
53   Date & Date::operator =(long l)
54   {
55     struct tm * p_tm = localtime(&l);
56     _day   = p_tm->tm_mday;
57     _month = p_tm->tm_mon  + 1;
58     _year  = p_tm->tm_year + 1900;
59     _hour  = p_tm->tm_hour;
60     _min   = p_tm->tm_min;
61     _sec   = p_tm->tm_sec;
62
63     return *this;
64   }
65
66   Date & Date::operator +(long l)
67   {
68     *this = epoch() + l;
69     return *this;
70   }
71
72   Date & Date::operator -(long l)
73   {
74     *this = epoch() - l;
75     return *this;
76   }
77
78   Date & Date::operator +=(long l)
79   {
80     *this = epoch() + l;
81     return *this;
82   }
83
84   Date & Date::operator -=(long l)
85   {
86     *this = epoch() - l;
87     return *this;
88   }
89
90   Date & Date::operator =(const string & s)
91   {
92     if ((s == "now") || (s == "Now") || (s == "NOW")) {
93       long l = time(0);
94       struct tm * p_tm = localtime(&l);
95       _day   = p_tm->tm_mday;
96       _month = p_tm->tm_mon  + 1;
97       _year  = p_tm->tm_year + 1900;
98       _hour  = p_tm->tm_hour;
99       _min   = p_tm->tm_min;
100       _sec   = p_tm->tm_sec;
101
102     } else {
103       char c;
104 //       istringstream ist(s);
105 //       ist >> _day   >> c
106 //        >> _month >> c
107 //        >> _year  >> c
108 //        >> _hour  >> c
109 //        >> _min   >> c
110 //        >> _sec;
111       sscanf(s.c_str(), "%ld/%ld/%ld-%ld:%ld:%ld", &_day, &_month, &_year, &_hour, &_min, &_sec);
112     }
113  
114     return *this;
115   }
116
117   string Date::str() const
118   {
119     char buf[64];
120     string datestr;
121
122     // _day to char * 
123     sprintf(buf, "%02ld", _day);
124     datestr += buf;
125     datestr += "/";
126
127     // _month to char * 
128     sprintf(buf, "%02ld", _month);
129     datestr += buf;
130     datestr += "/";
131
132     // _year to char * 
133     sprintf(buf, "%04ld", _year);
134     datestr += buf;
135     datestr += "-";
136
137     // _hour to char * 
138     sprintf(buf, "%02ld", _hour);
139     datestr += buf;
140     datestr += ":";
141
142     // _min to char * 
143     sprintf(buf, "%02ld", _min);
144     datestr += buf;
145     datestr += ":";
146
147     // _sec to char * 
148     sprintf(buf, "%02ld", _sec);
149     datestr += buf;
150
151     return datestr;
152   }
153
154   long Date::epoch() const
155   {
156     struct tm T;
157     T.tm_mday = _day;
158     T.tm_mon  = _month - 1;
159     T.tm_year = _year  - 1900;
160     T.tm_hour = _hour;
161     T.tm_min  = _min;
162     T.tm_sec  = _sec;
163     return mktime(&T);
164   }
165
166 }
167
168
169 // COMMENTS