![]() |
API 0.9.5
|
00001 /* 00002 * CPDate.j 00003 * Foundation 00004 * 00005 * Created by Thomas Robinson. 00006 * Copyright 2008, 280 North, Inc. 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this library; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 00024 var CPDateReferenceDate = new Date(Date.UTC(2001, 1, 1, 0, 0, 0, 0)); 00025 00033 @implementation CPDate : CPObject 00034 { 00035 id __doxygen__; 00036 } 00037 00038 + (id)alloc 00039 { 00040 var result = new Date; 00041 result.isa = [self class]; 00042 return result; 00043 } 00044 00045 + (id)date 00046 { 00047 return [[self alloc] init]; 00048 } 00049 00050 + (id)dateWithTimeIntervalSinceNow:(CPTimeInterval)seconds 00051 { 00052 return [[CPDate alloc] initWithTimeIntervalSinceNow:seconds]; 00053 } 00054 00055 + (id)dateWithTimeIntervalSince1970:(CPTimeInterval)seconds 00056 { 00057 return [[CPDate alloc] initWithTimeIntervalSince1970:seconds]; 00058 } 00059 00060 + (id)dateWithTimeIntervalSinceReferenceDate:(CPTimeInterval)seconds 00061 { 00062 return [[CPDate alloc] initWithTimeIntervalSinceReferenceDate:seconds]; 00063 } 00064 00065 + (id)distantPast 00066 { 00067 return new Date(-10000, 1, 1, 0, 0, 0, 0); 00068 } 00069 00070 + (id)distantFuture 00071 { 00072 return new Date(10000, 1, 1, 0, 0, 0, 0); 00073 } 00074 00075 - (id)initWithTimeIntervalSinceNow:(CPTimeInterval)seconds 00076 { 00077 self = new Date((new Date()).getTime() + seconds * 1000); 00078 return self; 00079 } 00080 00081 - (id)initWithTimeIntervalSince1970:(CPTimeInterval)seconds 00082 { 00083 self = new Date(seconds * 1000); 00084 return self; 00085 } 00086 00087 - (id)initWithTimeIntervalSinceReferenceDate:(CPTimeInterval)seconds 00088 { 00089 self = [self initWithTimeInterval:seconds sinceDate:CPDateReferenceDate]; 00090 return self; 00091 } 00092 00093 - (id)initWithTimeInterval:(CPTimeInterval)seconds sinceDate:(CPDate)refDate 00094 { 00095 self = new Date(refDate.getTime() + seconds * 1000); 00096 return self; 00097 } 00098 00104 - (id)initWithString:(CPString)description 00105 { 00106 var format = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2}) ([-+])(\d{2})(\d{2})/, 00107 d = description.match(new RegExp(format)); 00108 00109 if (!d || d.length != 10) 00110 [CPException raise:CPInvalidArgumentException 00111 reason:"initWithString: the string must be in YYYY-MM-DD HH:MM:SS ±HHMM format"]; 00112 00113 var date = new Date(d[1], d[2] - 1, d[3]), 00114 timeZoneOffset = (Number(d[8]) * 60 + Number(d[9])) * (d[7] === '-' ? 1 : -1); 00115 00116 date.setHours(d[4]); 00117 date.setMinutes(d[5]); 00118 date.setSeconds(d[6]); 00119 00120 self = new Date(date.getTime() + (timeZoneOffset - date.getTimezoneOffset()) * 60 * 1000); 00121 return self; 00122 } 00123 00124 - (CPTimeInterval)timeIntervalSinceDate:(CPDate)anotherDate 00125 { 00126 return (self.getTime() - anotherDate.getTime()) / 1000.0; 00127 } 00128 00129 - (CPTimeInterval)timeIntervalSinceNow 00130 { 00131 return [self timeIntervalSinceDate:[CPDate date]]; 00132 } 00133 00134 - (CPTimeInterval)timeIntervalSince1970 00135 { 00136 return self.getTime() / 1000.0; 00137 } 00138 00139 - (CPTimeInterval)timeIntervalSinceReferenceDate 00140 { 00141 return (self.getTime() - CPDateReferenceDate.getTime()) / 1000.0; 00142 } 00143 00144 + (CPTimeInterval)timeIntervalSinceReferenceDate 00145 { 00146 return [[CPDate date] timeIntervalSinceReferenceDate]; 00147 } 00148 00149 - (BOOL)isEqual:(CPDate)aDate 00150 { 00151 if (self === aDate) 00152 return YES; 00153 00154 if (!aDate || ![aDate isKindOfClass:[CPDate class]]) 00155 return NO; 00156 00157 return [self isEqualToDate:aDate]; 00158 } 00159 00160 - (BOOL)isEqualToDate:(CPDate)aDate 00161 { 00162 if (!aDate) 00163 return NO; 00164 00165 return !(self < aDate || self > aDate); 00166 } 00167 00168 - (CPComparisonResult)compare:(CPDate)anotherDate 00169 { 00170 return (self > anotherDate) ? CPOrderedDescending : ((self < anotherDate) ? CPOrderedAscending : CPOrderedSame); 00171 } 00172 00173 - (CPDate)earlierDate:(CPDate)anotherDate 00174 { 00175 return (self < anotherDate) ? self : anotherDate; 00176 } 00177 00178 - (CPDate)laterDate:(CPDate)anotherDate 00179 { 00180 return (self > anotherDate) ? self : anotherDate; 00181 } 00182 00186 + (CPString)timezoneOffsetString:(int)timezoneOffset 00187 { 00188 var offset = -timezoneOffset, 00189 positive = offset >= 0, 00190 hours = positive ? FLOOR(offset / 60) : CEIL(offset / 60), 00191 minutes = offset - hours * 60; 00192 return [CPString stringWithFormat:@"%s%02d%02d", positive ? "+" : "-", ABS(hours), ABS(minutes)]; 00193 } 00194 00199 - (CPString)description 00200 { 00201 return [CPString stringWithFormat:@"%04d-%02d-%02d %02d:%02d:%02d %s", self.getFullYear(), self.getMonth()+1, self.getDate(), self.getHours(), self.getMinutes(), self.getSeconds(), [CPDate timezoneOffsetString:self.getTimezoneOffset()]]; 00202 } 00203 00204 - (id)copy 00205 { 00206 return new Date(self.getTime()); 00207 } 00208 00209 @end 00210 00211 var CPDateTimeKey = @"CPDateTimeKey"; 00212 00213 @implementation CPDate (CPCoding) 00214 00215 - (id)initWithCoder:(CPCoder)aCoder 00216 { 00217 if (self) 00218 { 00219 self.setTime([aCoder decodeIntForKey:CPDateTimeKey]); 00220 } 00221 00222 return self; 00223 } 00224 00225 - (void)encodeWithCoder:(CPCoder)aCoder 00226 { 00227 [aCoder encodeInt:self.getTime() forKey:CPDateTimeKey]; 00228 } 00229 00230 @end 00231 00232 Date.prototype.isa = CPDate;