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
|
/* vi: set sw=4 ts=4:
*
* Copyright (C) 2001 - 2012 Christian Hohnstaedt.
*
* All rights reserved.
*/
#include <time.h>
#include "base.h"
#include "func.h"
#include "exception.h"
#include "asn1time.h"
#include <openssl/x509.h>
#include <openssl/err.h>
#include <openssl/opensslv.h>
#include <QObject>
#include <QLocale>
#include <QTimeZone>
/* As defined in rfc-5280 4.1.2.5 */
#define UNDEFINED_DATE "99991231235959Z"
#define UTC_FORMAT "yyMMddHHmmss'Z'"
#define GEN_FORMAT "yy" UTC_FORMAT
bool a1time::isUndefined() const
{
return toSecsSinceEpoch() == 0;
}
a1time &a1time::setUndefined()
{
/* This way we handle "Jan 01, 1970 00:00:00"
* like RFC-5280 undefined date. I dare it */
setTimeZone(QTimeZone::utc());
setSecsSinceEpoch(0);
return *this;
}
int a1time::from_asn1(const ASN1_TIME *a)
{
ASN1_GENERALIZEDTIME *gt;
QString t;
*this = QDateTime();
if (!a)
return -1;
gt = ASN1_TIME_to_generalizedtime((ASN1_TIME*)a, NULL);
if (!gt)
return -1;
t = QString::fromLatin1((char*)gt->data, gt->length);
ASN1_GENERALIZEDTIME_free(gt);
return fromPlain(t);
}
int a1time::fromPlain(const QString &plain)
{
setTimeZone(QTimeZone::systemTimeZone());
if (plain == UNDEFINED_DATE)
setUndefined();
else
*this = fromString(plain, GEN_FORMAT);
setTimeZone(QTimeZone::utc());
return isValid() ? 0 : -1;
}
int a1time::set_asn1(const QString &str, int type) const
{
if (!atime)
atime = ASN1_TIME_new();
if (!atime)
return -1;
atime->type = type;
if (ASN1_STRING_set(atime, str.toLatin1(), str.length()))
return -1;
return 0;
}
a1time &a1time::operator = (const a1time &a)
{
if (atime)
ASN1_TIME_free(atime);
QDateTime::operator=(a);
return *this;
}
a1time::a1time()
{
*this = now();
}
a1time::a1time(const ASN1_TIME *a)
{
from_asn1(a);
}
a1time::a1time(const QString &plain)
{
fromPlain(plain);
}
a1time::~a1time()
{
if (atime)
ASN1_TIME_free(atime);
}
const ASN1_TIME *a1time::get_utc() const
{
int year = date().year();
if (!isValid() || isUndefined() || year > 2049 || year < 1950)
return get();
set_asn1(toUTC().toString(UTC_FORMAT), V_ASN1_UTCTIME);
return atime;
}
const ASN1_TIME *a1time::get() const
{
if (isUndefined())
set_asn1(UNDEFINED_DATE, V_ASN1_GENERALIZEDTIME);
else if (!isValid())
throw errorEx("Invalid Time");
else
set_asn1(toUTC().toString(GEN_FORMAT),
V_ASN1_GENERALIZEDTIME);
return atime;
}
a1time &a1time::set(const ASN1_TIME *a)
{
from_asn1(a);
return *this;
}
QString a1time::toString(QString fmt, Qt::TimeSpec spec) const
{
if (isUndefined())
return QObject::tr("Undefined");
if (!isValid())
return QObject::tr("Broken / Invalid");
return QLocale().toString(
spec == Qt::UTC ? toUTC() : toLocalTime(), fmt);
}
QString a1time::toPretty() const
{
QString fmt = QLocale().dateTimeFormat();
return toString(fmt, Qt::LocalTime);
}
QString a1time::toPrettyGMT() const
{
return toString("yyyy-MM-dd'T'HH:mm:ss' GMT'");
}
QString a1time::toSortable() const
{
return toString("yyyy-MM-dd");
}
QString a1time::toPlain(const QString &fmt) const
{
if (isUndefined())
return QString(UNDEFINED_DATE);
if (!isValid())
return QString("Broken-InvalidZ");
return toString(fmt.isEmpty() ? GEN_FORMAT : fmt);
}
qint64 a1time::age() const
{
return secsTo(now());
}
QString a1time::toFancy() const
{
QString fmt("Dunno");
qint64 diff = age();
int dtn = toLocalTime().daysTo(now().toLocalTime());
bool future = false;
if (diff < 0) {
future = true;
diff *= -1;
}
if (diff < 2 * SECS_PER_MINUTE) {
fmt = future ? QObject::tr("in %1 seconds") :
QObject::tr("%1 seconds ago");
} else if (diff < 2 *SECS_PER_HOUR) {
diff /= SECS_PER_MINUTE;
fmt = future ? QObject::tr("in %1 minutes") :
QObject::tr("%1 minutes ago");
} else if (dtn == 1) {
return QObject::tr("Yesterday");
} else if (dtn == -1) {
return QObject::tr("Tomorrow");
} else if (diff < SECS_PER_DAY) {
diff /= SECS_PER_HOUR;
fmt = future ? QObject::tr("in %1 hours") :
QObject::tr("%1 hours ago");
} else {
return QLocale().toString(date(),
QLocale::ShortFormat);
}
return fmt.arg(diff);
}
QString a1time::toPlainUTC() const
{
return toPlain(UTC_FORMAT);
}
QDateTime a1time::now(int delta)
{
return QDateTime::currentDateTime().toUTC().addSecs(delta);
}
void a1time::d2i(QByteArray &ba)
{
ASN1_TIME *n = (ASN1_TIME*)d2i_bytearray( D2I_VOID(d2i_ASN1_TIME), ba);
openssl_error();
if (n) {
from_asn1(n);
ASN1_TIME_free(n);
}
}
QByteArray a1time::i2d() const
{
return i2d_bytearray(I2D_VOID(i2d_ASN1_TIME), get());
}
|