From cd6943ffb73788507dfe8dc3cf61e15d77a03980 Mon Sep 17 00:00:00 2001 From: marian Date: Wed, 26 May 2010 08:45:14 +0000 Subject: [PATCH] Added utility used to decode old XEP-0091 date/time stamp. git-svn-id: http://voip.null.ro/svn/yate@3341 acf43c95-373e-0410-b603-e72c3f656dc1 --- libs/yjabber/xmpputils.cpp | 46 ++++++++++++++++++++++++++++++++++++++ libs/yjabber/xmpputils.h | 7 ++++++ 2 files changed, 53 insertions(+) diff --git a/libs/yjabber/xmpputils.cpp b/libs/yjabber/xmpputils.cpp index 87e204a6..6ce26716 100644 --- a/libs/yjabber/xmpputils.cpp +++ b/libs/yjabber/xmpputils.cpp @@ -1303,6 +1303,52 @@ unsigned int XMPPUtils::decodeDateTimeSec(const String& time, unsigned int* frac return ret; } +// Decode a date/time stamp as defined in XEP-0091 (jabber:x:delay) +// Format: YYYYMMDDThh:mm:ss +unsigned int XMPPUtils::decodeDateTimeSecXDelay(const String& time) +{ + int year = -1; + unsigned int month = (unsigned int)-1; + unsigned int day = (unsigned int)-1; + unsigned int hh = (unsigned int)-1; + unsigned int mm = (unsigned int)-1; + unsigned int ss = (unsigned int)-1; + while (true) { + // Split date/time + int pos = time.find('T'); + if (pos == -1) + break; + // Decode date + if (pos == 8) { + year = time.substr(0,4).toInteger(-1,10); + month = (unsigned int)time.substr(4,2).toInteger(-1,10); + day = (unsigned int)time.substr(6,2).toInteger(-1,10); + } + else + break; + // Decode time + ObjList* list = time.substr(pos + 1).split(':'); + if (list->length() == 3 && list->count() == 3) { + hh = (unsigned int)(*list)[0]->toString().toInteger(-1,10); + mm = (unsigned int)(*list)[1]->toString().toInteger(-1,10); + ss = (unsigned int)(*list)[2]->toString().toInteger(-1,10); + } + TelEngine::destruct(list); + break; + } + if (!(year != -1 && month && month <= 12 && day && day <= 31 && + ((hh <= 23 && mm <= 59 && ss <= 59) || (hh == 24 && mm == 0 && ss == 0)))) { + Debug(DebugNote, + "XMPPUtils::decodeDateTimeSecXDelay() incorrect stamp '%s'",time.c_str()); + return (unsigned int)-1; + } + unsigned int ret = Time::toEpoch(year,month,day,hh,mm,ss); + if (ret == (unsigned int)-1) + Debug(DebugNote,"XMPPUtils::decodeDateTimeSecXDelay() failed to convert '%s'", + time.c_str()); + return ret; +} + void XMPPUtils::print(String& xmlStr, XmlChild& xml, bool verbose) { const XmlElement* element = xml.xmlElement(); diff --git a/libs/yjabber/xmpputils.h b/libs/yjabber/xmpputils.h index d586626e..a370e2b2 100644 --- a/libs/yjabber/xmpputils.h +++ b/libs/yjabber/xmpputils.h @@ -1513,6 +1513,13 @@ public: */ static unsigned int decodeDateTimeSec(const String& time, unsigned int* fractions = 0); + /** + * Decode a date/time stamp as defined in XEP-0091 (jabber:x:delay) + * @param time The date/time string + * @return The decoded time in seconds, -1 on error + */ + static unsigned int decodeDateTimeSecXDelay(const String& time); + /** * Print an XmlElement to a string * @param xmlStr The destination string