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
37
#include "CSStrUtil.h"
39
CSTime::CSTime(s_int year, s_int mon, s_int day, s_int hour, s_int min, s_int sec, s_int nsec)
41
setLocal(year, mon, day, hour, min, sec, nsec);
49
void CSTime::setNull()
56
void CSTime::setLocal(s_int year, s_int mon, s_int day, s_int hour, s_int min, s_int sec, s_int nsec)
58
struct tm ltime = { 0 };
65
ltime.tm_mon = mon - 1;
66
ltime.tm_year = year - 1900;
67
secs = mktime(<ime);
68
setUTC1970(secs, nsec);
71
void CSTime::getLocal(s_int& year, s_int& mon, s_int& day, s_int& hour, s_int& min, s_int& sec, s_int& nsec)
73
struct tm ltime = { 0 };
76
getUTC1970(secs, nsec);
77
localtime_r(&secs, <ime);
82
mon = ltime.tm_mon + 1;
83
year = ltime.tm_year + 1900;
86
void CSTime::setUTC(s_int year, s_int mon, s_int day, s_int hour, s_int min, s_int sec, s_int nsec)
98
void CSTime::getUTC(s_int& year, s_int& mon, s_int& day, s_int& hour, s_int& min, s_int& sec, s_int& nsec)
109
char *CSTime::getCString(const char *format)
112
strcpy(iCString, "NULL");
114
struct tm ltime = { 0 };
118
getUTC1970(secs, nsec);
119
localtime_r(&secs, <ime);
120
strftime(iCString, 100, format, <ime);
125
char *CSTime::getCString()
127
return getCString("%Y-%m-%d %H:%M:%S");
130
void CSTime::setUTC1970(time_t sec, s_int nsec)
132
struct tm ltime = { 0 };
134
gmtime_r(&sec, <ime);
135
setUTC(ltime.tm_year + 1900, ltime.tm_mon + 1, ltime.tm_mday, ltime.tm_hour, ltime.tm_min, ltime.tm_sec, nsec);
138
void CSTime::getUTC1970(time_t& sec, s_int& nsec)
143
/* Get the number of seconds since 1 Jan 1970 */
144
nsec100 = getUTC1601();
145
nsec100 = nsec100 - get1970as1601();
146
sec = (time_t) (nsec100 / 10000000);
148
struct tm ltime = { 0 };
150
ltime.tm_sec = iSeconds;
151
ltime.tm_min = iMinutes;
152
ltime.tm_hour = iHours;
153
ltime.tm_mday = iDay;
154
ltime.tm_mon = iMonth - 1;
155
ltime.tm_year = iYear - 1900;
156
sec = timegm(<ime);
163
void CSTime::setUTC1601(uint64_t nsec100)
168
/* The input is actually a FILETIME value: */
169
memcpy(&ftime, &nsec100, sizeof(ftime));
171
if (!FileTimeToSystemTime(&ftime, &stime))
172
CSException::throwLastError(CS_CONTEXT);
173
setUTC((s_int) stime.wYear, (s_int) stime.wMonth, (s_int) stime.wDay,
174
(s_int) stime.wHour, (s_int) stime.wMinute, (s_int) stime.wSecond, (s_int) stime.wMilliseconds * 1000);
177
uint64_t CSTime::getUTC1601()
184
stime.wMonth = iMonth;
186
stime.wHour = iHours;
187
stime.wMinute = iMinutes;
188
stime.wSecond = iSeconds;
189
stime.wMilliseconds = iNanoSeconds / 1000;
190
if (!SystemTimeToFileTime(&stime, &ftime))
191
CSException::throwLastError(CS_CONTEXT);
192
memcpy(&nsec100, &ftime, sizeof(nsec100));
196
uint64_t CSTime::get1970as1601()
208
stime.wMilliseconds = 0;
209
if (!SystemTimeToFileTime(&stime, &ftime))
210
CSException::throwLastError(CS_CONTEXT);
211
memcpy(&nsec100, &ftime, sizeof(nsec100));