1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
/*
* Copyright 2005-2014 Fabrice Colin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_TIMEGM
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE
#include <time.h>
#undef _XOPEN_SOURCE
#else
#include <time.h>
#endif
#else
#include <time.h>
/* This comment and function are taken from Wget's mktime_from_utc()
Converts struct tm to time_t, assuming the data in tm is UTC rather
than local timezone.
mktime is similar but assumes struct tm, also known as the
"broken-down" form of time, is in local time zone. mktime_from_utc
uses mktime to make the conversion understanding that an offset
will be introduced by the local time assumption.
mktime_from_utc then measures the introduced offset by applying
gmtime to the initial result and applying mktime to the resulting
"broken-down" form. The difference between the two mktime results
is the measured offset which is then subtracted from the initial
mktime result to yield a calendar time which is the value returned.
tm_isdst in struct tm is set to 0 to force mktime to introduce a
consistent offset (the non DST offset) since tm and tm+o might be
on opposite sides of a DST change.
Some implementations of mktime return -1 for the nonexistent
localtime hour at the beginning of DST. In this event, use
mktime(tm - 1hr) + 3600.
Schematically
mktime(tm) --> t+o
gmtime(t+o) --> tm+o
mktime(tm+o) --> t+2o
t+o - (t+2o - t+o) = t
Note that glibc contains a function of the same purpose named
`timegm' (reverse of gmtime). But obviously, it is not universally
available, and unfortunately it is not straightforwardly
extractable for use here. Perhaps configure should detect timegm
and use it where available.
Contributed by Roger Beeman <beeman@cisco.com>, with the help of
Mark Baushke <mdb@cisco.com> and the rest of the Gurus at CISCO.
Further improved by Roger with assistance from Edward J. Sabol
based on input by Jamie Zawinski. */
static time_t mktime_from_utc (struct tm *t)
{
time_t tl, tb;
struct tm *tg;
tl = mktime (t);
if (tl == -1)
{
t->tm_hour--;
tl = mktime (t);
if (tl == -1)
return -1; /* can't deal with output from strptime */
tl += 3600;
}
tg = gmtime (&tl);
tg->tm_isdst = 0;
tb = mktime (tg);
if (tb == -1)
{
tg->tm_hour--;
tb = mktime (tg);
if (tb == -1)
return -1; /* can't deal with output from gmtime */
tb += 3600;
}
return (tl - (tb - tl));
}
#endif
#ifdef USE_NEON
#include <neon/ne_dates.h>
#else
#ifdef USE_CURL
#include <curl/curl.h>
#endif
#endif
#include <iostream>
#include "TimeConverter.h"
using std::clog;
using std::endl;
using std::string;
TimeConverter::TimeConverter()
{
}
// Inverse of gmtime().
time_t TimeConverter::timegm(struct tm *tm)
{
#ifdef HAVE_TIMEGM
return ::timegm(tm);
#else
return mktime_from_utc(tm);
#endif
}
/// Converts into an RFC 822 timestamp.
string TimeConverter::toTimestamp(time_t aTime, bool inGMTime)
{
struct tm *pTimeTm = new struct tm;
if (((inGMTime == true) &&
#ifdef HAVE_GMTIME_R
(gmtime_r(&aTime, pTimeTm) != NULL)
#else
((pTimeTm = gmtime(&aTime)) != NULL)
#endif
) ||
#ifdef HAVE_LOCALTIME_R
(localtime_r(&aTime, pTimeTm) != NULL)
#else
((pTimeTm = localtime(&aTime)) != NULL)
#endif
)
{
char timeStr[64];
size_t formattedSize = 0;
if (inGMTime == true)
{
formattedSize = strftime(timeStr, 64, "%a, %d %b %Y %H:%M:%S GMT", pTimeTm);
}
else
{
// FIXME: don't use this extension ?
#if defined(__GNU_LIBRARY__)
// %z is a GNU extension
formattedSize = strftime(timeStr, 64, "%a, %d %b %Y %H:%M:%S %z", pTimeTm);
#else
formattedSize = strftime(timeStr, 64, "%a, %d %b %Y %H:%M:%S %Z", pTimeTm);
#endif
}
if (formattedSize > 0)
{
delete pTimeTm;
return timeStr;
}
}
delete pTimeTm;
return "";
}
/// Converts from a RFC 822 timestamp.
time_t TimeConverter::fromTimestamp(const string ×tamp)
{
if (timestamp.empty() == true)
{
return 0;
}
#ifdef USE_NEON
return ne_rfc1123_parse(timestamp.c_str());
#else
#ifdef USE_CURL
return curl_getdate(timestamp.c_str(), NULL);
#else
return time(NULL);
#endif
#endif
}
/// Converts to a YYYYMMDD-formatted string.
string TimeConverter::toYYYYMMDDString(int year, int month, int day)
{
char dateStr[64];
if (year < 0)
{
year = 0;
}
else if (year > 9999)
{
year = 9999;
}
if (month < 1)
{
month = 1;
}
else if (month > 12)
{
month = 12;
}
if (day < 1)
{
day = 1;
}
else if (day > 31)
{
day = 31;
}
if (snprintf(dateStr, 63, "%04d%02d%02d", year, month, day) > 0)
{
return dateStr;
}
return "";
}
/// Converts from a YYYYMMDD-formatted string.
time_t TimeConverter::fromYYYYMMDDString(const string &yyyymmdd, bool inGMTime)
{
struct tm timeTm;
time_t gmTime = 0;
// Initialize the structure
timeTm.tm_sec = timeTm.tm_min = timeTm.tm_hour = timeTm.tm_mday = 0;
timeTm.tm_mon = timeTm.tm_year = timeTm.tm_wday = timeTm.tm_yday = timeTm.tm_isdst = 0;
#ifdef HAVE_STRPTIME
strptime(yyyymmdd.c_str(), "%Y%m%d", &timeTm);
#else
timeTm.tm_year = atoi(yyyymmdd.substr(0, 4).c_str());
timeTm.tm_mon = atoi(yyyymmdd.substr(4, 2).c_str());
timeTm.tm_mday = atoi(yyyymmdd.substr(6, 2).c_str());
#endif
#ifdef DEBUG
clog << "TimeConverter::fromYYYYMMDDString: " << timeTm.tm_year << " " << timeTm.tm_mon << " " << timeTm.tm_mday << endl;
#endif
if (inGMTime == true)
{
gmTime = timegm(&timeTm);
}
else
{
gmTime = mktime(&timeTm);
}
return gmTime;
}
/// Converts to a HHMMSS-formatted string.
string TimeConverter::toHHMMSSString(int hours, int minutes, int seconds)
{
char timeStr[64];
if (hours < 0)
{
hours = 0;
}
else if (hours > 23)
{
hours = 23;
}
if (minutes < 0)
{
minutes = 0;
}
else if (minutes > 59)
{
minutes = 59;
}
if (seconds < 0)
{
seconds = 0;
}
else if (seconds > 59)
{
seconds = 59;
}
if (snprintf(timeStr, 63, "%02d%02d%02d", hours, minutes, seconds) > 0)
{
return timeStr;
}
return "";
}
/// Converts from a HHMMSS-formatted string.
time_t TimeConverter::fromHHMMSSString(const string &hhmmss, bool inGMTime)
{
struct tm timeTm;
time_t gmTime = 0;
// Initialize the structure
timeTm.tm_sec = timeTm.tm_min = timeTm.tm_hour = timeTm.tm_mday = 0;
timeTm.tm_mon = timeTm.tm_year = timeTm.tm_wday = timeTm.tm_yday = timeTm.tm_isdst = 0;
#ifdef HAVE_STRPTIME
strptime(hhmmss.c_str(), "%H%M%S", &timeTm);
#else
timeTm.tm_hour = atoi(hhmmss.substr(0, 2).c_str());
timeTm.tm_min = atoi(hhmmss.substr(2, 2).c_str());
timeTm.tm_sec = atoi(hhmmss.substr(4, 2).c_str());
#endif
#ifdef DEBUG
clog << "TimeConverter::fromHHMMSSString: " << timeTm.tm_hour << " " << timeTm.tm_min << " " << timeTm.tm_sec << endl;
#endif
if (inGMTime == true)
{
gmTime = timegm(&timeTm);
}
else
{
gmTime = mktime(&timeTm);
}
return gmTime;
}
string TimeConverter::toNormalDate(time_t aTime, DateFormat format)
{
struct tm *pTimeTm = new struct tm;
if (
#ifdef HAVE_LOCALTIME_R
(localtime_r(&aTime, pTimeTm) != NULL)
#else
((pTimeTm = localtime(&aTime)) != NULL)
#endif
)
{
char timeStr[64];
size_t formattedSize = 0;
if (format == DATE_EUROPE)
{
// FIXME: don't use this extension ?
#if defined(__GNU_LIBRARY__)
// %z is a GNU extension
formattedSize = strftime(timeStr, 64, "%A, %d %B %Y %H:%M:%S %z", pTimeTm);
#else
formattedSize = strftime(timeStr, 64, "%A, %d %B %Y %H:%M:%S %Z", pTimeTm);
#endif
}
else
{
// FIXME: don't use this extension ?
#if defined(__GNU_LIBRARY__)
// %z is a GNU extension
formattedSize = strftime(timeStr, 64, "%Y-%m-%d %a %H:%M:%S %z", pTimeTm);
#else
formattedSize = strftime(timeStr, 64, "%Y-%m-%d %a %H:%M:%S %Z", pTimeTm);
#endif
}
if (formattedSize > 0)
{
delete pTimeTm;
return timeStr;
}
}
delete pTimeTm;
return "";
}
|