source: sources/src/toolsTime.cc @ 1472:bbc6ac5ec16d

Revision 1472:bbc6ac5ec16d, 7.4 KB checked in by niam, 21 months ago (diff)

[time] renamed millinow->nowMs, microSleep->sleepUs; added sleepMs

Line 
1/***************************************************************************
2 *            toolsTime.cc
3 *
4 *  Sun Nov 27 2005
5 *  Copyright  2005  Dmytro Milinevskyy
6 *  milinevskyy@gmail.com
7 ****************************************************************************/
8
9/*
10 *  This program is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU Lesser General Public License version 2.1 as published by
12 *  the Free Software Foundation;
13 *
14 *  This program is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU Library General Public License for more details.
18 *
19 *  You should have received a copy of the GNU Lesser General Public License
20 *  along with this program; if not, write to the Free Software
21 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24/**
25 * vim indentation settings
26 * set tabstop=4
27 * set shiftwidth=4
28 */
29
30#include <libdodo/directives.h>
31
32#include <string.h>
33#include <time.h>
34#include <stdlib.h>
35#include <errno.h>
36#include <sys/time.h>
37
38#include <libdodo/toolsTime.h>
39#include <libdodo/toolsTimeEx.h>
40#include <libdodo/toolsString.h>
41#include <libdodo/types.h>
42
43using namespace dodo;
44
45tools::__time__::__time__() : sec(0),
46                              min(0),
47                              hour(0),
48                              day(1),
49                              month(1),
50                              year(1990),
51                              daylight(true)
52{
53}
54
55//-------------------------------------------------------------------
56
57tools::__time__::__time__(unsigned int a_sec,
58                          unsigned int a_min,
59                          unsigned int a_hour,
60                          unsigned int a_day,
61                          unsigned int a_month,
62                          unsigned int a_year,
63                          bool         a_daylight) : sec(a_sec),
64                                                     min(a_min),
65                                                     hour(a_hour),
66                                                     day(a_day),
67                                                     month(a_month),
68                                                     year(a_year),
69                                                     daylight(a_daylight)
70{
71}
72
73//-------------------------------------------------------------------
74
75dodo::string
76tools::time::byFormat(const dodo::string &format,
77                      long             timestamp,
78                      bool             local)
79{
80    tm *tTime;
81
82    if (local)
83        tTime = ::localtime((const time_t *)&timestamp);
84    else
85        tTime = gmtime((const time_t *)&timestamp);
86
87    if (tTime == NULL)
88        dodo_throw exception::basic(exception::MODULE_TOOLSTIME, TIMEEX_BYFORMAT, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);
89
90    char formatted[30];
91
92    strftime(formatted, 30, format.data(), tTime);
93
94    return formatted;
95}
96
97//-------------------------------------------------------------------
98
99unsigned long
100tools::time::nowMs()
101{
102    timeval tv;
103
104    if (gettimeofday(&tv, NULL) == -1)
105        dodo_throw exception::basic(exception::MODULE_TOOLSTIME, TIMEEX_MILLINOW, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);
106
107    return 1000 * tv.tv_sec + tv.tv_usec / 1000;
108}
109
110//-------------------------------------------------------------------
111
112long
113tools::time::now()
114{
115    time_t tTime = ::time(NULL);
116    if (tTime == (time_t)-1)
117        dodo_throw exception::basic(exception::MODULE_TOOLSTIME, TIMEEX_NOW, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);
118
119    return tTime;
120}
121
122//-------------------------------------------------------------------
123
124dodoStringArray
125tools::time::week(long             date,
126                  const dodo::string &format,
127                  bool             local)
128{
129    long daynum = string::stringToL(tools::time::byFormat("%w", date, local));
130    if (daynum == 0)
131        daynum = 7;
132
133    dodoStringArray week;
134    long mon = date - (daynum - 1) * 86400;
135
136    for (short i(0); i < 7; ++i, mon += 86400)
137        week.push_back(tools::time::byFormat(format, mon, local));
138
139    return week;
140}
141
142//-------------------------------------------------------------------
143
144dodoStringArray
145tools::time::dates(long             dateFrom,
146                   long             dateTo,
147                   const dodo::string &format,
148                   bool             local)
149{
150    dodoStringArray result;
151
152    if ((dateFrom == dateTo) || (dateFrom - dateTo < 86400)) {
153        result.push_back(tools::time::byFormat(format, dateFrom, local));
154        return result;
155    }
156
157    if (dateFrom > dateTo) {
158        long tmp = dateFrom;
159        dateFrom = dateTo;
160        dateTo = tmp;
161    }
162
163    while (dateFrom < dateTo) {
164        result.push_back(tools::time::byFormat(format, dateFrom, local));
165        dateFrom += 86400;
166    }
167    result.push_back(tools::time::byFormat(format, dateTo, local));
168
169    return result;
170}
171
172//-------------------------------------------------------------------
173
174long
175tools::time::timestamp(const __time__ &timeInfo)
176{
177    tm tTime;
178
179    tTime.tm_sec = timeInfo.sec;
180    tTime.tm_min = timeInfo.min;
181    tTime.tm_hour = timeInfo.hour;
182    tTime.tm_mday = timeInfo.day;
183    tTime.tm_mon = timeInfo.month - 1;
184    tTime.tm_year = timeInfo.year;
185    tTime.tm_isdst = timeInfo.daylight ? 1 : 0;
186
187    return ::mktime(&tTime);
188}
189
190//-------------------------------------------------------------------
191
192tools::__time__
193tools::time::timestamp(long seconds,
194                       bool local)
195{
196    tm *tTime;
197
198    if (local)
199        tTime = ::localtime((const time_t *)&seconds);
200    else
201        tTime = gmtime((const time_t *)&seconds);
202
203    if (tTime == NULL)
204        dodo_throw exception::basic(exception::MODULE_TOOLSTIME, TIMEEX_MAKETIME, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);
205
206    __time__ timeInfo;
207
208    timeInfo.sec = tTime->tm_sec;
209    timeInfo.min = tTime->tm_min;
210    timeInfo.hour = tTime->tm_hour;
211    timeInfo.day = tTime->tm_mday;
212    timeInfo.month = tTime->tm_mon + 1;
213    timeInfo.year = tTime->tm_year;
214    timeInfo.daylight = tTime->tm_isdst > 0 ? true : false;
215
216    return timeInfo;
217}
218
219//-------------------------------------------------------------------
220
221unsigned short
222tools::time::daysInMonth(unsigned int   year,
223                         unsigned short month)
224{
225    unsigned short day(0);
226
227    switch (month) {
228        case 1:
229        case 3:
230        case 5:
231        case 7:
232        case 8:
233        case 10:
234        case 12:
235
236            day = 31;
237
238            break;
239
240        case 4:
241        case 6:
242        case 9:
243        case 11:
244
245            day = 30;
246
247            break;
248
249        case 2:
250
251            unsigned int isleap = 0;
252            if (year % 4 == 0) {
253                isleap = 1;
254                if (year % 100 == 0 && year % 400 != 0)
255                    isleap = 0;
256            }
257            if (isleap == 1)
258                day = 29;
259            else
260                day = 28;
261            break;
262    }
263
264    return day;
265}
266
267//-------------------------------------------------------------------
268
269long
270tools::time::byFormat(const dodo::string &format,
271                      const dodo::string &dt)
272{
273    tm tTime;
274    strptime(dt.data(), format.data(), &tTime);
275
276    return ::mktime(&tTime);
277}
278
279//-------------------------------------------------------------------
280
Note: See TracBrowser for help on using the repository browser.