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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
* Original author: Paul McCullagh (H&G2JCtL)
20
* Continued development: Barry Leslie
33
extern int gettimeofday(struct timeval *tv, struct timezone *tz);
40
#include "CSStrUtil.h"
43
* timegm() is a none portable function so....
44
* This is an implimentation of timegm() based on one found
45
* here: http://www.opensync.org/changeset/1769
47
* Note to self: There is a better way to do this.
48
* Since this is just used internally and tm_isdst is always 0
49
* we only need to calculate the timezone offset to GM time once.
51
static time_t my_timegm(struct tm *my_time)
53
time_t local_secs, gm_secs;
54
struct tm gm__rec, *gm_time;
56
// Interpret 't' as the local time and convert it to seconds since the Epoch
57
local_secs = mktime(my_time);
58
if (local_secs == -1) {
60
local_secs = mktime (my_time);
66
// Get the gmtime based on the local seconds since the Epoch
67
gm_time = gmtime_r(&local_secs, &gm__rec);
68
gm_time->tm_isdst = 0;
70
// Interpret gmtime as the local time and convert it to seconds since the Epoch
71
gm_secs = mktime (gm_time);
74
gm_secs = mktime (gm_time);
80
// Return the local time adjusted by the difference from GM time.
81
return (local_secs - (gm_secs - local_secs));
84
CSTime::CSTime(s_int year, s_int mon, s_int day, s_int hour, s_int min, s_int sec, s_int nsec)
86
setLocal(year, mon, day, hour, min, sec, nsec);
94
void CSTime::setNull()
101
void CSTime::setLocal(s_int year, s_int mon, s_int day, s_int hour, s_int min, s_int sec, s_int nsec)
106
memset(<ime, 0, sizeof(ltime));
110
ltime.tm_hour = hour;
112
ltime.tm_mon = mon - 1;
113
ltime.tm_year = year - 1900;
114
secs = mktime(<ime);
115
setUTC1970(secs, nsec);
118
void CSTime::getLocal(s_int& year, s_int& mon, s_int& day, s_int& hour, s_int& min, s_int& sec, s_int& nsec)
123
memset(<ime, 0, sizeof(ltime));
125
getUTC1970(secs, nsec);
126
localtime_r(&secs, <ime);
129
hour = ltime.tm_hour;
131
mon = ltime.tm_mon + 1;
132
year = ltime.tm_year + 1900;
135
void CSTime::setUTC(s_int year, s_int mon, s_int day, s_int hour, s_int min, s_int sec, s_int nsec)
147
void CSTime::getUTC(s_int& year, s_int& mon, s_int& day, s_int& hour, s_int& min, s_int& sec, s_int& nsec)
158
char *CSTime::getCString(const char *format)
161
strcpy(iCString, "NULL");
167
memset(<ime, 0, sizeof(ltime));
169
getUTC1970(secs, nsec);
170
localtime_r(&secs, <ime);
171
strftime(iCString, 100, format, <ime);
176
char *CSTime::getCString()
178
return getCString("%Y-%m-%d %H:%M:%S");
181
void CSTime::setUTC1970(time_t sec, s_int nsec)
185
memset(<ime, 0, sizeof(ltime));
187
gmtime_r(&sec, <ime);
188
setUTC(ltime.tm_year + 1900, ltime.tm_mon + 1, ltime.tm_mday, ltime.tm_hour, ltime.tm_min, ltime.tm_sec, nsec);
191
bool CSTime::olderThen(time_t max_age)
196
getUTC1970(secs, nsec);
200
return ((now - secs) > max_age);
203
void CSTime::getUTC1970(time_t& sec, s_int& nsec)
208
/* Get the number of seconds since 1 Jan 1970 */
209
nsec100 = getUTC1601();
210
nsec100 = nsec100 - get1970as1601();
211
sec = (time_t) (nsec100 / 10000000);
215
memset(<ime, 0, sizeof(ltime));
217
ltime.tm_sec = iSeconds;
218
ltime.tm_min = iMinutes;
219
ltime.tm_hour = iHours;
220
ltime.tm_mday = iDay;
221
ltime.tm_mon = iMonth - 1;
222
ltime.tm_year = iYear - 1900;
223
sec = my_timegm(<ime);
230
void CSTime::setUTC1601(uint64_t nsec100)
235
/* The input is actually a FILETIME value: */
236
memcpy(&ftime, &nsec100, sizeof(ftime));
238
if (!FileTimeToSystemTime(&ftime, &stime))
239
CSException::throwLastError(CS_CONTEXT);
240
setUTC((s_int) stime.wYear, (s_int) stime.wMonth, (s_int) stime.wDay,
241
(s_int) stime.wHour, (s_int) stime.wMinute, (s_int) stime.wSecond, (s_int) stime.wMilliseconds * 1000);
244
uint64_t CSTime::getUTC1601()
251
stime.wMonth = iMonth;
253
stime.wHour = iHours;
254
stime.wMinute = iMinutes;
255
stime.wSecond = iSeconds;
256
stime.wMilliseconds = iNanoSeconds / 1000;
257
if (!SystemTimeToFileTime(&stime, &ftime))
258
CSException::throwLastError(CS_CONTEXT);
259
memcpy(&nsec100, &ftime, sizeof(nsec100));
263
uint64_t CSTime::get1970as1601()
275
stime.wMilliseconds = 0;
276
if (!SystemTimeToFileTime(&stime, &ftime))
277
CSException::throwLastError(CS_CONTEXT);
278
memcpy(&nsec100, &ftime, sizeof(nsec100));
284
uint64_t CSTime::getTimeCurrentTicks()
288
/* Get the current time in microseconds: */
289
gettimeofday(&now, NULL);
290
return (uint64_t) now.tv_sec * (uint64_t) 1000000 + (uint64_t) now.tv_usec;