Ignore:
Timestamp:
Aug 6, 2012, 4:46:11 PM (13 years ago)
Author:
Patrick Gansterer
Message:

Unify JSC date and time formating functions
https://bugs.webkit.org/show_bug.cgi?id=92282

Reviewed by Geoffrey Garen.

Replace the existing functions for formating GregorianDateTime
with one single function. This removes some code duplications
in DatePrototype and is a preperation to fix encoding issues,
since we can add UChar* values to the resulting string now.

  • runtime/DateConstructor.cpp:

(JSC::callDate):

  • runtime/DateConversion.cpp:

(JSC::formatDateTime):

  • runtime/DateConversion.h:

(JSC):

  • runtime/DatePrototype.cpp:

(JSC::formateDateInstance):
(JSC::dateProtoFuncToString):
(JSC::dateProtoFuncToUTCString):
(JSC::dateProtoFuncToDateString):
(JSC::dateProtoFuncToTimeString):
(JSC::dateProtoFuncToGMTString):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/runtime/DateConversion.cpp

    r123505 r124817  
    11/*
    2  * Copyright (C) 1999-2000 Harri Porten ([email protected])
    3  * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
    4  * Copyright (C) 2009 Google Inc. All rights reserved.
     2 * Copyright (C) 2012 Patrick Gansterer <[email protected]>
    53 *
    6  * The Original Code is Mozilla Communicator client code, released
    7  * March 31, 1998.
     4 * Redistribution and use in source and binary forms, with or without
     5 * modification, are permitted provided that the following conditions
     6 * are met:
     7 * 1. Redistributions of source code must retain the above copyright
     8 *    notice, this list of conditions and the following disclaimer.
     9 * 2. Redistributions in binary form must reproduce the above copyright
     10 *    notice, this list of conditions and the following disclaimer in the
     11 *    documentation and/or other materials provided with the distribution.
    812 *
    9  * The Initial Developer of the Original Code is
    10  * Netscape Communications Corporation.
    11  * Portions created by the Initial Developer are Copyright (C) 1998
    12  * the Initial Developer. All Rights Reserved.
    13  *
    14  * This library is free software; you can redistribute it and/or
    15  * modify it under the terms of the GNU Lesser General Public
    16  * License as published by the Free Software Foundation; either
    17  * version 2.1 of the License, or (at your option) any later version.
    18  *
    19  * This library is distributed in the hope that it will be useful,
    20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    22  * Lesser General Public License for more details.
    23  *
    24  * You should have received a copy of the GNU Lesser General Public
    25  * License along with this library; if not, write to the Free Software
    26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
    27  *
    28  * Alternatively, the contents of this file may be used under the terms
    29  * of either the Mozilla Public License Version 1.1, found at
    30  * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public
    31  * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html
    32  * (the "GPL"), in which case the provisions of the MPL or the GPL are
    33  * applicable instead of those above.  If you wish to allow use of your
    34  * version of this file only under the terms of one of those two
    35  * licenses (the MPL or the GPL) and not to allow others to use your
    36  * version of this file under the LGPL, indicate your decision by
    37  * deletingthe provisions above and replace them with the notice and
    38  * other provisions required by the MPL or the GPL, as the case may be.
    39  * If you do not delete the provisions above, a recipient may use your
    40  * version of this file under any of the LGPL, the MPL or the GPL.
     13 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     16 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     20 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    4123 */
    4224
     
    4426#include "DateConversion.h"
    4527
    46 #include "CallFrame.h"
    47 #include "JSDateMath.h"
    48 #include "JSObject.h"
    49 #include "ScopeChain.h"
    5028#include "UString.h"
    51 #include <wtf/StringExtras.h>
    52 #include <wtf/text/CString.h>
     29#include <wtf/Assertions.h>
     30#include <wtf/DateMath.h>
     31#include <wtf/text/StringBuilder.h>
    5332
    5433using namespace WTF;
     
    5635namespace JSC {
    5736
    58 void formatDate(const GregorianDateTime &t, DateConversionBuffer& buffer)
     37template<int width>
     38static inline void appendNumber(StringBuilder& builder, int value)
    5939{
    60     snprintf(buffer, DateConversionBufferSize, "%s %s %02d %04d",
    61         weekdayName[(t.weekDay() + 6) % 7],
    62         monthName[t.month()], t.monthDay(), t.year());
     40    int fillingZerosCount = width;
     41    if (value < 0) {
     42        builder.append('-');
     43        value = -value;
     44        --fillingZerosCount;
     45    }
     46    String valueString = String::number(value);
     47    fillingZerosCount -= valueString.length();
     48    for (int i = 0; i < fillingZerosCount; ++i)
     49        builder.append('0');
     50    builder.append(valueString);
    6351}
    6452
    65 void formatDateUTCVariant(const GregorianDateTime &t, DateConversionBuffer& buffer)
     53template<>
     54void appendNumber<2>(StringBuilder& builder, int value)
    6655{
    67     snprintf(buffer, DateConversionBufferSize, "%s, %02d %s %04d",
    68         weekdayName[(t.weekDay() + 6) % 7],
    69         t.monthDay(), monthName[t.month()], t.year());
     56    ASSERT(0 <= value && value <= 99);
     57    builder.append(static_cast<char>('0' + value / 10));
     58    builder.append(static_cast<char>('0' + value % 10));
    7059}
    7160
    72 void formatTime(const GregorianDateTime &t, DateConversionBuffer& buffer)
     61UString formatDateTime(const GregorianDateTime& t, DateTimeFormat format, bool asUTCVariant)
    7362{
    74     int offset = abs(t.utcOffset());
    75     char timeZoneName[70];
    76     struct tm gtm = t;
    77     strftime(timeZoneName, sizeof(timeZoneName), "%Z", &gtm);
     63    bool appendDate = format & DateTimeFormatDate;
     64    bool appendTime = format & DateTimeFormatTime;
    7865
    79     if (timeZoneName[0]) {
    80         snprintf(buffer, DateConversionBufferSize, "%02d:%02d:%02d GMT%c%02d%02d (%s)",
    81             t.hour(), t.minute(), t.second(),
    82             t.utcOffset() < 0 ? '-' : '+', offset / (60*60), (offset / 60) % 60, timeZoneName);
    83     } else {
    84         snprintf(buffer, DateConversionBufferSize, "%02d:%02d:%02d GMT%c%02d%02d",
    85             t.hour(), t.minute(), t.second(),
    86             t.utcOffset() < 0 ? '-' : '+', offset / (60*60), (offset / 60) % 60);
     66    StringBuilder builder;
     67
     68    if (appendDate) {
     69        builder.append(weekdayName[(t.weekDay() + 6) % 7]);
     70
     71        if (asUTCVariant) {
     72            builder.append(", ");
     73            appendNumber<2>(builder, t.monthDay());
     74            builder.append(' ');
     75            builder.append(monthName[t.month()]);
     76        } else {
     77            builder.append(' ');
     78            builder.append(monthName[t.month()]);
     79            builder.append(' ');
     80            appendNumber<2>(builder, t.monthDay());
     81        }
     82        builder.append(' ');
     83        appendNumber<4>(builder, t.year());
    8784    }
    88 }
    8985
    90 void formatTimeUTC(const GregorianDateTime &t, DateConversionBuffer& buffer)
    91 {
    92     snprintf(buffer, DateConversionBufferSize, "%02d:%02d:%02d GMT", t.hour(), t.minute(), t.second());
     86    if (appendDate && appendTime)
     87        builder.append(' ');
     88
     89    if (appendTime) {
     90        appendNumber<2>(builder, t.hour());
     91        builder.append(':');
     92        appendNumber<2>(builder, t.minute());
     93        builder.append(':');
     94        appendNumber<2>(builder, t.second());
     95        builder.append(" GMT");
     96
     97        if (!asUTCVariant) {
     98            int offset = abs(t.utcOffset()) / 60;
     99            builder.append(t.utcOffset() < 0 ? '-' : '+');
     100            appendNumber<2>(builder, offset / 60);
     101            appendNumber<2>(builder, offset % 60);
     102
     103            struct tm gtm = t;
     104            char timeZoneName[70];
     105            strftime(timeZoneName, sizeof(timeZoneName), "%Z", &gtm);
     106            if (timeZoneName[0]) {
     107                builder.append(" (");
     108                builder.append(timeZoneName);
     109                builder.append(')');
     110            }
     111        }
     112    }
     113
     114    return builder.toString().impl();
    93115}
    94116
Note: See TracChangeset for help on using the changeset viewer.