⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 date_object.cpp

📁 khtml在gtk上的移植版本
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// -*- c-basic-offset: 2 -*-/* *  This file is part of the KDE libraries *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org) *  Copyright (C) 2004 Apple Computer, Inc. * *  This library is free software; you can redistribute it and/or *  modify it under the terms of the GNU Lesser General Public *  License as published by the Free Software Foundation; either *  version 2 of the License, or (at your option) any later version. * *  This library is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *  Lesser General Public License for more details. * *  You should have received a copy of the GNU Lesser General Public *  License along with this library; if not, write to the Free Software *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA * */#ifdef HAVE_CONFIG_H#include <config.h>#endif#ifndef HAVE_SYS_TIMEB_H#define HAVE_SYS_TIMEB_H 0#endif#if TIME_WITH_SYS_TIME# include <sys/time.h># include <time.h>#else#if HAVE_SYS_TIME_H#include <sys/time.h>#else#  include <time.h># endif#endif#if HAVE_SYS_TIMEB_H#include <sys/timeb.h>#endif#ifdef HAVE_SYS_PARAM_H#  include <sys/param.h>#endif // HAVE_SYS_PARAM_H#include <math.h>#include <string.h>#include <stdio.h>#include <stdlib.h>#include <locale.h>#include <ctype.h>#include "date_object.h"#include "error_object.h"#include "operations.h"#include "date_object.lut.h"const time_t invalidDate = -1;#if APPLE_CHANGES && !KWIQ// Originally, we wrote our own implementation that uses Core Foundation because of a performance problem in Mac OS X 10.2.// But we need to keep using this rather than the standard library functions because this handles a larger range of dates.#include <notify.h>#include <CoreFoundation/CoreFoundation.h>#include <CoreServices/CoreServices.h>using KJS::UString;#define gmtime(x) gmtimeUsingCF(x)#define localtime(x) localtimeUsingCF(x)#define mktime(x) mktimeUsingCF(x)#define timegm(x) timegmUsingCF(x)#define time(x) timeUsingCF(x)#define ctime(x) NotAllowedToCallThis()#define strftime(a, b, c, d) NotAllowedToCallThis()static const char * const weekdayName[7] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };static const char * const monthName[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };    static struct tm *tmUsingCF(time_t clock, CFTimeZoneRef timeZone){    static struct tm result;    static char timeZoneCString[128];        CFAbsoluteTime absoluteTime = clock - kCFAbsoluteTimeIntervalSince1970;    CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(absoluteTime, timeZone);    CFStringRef abbreviation = CFTimeZoneCopyAbbreviation(timeZone, absoluteTime);    CFStringGetCString(abbreviation, timeZoneCString, sizeof(timeZoneCString), kCFStringEncodingASCII);    CFRelease(abbreviation);    result.tm_sec = (int)date.second;    result.tm_min = date.minute;    result.tm_hour = date.hour;    result.tm_mday = date.day;    result.tm_mon = date.month - 1;    result.tm_year = date.year - 1900;    result.tm_wday = CFAbsoluteTimeGetDayOfWeek(absoluteTime, timeZone) % 7;    result.tm_yday = CFAbsoluteTimeGetDayOfYear(absoluteTime, timeZone) - 1;    result.tm_isdst = CFTimeZoneIsDaylightSavingTime(timeZone, absoluteTime);    result.tm_gmtoff = (int)CFTimeZoneGetSecondsFromGMT(timeZone, absoluteTime);    result.tm_zone = timeZoneCString;        return &result;}static CFTimeZoneRef UTCTimeZone(){    static CFTimeZoneRef zone = CFTimeZoneCreateWithTimeIntervalFromGMT(NULL, 0.0);    return zone;}static CFTimeZoneRef CopyLocalTimeZone(){    // Check for a time zone notification, and tell CoreFoundation to re-get the time zone if it happened.    // Some day, CoreFoundation may do this itself, but for now it needs our help.    static bool registered = false;    static int notificationToken;    if (!registered) {        uint32_t status = notify_register_check("com.apple.system.timezone", &notificationToken);        if (status == NOTIFY_STATUS_OK) {            registered = true;        }    }    if (registered) {        int notified;        uint32_t status = notify_check(notificationToken, &notified);        if (status == NOTIFY_STATUS_OK && notified) {            CFTimeZoneResetSystem();        }    }    CFTimeZoneRef zone = CFTimeZoneCopyDefault();    if (zone) {        return zone;    }    zone = UTCTimeZone();    CFRetain(zone);    return zone;}static struct tm *gmtimeUsingCF(const time_t *clock){    return tmUsingCF(*clock, UTCTimeZone());}static struct tm *localtimeUsingCF(const time_t *clock){    CFTimeZoneRef timeZone = CopyLocalTimeZone();    struct tm *result = tmUsingCF(*clock, timeZone);    CFRelease(timeZone);    return result;}static time_t timetUsingCF(struct tm *tm, CFTimeZoneRef timeZone){    CFGregorianDate date;    date.second = tm->tm_sec;    date.minute = tm->tm_min;    date.hour = tm->tm_hour;    date.day = tm->tm_mday;    date.month = tm->tm_mon + 1;    date.year = tm->tm_year + 1900;    // CFGregorianDateGetAbsoluteTime will go nuts if the year is too large or small,    // so we pick an arbitrary cutoff.    if (date.year < -2500 || date.year > 2500) {        return invalidDate;    }    CFAbsoluteTime absoluteTime = CFGregorianDateGetAbsoluteTime(date, timeZone);    return (time_t)(absoluteTime + kCFAbsoluteTimeIntervalSince1970);}static time_t mktimeUsingCF(struct tm *tm){    CFTimeZoneRef timeZone = CopyLocalTimeZone();    time_t result = timetUsingCF(tm, timeZone);    CFRelease(timeZone);    return result;}static time_t timegmUsingCF(struct tm *tm){    return timetUsingCF(tm, UTCTimeZone());}static time_t timeUsingCF(time_t *clock){    time_t result = (time_t)(CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970);    if (clock) {        *clock = result;    }    return result;}static UString formatDate(struct tm &tm){    char buffer[100];    snprintf(buffer, sizeof(buffer), "%s %s %02d %04d",        weekdayName[(tm.tm_wday + 6) % 7],        monthName[tm.tm_mon], tm.tm_mday, tm.tm_year + 1900);    return buffer;}static UString formatDateUTCVariant(struct tm &tm){    char buffer[100];    snprintf(buffer, sizeof(buffer), "%s, %02d %s %04d",        weekdayName[(tm.tm_wday + 6) % 7],        tm.tm_mday, monthName[tm.tm_mon], tm.tm_year + 1900);    return buffer;}static UString formatTime(struct tm &tm){    char buffer[100];    if (tm.tm_gmtoff == 0) {        snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d GMT", tm.tm_hour, tm.tm_min, tm.tm_sec);    } else {        int offset = tm.tm_gmtoff;        if (offset < 0) {            offset = -offset;        }        snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d GMT%c%02d%02d",            tm.tm_hour, tm.tm_min, tm.tm_sec,            tm.tm_gmtoff < 0 ? '-' : '+', offset / (60*60), (offset / 60) % 60);    }    return UString(buffer);}static UString formatLocaleDate(time_t tv){    LongDateTime longDateTime;    UCConvertCFAbsoluteTimeToLongDateTime(tv - kCFAbsoluteTimeIntervalSince1970, &longDateTime);    unsigned char string[257];    LongDateString(&longDateTime, longDate, string, 0);    string[string[0] + 1] = '\0';    return (char *)&string[1];}static UString formatLocaleTime(time_t tv){    LongDateTime longDateTime;    UCConvertCFAbsoluteTimeToLongDateTime(tv - kCFAbsoluteTimeIntervalSince1970, &longDateTime);    unsigned char string[257];    LongTimeString(&longDateTime, true, string, 0);    string[string[0] + 1] = '\0';    return (char *)&string[1];}#endif // APPLE_CHANGESusing namespace KJS;// ------------------------------ DateInstanceImp ------------------------------const ClassInfo DateInstanceImp::info = {"Date", 0, 0, 0};DateInstanceImp::DateInstanceImp(ObjectImp *proto)  : ObjectImp(proto){}// ------------------------------ DatePrototypeImp -----------------------------const ClassInfo DatePrototypeImp::info = {"Date", 0, &dateTable, 0};/* Source for date_object.lut.h   We use a negative ID to denote the "UTC" variant.@begin dateTable 61  toString		DateProtoFuncImp::ToString		DontEnum|Function	0  toUTCString		-DateProtoFuncImp::ToUTCString		DontEnum|Function	0  toDateString		DateProtoFuncImp::ToDateString		DontEnum|Function	0  toTimeString		DateProtoFuncImp::ToTimeString		DontEnum|Function	0  toLocaleString	DateProtoFuncImp::ToLocaleString	DontEnum|Function	0  toLocaleDateString	DateProtoFuncImp::ToLocaleDateString	DontEnum|Function	0  toLocaleTimeString	DateProtoFuncImp::ToLocaleTimeString	DontEnum|Function	0  valueOf		DateProtoFuncImp::ValueOf		DontEnum|Function	0  getTime		DateProtoFuncImp::GetTime		DontEnum|Function	0  getFullYear		DateProtoFuncImp::GetFullYear		DontEnum|Function	0  getUTCFullYear	-DateProtoFuncImp::GetFullYear		DontEnum|Function	0  toGMTString		-DateProtoFuncImp::ToGMTString		DontEnum|Function	0  getMonth		DateProtoFuncImp::GetMonth		DontEnum|Function	0  getUTCMonth		-DateProtoFuncImp::GetMonth		DontEnum|Function	0  getDate		DateProtoFuncImp::GetDate		DontEnum|Function	0  getUTCDate		-DateProtoFuncImp::GetDate		DontEnum|Function	0  getDay		DateProtoFuncImp::GetDay		DontEnum|Function	0  getUTCDay		-DateProtoFuncImp::GetDay		DontEnum|Function	0  getHours		DateProtoFuncImp::GetHours		DontEnum|Function	0  getUTCHours		-DateProtoFuncImp::GetHours		DontEnum|Function	0  getMinutes		DateProtoFuncImp::GetMinutes		DontEnum|Function	0  getUTCMinutes		-DateProtoFuncImp::GetMinutes		DontEnum|Function	0  getSeconds		DateProtoFuncImp::GetSeconds		DontEnum|Function	0  getUTCSeconds		-DateProtoFuncImp::GetSeconds		DontEnum|Function	0  getMilliseconds	DateProtoFuncImp::GetMilliSeconds	DontEnum|Function	0  getUTCMilliseconds	-DateProtoFuncImp::GetMilliSeconds	DontEnum|Function	0  getTimezoneOffset	DateProtoFuncImp::GetTimezoneOffset	DontEnum|Function	0  setTime		DateProtoFuncImp::SetTime		DontEnum|Function	1  setMilliseconds	DateProtoFuncImp::SetMilliSeconds	DontEnum|Function	1  setUTCMilliseconds	-DateProtoFuncImp::SetMilliSeconds	DontEnum|Function	1  setSeconds		DateProtoFuncImp::SetSeconds		DontEnum|Function	2  setUTCSeconds		-DateProtoFuncImp::SetSeconds		DontEnum|Function	2  setMinutes		DateProtoFuncImp::SetMinutes		DontEnum|Function	3  setUTCMinutes		-DateProtoFuncImp::SetMinutes		DontEnum|Function	3  setHours		DateProtoFuncImp::SetHours		DontEnum|Function	4  setUTCHours		-DateProtoFuncImp::SetHours		DontEnum|Function	4  setDate		DateProtoFuncImp::SetDate		DontEnum|Function	1  setUTCDate		-DateProtoFuncImp::SetDate		DontEnum|Function	1  setMonth		DateProtoFuncImp::SetMonth		DontEnum|Function	2  setUTCMonth		-DateProtoFuncImp::SetMonth		DontEnum|Function	2  setFullYear		DateProtoFuncImp::SetFullYear		DontEnum|Function	3  setUTCFullYear	-DateProtoFuncImp::SetFullYear		DontEnum|Function	3  setYear		DateProtoFuncImp::SetYear		DontEnum|Function	1  getYear		DateProtoFuncImp::GetYear		DontEnum|Function	0@end*/// ECMA 15.9.4DatePrototypeImp::DatePrototypeImp(ExecState *,                                   ObjectPrototypeImp *objectProto)  : DateInstanceImp(objectProto){  Value protect(this);  setInternalValue(NumberImp::create(NaN));  // The constructor will be added later, after DateObjectImp has been built}Value DatePrototypeImp::get(ExecState *exec, const Identifier &propertyName) const{  return lookupGetFunction<DateProtoFuncImp, ObjectImp>( exec, propertyName, &dateTable, this );}// ------------------------------ DateProtoFuncImp -----------------------------DateProtoFuncImp::DateProtoFuncImp(ExecState *exec, int i, int len)  : InternalFunctionImp(    static_cast<FunctionPrototypeImp*>(exec->lexicalInterpreter()->builtinFunctionPrototype().imp())    ), id(abs(i)), utc(i<0)  // We use a negative ID to denote the "UTC" variant.{  Value protect(this);  putDirect(lengthPropertyName, len, DontDelete|ReadOnly|DontEnum);}bool DateProtoFuncImp::implementsCall() const{  return true;}Value DateProtoFuncImp::call(ExecState *exec, Object &thisObj, const List &args){  if ((id == ToString || id == ValueOf || id == GetTime || id == SetTime) &&      !thisObj.inherits(&DateInstanceImp::info)) {    // non-generic function called on non-date object    // ToString and ValueOf are generic according to the spec, but the mozilla    // tests suggest otherwise...    Object err = Error::create(exec,TypeError);    exec->setException(err);    return err;  }  Value result;  UString s;#if !APPLE_CHANGES || KWIQ  const int bufsize=100;  char timebuffer[bufsize];  CString oldlocale = setlocale(LC_TIME,NULL);  if (!oldlocale.c_str())    oldlocale = setlocale(LC_ALL, NULL);#endif  Value v = thisObj.internalValue();  double milli = v.toNumber(exec);    if (isNaN(milli)) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -