Replace WTF::getCurrentLocalTime() with GregorianDateTime::setToCurrentLocalTime()
authorparoga@webkit.org <paroga@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 1 Aug 2012 20:49:26 +0000 (20:49 +0000)
committerparoga@webkit.org <paroga@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 1 Aug 2012 20:49:26 +0000 (20:49 +0000)
https://bugs.webkit.org/show_bug.cgi?id=92286

Reviewed by Geoffrey Garen.

Add a method to GregorianDateTime to set its values to the current locale time.
Replacing all occurrences of getCurrentLocalTime with the new function allows
us to remove getCurrentLocalTime in a next step.

Source/JavaScriptCore:

* JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.def:

Source/WebCore:

* html/FTPDirectoryDocument.cpp:
(WebCore::processFileDateString):
* loader/archive/mhtml/MHTMLArchive.cpp:
(WebCore::MHTMLArchive::generateMHTMLData):

Source/WTF:

* GNUmakefile.list.am:
* WTF.gypi:
* WTF.pro:
* WTF.vcproj/WTF.vcproj:
* WTF.xcodeproj/project.pbxproj:
* wtf/CMakeLists.txt:
* wtf/DateMath.cpp:
(WTF::dayInYear):
* wtf/DateMath.h:
(WTF):
* wtf/GregorianDateTime.cpp: Added.
(WTF):
(WTF::GregorianDateTime::setToCurrentLocalTime):
* wtf/GregorianDateTime.h:
(GregorianDateTime):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@124365 268f45cc-cd09-0410-ab3c-d52691b4dbfc

16 files changed:
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.def
Source/WTF/ChangeLog
Source/WTF/GNUmakefile.list.am
Source/WTF/WTF.gypi
Source/WTF/WTF.pro
Source/WTF/WTF.vcproj/WTF.vcproj
Source/WTF/WTF.xcodeproj/project.pbxproj
Source/WTF/wtf/CMakeLists.txt
Source/WTF/wtf/DateMath.cpp
Source/WTF/wtf/DateMath.h
Source/WTF/wtf/GregorianDateTime.cpp [new file with mode: 0644]
Source/WTF/wtf/GregorianDateTime.h
Source/WebCore/ChangeLog
Source/WebCore/html/FTPDirectoryDocument.cpp
Source/WebCore/loader/archive/mhtml/MHTMLArchive.cpp

index 7bcb147d1e184846d4f7c2c12615087987ce26da..e718c14b2af6912c08919167e4d251b7d944b910 100644 (file)
@@ -1,3 +1,16 @@
+2012-08-01  Patrick Gansterer  <paroga@webkit.org>
+
+        Replace WTF::getCurrentLocalTime() with GregorianDateTime::setToCurrentLocalTime()
+        https://bugs.webkit.org/show_bug.cgi?id=92286
+
+        Reviewed by Geoffrey Garen.
+
+        Add a method to GregorianDateTime to set its values to the current locale time.
+        Replacing all occurrences of getCurrentLocalTime with the new function allows
+        us to remove getCurrentLocalTime in a next step.
+
+        * JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.def:
+
 2012-08-01  Mark Hahnenberg  <mhahnenberg@apple.com>
 
         C++ code should get ClassInfo from the Structure
index 32bec4da59ae798ec64515eea36e38a41b4fc4b9..9623655d600de444246de28fad0d34a15a0a3bf9 100755 (executable)
@@ -315,6 +315,7 @@ EXPORTS
     ?setOrderLowerFirst@Collator@WTF@@QAEX_N@Z
     ?setPrototype@JSObject@JSC@@QAEXAAVJSGlobalData@2@VJSValue@2@@Z
     ?setSetter@PropertyDescriptor@JSC@@QAEXVJSValue@2@@Z
+    ?setToCurrentLocalTime@GregorianDateTime@WTF@@QAEXXZ
     ?setUndefined@PropertyDescriptor@JSC@@QAEXXZ
     ?setUnwrappedObject@JSGlobalThis@JSC@@IAEXAAVJSGlobalData@2@PAVJSGlobalObject@2@@Z
     ?setUpStaticFunctionSlot@JSC@@YA_NPAVExecState@1@PBVHashEntry@1@PAVJSObject@1@VPropertyName@1@AAVPropertySlot@1@@Z
index 1f0743cff39d1ce6bb600555ea5c8a44a2183fd0..b3492bfb422c6e0bab9c22220764e9fdb800c3ff 100644 (file)
@@ -1,3 +1,30 @@
+2012-08-01  Patrick Gansterer  <paroga@webkit.org>
+
+        Replace WTF::getCurrentLocalTime() with GregorianDateTime::setToCurrentLocalTime()
+        https://bugs.webkit.org/show_bug.cgi?id=92286
+
+        Reviewed by Geoffrey Garen.
+
+        Add a method to GregorianDateTime to set its values to the current locale time.
+        Replacing all occurrences of getCurrentLocalTime with the new function allows
+        us to remove getCurrentLocalTime in a next step.
+
+        * GNUmakefile.list.am:
+        * WTF.gypi:
+        * WTF.pro:
+        * WTF.vcproj/WTF.vcproj:
+        * WTF.xcodeproj/project.pbxproj:
+        * wtf/CMakeLists.txt:
+        * wtf/DateMath.cpp:
+        (WTF::dayInYear):
+        * wtf/DateMath.h:
+        (WTF):
+        * wtf/GregorianDateTime.cpp: Added.
+        (WTF):
+        (WTF::GregorianDateTime::setToCurrentLocalTime):
+        * wtf/GregorianDateTime.h:
+        (GregorianDateTime):
+
 2012-08-01  Patrick Gansterer  <paroga@webkit.org>
 
         Fix FastMalloc build with assertions enabled.
index 2b844af25494393e2cd9a5a862b0742543996876..7d34ee1b16566f3e5288ecec633f5bbaf541885d 100644 (file)
@@ -52,6 +52,7 @@ wtf_sources += \
     Source/WTF/wtf/Forward.h \
     Source/WTF/wtf/Functional.h \
     Source/WTF/wtf/GetPtr.h \
+    Source/WTF/wtf/GregorianDateTime.cpp \
     Source/WTF/wtf/GregorianDateTime.h \
     Source/WTF/wtf/HashCountedSet.h \
     Source/WTF/wtf/HashFunctions.h \
index 664b40dea8f5abaa0d182eb1507b14ae11380bb8..5d27970cc1c2c5f2009302c3868a250de3b8d087 100644 (file)
             'wtf/FastMalloc.cpp',
             'wtf/Float32Array.h',
             'wtf/Float64Array.h',
+            'wtf/GregorianDateTime.cpp',
             'wtf/HashTable.cpp',
             'wtf/Int16Array.h',
             'wtf/Int32Array.h',
index 72d1d712a8662c4a650b9dcb6e4bd207cc1b51bb..00dee54b59e1fe99369721a586ab5566daa54f26 100644 (file)
@@ -190,6 +190,7 @@ SOURCES += \
     dtoa/fixed-dtoa.cc \
     dtoa/strtod.cc \
     FastMalloc.cpp \
+    GregorianDateTime.cpp \
     gobject/GOwnPtr.cpp \
     gobject/GRefPtr.cpp \
     HashTable.cpp \
index 94e8326f39949de8f2eee43a61bb261b47e21ee5..332a184196b9285109f430a3920a1dd249060c85 100644 (file)
                        RelativePath="..\wtf\GetPtr.h"
                        >
                </File>
+               <File
+                       RelativePath="..\wtf\GregorianDateTime.cpp"
+                       >
+               </File>
                <File
                        RelativePath="..\wtf\GregorianDateTime.h"
                        >
index f5347b24dccc2734ebf7dffc913fa3b9653939c3..97e2250b341b2a9a0852e7a4f62837eec850d180 100644 (file)
@@ -11,6 +11,7 @@
                143F611F1565F0F900DB514A /* RAMSize.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 143F611D1565F0F900DB514A /* RAMSize.cpp */; };
                143F61201565F0F900DB514A /* RAMSize.h in Headers */ = {isa = PBXBuildFile; fileRef = 143F611E1565F0F900DB514A /* RAMSize.h */; settings = {ATTRIBUTES = (Private, ); }; };
                2C05385415BC819000F21B96 /* GregorianDateTime.h in Headers */ = {isa = PBXBuildFile; fileRef = 2C05385315BC819000F21B96 /* GregorianDateTime.h */; };
+               2CCD892A15C0390200285083 /* GregorianDateTime.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2CCD892915C0390200285083 /* GregorianDateTime.cpp */; };
                4330F38F15745B0500AAFA8F /* URLString.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4330F38E15745B0500AAFA8F /* URLString.cpp */; };
                4F0321BC156AA8D1006EBAF6 /* BitArray.h in Headers */ = {isa = PBXBuildFile; fileRef = 4F0321BB156AA8D1006EBAF6 /* BitArray.h */; };
                8134013815B092FD001FF0B8 /* Base64.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8134013615B092FD001FF0B8 /* Base64.cpp */; };
                143F611D1565F0F900DB514A /* RAMSize.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RAMSize.cpp; sourceTree = "<group>"; };
                143F611E1565F0F900DB514A /* RAMSize.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RAMSize.h; sourceTree = "<group>"; };
                2C05385315BC819000F21B96 /* GregorianDateTime.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GregorianDateTime.h; sourceTree = "<group>"; };
+               2CCD892915C0390200285083 /* GregorianDateTime.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GregorianDateTime.cpp; sourceTree = "<group>"; };
                4330F38E15745B0500AAFA8F /* URLString.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = URLString.cpp; sourceTree = "<group>"; };
                4F0321BB156AA8D1006EBAF6 /* BitArray.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BitArray.h; sourceTree = "<group>"; };
                5D247B6214689B8600E78B76 /* libWTF.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libWTF.a; sourceTree = BUILT_PRODUCTS_DIR; };
                                A8A472A6151A825A004123FF /* Forward.h */,
                                A8A472A7151A825A004123FF /* Functional.h */,
                                A8A472A8151A825A004123FF /* GetPtr.h */,
+                               2CCD892915C0390200285083 /* GregorianDateTime.cpp */,
                                2C05385315BC819000F21B96 /* GregorianDateTime.h */,
                                A8A472B3151A825A004123FF /* HashCountedSet.h */,
                                A8A472B4151A825A004123FF /* HashFunctions.h */,
                                A8A473B3151A825B004123FF /* fast-dtoa.cc in Sources */,
                                A8A473C3151A825B004123FF /* FastMalloc.cpp in Sources */,
                                A8A473B5151A825B004123FF /* fixed-dtoa.cc in Sources */,
+                               2CCD892A15C0390200285083 /* GregorianDateTime.cpp in Sources */,
                                A8A473D8151A825B004123FF /* HashTable.cpp in Sources */,
                                A8A473E5151A825B004123FF /* MainThread.cpp in Sources */,
                                A8A473E4151A825B004123FF /* MainThreadMac.mm in Sources */,
index 4636b5e52c9cbea194c4150dad9f3432014d24bc..1f12fb851fcc57b992496e00a6c7e2d30c7aca16 100644 (file)
@@ -147,6 +147,7 @@ SET(WTF_SOURCES
     DecimalNumber.cpp
     DynamicAnnotations.cpp
     FastMalloc.cpp
+    GregorianDateTime.cpp
     HashTable.cpp
     MD5.cpp
     MainThread.cpp
index 2aaef3b984e15080c6485cf7bbc231fa448f236a..dc7a17a136a27cacac15acf34b0a2e8ff0ae4748 100644 (file)
@@ -286,7 +286,7 @@ int dayInMonthFromDayInYear(int dayInYear, bool leapYear)
     return d - step;
 }
 
-static inline int dayInYear(int year, int month, int day)
+int dayInYear(int year, int month, int day)
 {
     return firstDayOfMonth[isLeapYear(year)][month] + day - 1;
 }
index 62add5eeea0bb7e38c5429fdab1d5bfb61c8d4d3..508fdc3d4c3b08c2b21008d613b96d19fda32558 100644 (file)
@@ -95,6 +95,7 @@ WTF_EXPORT_PRIVATE int msToYear(double ms);
 double msToDays(double ms);
 int msToMinutes(double ms);
 int msToHours(double ms);
+int dayInYear(int year, int month, int day);
 WTF_EXPORT_PRIVATE int dayInYear(double ms, int year);
 WTF_EXPORT_PRIVATE int monthFromDayInYear(int dayInYear, bool leapYear);
 WTF_EXPORT_PRIVATE int dayInMonthFromDayInYear(int dayInYear, bool leapYear);
diff --git a/Source/WTF/wtf/GregorianDateTime.cpp b/Source/WTF/wtf/GregorianDateTime.cpp
new file mode 100644 (file)
index 0000000..61f73bf
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2012 Patrick Gansterer <paroga@paroga.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "GregorianDateTime.h"
+
+#include "DateMath.h"
+
+#if OS(WINDOWS)
+#include <windows.h>
+#else
+#include <time.h>
+#endif
+
+namespace WTF {
+
+void GregorianDateTime::setToCurrentLocalTime()
+{
+#if OS(WINDOWS)
+    SYSTEMTIME systemTime;
+    GetLocalTime(&systemTime);
+    TIME_ZONE_INFORMATION timeZoneInformation;
+    DWORD timeZoneId = GetTimeZoneInformation(&timeZoneInformation);
+
+    LONG bias = timeZoneInformation.Bias;
+    if (timeZoneId == TIME_ZONE_ID_DAYLIGHT)
+        bias += timeZoneInformation.DaylightBias;
+    else if (timeZoneId == TIME_ZONE_ID_STANDARD)
+        bias += timeZoneInformation.StandardBias;
+    else
+        ASSERT(timeZoneId == TIME_ZONE_ID_UNKNOWN);
+
+    m_year = systemTime.wYear;
+    m_month = systemTime.wMonth - 1;
+    m_monthDay = systemTime.wDay;
+    m_yearDay = dayInYear(m_year, m_month, m_monthDay);
+    m_weekDay = systemTime.wDayOfWeek;
+    m_hour = systemTime.wHour;
+    m_minute = systemTime.wMinute;
+    m_second = systemTime.wSecond;
+    m_utcOffset = -bias * secondsPerMinute;
+    m_isDST = timeZoneId == TIME_ZONE_ID_DAYLIGHT ? 1 : 0;
+#else
+    tm localTM;
+    time_t localTime = time(0);
+    localtime_r(&localTime, &localTM);
+
+    m_year = localTM.tm_year + 1900;
+    m_month = localTM.tm_mon;
+    m_monthDay = localTM.tm_mday;
+    m_yearDay = localTM.tm_yday;
+    m_weekDay = localTM.tm_wday;
+    m_hour = localTM.tm_hour;
+    m_minute = localTM.tm_min;
+    m_second = localTM.tm_sec;
+    m_isDST = localTM.tm_isdst;
+#if HAVE(TM_GMTOFF)
+    m_utcOffset = localTM.tm_gmtoff;
+#else
+    int utcOffset = calculateUTCOffset();
+    utcOffset += calculateDSTOffset(localTime * msPerSecond, utcOffset);
+    m_utcOffset = utcOffset / msPerSecond;
+#endif
+#endif
+}
+
+} // namespace WTF
index 37300f089b07714b27f01a76237734ff47e49b7b..49fadf27b9c0063117cec4000c679532248bb9c2 100644 (file)
@@ -70,6 +70,8 @@ public:
     inline void setUtcOffset(int utcOffset) { m_utcOffset = utcOffset; }
     inline void setIsDST(int isDST) { m_isDST = isDST; }
 
+    WTF_EXPORT_PRIVATE void setToCurrentLocalTime();
+
     operator tm() const
     {
         tm ret;
index 79ade8fceb8eaa47bf6584777f0eea3d41fafe83..a04c5ce8dc1ba1afe52fe73e2493e5ced394ca62 100644 (file)
@@ -1,3 +1,19 @@
+2012-08-01  Patrick Gansterer  <paroga@webkit.org>
+
+        Replace WTF::getCurrentLocalTime() with GregorianDateTime::setToCurrentLocalTime()
+        https://bugs.webkit.org/show_bug.cgi?id=92286
+
+        Reviewed by Geoffrey Garen.
+
+        Add a method to GregorianDateTime to set its values to the current locale time.
+        Replacing all occurrences of getCurrentLocalTime with the new function allows
+        us to remove getCurrentLocalTime in a next step.
+
+        * html/FTPDirectoryDocument.cpp:
+        (WebCore::processFileDateString):
+        * loader/archive/mhtml/MHTMLArchive.cpp:
+        (WebCore::MHTMLArchive::generateMHTMLData):
+
 2012-08-01  Ojan Vafai  <ojan@chromium.org>
 
         Remove code duplication in determining if display is inline or replaced
index 18689c97fd33970ec2cc80c3f771878f03c70551..5016df7681c9c1fc0cbdd8d526595e48c79643eb 100644 (file)
 #include "Settings.h"
 #include "SharedBuffer.h"
 #include "Text.h"
-#include <wtf/text/CString.h>
-#include <wtf/text/WTFString.h>
 #include <wtf/CurrentTime.h>
+#include <wtf/GregorianDateTime.h>
 #include <wtf/StdLibExtras.h>
+#include <wtf/text/CString.h>
+#include <wtf/text/WTFString.h>
 #include <wtf/unicode/CharacterNames.h>
 
 using namespace std;
@@ -219,26 +220,23 @@ static String processFileDateString(const FTPTime& fileTime)
     }
 
     // If it was today or yesterday, lets just do that - but we have to compare to the current time
-    struct tm now;
-    getCurrentLocalTime(&now);
-
-    // localtime does "year = current year - 1900", compensate for that for readability and comparison purposes
-    now.tm_year += 1900;
+    GregorianDateTime now;
+    now.setToCurrentLocalTime();
 
-    if (fileTime.tm_year == now.tm_year) {
-        if (fileTime.tm_mon == now.tm_mon) {
-            if (fileTime.tm_mday == now.tm_mday)
+    if (fileTime.tm_year == now.year()) {
+        if (fileTime.tm_mon == now.month()) {
+            if (fileTime.tm_mday == now.monthDay())
                 return "Today" + timeOfDay;
-            if (fileTime.tm_mday == now.tm_mday - 1)
+            if (fileTime.tm_mday == now.monthDay() - 1)
                 return "Yesterday" + timeOfDay;
         }
         
-        if (now.tm_mday == 1 && (now.tm_mon == fileTime.tm_mon + 1 || (now.tm_mon == 0 && fileTime.tm_mon == 11)) &&
+        if (now.monthDay() == 1 && (now.month() == fileTime.tm_mon + 1 || (now.month() == 0 && fileTime.tm_mon == 11)) &&
             wasLastDayOfMonth(fileTime.tm_year, fileTime.tm_mon, fileTime.tm_mday))
                 return "Yesterday" + timeOfDay;
     }
 
-    if (fileTime.tm_year == now.tm_year - 1 && fileTime.tm_mon == 12 && fileTime.tm_mday == 31 && now.tm_mon == 1 && now.tm_mday == 1)
+    if (fileTime.tm_year == now.year() - 1 && fileTime.tm_mon == 12 && fileTime.tm_mday == 31 && now.month() == 1 && now.monthDay() == 1)
         return "Yesterday" + timeOfDay;
 
     static const char* months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???" };
@@ -252,7 +250,7 @@ static String processFileDateString(const FTPTime& fileTime)
     if (fileTime.tm_year > -1)
         dateString = String(months[month]) + " " + String::number(fileTime.tm_mday) + ", " + String::number(fileTime.tm_year);
     else
-        dateString = String(months[month]) + " " + String::number(fileTime.tm_mday) + ", " + String::number(now.tm_year);
+        dateString = String(months[month]) + " " + String::number(fileTime.tm_mday) + ", " + String::number(now.year());
 
     return dateString + timeOfDay;
 }
index cffbef9b371f177d1bfb32271d7cd1a47d9f35db..5567746c35b001885cf5abecd1895d2a123e1e02 100644 (file)
@@ -45,6 +45,7 @@
 
 #include <wtf/CryptographicallyRandomNumber.h>
 #include <wtf/DateMath.h>
+#include <wtf/GregorianDateTime.h>
 #include <wtf/StdLibExtras.h>
 #include <wtf/text/Base64.h>
 #include <wtf/text/StringBuilder.h>
@@ -150,9 +151,9 @@ PassRefPtr<SharedBuffer> MHTMLArchive::generateMHTMLData(Page* page, bool useBin
     String boundary = generateRandomBoundary();
     String endOfResourceBoundary = makeString("--", boundary, "\r\n");
 
-    tm localTM;
-    getCurrentLocalTime(&localTM);
-    String dateString = makeRFC2822DateString(localTM.tm_wday, localTM.tm_mday, localTM.tm_mon, 1900 + localTM.tm_year, localTM.tm_hour, localTM.tm_min, localTM.tm_sec, calculateUTCOffset() / (1000 * 60));
+    GregorianDateTime now;
+    now.setToCurrentLocalTime();
+    String dateString = makeRFC2822DateString(now.weekDay(), now.monthDay(), now.month(), now.year(), now.hour(), now.minute(), now.second(), now.utcOffset() / 60);
 
     StringBuilder stringBuilder;
     stringBuilder.append("From: <Saved by WebKit>\r\n");