~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/cslib/CSTime.cc

Remove dead memset call.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
 
2
 *
 
3
 * PrimeBase Media Stream for MySQL
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 *
 
19
 * Original author: Paul McCullagh (H&G2JCtL)
 
20
 * Continued development: Barry Leslie
 
21
 *
 
22
 * 2007-05-20
 
23
 *
 
24
 *
 
25
 */
 
26
 
 
27
#include "CSConfig.h"
 
28
 
 
29
#include <string.h>
 
30
#include <time.h>
 
31
 
 
32
#ifdef OS_WINDOWS
 
33
#include <Windows.h>
 
34
#endif
 
35
 
 
36
#include "CSTime.h"
 
37
#include "CSGlobal.h"
 
38
#include "CSStrUtil.h"
 
39
 
 
40
/*
 
41
 * timegm() is a none portable function so....
 
42
 * This is an implimentation of timegm() based on one found
 
43
 * here: http://www.opensync.org/changeset/1769
 
44
 *
 
45
 * Note to self: There is a better way to do this.
 
46
 * Since this is just used internally and tm_isdst is always 0
 
47
 * we only need to calculate the timezone offset to GM time once.
 
48
 */
 
49
static time_t my_timegm(struct tm *my_time)
 
50
{
 
51
        time_t local_secs, gm_secs;
 
52
        struct tm gm__rec, *gm_time;
 
53
 
 
54
        // Interpret 't' as the local time and convert it to seconds since the Epoch
 
55
        local_secs = mktime(my_time);
 
56
        if (local_secs == -1) {
 
57
                my_time->tm_hour--;
 
58
                local_secs = mktime (my_time);
 
59
                if (local_secs == -1)
 
60
                        return -1; 
 
61
                local_secs += 3600;
 
62
        }
 
63
        
 
64
        // Get the gmtime based on the local seconds since the Epoch
 
65
        gm_time = gmtime_r(&local_secs, &gm__rec);
 
66
        gm_time->tm_isdst = 0;
 
67
        
 
68
        // Interpret gmtime as the local time and convert it to seconds since the Epoch
 
69
        gm_secs = mktime (gm_time);
 
70
        if (gm_secs == -1) {
 
71
                gm_time->tm_hour--;
 
72
                gm_secs = mktime (gm_time);
 
73
                if (gm_secs == -1)
 
74
                        return -1; 
 
75
                gm_secs += 3600;
 
76
        }
 
77
        
 
78
        // Return the local time adjusted by the difference from GM time.
 
79
        return (local_secs - (gm_secs - local_secs));
 
80
}
 
81
 
 
82
CSTime::CSTime(s_int year, s_int mon, s_int day, s_int hour, s_int min, s_int sec, s_int nsec)
 
83
{
 
84
        setLocal(year, mon, day, hour, min, sec, nsec);
 
85
}
 
86
 
 
87
bool CSTime::isNull()
 
88
{
 
89
        return iIsNull;
 
90
}
 
91
 
 
92
void CSTime::setNull()
 
93
{
 
94
        iIsNull = true;
 
95
        iSeconds = 0;
 
96
        iNanoSeconds = 0;
 
97
}
 
98
 
 
99
void CSTime::setLocal(s_int year, s_int mon, s_int day, s_int hour, s_int min, s_int sec, s_int nsec)
 
100
{
 
101
        struct tm       ltime;
 
102
        time_t          secs;
 
103
        
 
104
        memset(&ltime, 0, sizeof(ltime));
 
105
 
 
106
        ltime.tm_sec = sec;
 
107
        ltime.tm_min = min;
 
108
        ltime.tm_hour = hour;
 
109
        ltime.tm_mday = day;
 
110
        ltime.tm_mon = mon - 1;
 
111
        ltime.tm_year = year - 1900;
 
112
        secs = mktime(&ltime);
 
113
        setUTC1970(secs, nsec);
 
114
}
 
115
 
 
116
void CSTime::getLocal(s_int& year, s_int& mon, s_int& day, s_int& hour, s_int& min, s_int& sec, s_int& nsec)
 
117
{
 
118
        struct tm       ltime;
 
119
        time_t          secs;
 
120
 
 
121
        memset(&ltime, 0, sizeof(ltime));
 
122
        
 
123
        getUTC1970(secs, nsec);
 
124
        localtime_r(&secs, &ltime);
 
125
        sec = ltime.tm_sec;
 
126
        min = ltime.tm_min;
 
127
        hour = ltime.tm_hour;
 
128
        day = ltime.tm_mday;
 
129
        mon = ltime.tm_mon + 1;
 
130
        year = ltime.tm_year + 1900;
 
131
}
 
132
 
 
133
void CSTime::setUTC(s_int year, s_int mon, s_int day, s_int hour, s_int min, s_int sec, s_int nsec)
 
134
{
 
135
        iNanoSeconds = nsec;
 
136
        iSeconds = sec;
 
137
        iMinutes = min;
 
138
        iHours = hour;
 
139
        iDay = day;
 
140
        iMonth = mon;
 
141
        iYear = year;
 
142
        iIsNull = false;
 
143
}
 
144
 
 
145
void CSTime::getUTC(s_int& year, s_int& mon, s_int& day, s_int& hour, s_int& min, s_int& sec, s_int& nsec)
 
146
{
 
147
        nsec = iNanoSeconds;
 
148
        sec = iSeconds;
 
149
        min = iMinutes;
 
150
        hour = iHours;
 
151
        day = iDay;
 
152
        mon = iMonth;
 
153
        year = iYear;
 
154
}
 
155
 
 
156
char *CSTime::getCString(const char *format)
 
157
{
 
158
        if (iIsNull)
 
159
                strcpy(iCString, "NULL");
 
160
        else {
 
161
                struct tm       ltime;
 
162
                time_t          secs;
 
163
                s_int           nsec;
 
164
 
 
165
                memset(&ltime, 0, sizeof(ltime));
 
166
        
 
167
                getUTC1970(secs, nsec);
 
168
                localtime_r(&secs, &ltime);
 
169
                strftime(iCString, 100, format, &ltime);
 
170
        }
 
171
        return iCString;
 
172
}
 
173
 
 
174
char *CSTime::getCString()
 
175
{
 
176
        return getCString("%Y-%m-%d %H:%M:%S");
 
177
}
 
178
 
 
179
void CSTime::setUTC1970(time_t sec, s_int nsec)
 
180
{
 
181
        struct tm       ltime;
 
182
 
 
183
        memset(&ltime, 0, sizeof(ltime));
 
184
 
 
185
        gmtime_r(&sec, &ltime);
 
186
        setUTC(ltime.tm_year + 1900, ltime.tm_mon + 1, ltime.tm_mday, ltime.tm_hour, ltime.tm_min, ltime.tm_sec, nsec);
 
187
}
 
188
 
 
189
void CSTime::getUTC1970(time_t& sec, s_int& nsec)
 
190
{
 
191
#ifdef OS_WINDOWS
 
192
        uint64_t nsec100;
 
193
 
 
194
        /* Get the number of seconds since 1 Jan 1970 */
 
195
        nsec100 = getUTC1601();
 
196
        nsec100 = nsec100 - get1970as1601();
 
197
        sec = (time_t) (nsec100 / 10000000);
 
198
#else
 
199
        struct tm       ltime;
 
200
 
 
201
        memset(&ltime, 0, sizeof(ltime));
 
202
 
 
203
        ltime.tm_sec = iSeconds;
 
204
        ltime.tm_min = iMinutes;
 
205
        ltime.tm_hour = iHours;
 
206
        ltime.tm_mday = iDay;
 
207
        ltime.tm_mon = iMonth - 1;
 
208
        ltime.tm_year = iYear - 1900;
 
209
        sec = my_timegm(&ltime);
 
210
#endif
 
211
        nsec = iNanoSeconds;
 
212
}
 
213
 
 
214
#ifdef OS_WINDOWS
 
215
 
 
216
void CSTime::setUTC1601(uint64_t nsec100)
 
217
{
 
218
        FILETIME                ftime;
 
219
        SYSTEMTIME              stime;
 
220
 
 
221
        /* The input is actually a FILETIME value: */
 
222
        memcpy(&ftime, &nsec100, sizeof(ftime));
 
223
 
 
224
        if (!FileTimeToSystemTime(&ftime, &stime))
 
225
                CSException::throwLastError(CS_CONTEXT);
 
226
        setUTC((s_int) stime.wYear, (s_int) stime.wMonth, (s_int) stime.wDay,
 
227
                (s_int) stime.wHour, (s_int) stime.wMinute, (s_int) stime.wSecond, (s_int) stime.wMilliseconds * 1000);
 
228
}
 
229
 
 
230
uint64_t CSTime::getUTC1601()
 
231
{
 
232
        FILETIME        ftime;
 
233
        SYSTEMTIME      stime;
 
234
        uint64_t                nsec100;
 
235
 
 
236
        stime.wYear = iYear;
 
237
        stime.wMonth = iMonth;
 
238
        stime.wDay = iDay;
 
239
        stime.wHour = iHours;
 
240
        stime.wMinute = iMinutes;
 
241
        stime.wSecond = iSeconds;
 
242
        stime.wMilliseconds = iNanoSeconds / 1000;
 
243
        if (!SystemTimeToFileTime(&stime, &ftime))
 
244
                CSException::throwLastError(CS_CONTEXT);
 
245
        memcpy(&nsec100, &ftime, sizeof(nsec100));
 
246
        return nsec100;
 
247
}
 
248
 
 
249
uint64_t CSTime::get1970as1601()
 
250
{
 
251
        FILETIME        ftime;
 
252
        SYSTEMTIME      stime;
 
253
        uint64_t                nsec100;
 
254
 
 
255
        stime.wYear = 1970;
 
256
        stime.wMonth = 1;
 
257
        stime.wDay = 1;
 
258
        stime.wHour = 0;
 
259
        stime.wMinute = 0;
 
260
        stime.wSecond = 0;
 
261
        stime.wMilliseconds = 0;
 
262
        if (!SystemTimeToFileTime(&stime, &ftime))
 
263
                CSException::throwLastError(CS_CONTEXT);
 
264
        memcpy(&nsec100, &ftime, sizeof(nsec100));
 
265
        return nsec100;
 
266
}
 
267
 
 
268
#endif
 
269
 
 
270