[mapguide-commits] r7690 - in sandbox/jng/http304: Common/Foundation/Data Server/src/UnitTesting

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Tue Jul 16 07:14:17 PDT 2013


Author: jng
Date: 2013-07-16 07:14:16 -0700 (Tue, 16 Jul 2013)
New Revision: 7690

Modified:
   sandbox/jng/http304/Common/Foundation/Data/DateTime.cpp
   sandbox/jng/http304/Common/Foundation/Data/DateTime.h
   sandbox/jng/http304/Server/src/UnitTesting/TestMisc.cpp
   sandbox/jng/http304/Server/src/UnitTesting/TestMisc.h
Log:
Add a ToRfc1123String() method to MgDateTime. This may be the most hackiest, hodge-podge way of generating an RFC 1123 date string but based on the (also) included test cases the method works as expected.

We need to be able to convert MgDateTime objects to RFC 1123 strings as this is the date format required for the "Expires" and "If-Modified-Since" headers that we want to write and check for in our new GETTILE operation

Modified: sandbox/jng/http304/Common/Foundation/Data/DateTime.cpp
===================================================================
--- sandbox/jng/http304/Common/Foundation/Data/DateTime.cpp	2013-07-15 14:41:16 UTC (rev 7689)
+++ sandbox/jng/http304/Common/Foundation/Data/DateTime.cpp	2013-07-16 14:14:16 UTC (rev 7690)
@@ -23,6 +23,25 @@
 #define MG_MAX_DATE_TIME_BUFFER_LENGTH  64
 #define MG_MICRO_VALUE                  1000000
 
+//Lookups for RFC1123 date format conversion
+
+//Day names. Indices directly map to tm_wday of a tm structure
+static const char *DAY_NAMES[] =
+  { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
+
+//Month names. Must offset m_month by -1 for indexed access
+static const char *MONTH_NAMES[] =
+  { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
+
+//Lookups for day of week calculation
+
+static void PadLeft(std::string& str, const char padChar, size_t len)
+{
+    while (str.length() < len)
+        str = padChar + str;
+}
+
 MG_IMPL_DYNCREATE(MgDateTime);
 
 ///<summary>Construct a date time value initialized to the
@@ -766,8 +785,89 @@
     return MgUtil::MultiByteToWideChar(fdoDateTime);
 }
 
+INT8 MgDateTime::GetDayOfWeek()
+{
+    //I can't trust you to give me a proper struct tm for RFC 1123 formatting
+    //but at least you still give a reliable tm_wday
+    time_t tv = ToTimeValue();
+    struct tm timeInfo = *::ACE_OS::localtime(&tv);
+
+    return timeInfo.tm_wday;
+}
+
 ///----------------------------------------------------------------------------
 /// <summary>
+/// Returns the date time string in the following format:
+///     "TLD, DD TLM YYYY HH:MM:SS GMT"
+/// Where
+///     TLD - Sun/Mon/Tue/Wed/Thu/Fri/Sat
+///     TLM - Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
+/// </summary>
+///----------------------------------------------------------------------------
+STRING MgDateTime::ToRfc1123String()
+{
+    STRING result;
+    if (IsDateTime())
+    {
+        //Goodness gracious! The C/C++ standard library is *woeful* here! ACE stdlib abstraction
+        //doesn't save us either. So we'll do it live! Assume this date is already in UTC and format the
+        //RFC 1123 string ourselves!
+        
+        //Abbreviated day of week
+        std::string mbStr;
+        INT8 dayOfWeek = GetDayOfWeek();
+        mbStr += DAY_NAMES[dayOfWeek];
+        mbStr += ", ";
+        
+        //Day
+        std::string sNum;
+        MgUtil::Int32ToString(m_day, sNum);
+        PadLeft(sNum, '0', 2);
+        mbStr += sNum;
+
+        //Abbreviated month
+        mbStr += " ";
+        //Sanity
+        if (m_month <= 0 || m_month > 12)
+            return result;
+        mbStr += MONTH_NAMES[m_month - 1];
+        mbStr += " ";
+
+        //Year
+        sNum = "";
+        MgUtil::Int32ToString(m_year, sNum);
+        PadLeft(sNum, '0', 4);
+        mbStr += sNum;
+        mbStr += " ";
+
+        //Hour
+        sNum = "";
+        MgUtil::Int32ToString(m_hour, sNum);
+        PadLeft(sNum, '0', 2);
+        mbStr += sNum;
+        mbStr += ":";
+
+        //Minutes
+        sNum = "";
+        MgUtil::Int32ToString(m_minute, sNum);
+        PadLeft(sNum, '0', 2);
+        mbStr += sNum;
+        mbStr += ":";
+
+        //Seconds
+        sNum = "";
+        MgUtil::Int32ToString(m_second, sNum);
+        PadLeft(sNum, '0', 2);
+        mbStr += sNum;
+        mbStr += " GMT";
+
+        result = MgUtil::MultiByteToWideChar(mbStr);
+    }
+    return result;
+}
+
+///----------------------------------------------------------------------------
+/// <summary>
 /// Returns the XML date time string in the following formats:
 ///     "CCYY-MM-DDThh:mm:ss[.ssssss]"
 ///     "CCYY-MM-DDThh:mm:ss[.ssssss]Z"

Modified: sandbox/jng/http304/Common/Foundation/Data/DateTime.h
===================================================================
--- sandbox/jng/http304/Common/Foundation/Data/DateTime.h	2013-07-15 14:41:16 UTC (rev 7689)
+++ sandbox/jng/http304/Common/Foundation/Data/DateTime.h	2013-07-16 14:14:16 UTC (rev 7690)
@@ -546,9 +546,11 @@
     void Serialize(MgStream* stream);
     void Deserialize(MgStream* stream);
 
+    STRING ToRfc1123String();
     STRING ToXmlString(bool utc = true);
     string ToXmlStringUtf8(bool utc = true);
     time_t ToTimeValue();
+    INT8 GetDayOfWeek();
     double ToNumber();
     double ToMilliSeconds();
 

Modified: sandbox/jng/http304/Server/src/UnitTesting/TestMisc.cpp
===================================================================
--- sandbox/jng/http304/Server/src/UnitTesting/TestMisc.cpp	2013-07-15 14:41:16 UTC (rev 7689)
+++ sandbox/jng/http304/Server/src/UnitTesting/TestMisc.cpp	2013-07-16 14:14:16 UTC (rev 7690)
@@ -541,4 +541,36 @@
     {
         throw;
     }
+}
+
+void TestMisc::TestCase_Rfc1123Dates()
+{
+    try
+    {
+        MgDateTime dt1(2013, 12, 31, 23, 59, 59);
+        STRING sDate1 = dt1.ToRfc1123String();
+        CPPUNIT_ASSERT(sDate1 == L"Tue, 31 Dec 2013 23:59:59 GMT");
+
+        MgDateTime dt2(2014, 1, 1, 0, 0, 0);
+        STRING sDate2 = dt2.ToRfc1123String();
+        CPPUNIT_ASSERT(sDate2 == L"Wed, 01 Jan 2014 00:00:00 GMT");
+
+        MgDateTime dt3(2013, 12, 30, 23, 59, 59);
+        STRING sDate3 = dt3.ToRfc1123String();
+        CPPUNIT_ASSERT(sDate3 == L"Mon, 30 Dec 2013 23:59:59 GMT");
+
+        MgDateTime dt4(2014, 1, 1, 0, 0, 1);
+        STRING sDate4 = dt4.ToRfc1123String();
+        CPPUNIT_ASSERT(sDate4 == L"Wed, 01 Jan 2014 00:00:01 GMT");
+    }
+    catch (MgException* e)
+    {
+        STRING message = e->GetDetails(TEST_LOCALE);
+        SAFE_RELEASE(e);
+        CPPUNIT_FAIL(MG_WCHAR_TO_CHAR(message.c_str()));
+    }
+    catch (...)
+    {
+        throw;
+    }
 }
\ No newline at end of file

Modified: sandbox/jng/http304/Server/src/UnitTesting/TestMisc.h
===================================================================
--- sandbox/jng/http304/Server/src/UnitTesting/TestMisc.h	2013-07-15 14:41:16 UTC (rev 7689)
+++ sandbox/jng/http304/Server/src/UnitTesting/TestMisc.h	2013-07-16 14:14:16 UTC (rev 7690)
@@ -29,6 +29,7 @@
     CPPUNIT_TEST(TestCase_611);
     CPPUNIT_TEST(TestCase_1304);
     CPPUNIT_TEST(TestCase_MapLayerCollections);
+    CPPUNIT_TEST(TestCase_Rfc1123Dates);
 
     CPPUNIT_TEST(TestEnd); // This must be the very last unit test
     CPPUNIT_TEST_SUITE_END();
@@ -46,6 +47,7 @@
     void TestCase_611();
     void TestCase_1304();
     void TestCase_MapLayerCollections();
+    void TestCase_Rfc1123Dates();
 
 private:
     Ptr<MgSiteConnection> m_siteConnection;



More information about the mapguide-commits mailing list