1
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
3
* PrimeBase Media Stream for MySQL
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.
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.
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
19
* Original author: Paul McCullagh (H&G2JCtL)
20
* Continued development: Barry Leslie
38
#include "CSStrUtil.h"
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
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.
49
static time_t my_timegm(struct tm *my_time)
51
time_t local_secs, gm_secs;
52
struct tm gm__rec, *gm_time;
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) {
58
local_secs = mktime (my_time);
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;
68
// Interpret gmtime as the local time and convert it to seconds since the Epoch
69
gm_secs = mktime (gm_time);
72
gm_secs = mktime (gm_time);
78
// Return the local time adjusted by the difference from GM time.
79
return (local_secs - (gm_secs - local_secs));
82
CSTime::CSTime(s_int year, s_int mon, s_int day, s_int hour, s_int min, s_int sec, s_int nsec)
84
setLocal(year, mon, day, hour, min, sec, nsec);
92
void CSTime::setNull()
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)
104
memset(<ime, 0, sizeof(ltime));
108
ltime.tm_hour = hour;
110
ltime.tm_mon = mon - 1;
111
ltime.tm_year = year - 1900;
112
secs = mktime(<ime);
113
setUTC1970(secs, nsec);
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)
121
memset(<ime, 0, sizeof(ltime));
123
getUTC1970(secs, nsec);
124
localtime_r(&secs, <ime);
127
hour = ltime.tm_hour;
129
mon = ltime.tm_mon + 1;
130
year = ltime.tm_year + 1900;
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)
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)
156
char *CSTime::getCString(const char *format)
159
strcpy(iCString, "NULL");
165
memset(<ime, 0, sizeof(ltime));
167
getUTC1970(secs, nsec);
168
localtime_r(&secs, <ime);
169
strftime(iCString, 100, format, <ime);
174
char *CSTime::getCString()
176
return getCString("%Y-%m-%d %H:%M:%S");
179
void CSTime::setUTC1970(time_t sec, s_int nsec)
183
memset(<ime, 0, sizeof(ltime));
185
gmtime_r(&sec, <ime);
186
setUTC(ltime.tm_year + 1900, ltime.tm_mon + 1, ltime.tm_mday, ltime.tm_hour, ltime.tm_min, ltime.tm_sec, nsec);
189
void CSTime::getUTC1970(time_t& sec, s_int& nsec)
194
/* Get the number of seconds since 1 Jan 1970 */
195
nsec100 = getUTC1601();
196
nsec100 = nsec100 - get1970as1601();
197
sec = (time_t) (nsec100 / 10000000);
201
memset(<ime, 0, sizeof(ltime));
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(<ime);
216
void CSTime::setUTC1601(uint64_t nsec100)
221
/* The input is actually a FILETIME value: */
222
memcpy(&ftime, &nsec100, sizeof(ftime));
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);
230
uint64_t CSTime::getUTC1601()
237
stime.wMonth = iMonth;
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));
249
uint64_t CSTime::get1970as1601()
261
stime.wMilliseconds = 0;
262
if (!SystemTimeToFileTime(&stime, &ftime))
263
CSException::throwLastError(CS_CONTEXT);
264
memcpy(&nsec100, &ftime, sizeof(nsec100));